|
| 1 | +/* |
| 2 | + * MD5 hw acceleration |
| 3 | + ******************************************************************************* |
| 4 | + * Copyright (c) 2017, STMicroelectronics |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | + * not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | +#if defined(MBEDTLS_MD5_C) |
| 21 | +#include "mbedtls/md5.h" |
| 22 | + |
| 23 | +#if defined(MBEDTLS_MD5_ALT) |
| 24 | +#include "mbedtls/platform.h" |
| 25 | + |
| 26 | +/* Implementation that should never be optimized out by the compiler */ |
| 27 | +static void mbedtls_zeroize( void *v, size_t n ) { |
| 28 | + volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 29 | +} |
| 30 | + |
| 31 | +static int st_md5_restore_hw_context(mbedtls_md5_context *ctx) |
| 32 | +{ |
| 33 | + uint32_t i; |
| 34 | + uint32_t tickstart; |
| 35 | + /* allow multi-instance of HASH use: save context for HASH HW module CR */ |
| 36 | + /* Check that there is no HASH activity on going */ |
| 37 | + tickstart = HAL_GetTick(); |
| 38 | + while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) { |
| 39 | + if ((HAL_GetTick() - tickstart) > ST_MD5_TIMEOUT) { |
| 40 | + return 0; // timeout: HASH processor is busy |
| 41 | + } |
| 42 | + } |
| 43 | + HASH->STR = ctx->ctx_save_str; |
| 44 | + HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT); |
| 45 | + for (i=0;i<38;i++) { |
| 46 | + HASH->CSR[i] = ctx->ctx_save_csr[i]; |
| 47 | + } |
| 48 | + return 1; |
| 49 | +} |
| 50 | + |
| 51 | +static int st_md5_save_hw_context(mbedtls_md5_context *ctx) |
| 52 | +{ |
| 53 | + uint32_t i; |
| 54 | + uint32_t tickstart; |
| 55 | + /* Check that there is no HASH activity on going */ |
| 56 | + tickstart = HAL_GetTick(); |
| 57 | + while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) { |
| 58 | + if ((HAL_GetTick() - tickstart) > ST_MD5_TIMEOUT) { |
| 59 | + return 0; // timeout: HASH processor is busy |
| 60 | + } |
| 61 | + } |
| 62 | + /* allow multi-instance of HASH use: restore context for HASH HW module CR */ |
| 63 | + ctx->ctx_save_cr = HASH->CR; |
| 64 | + ctx->ctx_save_str = HASH->STR; |
| 65 | + for (i=0;i<38;i++) { |
| 66 | + ctx->ctx_save_csr[i] = HASH->CSR[i]; |
| 67 | + } |
| 68 | + return 1; |
| 69 | +} |
| 70 | + |
| 71 | +void mbedtls_md5_init( mbedtls_md5_context *ctx ) |
| 72 | +{ |
| 73 | + mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); |
| 74 | + |
| 75 | + /* Enable HASH clock */ |
| 76 | + __HAL_RCC_HASH_CLK_ENABLE(); |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +void mbedtls_md5_free( mbedtls_md5_context *ctx ) |
| 81 | +{ |
| 82 | + if( ctx == NULL ) |
| 83 | + return; |
| 84 | + mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); |
| 85 | +} |
| 86 | + |
| 87 | +void mbedtls_md5_clone( mbedtls_md5_context *dst, |
| 88 | + const mbedtls_md5_context *src ) |
| 89 | +{ |
| 90 | + *dst = *src; |
| 91 | +} |
| 92 | + |
| 93 | +void mbedtls_md5_starts( mbedtls_md5_context *ctx ) |
| 94 | +{ |
| 95 | + /* HASH IP initialization */ |
| 96 | + if (HAL_HASH_DeInit(&ctx->hhash_md5) != 0) { |
| 97 | + // error found to be returned |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + /* HASH Configuration */ |
| 102 | + ctx->hhash_md5.Init.DataType = HASH_DATATYPE_8B; |
| 103 | + /* clear CR ALGO value */ |
| 104 | + HASH->CR &= ~HASH_CR_ALGO_Msk; |
| 105 | + if (HAL_HASH_Init(&ctx->hhash_md5) != 0) { |
| 106 | + // return error code |
| 107 | + return; |
| 108 | + } |
| 109 | + if (st_md5_save_hw_context(ctx) != 1) { |
| 110 | + return; // return HASH_BUSY timeout Error here |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] ) |
| 115 | +{ |
| 116 | + if (st_md5_restore_hw_context(ctx) != 1) { |
| 117 | + return; // Return HASH_BUSY timout error here |
| 118 | + } |
| 119 | + if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, ST_MD5_BLOCK_SIZE) != 0) { |
| 120 | + return; // Return error code here |
| 121 | + } |
| 122 | + if (st_md5_save_hw_context(ctx) != 1) { |
| 123 | + return; // return HASH_BUSY timeout Error here |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) |
| 128 | +{ |
| 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; |
| 139 | + } |
| 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 |
| 155 | + } |
| 156 | + } |
| 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); |
| 161 | + } |
| 162 | + } |
| 163 | + if (st_md5_save_hw_context(ctx) != 1) { |
| 164 | + return; // return HASH_BUSY timeout Error here |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) |
| 169 | +{ |
| 170 | + if (st_md5_restore_hw_context(ctx) != 1) { |
| 171 | + return; // Return HASH_BUSY timout error here |
| 172 | + } |
| 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 | + } |
| 177 | + } |
| 178 | + mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE); |
| 179 | + ctx->sbuf_len = 0; |
| 180 | + __HAL_HASH_START_DIGEST(); |
| 181 | + |
| 182 | + if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) { |
| 183 | + // error code to be returned |
| 184 | + } |
| 185 | + if (st_md5_save_hw_context(ctx) != 1) { |
| 186 | + return; // return HASH_BUSY timeout Error here |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +#endif /* MBEDTLS_MD5_ALT */ |
| 191 | +#endif /* MBEDTLS_MD5_C */ |
0 commit comments