示例#1
0
int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx, unsigned char output[16] )
{
    if (st_md5_restore_hw_context(ctx) != 1) {
        // Return HASH_BUSY timeout error here
        return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED;
    }
    /* Last accumulation for extra bytes in sbuf_len */
    /* This sets HW flags in case mbedtls_md5_update has not been called yet */
    if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
        return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED;
    }

    mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE);
    ctx->sbuf_len = 0;
    __HAL_HASH_START_DIGEST();

    if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) {
        return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED;
    }
    if (st_md5_save_hw_context(ctx) != 1) {
        // Return HASH_BUSY timeout error here
        return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED;
    }
    return 0;
}
示例#2
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /* Configure LED1, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
  /****************************************************************************/
  /******************************** SHA1 **************************************/
  /****************************************************************************/
  HAL_HASH_DeInit(&HashHandle);
  HashHandle.Init.DataType = HASH_DATATYPE_8B;
  
  if(HAL_HASH_Init(&HashHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
  /* Start HASH computation using DMA transfer */
  HAL_HASH_SHA1_Start_DMA(&HashHandle, (uint8_t*)aInput, strlen((char const*)aInput));
  while (HAL_HASH_GetState(&HashHandle) == HAL_HASH_STATE_BUSY);
  /* Get the computed digest value */
  HAL_HASH_SHA1_Finish(&HashHandle, aSHA1Digest, 0xFF);
  while (HAL_HASH_GetState(&HashHandle) == HAL_HASH_STATE_BUSY);
  
  /* Compare computed digest with expected one */
  if(memcmp(aSHA1Digest, aExpectSHA1Digest, sizeof(aExpectSHA1Digest)/sizeof(aExpectSHA1Digest[0])) != 0)
  {
    Error_Handler();
  }
  else
  {
    BSP_LED_On(LED1);
  }
  
  /****************************************************************************/
  /******************************** MD5 ***************************************/
  /****************************************************************************/
  HAL_HASH_DeInit(&HashHandle);
  HashHandle.Init.DataType = HASH_DATATYPE_8B;
  
  if(HAL_HASH_Init(&HashHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
  /* Start HASH computation using DMA transfer */
  HAL_HASH_MD5_Start_DMA(&HashHandle, (uint8_t*)aInput, strlen((char const*)aInput));
  while (HAL_HASH_GetState(&HashHandle) == HAL_HASH_STATE_BUSY);
  /* Get the computed digest value */
  HAL_HASH_MD5_Finish(&HashHandle, aMD5Digest, 0xFF);
  while (HAL_HASH_GetState(&HashHandle) == HAL_HASH_STATE_BUSY);
  
  /* Compare computed digest with expected one */
  if(memcmp(aMD5Digest, aExpectMD5Digest, sizeof(aExpectMD5Digest)/sizeof(aExpectMD5Digest[0])) != 0)
  {
    Error_Handler();
  }
  else
  {
    BSP_LED_On(LED4);
  }
  while(1)
  {
  }
}