Skip to content

Commit 268eec6

Browse files
committed
Replace 64 by a define
1 parent 074695a commit 268eec6

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

features/mbedtls/targets/TARGET_STM/md5_alt.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx )
7070
}
7171
}
7272

73-
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] )
73+
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[MBEDTLS_MD5_BLOCK_SIZE] )
7474
{
75-
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, 64);
75+
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, MBEDTLS_MD5_BLOCK_SIZE);
7676
}
7777

7878
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
7979
{
8080
size_t currentlen = ilen;
81-
// store mechanism to handle 64 bytes per 64 bytes
81+
// store mechanism to handle MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes
8282
if (currentlen == 0){ // only change HW status is size if 0
8383
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY)
8484
{
@@ -87,18 +87,18 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s
8787
HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
8888
}
8989
ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS;
90-
} else if (currentlen < (64-ctx->sbuf_len)) {
90+
} else if (currentlen < (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len)) {
9191
// only buffurize
9292
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
9393
ctx->sbuf_len += currentlen;
9494
} else {
9595
// fill buffer and process it
96-
memcpy(ctx->sbuf + ctx->sbuf_len, input, (64-ctx->sbuf_len));
97-
currentlen -= (64-ctx->sbuf_len);
96+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len));
97+
currentlen -= (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len);
9898
mbedtls_md5_process(ctx, ctx->sbuf);
9999
// now process every input as long as it is %4 bytes
100100
size_t iter = currentlen / 4;
101-
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input+64-ctx->sbuf_len), (iter*4));
101+
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input+MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len), (iter*4));
102102
// sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
103103
ctx->sbuf_len = currentlen % 4;
104104
if (ctx->sbuf_len !=0) {
@@ -112,7 +112,7 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
112112
if (ctx->sbuf_len > 0) {
113113
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len);
114114
}
115-
mbedtls_zeroize( ctx->sbuf, 64);
115+
mbedtls_zeroize( ctx->sbuf, MBEDTLS_MD5_BLOCK_SIZE);
116116
ctx->sbuf_len = 0;
117117
__HAL_HASH_START_DIGEST();
118118

features/mbedtls/targets/TARGET_STM/md5_alt.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@
3131
extern "C" {
3232
#endif
3333

34+
#define MBEDTLS_MD5_BLOCK_SIZE (64) // must be a multiple of 4
3435
/**
3536
* \brief MD5 context structure
3637
* \note HAL_HASH_MD5_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function
37-
* A 64 bytes buffer is used to save values and handle the processing 64 bytes per 64 bytes
38+
* A MBEDTLS_MD5_BLOCK_SIZE bytes buffer is used to save values and handle the processing
39+
* MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes
3840
* If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish
3941
*/
4042
typedef struct
4143
{
4244
HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */
43-
unsigned char sbuf[64]; /*!< 64 buffer to store values so that algorithm is caled once the buffer is filled */
45+
unsigned char sbuf[MBEDTLS_MD5_BLOCK_SIZE]; /*!< MBEDTLS_MD5_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
4446
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
4547
}
4648
mbedtls_md5_context;

0 commit comments

Comments
 (0)