|
| 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 | + |
| 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 | +void mbedtls_md5_init( mbedtls_md5_context *ctx ) |
| 32 | +{ |
| 33 | + mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); |
| 34 | + |
| 35 | + /* Enable HASH clock */ |
| 36 | + __HAL_RCC_HASH_CLK_ENABLE(); |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +void mbedtls_md5_free( mbedtls_md5_context *ctx ) |
| 41 | +{ |
| 42 | + if( ctx == NULL ) |
| 43 | + return; |
| 44 | + |
| 45 | + /* Force the HASH Periheral Clock Reset */ |
| 46 | + __HAL_RCC_HASH_FORCE_RESET(); |
| 47 | + |
| 48 | + /* Release the HASH Periheral Clock Reset */ |
| 49 | + __HAL_RCC_HASH_RELEASE_RESET(); |
| 50 | + |
| 51 | + mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); |
| 52 | +} |
| 53 | + |
| 54 | +void mbedtls_md5_clone( mbedtls_md5_context *dst, |
| 55 | + const mbedtls_md5_context *src ) |
| 56 | +{ |
| 57 | + *dst = *src; |
| 58 | +} |
| 59 | + |
| 60 | +/* |
| 61 | + * MD5 context setup |
| 62 | + */ |
| 63 | +void mbedtls_md5_starts( mbedtls_md5_context *ctx ) |
| 64 | +{ |
| 65 | + /* HASH IP initialization */ |
| 66 | + HAL_HASH_DeInit(&ctx->hhash_md5); |
| 67 | + |
| 68 | + /* HASH Configuration */ |
| 69 | + ctx->hhash_md5.Init.DataType = HASH_DATATYPE_8B; |
| 70 | + if (HAL_HASH_Init(&ctx->hhash_md5) == HAL_ERROR) { |
| 71 | + // return error code |
| 72 | + return; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] ) |
| 77 | +{ |
| 78 | + HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, 64); |
| 79 | +} |
| 80 | + |
| 81 | +/* |
| 82 | + * MD5 process buffer |
| 83 | + */ |
| 84 | +void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) |
| 85 | +{ |
| 86 | + unsigned char i=0; |
| 87 | + int currentlen = ilen; |
| 88 | + /* store mechanism to handle 64 bytes per 64 bytes */ |
| 89 | + while ((currentlen+ctx->sbuf_len) >=64) { |
| 90 | + if (ctx->sbuf_len ==0) { /* straight forward */ |
| 91 | + mbedtls_md5_process(ctx, input+(i*64)); |
| 92 | + } else { |
| 93 | + memcpy(ctx->sbuf+ctx->sbuf_len, input+(i*64),64-ctx->sbuf_len); |
| 94 | + mbedtls_md5_process(ctx, ctx->sbuf); |
| 95 | + memcpy(ctx->sbuf,input+(i+1)*64-ctx->sbuf_len, ctx->sbuf_len); |
| 96 | + // ctx->sbuf_len remains the same |
| 97 | + } |
| 98 | + currentlen -= 64; |
| 99 | + i++; |
| 100 | + } |
| 101 | + if (currentlen <0) { |
| 102 | + currentlen +=64; |
| 103 | + } |
| 104 | + /* Store the remaining <64 values */ |
| 105 | + memcpy(ctx->sbuf+ctx->sbuf_len, input+(i*64), currentlen); |
| 106 | + ctx->sbuf_len += currentlen; |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +/* |
| 111 | + * MD5 final digest |
| 112 | + */ |
| 113 | +void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) |
| 114 | +{ |
| 115 | + if (ctx->sbuf_len > 0) { |
| 116 | + HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len); |
| 117 | + } |
| 118 | + mbedtls_zeroize( ctx->sbuf, 64); |
| 119 | + ctx->sbuf_len = 0; |
| 120 | + __HAL_HASH_START_DIGEST(); |
| 121 | + |
| 122 | + if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) { |
| 123 | + // error code to be returned |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#endif /* MBEDTLS_MD5_ALT */ |
0 commit comments