|
17 | 17 | * limitations under the License.
|
18 | 18 | *
|
19 | 19 | */
|
20 |
| -#if defined(MBEDTLS_MD5_C) |
21 | 20 | #include "mbedtls/md5.h"
|
| 21 | +#if defined(MBEDTLS_MD5_C) |
22 | 22 |
|
23 | 23 | #if defined(MBEDTLS_MD5_ALT)
|
24 | 24 | #include "mbedtls/platform.h"
|
@@ -127,54 +127,53 @@ void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_
|
127 | 127 | void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
|
128 | 128 | {
|
129 | 129 | size_t currentlen = ilen;
|
130 |
| - if (st_md5_restore_hw_context(ctx) != 1) { |
131 |
| - return; // Return HASH_BUSY timout error here |
132 |
| - } |
133 |
| - // store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW |
134 |
| - if (currentlen == 0){ // only change HW status is size if 0 |
135 |
| - if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) { |
136 |
| - /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute |
137 |
| - the message digest of a new message */ |
138 |
| - HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT; |
| 130 | + /* If ilen = 0 : do nothing */ |
| 131 | + if (currentlen != 0) { |
| 132 | + if (st_md5_restore_hw_context(ctx) != 1) { |
| 133 | + return; // Return HASH_BUSY timout error here |
139 | 134 | }
|
140 |
| - ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS; |
141 |
| - } else if (currentlen < (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)) { |
142 |
| - // only buffurize |
143 |
| - memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen); |
144 |
| - ctx->sbuf_len += currentlen; |
145 |
| - } else { |
146 |
| - // fill buffer and process it |
147 |
| - memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)); |
148 |
| - currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len); |
149 |
| - mbedtls_md5_process(ctx, ctx->sbuf); |
150 |
| - // Process every input as long as it is %64 bytes, ie 512 bits |
151 |
| - size_t iter = currentlen / ST_MD5_BLOCK_SIZE; |
152 |
| - if (iter !=0) { |
153 |
| - if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) { |
154 |
| - return; // Return error code here |
| 135 | + |
| 136 | + // store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW |
| 137 | + if (currentlen < (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)) { |
| 138 | + // only buffurize |
| 139 | + memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen); |
| 140 | + ctx->sbuf_len += currentlen; |
| 141 | + } else { |
| 142 | + // fill buffer and process it |
| 143 | + memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)); |
| 144 | + currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len); |
| 145 | + mbedtls_md5_process(ctx, ctx->sbuf); |
| 146 | + // Process every input as long as it is %64 bytes, ie 512 bits |
| 147 | + size_t iter = currentlen / ST_MD5_BLOCK_SIZE; |
| 148 | + if (iter !=0) { |
| 149 | + if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) { |
| 150 | + return; // Return error code here |
| 151 | + } |
| 152 | + } |
| 153 | + // sbuf is completely accumulated, now copy up to 63 remaining bytes |
| 154 | + ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE; |
| 155 | + if (ctx->sbuf_len !=0) { |
| 156 | + memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len); |
155 | 157 | }
|
156 | 158 | }
|
157 |
| - // sbuf is completely accumulated, now copy up to 63 remaining bytes |
158 |
| - ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE; |
159 |
| - if (ctx->sbuf_len !=0) { |
160 |
| - memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len); |
| 159 | + |
| 160 | + if (st_md5_save_hw_context(ctx) != 1) { |
| 161 | + return; // return HASH_BUSY timeout Error here |
161 | 162 | }
|
162 | 163 | }
|
163 |
| - if (st_md5_save_hw_context(ctx) != 1) { |
164 |
| - return; // return HASH_BUSY timeout Error here |
165 |
| - } |
166 | 164 | }
|
167 | 165 |
|
168 | 166 | void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
|
169 | 167 | {
|
170 | 168 | if (st_md5_restore_hw_context(ctx) != 1) {
|
171 | 169 | return; // Return HASH_BUSY timout error here
|
172 | 170 | }
|
173 |
| - if (ctx->sbuf_len > 0) { |
174 |
| - if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) { |
175 |
| - return; // Return error code here |
176 |
| - } |
| 171 | + /* Last accumulation for extra bytes in sbuf_len */ |
| 172 | + /* This sets HW flags in case mbedtls_md5_update has not been called yet */ |
| 173 | + if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) { |
| 174 | + return; // Return error code here |
177 | 175 | }
|
| 176 | + |
178 | 177 | mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE);
|
179 | 178 | ctx->sbuf_len = 0;
|
180 | 179 | __HAL_HASH_START_DIGEST();
|
|
0 commit comments