diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c index 35d7122610..bd83ea03ae 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c @@ -103,37 +103,40 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst, /* * SHA-1 context setup */ -void mbedtls_sha1_starts(mbedtls_sha1_context *ctx) +int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha1_hw_starts(&ctx->hw_ctx); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha1_sw_starts(&ctx->sw_ctx); } + return 0; } /* * SHA-1 process buffer */ -void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen) +int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha1_hw_update(&ctx->hw_ctx, input, ilen); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha1_sw_update(&ctx->sw_ctx, input, ilen); } + return 0; } /* * SHA-1 final digest */ -void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20]) +int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx, unsigned char output[20]) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha1_hw_finish(&ctx->hw_ctx, output); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha1_sw_finish(&ctx->sw_ctx, output); } + return 0; } void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64]) diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.h b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.h index 59f519fac9..f90b335c62 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.h +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.h @@ -66,8 +66,10 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst, * \brief SHA-1 context setup * * \param ctx context to be initialized + * + * \return 0 if successful */ -void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); +int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ); /** * \brief SHA-1 process buffer @@ -75,16 +77,20 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); * \param ctx SHA-1 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); +int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); /** * \brief SHA-1 final digest * * \param ctx SHA-1 context * \param output SHA-1 checksum result + * + * \return 0 if successful */ -void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ); +int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] ); /* Internal use */ void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c index c4e27dfa5f..71b2bfe322 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c @@ -104,37 +104,40 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst, /* * SHA-256 context setup */ -void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224) +int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha256_hw_starts(&ctx->hw_ctx, is224); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha256_sw_starts(&ctx->sw_ctx, is224); } + return 0; } /* * SHA-256 process buffer */ -void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen) +int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha256_hw_update(&ctx->hw_ctx, input, ilen); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha256_sw_update(&ctx->sw_ctx, input, ilen); } + return 0; } /* * SHA-256 final digest */ -void mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char output[32]) +int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32]) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha256_hw_finish(&ctx->hw_ctx, output); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha256_sw_finish(&ctx->sw_ctx, output); } + return 0; } void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64]) diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.h b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.h index 04ff65fd18..aa5217da32 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.h +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.h @@ -67,8 +67,10 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst, * * \param ctx context to be initialized * \param is224 0 = use SHA256, 1 = use SHA224 + * + * \return 0 if successful */ -void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); +int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ); /** * \brief SHA-256 process buffer @@ -76,17 +78,21 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); * \param ctx SHA-256 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, - size_t ilen ); +int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input, + size_t ilen ); /** * \brief SHA-256 final digest * * \param ctx SHA-256 context * \param output SHA-224/256 checksum result + * + * \return 0 if successful */ -void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); +int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] ); /* Internal use */ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c index 48baf9132f..a2854f8cba 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c @@ -105,37 +105,40 @@ void mbedtls_sha512_clone(mbedtls_sha512_context *dst, /* * SHA-512 context setup */ -void mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384) +int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha512_hw_starts(&ctx->hw_ctx, is384); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha512_sw_starts(&ctx->sw_ctx, is384); } + return 0; } /* * SHA-512 process buffer */ -void mbedtls_sha512_update(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen) +int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha512_hw_update(&ctx->hw_ctx, input, ilen); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha512_sw_update(&ctx->sw_ctx, input, ilen); } + return 0; } /* * SHA-512 final digest */ -void mbedtls_sha512_finish(mbedtls_sha512_context *ctx, unsigned char output[64]) +int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx, unsigned char output[64]) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha512_hw_finish(&ctx->hw_ctx, output); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha512_sw_finish(&ctx->sw_ctx, output); } + return 0; } void mbedtls_sha512_process(mbedtls_sha512_context *ctx, const unsigned char data[128]) diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.h b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.h index afd0294ca3..1dbd820b6b 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.h +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.h @@ -67,8 +67,10 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst, * * \param ctx context to be initialized * \param is384 0 = use SHA512, 1 = use SHA384 + * + * \return 0 if successful */ -void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 ); +int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 ); /** * \brief SHA-512 process buffer @@ -76,17 +78,21 @@ void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 ); * \param ctx SHA-512 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input, - size_t ilen ); +int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char *input, + size_t ilen ); /** * \brief SHA-512 final digest * * \param ctx SHA-512 context * \param output SHA-384/512 checksum result + * + * \return 0 if successful */ -void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64] ); +int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, unsigned char output[64] ); /* Internal use */ void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] ); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c index 35d7122610..bd83ea03ae 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c @@ -103,37 +103,40 @@ void mbedtls_sha1_clone(mbedtls_sha1_context *dst, /* * SHA-1 context setup */ -void mbedtls_sha1_starts(mbedtls_sha1_context *ctx) +int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha1_hw_starts(&ctx->hw_ctx); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha1_sw_starts(&ctx->sw_ctx); } + return 0; } /* * SHA-1 process buffer */ -void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen) +int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha1_hw_update(&ctx->hw_ctx, input, ilen); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha1_sw_update(&ctx->sw_ctx, input, ilen); } + return 0; } /* * SHA-1 final digest */ -void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20]) +int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx, unsigned char output[20]) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha1_hw_finish(&ctx->hw_ctx, output); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha1_sw_finish(&ctx->sw_ctx, output); } + return 0; } void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64]) diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.h b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.h index 59f519fac9..f90b335c62 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.h +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.h @@ -66,8 +66,10 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst, * \brief SHA-1 context setup * * \param ctx context to be initialized + * + * \return 0 if successful */ -void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); +int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ); /** * \brief SHA-1 process buffer @@ -75,16 +77,20 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); * \param ctx SHA-1 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); +int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); /** * \brief SHA-1 final digest * * \param ctx SHA-1 context * \param output SHA-1 checksum result + * + * \return 0 if successful */ -void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ); +int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] ); /* Internal use */ void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ); diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c index b576d7a767..2ae555b492 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c @@ -104,37 +104,40 @@ void mbedtls_sha256_clone(mbedtls_sha256_context *dst, /* * SHA-256 context setup */ -void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224) +int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha256_hw_starts(&ctx->hw_ctx, is224); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha256_sw_starts(&ctx->sw_ctx, is224); } + return 0; } /* * SHA-256 process buffer */ -void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen) +int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha256_hw_update(&ctx->hw_ctx, input, ilen); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha256_sw_update(&ctx->sw_ctx, input, ilen); } + return 0; } /* * SHA-256 final digest */ -void mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char output[32]) +int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32]) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha256_hw_finish(&ctx->hw_ctx, output); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha256_sw_finish(&ctx->sw_ctx, output); } + return 0; } void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64]) diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.h b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.h index 04ff65fd18..aa5217da32 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.h +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.h @@ -67,8 +67,10 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst, * * \param ctx context to be initialized * \param is224 0 = use SHA256, 1 = use SHA224 + * + * \return 0 if successful */ -void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); +int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ); /** * \brief SHA-256 process buffer @@ -76,17 +78,21 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); * \param ctx SHA-256 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, - size_t ilen ); +int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input, + size_t ilen ); /** * \brief SHA-256 final digest * * \param ctx SHA-256 context * \param output SHA-224/256 checksum result + * + * \return 0 if successful */ -void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); +int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] ); /* Internal use */ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ); diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index 1a954ca213..2f44d8136b 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -90,12 +90,11 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst, *dst = *src; } -void mbedtls_md5_starts( mbedtls_md5_context *ctx ) +int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx ) { /* HASH IP initialization */ if (HAL_HASH_DeInit(&ctx->hhash_md5) != 0) { - // error found to be returned - return; + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; } /* HASH Configuration */ @@ -103,32 +102,35 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx ) /* clear CR ALGO value */ HASH->CR &= ~HASH_CR_ALGO_Msk; if (HAL_HASH_Init(&ctx->hhash_md5) != 0) { - // return error code - return; + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; } if (st_md5_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] ) +int mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] ) { if (st_md5_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Hash busy timeout } if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, ST_MD5_BLOCK_SIZE) != 0) { - return; // Return error code here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; } if (st_md5_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) +int mbedtls_md5_update_ret( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) { size_t currentlen = ilen; if (st_md5_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Hash busy timeout } // store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW if (currentlen == 0){ // only change HW status is size if 0 @@ -146,12 +148,14 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s // fill buffer and process it memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)); currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len); - mbedtls_md5_process(ctx, ctx->sbuf); + if( mbedtls_md5_process(ctx, ctx->sbuf) != 0) { + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; + } // Process every input as long as it is %64 bytes, ie 512 bits size_t iter = currentlen / ST_MD5_BLOCK_SIZE; if (iter !=0) { if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) { - return; // Return error code here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; } } // sbuf is completely accumulated, now copy up to 63 remaining bytes @@ -161,18 +165,20 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s } } if (st_md5_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) +int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx, unsigned char output[16] ) { if (st_md5_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Hash busy timeout } if (ctx->sbuf_len > 0) { if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Accumulation error } } mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE); @@ -183,8 +189,10 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) // error code to be returned } if (st_md5_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_MD5_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } #endif /* MBEDTLS_MD5_ALT */ diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.h b/features/mbedtls/targets/TARGET_STM/md5_alt.h index 7ea0df21e1..17867c3e4a 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.h +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.h @@ -79,8 +79,10 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst, * \brief MD5 context setup * * \param ctx context to be initialized + * + * \return 0 if successful */ -void mbedtls_md5_starts( mbedtls_md5_context *ctx ); +int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx ); /** * \brief MD5 process buffer @@ -88,19 +90,23 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx ); * \param ctx MD5 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ); +int mbedtls_md5_update_ret( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ); /** * \brief MD5 final digest * * \param ctx MD5 context * \param output MD5 checksum result + * + * \return 0 if successful */ -void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ); +int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx, unsigned char output[16] ); /* Internal use */ -void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] ); +int mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] ); #ifdef __cplusplus } diff --git a/features/mbedtls/targets/TARGET_STM/sha1_alt.c b/features/mbedtls/targets/TARGET_STM/sha1_alt.c index cfd90416f2..a5229dc9c2 100644 --- a/features/mbedtls/targets/TARGET_STM/sha1_alt.c +++ b/features/mbedtls/targets/TARGET_STM/sha1_alt.c @@ -88,12 +88,11 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst, *dst = *src; } -void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) +int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ) { /* Deinitializes the HASH peripheral */ if (HAL_HASH_DeInit(&ctx->hhash_sha1) == HAL_ERROR) { - // error found to be returned - return; + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; } /* HASH Configuration */ @@ -101,33 +100,36 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) /* clear CR ALGO value */ HASH->CR &= ~HASH_CR_ALGO_Msk; if (HAL_HASH_Init(&ctx->hhash_sha1) == HAL_ERROR) { - // error found to be returned - return; + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; } if (st_sha1_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] ) +int mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] ) { if (st_sha1_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; // Hash busy timeout } if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, ST_SHA1_BLOCK_SIZE) != 0) { - return; // Return error code + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; } if (st_sha1_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) +int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) { size_t currentlen = ilen; if (st_sha1_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; // Hash busy timeout } // store mechanism to accumulate ST_SHA1_BLOCK_SIZE bytes (512 bits) in the HW @@ -146,11 +148,14 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, // fill buffer and process it memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len)); currentlen -= (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len); - mbedtls_sha1_process(ctx, ctx->sbuf); + if( mbedtls_sha1_process(ctx, ctx->sbuf) != 0 ) { + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; + } + // Process every input as long as it is %64 bytes, ie 512 bits size_t iter = currentlen / ST_SHA1_BLOCK_SIZE; if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)(input + ST_SHA1_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA1_BLOCK_SIZE)) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; } // sbuf is completely accumulated, now copy up to 63 remaining bytes ctx->sbuf_len = currentlen % ST_SHA1_BLOCK_SIZE; @@ -159,19 +164,21 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, } } if (st_sha1_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ) +int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] ) { if (st_sha1_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; // Hash busy timeout } if (ctx->sbuf_len > 0) { if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; } } mbedtls_zeroize(ctx->sbuf, ST_SHA1_BLOCK_SIZE); @@ -179,11 +186,13 @@ void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ) __HAL_HASH_START_DIGEST(); if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10) != 0){ - return; // error code to be returned + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; } if (st_sha1_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } #endif /*MBEDTLS_SHA1_ALT*/ diff --git a/features/mbedtls/targets/TARGET_STM/sha1_alt.h b/features/mbedtls/targets/TARGET_STM/sha1_alt.h index 4db4880bea..f857ca2f76 100644 --- a/features/mbedtls/targets/TARGET_STM/sha1_alt.h +++ b/features/mbedtls/targets/TARGET_STM/sha1_alt.h @@ -79,8 +79,10 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst, * \brief SHA-1 context setup * * \param ctx context to be initialized + * + * \return 0 if successful */ -void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); +int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ); /** * \brief SHA-1 process buffer @@ -88,19 +90,23 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); * \param ctx SHA-1 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); +int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); /** * \brief SHA-1 final digest * * \param ctx SHA-1 context * \param output SHA-1 checksum result + * + * \return 0 if successful */ -void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ); +int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] ); /* Internal use */ -void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] ); +int mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] ); #ifdef __cplusplus } diff --git a/features/mbedtls/targets/TARGET_STM/sha256_alt.c b/features/mbedtls/targets/TARGET_STM/sha256_alt.c index 6729cdac2e..6a26a5ca33 100644 --- a/features/mbedtls/targets/TARGET_STM/sha256_alt.c +++ b/features/mbedtls/targets/TARGET_STM/sha256_alt.c @@ -88,12 +88,11 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst, *dst = *src; } -void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ) +int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ) { /* HASH IP initialization */ if (HAL_HASH_DeInit(&ctx->hhash_sha256) == HAL_ERROR) { - // error found to be returned - return; + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } ctx->is224 = is224; @@ -102,39 +101,42 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ) /* clear CR ALGO value */ HASH->CR &= ~HASH_CR_ALGO_Msk; if (HAL_HASH_Init(&ctx->hhash_sha256) == HAL_ERROR) { - // error found to be returned - return; + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } if (st_sha256_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] ) +int mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] ) { if (st_sha256_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; // Hash busy timeout } if (ctx->is224 == 0) { if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) { - return; // Return error code + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } else { if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) { - return; // Return error code + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } if (st_sha256_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen ) +int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen ) { size_t currentlen = ilen; if (st_sha256_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; // Hash busy timeout } // store mechanism to accumulate ST_SHA256_BLOCK_SIZE bytes (512 bits) in the HW @@ -157,17 +159,19 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in // fill buffer and process it memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len)); currentlen -= (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len); - mbedtls_sha256_process(ctx, ctx->sbuf); + if( mbedtls_sha256_process(ctx, ctx->sbuf) != 0) { + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; + } // Process every input as long as it is %64 bytes, ie 512 bits size_t iter = currentlen / ST_SHA256_BLOCK_SIZE; if (iter !=0) { if (ctx->is224 == 0) { if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + ST_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA256_BLOCK_SIZE)) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } else { if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + ST_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA256_BLOCK_SIZE)) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } } @@ -178,23 +182,25 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in } } if (st_sha256_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } -void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ) +int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] ) { if (st_sha256_restore_hw_context(ctx) != 1) { - return; // Return HASH_BUSY timout error here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; // Hash busy timeout } if (ctx->sbuf_len > 0) { if (ctx->is224 == 0) { if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } else { if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } } @@ -204,16 +210,18 @@ void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32 if (ctx->is224 == 0) { if (HAL_HASHEx_SHA256_Finish(&ctx->hhash_sha256, output, 10) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } else { if (HAL_HASHEx_SHA224_Finish(&ctx->hhash_sha256, output, 10) != 0) { - return; // Return error code here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; } } if (st_sha256_save_hw_context(ctx) != 1) { - return; // return HASH_BUSY timeout Error here + return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED; // Hash busy timeout } + + return 0; } #endif /*MBEDTLS_SHA256_ALT*/ diff --git a/features/mbedtls/targets/TARGET_STM/sha256_alt.h b/features/mbedtls/targets/TARGET_STM/sha256_alt.h index b2ae7a3c29..073354ee3c 100644 --- a/features/mbedtls/targets/TARGET_STM/sha256_alt.h +++ b/features/mbedtls/targets/TARGET_STM/sha256_alt.h @@ -80,8 +80,10 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst, * * \param ctx context to be initialized * \param is224 0 = use SHA256, 1 = use SHA224 + * + * \return 0 if successful */ -void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); +int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ); /** * \brief SHA-256 process buffer @@ -89,8 +91,10 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); * \param ctx SHA-256 context * \param input buffer holding the data * \param ilen length of the input data + * + * \return 0 if successful */ -void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, +int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen ); /** @@ -98,11 +102,13 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in * * \param ctx SHA-256 context * \param output SHA-224/256 checksum result + * + * \return 0 if successful */ -void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); +int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] ); /* Internal use */ -void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] ); +int mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] ); #ifdef __cplusplus }