Skip to content

Commit c2a85fb

Browse files
committed
Rework SHA1 update for buffer <4 bytes management
1 parent ca65e01 commit c2a85fb

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

features/mbedtls/targets/TARGET_STM/sha1_alt.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,42 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
7373

7474
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[MBEDTLS_SHA1_BLOCK_SIZE] )
7575
{
76-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, MBEDTLS_SHA1_BLOCK_SIZE);
76+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, MBEDTLS_SHA1_BLOCK_SIZE);
7777
}
7878

7979
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
8080
{
81-
unsigned char i=0;
82-
int currentlen = ilen;
83-
/* store mechanism to handle 64 bytes per 64 bytes */
84-
if (currentlen == 0){ // change HW status is size if 0
85-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) input, ilen);
86-
}
87-
while ((currentlen+ctx->sbuf_len) >=MBEDTLS_SHA1_BLOCK_SIZE) {
88-
if (ctx->sbuf_len ==0) { /* straight forward */
89-
mbedtls_sha1_process(ctx, input+(i*MBEDTLS_SHA1_BLOCK_SIZE));
90-
} else {
91-
unsigned char tmp = ctx->sbuf_len;
92-
memcpy(ctx->sbuf+tmp, input+(i*MBEDTLS_SHA1_BLOCK_SIZE),MBEDTLS_SHA1_BLOCK_SIZE-tmp);
93-
mbedtls_sha1_process(ctx, ctx->sbuf);
94-
if ((currentlen-(MBEDTLS_SHA1_BLOCK_SIZE-tmp)) < tmp) {
95-
ctx->sbuf_len = currentlen-(MBEDTLS_SHA1_BLOCK_SIZE-tmp);
96-
}
97-
memcpy(ctx->sbuf,input+(i+1)*MBEDTLS_SHA1_BLOCK_SIZE-tmp, ctx->sbuf_len);
81+
size_t currentlen = ilen;
82+
// store mechanism to handle MBEDTLS_SHA1_BLOCK_SIZE bytes per MBEDTLS_SHA1_BLOCK_SIZE bytes
83+
if (currentlen == 0){ // only change HW status is size if 0
84+
if(ctx->hhash_sha1.Phase == HAL_HASH_PHASE_READY) {
85+
/* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute
86+
the message digest of a new message */
87+
HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT;
9888
}
99-
currentlen -= MBEDTLS_SHA1_BLOCK_SIZE;
100-
i++;
101-
}
102-
if (currentlen >0) {
103-
/* Store the remaining <64 values */
104-
memcpy(ctx->sbuf+ctx->sbuf_len, input+(i*MBEDTLS_SHA1_BLOCK_SIZE), currentlen);
89+
ctx->hhash_sha1.Phase = HAL_HASH_PHASE_PROCESS;
90+
} else if (currentlen < (MBEDTLS_SHA1_BLOCK_SIZE-ctx->sbuf_len)) {
91+
// only buffurize
92+
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
10593
ctx->sbuf_len += currentlen;
94+
} else {
95+
// fill buffer and process it
96+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_SHA1_BLOCK_SIZE-ctx->sbuf_len));
97+
currentlen -= (MBEDTLS_SHA1_BLOCK_SIZE-ctx->sbuf_len);
98+
mbedtls_sha1_process(ctx, ctx->sbuf);
99+
// now process every input as long as it is %4 bytes
100+
size_t iter = currentlen / 4;
101+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)(input+MBEDTLS_SHA1_BLOCK_SIZE-ctx->sbuf_len), (iter*4));
102+
// sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
103+
ctx->sbuf_len = currentlen % 4;
104+
if (ctx->sbuf_len !=0) {
105+
memcpy(ctx->sbuf, input+iter, ctx->sbuf_len);
106+
}
106107
}
107108
}
108109

109110
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
110111
{
111-
112112
if (ctx->sbuf_len > 0) {
113113
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len);
114114
}

0 commit comments

Comments
 (0)