Skip to content

Commit c06a42b

Browse files
authored
Merge pull request #5630 from adustm/fix5079_sha1_md5_sha256_hwcrypto
Fix #5079. Support of call to mbedtls_x_finish without calling mbedtls_x_update
2 parents 195b3ea + 88c3b3e commit c06a42b

File tree

4 files changed

+52
-54
lines changed

4 files changed

+52
-54
lines changed

features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222

2323
#define MBEDTLS_AES_ALT
2424

25-
/* FIXME: Don't enable SHA1, SHA256 and MD5 hardware acceleration until issue
26-
* #5079 is fixed. (https://github.com/ARMmbed/mbed-os/issues/5079) */
27-
/* #define MBEDTLS_SHA256_ALT */
25+
#define MBEDTLS_SHA256_ALT
2826

29-
/* #define MBEDTLS_SHA1_ALT */
27+
#define MBEDTLS_SHA1_ALT
3028

31-
/* #define MBEDTLS_MD5_ALT */
29+
#define MBEDTLS_MD5_ALT
3230

3331
#endif /* MBEDTLS_DEVICE_H */

features/mbedtls/targets/TARGET_STM/md5_alt.c

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* limitations under the License.
1818
*
1919
*/
20-
#if defined(MBEDTLS_MD5_C)
2120
#include "mbedtls/md5.h"
21+
#if defined(MBEDTLS_MD5_C)
2222

2323
#if defined(MBEDTLS_MD5_ALT)
2424
#include "mbedtls/platform.h"
@@ -127,54 +127,53 @@ void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_
127127
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
128128
{
129129
size_t currentlen = ilen;
130-
if (st_md5_restore_hw_context(ctx) != 1) {
131-
return; // Return HASH_BUSY timout error here
132-
}
133-
// store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
134-
if (currentlen == 0){ // only change HW status is size if 0
135-
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) {
136-
/* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
137-
the message digest of a new message */
138-
HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
130+
/* If ilen = 0 : do nothing */
131+
if (currentlen != 0) {
132+
if (st_md5_restore_hw_context(ctx) != 1) {
133+
return; // Return HASH_BUSY timout error here
139134
}
140-
ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS;
141-
} else if (currentlen < (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)) {
142-
// only buffurize
143-
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
144-
ctx->sbuf_len += currentlen;
145-
} else {
146-
// fill buffer and process it
147-
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len));
148-
currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len);
149-
mbedtls_md5_process(ctx, ctx->sbuf);
150-
// Process every input as long as it is %64 bytes, ie 512 bits
151-
size_t iter = currentlen / ST_MD5_BLOCK_SIZE;
152-
if (iter !=0) {
153-
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) {
154-
return; // Return error code here
135+
136+
// store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
137+
if (currentlen < (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)) {
138+
// only buffurize
139+
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
140+
ctx->sbuf_len += currentlen;
141+
} else {
142+
// fill buffer and process it
143+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len));
144+
currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len);
145+
mbedtls_md5_process(ctx, ctx->sbuf);
146+
// Process every input as long as it is %64 bytes, ie 512 bits
147+
size_t iter = currentlen / ST_MD5_BLOCK_SIZE;
148+
if (iter !=0) {
149+
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) {
150+
return; // Return error code here
151+
}
152+
}
153+
// sbuf is completely accumulated, now copy up to 63 remaining bytes
154+
ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE;
155+
if (ctx->sbuf_len !=0) {
156+
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
155157
}
156158
}
157-
// sbuf is completely accumulated, now copy up to 63 remaining bytes
158-
ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE;
159-
if (ctx->sbuf_len !=0) {
160-
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
159+
160+
if (st_md5_save_hw_context(ctx) != 1) {
161+
return; // return HASH_BUSY timeout Error here
161162
}
162163
}
163-
if (st_md5_save_hw_context(ctx) != 1) {
164-
return; // return HASH_BUSY timeout Error here
165-
}
166164
}
167165

168166
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
169167
{
170168
if (st_md5_restore_hw_context(ctx) != 1) {
171169
return; // Return HASH_BUSY timout error here
172170
}
173-
if (ctx->sbuf_len > 0) {
174-
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
175-
return; // Return error code here
176-
}
171+
/* Last accumulation for extra bytes in sbuf_len */
172+
/* This sets HW flags in case mbedtls_md5_update has not been called yet */
173+
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
174+
return; // Return error code here
177175
}
176+
178177
mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE);
179178
ctx->sbuf_len = 0;
180179
__HAL_HASH_START_DIGEST();

features/mbedtls/targets/TARGET_STM/sha1_alt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
169169
return; // Return HASH_BUSY timout error here
170170
}
171171

172-
if (ctx->sbuf_len > 0) {
173-
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) {
174-
return; // Return error code here
175-
}
172+
/* Last accumulation for extra bytes in sbuf_len */
173+
/* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */
174+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) {
175+
return; // Return error code here
176176
}
177177
mbedtls_zeroize(ctx->sbuf, ST_SHA1_BLOCK_SIZE);
178178
ctx->sbuf_len = 0;

features/mbedtls/targets/TARGET_STM/sha256_alt.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,18 @@ void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32
187187
if (st_sha256_restore_hw_context(ctx) != 1) {
188188
return; // Return HASH_BUSY timout error here
189189
}
190-
if (ctx->sbuf_len > 0) {
191-
if (ctx->is224 == 0) {
192-
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
193-
return; // Return error code here
194-
}
195-
} else {
196-
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
197-
return; // Return error code here
198-
}
190+
/* Last accumulation for extra bytes in sbuf_len */
191+
/* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */
192+
if (ctx->is224 == 0) {
193+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
194+
return; // Return error code here
195+
}
196+
} else {
197+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
198+
return; // Return error code here
199199
}
200200
}
201+
201202
mbedtls_zeroize(ctx->sbuf, ST_SHA256_BLOCK_SIZE);
202203
ctx->sbuf_len = 0;
203204
__HAL_HASH_START_DIGEST();

0 commit comments

Comments
 (0)