@@ -73,42 +73,42 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
73
73
74
74
void mbedtls_sha1_process ( mbedtls_sha1_context * ctx , const unsigned char data [MBEDTLS_SHA1_BLOCK_SIZE ] )
75
75
{
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 );
77
77
}
78
78
79
79
void mbedtls_sha1_update ( mbedtls_sha1_context * ctx , const unsigned char * input , size_t ilen )
80
80
{
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 ;
98
88
}
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 );
105
93
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
+ }
106
107
}
107
108
}
108
109
109
110
void mbedtls_sha1_finish ( mbedtls_sha1_context * ctx , unsigned char output [20 ] )
110
111
{
111
-
112
112
if (ctx -> sbuf_len > 0 ) {
113
113
HAL_HASH_SHA1_Accumulate (& ctx -> hhash_sha1 , ctx -> sbuf , ctx -> sbuf_len );
114
114
}
0 commit comments