Skip to content

Commit a8ab0e4

Browse files
adustmadbridge
authored andcommitted
SHA256: get ready to return error codes
1 parent 6d0903b commit a8ab0e4

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

features/mbedtls/targets/TARGET_STM/sha256_alt.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,15 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
7474
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[MBEDTLS_SHA256_BLOCK_SIZE] )
7575
{
7676
if (ctx->is224 == 0) {
77-
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, MBEDTLS_SHA256_BLOCK_SIZE);
77+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, MBEDTLS_SHA256_BLOCK_SIZE) != 0) {
78+
// return 0; // Return error code
79+
}
7880
} else {
79-
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, MBEDTLS_SHA256_BLOCK_SIZE);
81+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, MBEDTLS_SHA256_BLOCK_SIZE) != 0) {
82+
// return 0; // Return error code
83+
}
8084
}
85+
// return 1;
8186
}
8287

8388
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen )
@@ -107,9 +112,13 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in
107112
// now process every input as long as it is %4 bytes
108113
size_t iter = currentlen / 4;
109114
if (ctx->is224 == 0) {
110-
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4));
115+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4)) != 0) {
116+
//return 1; // Return error code here
117+
}
111118
} else {
112-
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4));
119+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4)) != 0) {
120+
//return 1; // Return error code here
121+
}
113122
}
114123
// sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
115124
ctx->sbuf_len = currentlen % 4;
@@ -123,19 +132,28 @@ void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32
123132
{
124133
if (ctx->sbuf_len > 0) {
125134
if (ctx->is224 == 0) {
126-
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len);
135+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
136+
//return 1; // Return error code here
137+
}
127138
} else {
128-
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len);
139+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
140+
//return 1; // Return error code here
141+
}
129142
}
130143
}
131144
mbedtls_zeroize(ctx->sbuf, MBEDTLS_SHA256_BLOCK_SIZE);
132145
ctx->sbuf_len = 0;
133146
__HAL_HASH_START_DIGEST();
134147

135-
if (ctx->is224 == 0)
136-
HAL_HASHEx_SHA256_Finish(&ctx->hhash_sha256, output, 10);
137-
else
138-
HAL_HASHEx_SHA224_Finish(&ctx->hhash_sha256, output, 10);
148+
if (ctx->is224 == 0) {
149+
if (HAL_HASHEx_SHA256_Finish(&ctx->hhash_sha256, output, 10) != 0) {
150+
//return 1; // Return error code here
151+
}
152+
} else {
153+
if (HAL_HASHEx_SHA224_Finish(&ctx->hhash_sha256, output, 10) != 0) {
154+
//return 1; // Return error code here
155+
}
156+
}
139157
}
140158

141159
#endif /*MBEDTLS_SHA256_ALT*/

0 commit comments

Comments
 (0)