Skip to content

Commit

Permalink
Replace 64 by a define
Browse files Browse the repository at this point in the history
  • Loading branch information
adustm committed May 22, 2017
1 parent 074695a commit 268eec6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
16 changes: 8 additions & 8 deletions features/mbedtls/targets/TARGET_STM/md5_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx )
}
}

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

void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
{
size_t currentlen = ilen;
// store mechanism to handle 64 bytes per 64 bytes
// store mechanism to handle MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes
if (currentlen == 0){ // only change HW status is size if 0
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY)
{
Expand All @@ -87,18 +87,18 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s
HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
}
ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS;
} else if (currentlen < (64-ctx->sbuf_len)) {
} else if (currentlen < (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len)) {
// only buffurize
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
ctx->sbuf_len += currentlen;
} else {
// fill buffer and process it
memcpy(ctx->sbuf + ctx->sbuf_len, input, (64-ctx->sbuf_len));
currentlen -= (64-ctx->sbuf_len);
memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len));
currentlen -= (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len);
mbedtls_md5_process(ctx, ctx->sbuf);
// now process every input as long as it is %4 bytes
size_t iter = currentlen / 4;
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input+64-ctx->sbuf_len), (iter*4));
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input+MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len), (iter*4));
// sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
ctx->sbuf_len = currentlen % 4;
if (ctx->sbuf_len !=0) {
Expand All @@ -112,7 +112,7 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
if (ctx->sbuf_len > 0) {
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len);
}
mbedtls_zeroize( ctx->sbuf, 64);
mbedtls_zeroize( ctx->sbuf, MBEDTLS_MD5_BLOCK_SIZE);
ctx->sbuf_len = 0;
__HAL_HASH_START_DIGEST();

Expand Down
6 changes: 4 additions & 2 deletions features/mbedtls/targets/TARGET_STM/md5_alt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
extern "C" {
#endif

#define MBEDTLS_MD5_BLOCK_SIZE (64) // must be a multiple of 4
/**
* \brief MD5 context structure
* \note HAL_HASH_MD5_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function
* A 64 bytes buffer is used to save values and handle the processing 64 bytes per 64 bytes
* A MBEDTLS_MD5_BLOCK_SIZE bytes buffer is used to save values and handle the processing
* MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes
* If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish
*/
typedef struct
{
HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */
unsigned char sbuf[64]; /*!< 64 buffer to store values so that algorithm is caled once the buffer is filled */
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 */
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
}
mbedtls_md5_context;
Expand Down

0 comments on commit 268eec6

Please sign in to comment.