Skip to content

Commit 9edd0e8

Browse files
Merge pull request #4695 from 0xc0170/fix_4160
F756/F439: add mbedtls md5 hash feature
2 parents 1c94828 + 744c364 commit 9edd0e8

File tree

6 files changed

+311
-2
lines changed

6 files changed

+311
-2
lines changed

features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* mbedtls_device.h
2+
* mbedtls_device.h
33
*******************************************************************************
44
* Copyright (c) 2017, STMicroelectronics
55
* SPDX-License-Identifier: Apache-2.0
@@ -26,4 +26,6 @@
2626

2727
#define MBEDTLS_SHA1_ALT
2828

29+
#define MBEDTLS_MD5_ALT
30+
2931
#endif /* MBEDTLS_DEVICE_H */

features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* mbedtls_device.h
2+
* mbedtls_device.h
33
*******************************************************************************
44
* Copyright (c) 2017, STMicroelectronics
55
* SPDX-License-Identifier: Apache-2.0
@@ -25,5 +25,6 @@
2525

2626

2727
#define MBEDTLS_SHA256_ALT
28+
#define MBEDTLS_MD5_ALT
2829

2930
#endif /* MBEDTLS_DEVICE_H */
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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 */
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* \file md5_alt.h
3+
*
4+
* \brief MD5 hw acceleration (hash function)
5+
*
6+
* Copyright (c) 2017, STMicroelectronics
7+
* SPDX-License-Identifier: Apache-2.0
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
10+
* not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
*/
22+
#ifndef MBEDTLS_MD5_ALT_H
23+
#define MBEDTLS_MD5_ALT_H
24+
25+
#if defined(MBEDTLS_MD5_ALT)
26+
27+
#include "cmsis.h"
28+
#include <string.h>
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
#define ST_MD5_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes
35+
#define ST_MD5_TIMEOUT ((uint32_t) 3)
36+
37+
/**
38+
* \brief MD5 context structure
39+
* \note HAL_HASH_MD5_Accumulate will accumulate 512 bits packets, unless it is the last call to the function
40+
* A ST_MD5_BLOCK_SIZE bytes buffer is used to save values and handle the processing
41+
* ST_MD5_BLOCK_SIZE bytes per ST_MD5_BLOCK_SIZE bytes
42+
* If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish
43+
*/
44+
typedef struct
45+
{
46+
HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */
47+
unsigned char sbuf[ST_MD5_BLOCK_SIZE]; /*!< MBEDTLS_MD5_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
48+
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
49+
uint32_t ctx_save_cr;
50+
uint32_t ctx_save_str;
51+
uint32_t ctx_save_csr[38];
52+
}
53+
mbedtls_md5_context;
54+
55+
/**
56+
* \brief Initialize MD5 context
57+
*
58+
* \param ctx MD5 context to be initialized
59+
*/
60+
void mbedtls_md5_init( mbedtls_md5_context *ctx );
61+
62+
/**
63+
* \brief Clear MD5 context
64+
*
65+
* \param ctx MD5 context to be cleared
66+
*/
67+
void mbedtls_md5_free( mbedtls_md5_context *ctx );
68+
69+
/**
70+
* \brief Clone (the state of) an MD5 context
71+
*
72+
* \param dst The destination context
73+
* \param src The context to be cloned
74+
*/
75+
void mbedtls_md5_clone( mbedtls_md5_context *dst,
76+
const mbedtls_md5_context *src );
77+
78+
/**
79+
* \brief MD5 context setup
80+
*
81+
* \param ctx context to be initialized
82+
*/
83+
void mbedtls_md5_starts( mbedtls_md5_context *ctx );
84+
85+
/**
86+
* \brief MD5 process buffer
87+
*
88+
* \param ctx MD5 context
89+
* \param input buffer holding the data
90+
* \param ilen length of the input data
91+
*/
92+
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen );
93+
94+
/**
95+
* \brief MD5 final digest
96+
*
97+
* \param ctx MD5 context
98+
* \param output MD5 checksum result
99+
*/
100+
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] );
101+
102+
/* Internal use */
103+
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] );
104+
105+
#ifdef __cplusplus
106+
}
107+
#endif
108+
109+
#endif /* MBEDTLS_MD5_ALT */
110+
111+
#endif /* md5_alt.h */

features/mbedtls/targets/TARGET_STM/sha1_alt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
9898

9999
/* HASH Configuration */
100100
ctx->hhash_sha1.Init.DataType = HASH_DATATYPE_8B;
101+
/* clear CR ALGO value */
102+
HASH->CR &= ~HASH_CR_ALGO_Msk;
101103
if (HAL_HASH_Init(&ctx->hhash_sha1) == HAL_ERROR) {
102104
// error found to be returned
103105
return;

features/mbedtls/targets/TARGET_STM/sha256_alt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
9999
ctx->is224 = is224;
100100
/* HASH Configuration */
101101
ctx->hhash_sha256.Init.DataType = HASH_DATATYPE_8B;
102+
/* clear CR ALGO value */
103+
HASH->CR &= ~HASH_CR_ALGO_Msk;
102104
if (HAL_HASH_Init(&ctx->hhash_sha256) == HAL_ERROR) {
103105
// error found to be returned
104106
return;

0 commit comments

Comments
 (0)