Skip to content

Commit f033c87

Browse files
adustm0xc0170
authored andcommitted
Handle context swap + Modify macro name ST_MD5_BLOCK_SIZE
1 parent 9572183 commit f033c87

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

features/mbedtls/targets/TARGET_STM/md5_alt.c

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*
1919
*/
20-
20+
#if defined(MBEDTLS_MD5_C)
2121
#include "mbedtls/md5.h"
2222

2323
#if defined(MBEDTLS_MD5_ALT)
@@ -28,6 +28,28 @@ static void mbedtls_zeroize( void *v, size_t n ) {
2828
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
2929
}
3030

31+
static void st_md5_restore_hw_context(mbedtls_md5_context *ctx)
32+
{
33+
uint32_t i;
34+
/* allow multi-instance of HASH use: save context for HASH HW module CR */
35+
HASH->STR = ctx->ctx_save_str;
36+
HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT);
37+
for (i=0;i<38;i++) {
38+
HASH->CSR[i] = ctx->ctx_save_csr[i];
39+
}
40+
}
41+
42+
static void st_md5_save_hw_context(mbedtls_md5_context *ctx)
43+
{
44+
uint32_t i;
45+
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
46+
ctx->ctx_save_cr = HASH->CR;
47+
ctx->ctx_save_str = HASH->STR;
48+
for (i=0;i<38;i++) {
49+
ctx->ctx_save_csr[i] = HASH->CSR[i];
50+
}
51+
}
52+
3153
void mbedtls_md5_init( mbedtls_md5_context *ctx )
3254
{
3355
mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
@@ -41,13 +63,6 @@ void mbedtls_md5_free( mbedtls_md5_context *ctx )
4163
{
4264
if( ctx == NULL )
4365
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-
5166
mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
5267
}
5368

@@ -60,65 +75,84 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst,
6075
void mbedtls_md5_starts( mbedtls_md5_context *ctx )
6176
{
6277
/* HASH IP initialization */
63-
HAL_HASH_DeInit(&ctx->hhash_md5);
78+
if (HAL_HASH_DeInit(&ctx->hhash_md5) != 0) {
79+
// error found to be returned
80+
return;
81+
}
6482

6583
/* HASH Configuration */
6684
ctx->hhash_md5.Init.DataType = HASH_DATATYPE_8B;
67-
if (HAL_HASH_Init(&ctx->hhash_md5) == HAL_ERROR) {
85+
if (HAL_HASH_Init(&ctx->hhash_md5) != 0) {
6886
// return error code
6987
return;
7088
}
89+
st_md5_save_hw_context(ctx);
7190
}
7291

73-
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[MBEDTLS_MD5_BLOCK_SIZE] )
92+
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] )
7493
{
75-
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, MBEDTLS_MD5_BLOCK_SIZE);
94+
st_md5_restore_hw_context(ctx);
95+
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, ST_MD5_BLOCK_SIZE) != 0) {
96+
return; // Return error code here
97+
}
98+
st_md5_save_hw_context(ctx);
7699
}
77100

78101
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
79102
{
80103
size_t currentlen = ilen;
81-
// store mechanism to handle MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes
104+
st_md5_restore_hw_context(ctx);
105+
106+
// store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
82107
if (currentlen == 0){ // only change HW status is size if 0
83-
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY)
84-
{
108+
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) {
85109
/* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
86110
the message digest of a new message */
87111
HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
88112
}
89113
ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS;
90-
} else if (currentlen < (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len)) {
114+
} else if (currentlen < (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)) {
91115
// only buffurize
92116
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
93117
ctx->sbuf_len += currentlen;
94118
} else {
95119
// fill buffer and process it
96-
memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len));
97-
currentlen -= (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len);
120+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len));
121+
currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len);
98122
mbedtls_md5_process(ctx, ctx->sbuf);
99-
// now process every input as long as it is %4 bytes
100-
size_t iter = currentlen / 4;
101-
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input+MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len), (iter*4));
102-
// sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
103-
ctx->sbuf_len = currentlen % 4;
123+
// Process every input as long as it is %64 bytes, ie 512 bits
124+
size_t iter = currentlen / ST_MD5_BLOCK_SIZE;
125+
if (iter !=0) {
126+
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) {
127+
return; // Return error code here
128+
}
129+
}
130+
// sbuf is completely accumulated, now copy up to 63 remaining bytes
131+
ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE;
104132
if (ctx->sbuf_len !=0) {
105-
memcpy(ctx->sbuf, input+iter, ctx->sbuf_len);
133+
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
106134
}
107135
}
136+
st_md5_save_hw_context(ctx);
108137
}
109138

110139
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
111140
{
141+
st_md5_restore_hw_context(ctx);
112142
if (ctx->sbuf_len > 0) {
113-
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len);
143+
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
144+
return; // Return error code here
145+
}
114146
}
115-
mbedtls_zeroize( ctx->sbuf, MBEDTLS_MD5_BLOCK_SIZE);
147+
mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE);
116148
ctx->sbuf_len = 0;
117149
__HAL_HASH_START_DIGEST();
118150

119151
if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) {
120152
// error code to be returned
121153
}
154+
st_md5_save_hw_context(ctx);
122155
}
123156

124157
#endif /* MBEDTLS_MD5_ALT */
158+
#endif /* MBEDTLS_MD5_C */

features/mbedtls/targets/TARGET_STM/md5_alt.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@
3131
extern "C" {
3232
#endif
3333

34-
#define MBEDTLS_MD5_BLOCK_SIZE (64) // must be a multiple of 4
34+
#define ST_MD5_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes
3535
/**
3636
* \brief MD5 context structure
37-
* \note HAL_HASH_MD5_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function
38-
* A MBEDTLS_MD5_BLOCK_SIZE bytes buffer is used to save values and handle the processing
39-
* MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes
37+
* \note HAL_HASH_MD5_Accumulate will accumulate 512 bits packets, unless it is the last call to the function
38+
* A ST_MD5_BLOCK_SIZE bytes buffer is used to save values and handle the processing
39+
* ST_MD5_BLOCK_SIZE bytes per ST_MD5_BLOCK_SIZE bytes
4040
* If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish
4141
*/
4242
typedef struct
4343
{
44-
HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */
45-
unsigned char sbuf[MBEDTLS_MD5_BLOCK_SIZE]; /*!< MBEDTLS_MD5_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
46-
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
44+
HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */
45+
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 */
46+
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
47+
uint32_t ctx_save_cr;
48+
uint32_t ctx_save_str;
49+
uint32_t ctx_save_csr[38];
4750
}
4851
mbedtls_md5_context;
4952

@@ -95,7 +98,7 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s
9598
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] );
9699

97100
/* Internal use */
98-
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] );
101+
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] );
99102

100103
#ifdef __cplusplus
101104
}

0 commit comments

Comments
 (0)