Skip to content

Commit ca65e01

Browse files
committed
Handle 64 bytes buffers
1 parent dac9004 commit ca65e01

File tree

2 files changed

+31
-74
lines changed

2 files changed

+31
-74
lines changed

features/mbedtls/targets/TARGET_STM/sha1_alt.c

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,13 @@ static void mbedtls_zeroize( void *v, size_t n ) {
2626
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
2727
}
2828

29-
/* mbedtls_sha1_store will store in ctx->sbuf size new values located at *ptr */
30-
/* wether ctx->sbuf already contains something or not */
31-
static void mbedtls_sha1_store( mbedtls_sha1_context *ctx, uint8_t *ptr, unsigned char size)
32-
{
33-
if (ctx->sbuf == NULL) { // new allocation
34-
ctx->sbuf = malloc(size);
35-
} else { // realloc
36-
ctx->sbuf = realloc(ptr, size);
37-
}
38-
if (ctx->sbuf !=NULL) { // allocation occured
39-
memcpy(ctx->sbuf, ptr, size);
40-
ctx->flag = 1;
41-
ctx->sbuf_len += size;
42-
}
43-
}
44-
45-
/* mbedtls_sha1_clear_ctxbuf will clear the ctx buff, free memory */
46-
static void mbedtls_sha1_clear_ctxbuf( mbedtls_sha1_context *ctx)
47-
{
48-
ctx->flag=0;
49-
mbedtls_zeroize( ctx->sbuf, ctx->sbuf_len);
50-
free(ctx->sbuf);
51-
ctx->sbuf = NULL;
52-
ctx->sbuf_len = 0;
53-
54-
}
55-
5629
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
5730
{
5831
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
5932

6033
/* Enable HASH clock */
6134
__HAL_RCC_HASH_CLK_ENABLE();
6235

63-
ctx->flag=0;
6436
}
6537

6638
void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
@@ -83,9 +55,6 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
8355
*dst = *src;
8456
}
8557

86-
/*
87-
* SHA-1 context setup
88-
*/
8958
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
9059
{
9160
/* Deinitializes the HASH peripheral */
@@ -100,61 +69,51 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
10069
// error found to be returned
10170
return;
10271
}
103-
104-
ctx->flag=0;
10572
}
10673

107-
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
74+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[MBEDTLS_SHA1_BLOCK_SIZE] )
10875
{
109-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, 64);
76+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, MBEDTLS_SHA1_BLOCK_SIZE);
11077
}
11178

112-
/*
113-
* SHA-1 process buffer
114-
*/
11579
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
11680
{
117-
unsigned char *intermediate_buf=NULL;
118-
unsigned char modulus=0;
119-
unsigned char buf_len=0;
120-
// Accumulate cannot be called for a size <4 unless it is the last call
121-
modulus = ilen % 4;
122-
123-
if (ilen <4) {
124-
mbedtls_sha1_store(ctx, (uint8_t *)input, ilen);
125-
} else {
126-
if (modulus !=0) {
127-
buf_len = ilen - modulus;
128-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, buf_len);
129-
mbedtls_sha1_store(ctx, (uint8_t *)(input+buf_len), modulus);
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));
13090
} else {
131-
if (ctx->flag==0)
132-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, ilen);
133-
else {
134-
intermediate_buf=malloc(ilen + ctx->sbuf_len);
135-
memcpy(intermediate_buf, ctx->sbuf, ctx->sbuf_len);
136-
memcpy(intermediate_buf+ctx->sbuf_len, input, ilen);
137-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, intermediate_buf, ilen+ctx->sbuf_len);
138-
mbedtls_zeroize( intermediate_buf, (ilen + ctx->sbuf_len ) );
139-
free(intermediate_buf);
140-
intermediate_buf = NULL;
141-
mbedtls_sha1_clear_ctxbuf(ctx);
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);
14296
}
97+
memcpy(ctx->sbuf,input+(i+1)*MBEDTLS_SHA1_BLOCK_SIZE-tmp, ctx->sbuf_len);
14398
}
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);
105+
ctx->sbuf_len += currentlen;
144106
}
145107
}
146108

147-
/*
148-
* SHA-1 final digest
149-
*/
150109
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
151110
{
152111

153-
if (ctx->flag == 1) {
112+
if (ctx->sbuf_len > 0) {
154113
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len);
155-
mbedtls_sha1_clear_ctxbuf(ctx);
156114
}
157-
115+
mbedtls_zeroize(ctx->sbuf, MBEDTLS_SHA1_BLOCK_SIZE);
116+
ctx->sbuf_len = 0;
158117
__HAL_HASH_START_DIGEST();
159118

160119
if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10)){

features/mbedtls/targets/TARGET_STM/sha1_alt.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@
3030
extern "C" {
3131
#endif
3232

33+
#define MBEDTLS_SHA1_BLOCK_SIZE (64)
3334
/**
3435
* \brief SHA-1 context structure
3536
* \note HAL_HASH_SHA1_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function
36-
* In case of buffer size < 4, flag is set to 1, remaining bytes are copied in a temp buffer.
37-
* The pointer and the length are saved in sbuf and sbuf_len.
38-
* At the next accumulation, the saved values are taken into account, and flag is set to 0
39-
* If SHA1_finish is called and flag=1, the remaining bytes are accumulated before the call to HAL_HASH_SHA1_Finish
37+
* A 64 bytes buffer is used to save values and handle the processing 64 bytes per 64 bytes
38+
* If SHA1_finish is called and sbuf_len>0, the remaining bytes are accumulated before the call to HAL_HASH_SHA1_Finish
4039
*/
4140
typedef struct
4241
{
43-
unsigned char *sbuf; /*!< pointer to the remaining buffer to be processed */
42+
unsigned char sbuf[MBEDTLS_SHA1_BLOCK_SIZE]; /*!< 64 buffer to store values so that algorithm is caled once the buffer is filled */
4443
unsigned char sbuf_len; /*!< number of bytes remaining in sbuf to be processed */
4544
HASH_HandleTypeDef hhash_sha1; /*!< ST HAL HASH struct */
46-
int flag; /*!< 1 : there are sbuf_len bytes to be processed in sbuf, 0 : every data have been processed. */
4745
}
4846
mbedtls_sha1_context;
4947

0 commit comments

Comments
 (0)