@@ -26,41 +26,13 @@ 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
-
56
29
void mbedtls_sha1_init ( mbedtls_sha1_context * ctx )
57
30
{
58
31
mbedtls_zeroize ( ctx , sizeof ( mbedtls_sha1_context ) );
59
32
60
33
/* Enable HASH clock */
61
34
__HAL_RCC_HASH_CLK_ENABLE ();
62
35
63
- ctx -> flag = 0 ;
64
36
}
65
37
66
38
void mbedtls_sha1_free ( mbedtls_sha1_context * ctx )
@@ -83,9 +55,6 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
83
55
* dst = * src ;
84
56
}
85
57
86
- /*
87
- * SHA-1 context setup
88
- */
89
58
void mbedtls_sha1_starts ( mbedtls_sha1_context * ctx )
90
59
{
91
60
/* Deinitializes the HASH peripheral */
@@ -100,61 +69,51 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
100
69
// error found to be returned
101
70
return ;
102
71
}
103
-
104
- ctx -> flag = 0 ;
105
72
}
106
73
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 ] )
108
75
{
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 );
110
77
}
111
78
112
- /*
113
- * SHA-1 process buffer
114
- */
115
79
void mbedtls_sha1_update ( mbedtls_sha1_context * ctx , const unsigned char * input , size_t ilen )
116
80
{
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 ));
130
90
} 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 );
142
96
}
97
+ memcpy (ctx -> sbuf ,input + (i + 1 )* MBEDTLS_SHA1_BLOCK_SIZE - tmp , ctx -> sbuf_len );
143
98
}
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 ;
144
106
}
145
107
}
146
108
147
- /*
148
- * SHA-1 final digest
149
- */
150
109
void mbedtls_sha1_finish ( mbedtls_sha1_context * ctx , unsigned char output [20 ] )
151
110
{
152
111
153
- if (ctx -> flag == 1 ) {
112
+ if (ctx -> sbuf_len > 0 ) {
154
113
HAL_HASH_SHA1_Accumulate (& ctx -> hhash_sha1 , ctx -> sbuf , ctx -> sbuf_len );
155
- mbedtls_sha1_clear_ctxbuf (ctx );
156
114
}
157
-
115
+ mbedtls_zeroize (ctx -> sbuf , MBEDTLS_SHA1_BLOCK_SIZE );
116
+ ctx -> sbuf_len = 0 ;
158
117
__HAL_HASH_START_DIGEST ();
159
118
160
119
if (HAL_HASH_SHA1_Finish (& ctx -> hhash_sha1 , output , 10 )){
0 commit comments