|
| 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 | + |
| 22 | +#if defined(MBEDTLS_SHA1_ALT) |
| 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 | +void mbedtls_sha1_init( mbedtls_sha1_context *ctx ) |
| 30 | +{ |
| 31 | + memset( ctx, 0, sizeof( mbedtls_sha1_context ) ); |
| 32 | + |
| 33 | + /* Enable HASH clock */ |
| 34 | + __HAL_RCC_HASH_CLK_ENABLE(); |
| 35 | + |
| 36 | + ctx->flag=0; |
| 37 | +} |
| 38 | + |
| 39 | +void mbedtls_sha1_free( mbedtls_sha1_context *ctx ) |
| 40 | +{ |
| 41 | + if( ctx == NULL ) |
| 42 | + return; |
| 43 | + |
| 44 | + /* Force the HASH Periheral Clock Reset */ |
| 45 | + __HAL_RCC_HASH_FORCE_RESET(); |
| 46 | + |
| 47 | + /* Release the HASH Periheral Clock Reset */ |
| 48 | + __HAL_RCC_HASH_RELEASE_RESET(); |
| 49 | + |
| 50 | + mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) ); |
| 51 | +} |
| 52 | + |
| 53 | +void mbedtls_sha1_clone( mbedtls_sha1_context *dst, |
| 54 | + const mbedtls_sha1_context *src ) |
| 55 | +{ |
| 56 | + *dst = *src; |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | + * SHA-1 context setup |
| 61 | + */ |
| 62 | +void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) |
| 63 | +{ |
| 64 | + /* Deinitializes the HASH peripheral */ |
| 65 | + if (HAL_HASH_DeInit(&ctx->hhash_sha1) == HAL_ERROR) { |
| 66 | + // error found to be returned |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + /* HASH Configuration */ |
| 71 | + ctx->hhash_sha1.Init.DataType = HASH_DATATYPE_8B; |
| 72 | + if (HAL_HASH_Init(&ctx->hhash_sha1) == HAL_ERROR) { |
| 73 | + // error found to be returned |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + ctx->flag=0; |
| 78 | +} |
| 79 | + |
| 80 | +void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ) |
| 81 | +{ |
| 82 | + HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, 64); |
| 83 | +} |
| 84 | + |
| 85 | +/* |
| 86 | + * SHA-1 process buffer |
| 87 | + */ |
| 88 | +void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) |
| 89 | +{ |
| 90 | + unsigned char *intermediate_buf=NULL; |
| 91 | + unsigned char modulus=0; |
| 92 | + unsigned char buf_len=0; |
| 93 | + |
| 94 | + // Accumulate cannot be called for a size <4 unless it is the last call |
| 95 | + |
| 96 | + modulus = ilen % 4; |
| 97 | + |
| 98 | + if (ilen <4) |
| 99 | + { |
| 100 | + ctx->sbuf=malloc(ilen); |
| 101 | + memcpy(ctx->sbuf, input, ilen); |
| 102 | + ctx->flag = 1; |
| 103 | + ctx->sbuf_len=ilen; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + if (modulus !=0) |
| 108 | + { |
| 109 | + buf_len = ilen - modulus; |
| 110 | + HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, buf_len); |
| 111 | + ctx->sbuf_len=modulus; |
| 112 | + ctx->sbuf=malloc(ctx->sbuf_len); |
| 113 | + memcpy(ctx->sbuf, input+buf_len, modulus); |
| 114 | + ctx->flag = 1; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + if (ctx->flag==0) |
| 119 | + HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, ilen); |
| 120 | + else |
| 121 | + { |
| 122 | + intermediate_buf=malloc(ilen+ctx->sbuf_len); |
| 123 | + memcpy(intermediate_buf, ctx->sbuf, ctx->sbuf_len); |
| 124 | + memcpy(intermediate_buf+ctx->sbuf_len, input, ilen); |
| 125 | + HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, intermediate_buf, ilen+ctx->sbuf_len); |
| 126 | + ctx->flag=0; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/* |
| 133 | + * SHA-1 final digest |
| 134 | + */ |
| 135 | +void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ) |
| 136 | +{ |
| 137 | + if (ctx->flag == 1) { |
| 138 | + HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len); |
| 139 | + ctx->flag=0; |
| 140 | + } |
| 141 | + |
| 142 | + __HAL_HASH_START_DIGEST(); |
| 143 | + |
| 144 | + if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10)){ |
| 145 | + // error code to be returned |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +#endif /*MBEDTLS_SHA1_ALT*/ |
0 commit comments