@@ -26,6 +26,33 @@ static void mbedtls_zeroize( void *v, size_t n ) {
26
26
volatile unsigned char * p = (unsigned char * )v ; while ( n -- ) * p ++ = 0 ;
27
27
}
28
28
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
+
29
56
void mbedtls_sha1_init ( mbedtls_sha1_context * ctx )
30
57
{
31
58
mbedtls_zeroize ( ctx , sizeof ( mbedtls_sha1_context ) );
@@ -90,40 +117,28 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
90
117
unsigned char * intermediate_buf = NULL ;
91
118
unsigned char modulus = 0 ;
92
119
unsigned char buf_len = 0 ;
93
-
94
120
// Accumulate cannot be called for a size <4 unless it is the last call
95
-
96
121
modulus = ilen % 4 ;
97
122
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 ) {
109
127
buf_len = ilen - modulus ;
110
128
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 {
118
131
if (ctx -> flag == 0 )
119
132
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 );
123
135
memcpy (intermediate_buf , ctx -> sbuf , ctx -> sbuf_len );
124
136
memcpy (intermediate_buf + ctx -> sbuf_len , input , ilen );
125
137
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 );
127
142
}
128
143
}
129
144
}
@@ -134,9 +149,10 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
134
149
*/
135
150
void mbedtls_sha1_finish ( mbedtls_sha1_context * ctx , unsigned char output [20 ] )
136
151
{
152
+
137
153
if (ctx -> flag == 1 ) {
138
154
HAL_HASH_SHA1_Accumulate (& ctx -> hhash_sha1 , ctx -> sbuf , ctx -> sbuf_len );
139
- ctx -> flag = 0 ;
155
+ mbedtls_sha1_clear_ctxbuf ( ctx ) ;
140
156
}
141
157
142
158
__HAL_HASH_START_DIGEST ();
0 commit comments