|
| 1 | +/* |
| 2 | + * sha1_alt.c for SHA1 HASH |
| 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 | +#include "mbedtls/sha1.h" |
| 21 | +#if defined(MBEDTLS_SHA1_ALT) |
| 22 | +#include "mbedtls/platform.h" |
| 23 | + |
| 24 | +/* Implementation that should never be optimized out by the compiler */ |
| 25 | +static void mbedtls_zeroize( void *v, size_t n ) { |
| 26 | + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
| 27 | +} |
| 28 | + |
| 29 | +static int st_sha1_restore_hw_context(mbedtls_sha1_context *ctx) |
| 30 | +{ |
| 31 | + uint32_t i; |
| 32 | + uint32_t tickstart; |
| 33 | + /* allow multi-instance of HASH use: save context for HASH HW module CR */ |
| 34 | + /* Check that there is no HASH activity on going */ |
| 35 | + tickstart = HAL_GetTick(); |
| 36 | + while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) { |
| 37 | + if ((HAL_GetTick() - tickstart) > ST_SHA1_TIMEOUT) { |
| 38 | + return 0; // timeout: HASH processor is busy |
| 39 | + } |
| 40 | + } |
| 41 | + HASH->STR = ctx->ctx_save_str; |
| 42 | + HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT); |
| 43 | + for (i=0;i<38;i++) { |
| 44 | + HASH->CSR[i] = ctx->ctx_save_csr[i]; |
| 45 | + } |
| 46 | + return 1; |
| 47 | +} |
| 48 | + |
| 49 | +static int st_sha1_save_hw_context(mbedtls_sha1_context *ctx) |
| 50 | +{ |
| 51 | + uint32_t i; |
| 52 | + uint32_t tickstart; |
| 53 | + /* Check that there is no HASH activity on going */ |
| 54 | + tickstart = HAL_GetTick(); |
| 55 | + while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) { |
| 56 | + if ((HAL_GetTick() - tickstart) > ST_SHA1_TIMEOUT) { |
| 57 | + return 0; // timeout: HASH processor is busy |
| 58 | + } |
| 59 | + } |
| 60 | + /* allow multi-instance of HASH use: restore context for HASH HW module CR */ |
| 61 | + ctx->ctx_save_cr = HASH->CR; |
| 62 | + ctx->ctx_save_str = HASH->STR; |
| 63 | + for (i=0;i<38;i++) { |
| 64 | + ctx->ctx_save_csr[i] = HASH->CSR[i]; |
| 65 | + } |
| 66 | + return 1; |
| 67 | +} |
| 68 | + |
| 69 | +void mbedtls_sha1_init( mbedtls_sha1_context *ctx ) |
| 70 | +{ |
| 71 | + mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) ); |
| 72 | + |
| 73 | + /* Enable HASH clock */ |
| 74 | + __HAL_RCC_HASH_CLK_ENABLE(); |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +void mbedtls_sha1_free( mbedtls_sha1_context *ctx ) |
| 79 | +{ |
| 80 | + if( ctx == NULL ) |
| 81 | + return; |
| 82 | + mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) ); |
| 83 | +} |
| 84 | + |
| 85 | +void mbedtls_sha1_clone( mbedtls_sha1_context *dst, |
| 86 | + const mbedtls_sha1_context *src ) |
| 87 | +{ |
| 88 | + *dst = *src; |
| 89 | +} |
| 90 | + |
| 91 | +void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) |
| 92 | +{ |
| 93 | + /* Deinitializes the HASH peripheral */ |
| 94 | + if (HAL_HASH_DeInit(&ctx->hhash_sha1) == HAL_ERROR) { |
| 95 | + // error found to be returned |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + /* HASH Configuration */ |
| 100 | + ctx->hhash_sha1.Init.DataType = HASH_DATATYPE_8B; |
| 101 | + if (HAL_HASH_Init(&ctx->hhash_sha1) == HAL_ERROR) { |
| 102 | + // error found to be returned |
| 103 | + return; |
| 104 | + } |
| 105 | + if (st_sha1_save_hw_context(ctx) != 1) { |
| 106 | + return; // return HASH_BUSY timeout Error here |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] ) |
| 111 | +{ |
| 112 | + if (st_sha1_restore_hw_context(ctx) != 1) { |
| 113 | + return; // Return HASH_BUSY timout error here |
| 114 | + } |
| 115 | + if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, ST_SHA1_BLOCK_SIZE) != 0) { |
| 116 | + return; // Return error code |
| 117 | + } |
| 118 | + |
| 119 | + if (st_sha1_save_hw_context(ctx) != 1) { |
| 120 | + return; // return HASH_BUSY timeout Error here |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) |
| 125 | +{ |
| 126 | + size_t currentlen = ilen; |
| 127 | + if (st_sha1_restore_hw_context(ctx) != 1) { |
| 128 | + return; // Return HASH_BUSY timout error here |
| 129 | + } |
| 130 | + |
| 131 | + // store mechanism to accumulate ST_SHA1_BLOCK_SIZE bytes (512 bits) in the HW |
| 132 | + if (currentlen == 0){ // only change HW status is size if 0 |
| 133 | + if(ctx->hhash_sha1.Phase == HAL_HASH_PHASE_READY) { |
| 134 | + /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute |
| 135 | + the message digest of a new message */ |
| 136 | + HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT; |
| 137 | + } |
| 138 | + ctx->hhash_sha1.Phase = HAL_HASH_PHASE_PROCESS; |
| 139 | + } else if (currentlen < (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len)) { |
| 140 | + // only buffurize |
| 141 | + memcpy(ctx->sbuf + ctx->sbuf_len, input, currentlen); |
| 142 | + ctx->sbuf_len += currentlen; |
| 143 | + } else { |
| 144 | + // fill buffer and process it |
| 145 | + memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len)); |
| 146 | + currentlen -= (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len); |
| 147 | + mbedtls_sha1_process(ctx, ctx->sbuf); |
| 148 | + // Process every input as long as it is %64 bytes, ie 512 bits |
| 149 | + size_t iter = currentlen / ST_SHA1_BLOCK_SIZE; |
| 150 | + if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)(input + ST_SHA1_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA1_BLOCK_SIZE)) != 0) { |
| 151 | + return; // Return error code here |
| 152 | + } |
| 153 | + // sbuf is completely accumulated, now copy up to 63 remaining bytes |
| 154 | + ctx->sbuf_len = currentlen % ST_SHA1_BLOCK_SIZE; |
| 155 | + if (ctx->sbuf_len !=0) { |
| 156 | + memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len); |
| 157 | + } |
| 158 | + } |
| 159 | + if (st_sha1_save_hw_context(ctx) != 1) { |
| 160 | + return; // return HASH_BUSY timeout Error here |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ) |
| 165 | +{ |
| 166 | + if (st_sha1_restore_hw_context(ctx) != 1) { |
| 167 | + return; // Return HASH_BUSY timout error here |
| 168 | + } |
| 169 | + |
| 170 | + if (ctx->sbuf_len > 0) { |
| 171 | + if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) { |
| 172 | + return; // Return error code here |
| 173 | + } |
| 174 | + } |
| 175 | + mbedtls_zeroize(ctx->sbuf, ST_SHA1_BLOCK_SIZE); |
| 176 | + ctx->sbuf_len = 0; |
| 177 | + __HAL_HASH_START_DIGEST(); |
| 178 | + |
| 179 | + if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10) != 0){ |
| 180 | + return; // error code to be returned |
| 181 | + } |
| 182 | + if (st_sha1_save_hw_context(ctx) != 1) { |
| 183 | + return; // return HASH_BUSY timeout Error here |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +#endif /*MBEDTLS_SHA1_ALT*/ |
0 commit comments