Skip to content

Commit 8dc7851

Browse files
adustmadbridge
authored andcommitted
Align SHA256 with MD5 and SHA1 implementation
This will solve Size <4 issues
1 parent 2300386 commit 8dc7851

File tree

2 files changed

+75
-56
lines changed

2 files changed

+75
-56
lines changed

features/mbedtls/targets/TARGET_STM/sha256_alt.c

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mbedtls/sha256.h"
2121

2222
#if defined(MBEDTLS_SHA256_ALT)
23+
#include "mbedtls/platform.h"
2324

2425
/* Implementation that should never be optimized out by the compiler */
2526
static void mbedtls_zeroize( void *v, size_t n ) {
@@ -28,7 +29,7 @@ static void mbedtls_zeroize( void *v, size_t n ) {
2829

2930
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
3031
{
31-
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
32+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
3233

3334
/* Enable HASH clock */
3435
__HAL_RCC_HASH_CLK_ENABLE();
@@ -38,8 +39,7 @@ void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
3839
{
3940
if( ctx == NULL )
4041
return;
41-
42-
/* Force the HASH Periheral Clock Reset */
42+
/* Force the HASH Periheral Clock Reset */
4343
__HAL_RCC_HASH_FORCE_RESET();
4444

4545
/* Release the HASH Periheral Clock Reset */
@@ -54,45 +54,82 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
5454
*dst = *src;
5555
}
5656

57-
/*
58-
* SHA-256 context setup
59-
*/
6057
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
6158
{
6259
/* HASH IP initialization */
63-
HAL_HASH_DeInit(&ctx->hhash_sha256);
64-
65-
/* HASH Configuration */
66-
ctx->hhash_sha256.Init.DataType = HASH_DATATYPE_8B;
67-
HAL_HASH_Init(&ctx->hhash_sha256);
60+
if (HAL_HASH_DeInit(&ctx->hhash_sha256) == HAL_ERROR) {
61+
// error found to be returned
62+
return;
63+
}
6864

6965
ctx->is224 = is224;
66+
/* HASH Configuration */
67+
ctx->hhash_sha256.Init.DataType = HASH_DATATYPE_8B;
68+
if (HAL_HASH_Init(&ctx->hhash_sha256) == HAL_ERROR) {
69+
// error found to be returned
70+
return;
71+
}
7072
}
7173

7274
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
7375
{
74-
if (ctx->is224 == 0)
76+
if (ctx->is224 == 0) {
7577
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, 64);
76-
else
78+
} else {
7779
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, 64);
80+
}
7881
}
7982

80-
/*
81-
* SHA-256 process buffer
82-
*/
8383
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen )
8484
{
85-
if (ctx->is224 == 0)
86-
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)input, ilen);
87-
else
88-
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)input, ilen);
85+
size_t currentlen = ilen;
86+
// store mechanism to handle MBEDTLS_SHA256_BLOCK_SIZE bytes per MBEDTLS_SHA256_BLOCK_SIZE bytes
87+
if (currentlen == 0){ // only change HW status is size if 0
88+
if(ctx->hhash_sha256.Phase == HAL_HASH_PHASE_READY) {
89+
/* Select the SHA256 or SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute
90+
the message digest of a new message */
91+
if (ctx->is224 == 0) {
92+
HASH->CR |= HASH_ALGOSELECTION_SHA256 | HASH_CR_INIT;
93+
} else {
94+
HASH->CR |= HASH_ALGOSELECTION_SHA224 | HASH_CR_INIT;
95+
}
96+
}
97+
ctx->hhash_sha256.Phase = HAL_HASH_PHASE_PROCESS;
98+
} else if (currentlen < (MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len)) {
99+
// only buffurize
100+
memcpy(ctx->sbuf + ctx->sbuf_len, input, currentlen);
101+
ctx->sbuf_len += currentlen;
102+
} else {
103+
// fill buffer and process it
104+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_SHA256_BLOCK_SIZE-ctx->sbuf_len));
105+
currentlen -= (MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len);
106+
mbedtls_sha256_process(ctx, ctx->sbuf);
107+
// now process every input as long as it is %4 bytes
108+
size_t iter = currentlen / 4;
109+
if (ctx->is224 == 0) {
110+
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4));
111+
} else {
112+
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4));
113+
}
114+
// sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
115+
ctx->sbuf_len = currentlen % 4;
116+
if (ctx->sbuf_len !=0) {
117+
memcpy(ctx->sbuf, input + iter, ctx->sbuf_len);
118+
}
119+
}
89120
}
90121

91-
/*
92-
* SHA-256 final digest
93-
*/
94122
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
95123
{
124+
if (ctx->sbuf_len > 0) {
125+
if (ctx->is224 == 0) {
126+
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len);
127+
} else {
128+
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len);
129+
}
130+
}
131+
mbedtls_zeroize(ctx->sbuf, MBEDTLS_SHA256_BLOCK_SIZE);
132+
ctx->sbuf_len = 0;
96133
__HAL_HASH_START_DIGEST();
97134

98135
if (ctx->is224 == 0)

features/mbedtls/targets/TARGET_STM/sha256_alt.h

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/*
2-
* sha256_alt.h SHA-256 hash
3-
*******************************************************************************
4-
* Copyright (C) 2017, STMicroelectronics
1+
/**
2+
* \file sha256_alt.h
3+
*
4+
* \brief SHA256 hw acceleration (hash function)
5+
*
6+
* Copyright (c) 2017, STMicroelectronics
57
* SPDX-License-Identifier: Apache-2.0
68
*
79
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -21,22 +23,28 @@
2123
#define MBEDTLS_SHA256_ALT_H
2224

2325
#if defined (MBEDTLS_SHA256_ALT)
24-
#include "mbedtls/platform.h"
25-
#include "mbedtls/config.h"
2626

2727
#include "cmsis.h"
2828
#include <string.h>
29+
2930
#ifdef __cplusplus
3031
extern "C" {
3132
#endif
3233

34+
#define MBEDTLS_SHA256_BLOCK_SIZE ((size_t)(64)) // must be a multiple of 4
3335
/**
3436
* \brief SHA-256 context structure
37+
* \note HAL_HASH_SHA256_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function
38+
* A MBEDTLS_SHA256_BLOCK_SIZE bytes buffer is used to save values and handle the processing
39+
* MBEDTLS_SHA256_BLOCK_SIZE bytes per MBEDTLS_SHA256_BLOCK_SIZE bytes
40+
* If sha256_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA256_Finish
3541
*/
3642
typedef struct
3743
{
3844
int is224; /*!< 0 => SHA-256, else SHA-224 */
3945
HASH_HandleTypeDef hhash_sha256;
46+
unsigned char sbuf[MBEDTLS_SHA256_BLOCK_SIZE]; /*!< MBEDTLS_SHA256_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
47+
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
4048
}
4149
mbedtls_sha256_context;
4250

@@ -96,32 +104,6 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char da
96104
}
97105
#endif
98106

99-
#ifdef __cplusplus
100-
extern "C" {
101-
#endif
102-
103-
/**
104-
* \brief Output = SHA-256( input buffer )
105-
*
106-
* \param input buffer holding the data
107-
* \param ilen length of the input data
108-
* \param output SHA-224/256 checksum result
109-
* \param is224 0 = use SHA256, 1 = use SHA224
110-
*/
111-
void mbedtls_sha256( const unsigned char *input, size_t ilen,
112-
unsigned char output[32], int is224 );
113-
114-
/**
115-
* \brief Checkup routine
116-
*
117-
* \return 0 if successful, or 1 if the test failed
118-
*/
119-
int mbedtls_sha256_self_test( int verbose );
120-
121-
#ifdef __cplusplus
122-
}
123-
#endif
124-
125107
#endif /* MBEDTLS_SHA256_ALT */
126108

127-
#endif /* sha1_alt.h */
109+
#endif /* sha256_alt.h */

0 commit comments

Comments
 (0)