Skip to content

Commit 842791b

Browse files
committed
handle context swap + change macro name
1 parent 85d68e3 commit 842791b

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

features/mbedtls/targets/TARGET_STM/sha1_alt.c

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ static void mbedtls_zeroize( void *v, size_t n ) {
2626
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
2727
}
2828

29+
static void st_sha1_restore_hw_context(mbedtls_sha1_context *ctx)
30+
{
31+
uint32_t i;
32+
/* allow multi-instance of HASH use: save context for HASH HW module CR */
33+
HASH->STR = ctx->ctx_save_str;
34+
HASH->CR = (ctx->ctx_save_cr|HASH_CR_INIT);
35+
for (i=0;i<38;i++) {
36+
HASH->CSR[i] = ctx->ctx_save_csr[i];
37+
}
38+
}
39+
40+
static void st_sha1_save_hw_context(mbedtls_sha1_context *ctx)
41+
{
42+
uint32_t i;
43+
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
44+
ctx->ctx_save_cr = HASH->CR;
45+
ctx->ctx_save_str = HASH->STR;
46+
for (i=0;i<38;i++) {
47+
ctx->ctx_save_csr[i] = HASH->CSR[i];
48+
}
49+
}
50+
2951
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
3052
{
3153
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
@@ -39,13 +61,6 @@ void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
3961
{
4062
if( ctx == NULL )
4163
return;
42-
43-
/* Force the HASH Periheral Clock Reset */
44-
__HAL_RCC_HASH_FORCE_RESET();
45-
46-
/* Release the HASH Periheral Clock Reset */
47-
__HAL_RCC_HASH_RELEASE_RESET();
48-
4964
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
5065
}
5166

@@ -69,56 +84,72 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
6984
// error found to be returned
7085
return;
7186
}
87+
st_sha1_save_hw_context(ctx);
7288
}
7389

74-
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[MBEDTLS_SHA1_BLOCK_SIZE] )
90+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] )
7591
{
76-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, MBEDTLS_SHA1_BLOCK_SIZE);
92+
st_sha1_restore_hw_context(ctx);
93+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, ST_SHA1_BLOCK_SIZE) != 0) {
94+
return; // Return error code
95+
}
96+
97+
st_sha1_save_hw_context(ctx);
7798
}
7899

79100
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
80101
{
81102
size_t currentlen = ilen;
82-
// store mechanism to handle MBEDTLS_SHA1_BLOCK_SIZE bytes per MBEDTLS_SHA1_BLOCK_SIZE bytes
103+
st_sha1_restore_hw_context(ctx);
104+
105+
// store mechanism to accumulate ST_SHA1_BLOCK_SIZE bytes (512 bits) in the HW
83106
if (currentlen == 0){ // only change HW status is size if 0
84107
if(ctx->hhash_sha1.Phase == HAL_HASH_PHASE_READY) {
85108
/* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute
86109
the message digest of a new message */
87110
HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT;
88111
}
89112
ctx->hhash_sha1.Phase = HAL_HASH_PHASE_PROCESS;
90-
} else if (currentlen < (MBEDTLS_SHA1_BLOCK_SIZE-ctx->sbuf_len)) {
113+
} else if (currentlen < (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len)) {
91114
// only buffurize
92-
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
115+
memcpy(ctx->sbuf + ctx->sbuf_len, input, currentlen);
93116
ctx->sbuf_len += currentlen;
94117
} else {
95118
// fill buffer and process it
96-
memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_SHA1_BLOCK_SIZE-ctx->sbuf_len));
97-
currentlen -= (MBEDTLS_SHA1_BLOCK_SIZE-ctx->sbuf_len);
119+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len));
120+
currentlen -= (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len);
98121
mbedtls_sha1_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_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)(input+MBEDTLS_SHA1_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;
122+
// Process every input as long as it is %64 bytes, ie 512 bits
123+
size_t iter = currentlen / ST_SHA1_BLOCK_SIZE;
124+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)(input + ST_SHA1_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA1_BLOCK_SIZE)) != 0) {
125+
return; // Return error code here
126+
}
127+
// sbuf is completely accumulated, now copy up to 63 remaining bytes
128+
ctx->sbuf_len = currentlen % ST_SHA1_BLOCK_SIZE;
104129
if (ctx->sbuf_len !=0) {
105-
memcpy(ctx->sbuf, input+iter, ctx->sbuf_len);
130+
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
106131
}
107132
}
133+
st_sha1_save_hw_context(ctx);
108134
}
109135

110136
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
111137
{
138+
st_sha1_restore_hw_context(ctx);
139+
112140
if (ctx->sbuf_len > 0) {
113-
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len);
141+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) {
142+
return; // Return error code here
143+
}
114144
}
115-
mbedtls_zeroize(ctx->sbuf, MBEDTLS_SHA1_BLOCK_SIZE);
145+
mbedtls_zeroize(ctx->sbuf, ST_SHA1_BLOCK_SIZE);
116146
ctx->sbuf_len = 0;
117147
__HAL_HASH_START_DIGEST();
118148

119-
if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10)){
120-
// error code to be returned
149+
if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10) != 0){
150+
return; // error code to be returned
121151
}
152+
st_sha1_save_hw_context(ctx);
122153
}
123154

124155
#endif /*MBEDTLS_SHA1_ALT*/

features/mbedtls/targets/TARGET_STM/sha1_alt.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#ifndef MBEDTLS_SHA1_ALT_H
2323
#define MBEDTLS_SHA1_ALT_H
2424

25-
#if defined MBEDTLS_SHA1_ALT
25+
#if defined (MBEDTLS_SHA1_ALT)
2626

2727

2828
#include "cmsis.h"
@@ -32,19 +32,22 @@
3232
extern "C" {
3333
#endif
3434

35-
#define MBEDTLS_SHA1_BLOCK_SIZE (64) // must be a multiple of 4
35+
#define ST_SHA1_BLOCK_SIZE (64) // HW handles 512 bits, ie 64 bytes
3636
/**
3737
* \brief SHA-1 context structure
38-
* \note HAL_HASH_SHA1_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function
39-
* A MBEDTLS_SHA1_BLOCK_SIZE bytes buffer is used to save values and handle the processing
40-
* MBEDTLS_SHA1_BLOCK_SIZE bytes per MBEDTLS_SHA1_BLOCK_SIZE bytes
38+
* \note HAL_HASH_SHA1_Accumulate will accumulate 512 bits packets, unless it is the last call to the function
39+
* A ST_SHA1_BLOCK_SIZE bytes buffer is used to save values and handle the processing
40+
* multiples of ST_SHA1_BLOCK_SIZE bytes
4141
* If SHA1_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA1_Finish
4242
*/
4343
typedef struct
4444
{
45-
unsigned char sbuf[MBEDTLS_SHA1_BLOCK_SIZE]; /*!< MBEDTLS_SHA1_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
45+
unsigned char sbuf[ST_SHA1_BLOCK_SIZE]; /*!< MBEDTLS_SHA1_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
4646
unsigned char sbuf_len; /*!< number of bytes remaining in sbuf to be processed */
4747
HASH_HandleTypeDef hhash_sha1; /*!< ST HAL HASH struct */
48+
uint32_t ctx_save_cr;
49+
uint32_t ctx_save_str;
50+
uint32_t ctx_save_csr[38];
4851
}
4952
mbedtls_sha1_context;
5053

@@ -96,7 +99,7 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
9699
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] );
97100

98101
/* Internal use */
99-
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[MBEDTLS_SHA1_BLOCK_SIZE] );
102+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] );
100103

101104
#ifdef __cplusplus
102105
}

0 commit comments

Comments
 (0)