int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32]) { if (st_sha256_restore_hw_context(ctx) != 1) { // Return HASH_BUSY timeout error here return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } /* Last accumulation for extra bytes in sbuf_len */ /* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */ if (ctx->is224 == 0) { if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } else { if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } mbedtls_zeroize(ctx->sbuf, ST_SHA256_BLOCK_SIZE); ctx->sbuf_len = 0; __HAL_HASH_START_DIGEST(); if (ctx->is224 == 0) { if (HAL_HASHEx_SHA256_Finish(&ctx->hhash_sha256, output, 10) != 0) { return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } else { if (HAL_HASHEx_SHA224_Finish(&ctx->hhash_sha256, output, 10) != 0) { return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } if (st_sha256_save_hw_context(ctx) != 1) { // Return HASH_BUSY timeout error here return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } return 0; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization: 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); /****************************************************************************/ /****************************** SHA224 **************************************/ /****************************************************************************/ 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 */ if (HAL_HASHEx_SHA224_Start_DMA(&HashHandle, (uint8_t*)aInput, strlen((char const*)aInput)) != HAL_OK) { Error_Handler(); } /* Get the computed digest value */ if(HAL_HASHEx_SHA224_Finish(&HashHandle, aSHA224Digest, 0xFF) != HAL_OK) { Error_Handler(); } /* Compare computed digest with expected one */ if(memcmp(aSHA224Digest, aExpectSHA224Digest, sizeof(aExpectSHA224Digest)/sizeof(aExpectSHA224Digest[0])) != 0) { Error_Handler(); } else { BSP_LED_On(LED1); } /****************************************************************************/ /***************************** SHA256 ***************************************/ /****************************************************************************/ 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 */ if (HAL_HASHEx_SHA256_Start_DMA(&HashHandle, (uint8_t*)aInput, strlen((char const*)aInput)) != HAL_OK) { Error_Handler(); } /* Get the computed digest value */ if (HAL_HASHEx_SHA256_Finish(&HashHandle, aSHA256Digest, 0xFF) != HAL_OK) { Error_Handler(); } /* Compare computed digest with expected one */ if(memcmp(aSHA256Digest, aExpectSHA256Digest, sizeof(aExpectSHA256Digest)/sizeof(aExpectSHA256Digest[0])) != 0) { Error_Handler(); } else { BSP_LED_On(LED4); } while(1) { } }
/** * @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); /****************************************************************************/ /****************************** SHA224 **************************************/ /****************************************************************************/ 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 */ if (HAL_HASHEx_SHA224_Start_DMA(&HashHandle, (uint8_t*)aInput, strlen((char const*)aInput)) != HAL_OK) { Error_Handler(); } /* Get the computed digest value */ if(HAL_HASHEx_SHA224_Finish(&HashHandle, aSHA224Digest, 0xFF) != HAL_OK) { Error_Handler(); } /* Compare computed digest with expected one */ if(memcmp(aSHA224Digest, aExpectSHA224Digest, sizeof(aExpectSHA224Digest)/sizeof(aExpectSHA224Digest[0])) != 0) { Error_Handler(); } else { BSP_LED_On(LED1); } /****************************************************************************/ /***************************** SHA256 ***************************************/ /****************************************************************************/ 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 */ if (HAL_HASHEx_SHA256_Start_DMA(&HashHandle, (uint8_t*)aInput, strlen((char const*)aInput)) != HAL_OK) { Error_Handler(); } /* Get the computed digest value */ if (HAL_HASHEx_SHA256_Finish(&HashHandle, aSHA256Digest, 0xFF) != HAL_OK) { Error_Handler(); } /* Compare computed digest with expected one */ if(memcmp(aSHA256Digest, aExpectSHA256Digest, sizeof(aExpectSHA256Digest)/sizeof(aExpectSHA256Digest[0])) != 0) { Error_Handler(); } else { BSP_LED_On(LED4); } while(1) { } }