Skip to content

Commit 2d83ea6

Browse files
adustmadbridge
authored andcommitted
Handle 64bytes per 64 bytes + remove unused includes files
1 parent 166a130 commit 2d83ea6

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 */

features/mbedtls/targets/TARGET_STM/md5_alt.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#define MBEDTLS_MD5_ALT_H
2424

2525
#if defined(MBEDTLS_MD5_ALT)
26-
#include "mbedtls/platform.h"
27-
#include "mbedtls/config.h"
2826

2927
#include "cmsis.h"
3028
#include <string.h>
@@ -35,10 +33,15 @@ extern "C" {
3533

3634
/**
3735
* \brief MD5 context structure
36+
* \note HAL_HASH_MD5_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function
37+
* A 64 bytes buffer is used to save values and handle the processing 64 bytes per 64 bytes
38+
* If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish
3839
*/
3940
typedef struct
4041
{
41-
HASH_HandleTypeDef hhash_md5;
42+
HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */
43+
unsigned char sbuf[64]; /*!< 64 buffer to store values so that algorithm is caled once the buffer is filled */
44+
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
4245
}
4346
mbedtls_md5_context;
4447

0 commit comments

Comments
 (0)