Skip to content

Commit 97fc115

Browse files
committed
Improve memory management
1 parent ff3017a commit 97fc115

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

features/mbedtls/targets/TARGET_STM/sha1_alt.c

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ 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+
2956
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
3057
{
3158
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
@@ -90,40 +117,28 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
90117
unsigned char *intermediate_buf=NULL;
91118
unsigned char modulus=0;
92119
unsigned char buf_len=0;
93-
94120
// Accumulate cannot be called for a size <4 unless it is the last call
95-
96121
modulus = ilen % 4;
97122

98-
if (ilen <4)
99-
{
100-
ctx->sbuf=malloc(ilen);
101-
memcpy(ctx->sbuf, input, ilen);
102-
ctx->flag = 1;
103-
ctx->sbuf_len=ilen;
104-
}
105-
else
106-
{
107-
if (modulus !=0)
108-
{
123+
if (ilen <4) {
124+
mbedtls_sha1_store(ctx, (uint8_t *)input, ilen);
125+
} else {
126+
if (modulus !=0) {
109127
buf_len = ilen - modulus;
110128
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, buf_len);
111-
ctx->sbuf_len=modulus;
112-
ctx->sbuf=malloc(ctx->sbuf_len);
113-
memcpy(ctx->sbuf, input+buf_len, modulus);
114-
ctx->flag = 1;
115-
}
116-
else
117-
{
129+
mbedtls_sha1_store(ctx, (uint8_t *)(input+buf_len), modulus);
130+
} else {
118131
if (ctx->flag==0)
119132
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, ilen);
120-
else
121-
{
122-
intermediate_buf=malloc(ilen+ctx->sbuf_len);
133+
else {
134+
intermediate_buf=malloc(ilen + ctx->sbuf_len);
123135
memcpy(intermediate_buf, ctx->sbuf, ctx->sbuf_len);
124136
memcpy(intermediate_buf+ctx->sbuf_len, input, ilen);
125137
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, intermediate_buf, ilen+ctx->sbuf_len);
126-
ctx->flag=0;
138+
mbedtls_zeroize( intermediate_buf, (ilen + ctx->sbuf_len ) );
139+
free(intermediate_buf);
140+
intermediate_buf = NULL;
141+
mbedtls_sha1_clear_ctxbuf(ctx);
127142
}
128143
}
129144
}
@@ -134,9 +149,10 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
134149
*/
135150
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
136151
{
152+
137153
if (ctx->flag == 1) {
138154
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len);
139-
ctx->flag=0;
155+
mbedtls_sha1_clear_ctxbuf(ctx);
140156
}
141157

142158
__HAL_HASH_START_DIGEST();

0 commit comments

Comments
 (0)