Skip to content

Commit 021b84a

Browse files
committed
Handle context swapping + rename macro ST_SHA256_BLOCK_SIZE
Handle 64 bytes accumulation
1 parent f170473 commit 021b84a

File tree

3 files changed

+214
-39
lines changed

3 files changed

+214
-39
lines changed

TESTS/mbedtls/multi/main.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include <stdio.h>
18+
#include <string.h>
19+
#include "mbed.h"
20+
#include "greentea-client/test_env.h"
21+
#include "unity/unity.h"
22+
#include "utest/utest.h"
23+
24+
#include "mbedtls/sha256.h"
25+
26+
27+
using namespace utest::v1;
28+
29+
#if defined(MBEDTLS_SHA256_C)
30+
/* Tests several call to mbedtls_sha256_update function that are not modulo 64 bytes */
31+
void test_case_sha256_split() {
32+
const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
33+
// sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
34+
const unsigned char test_sum[] =
35+
{ 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22,
36+
0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93,
37+
0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0,
38+
0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C};
39+
unsigned char outsum[32];
40+
int i;
41+
42+
mbedtls_sha256_context ctx;
43+
printf("test sha256\n");
44+
mbedtls_sha256_init( &ctx );
45+
mbedtls_sha256_starts( &ctx, 0);
46+
#if 0
47+
printf("test not splitted\n");
48+
mbedtls_sha256_update( &ctx, test_buf, 168 );
49+
#else
50+
printf("test splitted into 3 pieces\n");
51+
mbedtls_sha256_update( &ctx, test_buf, 2 );
52+
mbedtls_sha256_update( &ctx, test_buf+2, 66 );
53+
mbedtls_sha256_update( &ctx, test_buf+68, 100 );
54+
#endif
55+
56+
mbedtls_sha256_finish( &ctx, outsum );
57+
mbedtls_sha256_free( &ctx );
58+
59+
printf("\nreceived result : ");
60+
for (i=0;i<32;i++) { printf("%02X",outsum[i]);}
61+
printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
62+
63+
printf("\nend of test sha256\n");
64+
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum, test_sum,32);
65+
}
66+
67+
/* Tests that treating 2 sha256 objects in // does not impact the result */
68+
void test_case_sha256_multi() {
69+
const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
70+
// sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
71+
const unsigned char test_sum1[] =
72+
{ 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
73+
0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
74+
0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
75+
0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 };
76+
// sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
77+
const unsigned char test_sum2[] =
78+
{ 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22,
79+
0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93,
80+
0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0,
81+
0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C};
82+
unsigned char outsum1[32], outsum2[32];
83+
int i;
84+
85+
mbedtls_sha256_context ctx1;
86+
mbedtls_sha256_context ctx2;
87+
printf("test sha256_multi\n");
88+
//Init both contexts
89+
mbedtls_sha256_init( &ctx1 );
90+
mbedtls_sha256_init( &ctx2 );
91+
//Start both contexts
92+
mbedtls_sha256_starts( &ctx1, 0);
93+
mbedtls_sha256_starts( &ctx2, 0);
94+
95+
printf("upd ctx1\n");
96+
mbedtls_sha256_update( &ctx1, test_buf, 56 );
97+
printf("upd ctx2\n");
98+
mbedtls_sha256_update( &ctx2, test_buf, 66 );
99+
printf("finish ctx1\n");
100+
mbedtls_sha256_finish( &ctx1, outsum1 );
101+
printf("upd ctx2\n");
102+
mbedtls_sha256_update( &ctx2, test_buf+66, 46 );
103+
printf("free ctx1\n");
104+
mbedtls_sha256_free( &ctx1 );
105+
printf("upd ctx2\n");
106+
mbedtls_sha256_update( &ctx2, test_buf+112, 56 );
107+
printf("finish ctx2\n");
108+
mbedtls_sha256_finish( &ctx2, outsum2 );
109+
printf("free ctx2\n");
110+
mbedtls_sha256_free( &ctx2 );
111+
112+
printf("\nreceived result ctx1 : ");
113+
for (i=0;i<32;i++) { printf("%02X",outsum1[i]);}
114+
printf("\nawaited result : 248D6A61D20638B8E5C026930C3E6039A33CE45964FF216F6ECEDD19DB06C1\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
115+
printf("\nreceived result ctx2 : ");
116+
for (i=0;i<32;i++) { printf("%02X",outsum2[i]);}
117+
printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
118+
119+
printf("\nend of test sha256\n");
120+
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum1, test_sum1,32);
121+
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum2, test_sum2,32);
122+
}
123+
#endif //MBEDTLS_SHA256_C
124+
125+
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
126+
greentea_case_failure_abort_handler(source, reason);
127+
return STATUS_CONTINUE;
128+
}
129+
130+
Case cases[] = {
131+
#if defined(MBEDTLS_SHA256_C)
132+
Case("Crypto: sha256_split", test_case_sha256_split, greentea_failure_handler),
133+
Case("Crypto: sha256_multi", test_case_sha256_multi, greentea_failure_handler),
134+
#endif
135+
};
136+
137+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
138+
GREENTEA_SETUP(10, "default_auto");
139+
return greentea_test_setup_handler(number_of_cases);
140+
}
141+
142+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
143+
144+
int main() {
145+
Harness::run(specification);
146+
}

features/mbedtls/targets/TARGET_STM/sha256_alt.c

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

30+
static void st_sha256_restore_hw_context(mbedtls_sha256_context *ctx)
31+
{
32+
uint32_t i;
33+
/* allow multi-instance of HASH use: save context for HASH HW module CR */
34+
HASH->STR = ctx->ctx_save_str;
35+
HASH->CR = (ctx->ctx_save_cr|HASH_CR_INIT);
36+
for (i=0;i<38;i++) {
37+
HASH->CSR[i] = ctx->ctx_save_csr[i];
38+
}
39+
}
40+
41+
static void st_sha256_save_hw_context(mbedtls_sha256_context *ctx)
42+
{
43+
uint32_t i;
44+
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
45+
ctx->ctx_save_cr = HASH->CR;
46+
ctx->ctx_save_str = HASH->STR;
47+
for (i=0;i<38;i++) {
48+
ctx->ctx_save_csr[i] = HASH->CSR[i];
49+
}
50+
}
51+
3052
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
3153
{
3254
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
@@ -39,12 +61,6 @@ void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
3961
{
4062
if( ctx == NULL )
4163
return;
42-
/* Force the HASH Periheral Clock Reset */
43-
__HAL_RCC_HASH_FORCE_RESET();
44-
45-
/* Release the HASH Periheral Clock Reset */
46-
__HAL_RCC_HASH_RELEASE_RESET();
47-
4864
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
4965
}
5066

@@ -69,26 +85,31 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
6985
// error found to be returned
7086
return;
7187
}
88+
st_sha256_save_hw_context(ctx);
7289
}
7390

74-
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[MBEDTLS_SHA256_BLOCK_SIZE] )
91+
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] )
7592
{
93+
st_sha256_restore_hw_context(ctx);
7694
if (ctx->is224 == 0) {
77-
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, MBEDTLS_SHA256_BLOCK_SIZE) != 0) {
78-
// return 0; // Return error code
95+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) {
96+
return; // Return error code
7997
}
8098
} else {
81-
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, MBEDTLS_SHA256_BLOCK_SIZE) != 0) {
82-
// return 0; // Return error code
99+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) {
100+
return; // Return error code
83101
}
84102
}
85-
// return 1;
103+
104+
st_sha256_save_hw_context(ctx);
86105
}
87106

88107
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen )
89108
{
90109
size_t currentlen = ilen;
91-
// store mechanism to handle MBEDTLS_SHA256_BLOCK_SIZE bytes per MBEDTLS_SHA256_BLOCK_SIZE bytes
110+
st_sha256_restore_hw_context(ctx);
111+
112+
// store mechanism to accumulate ST_SHA256_BLOCK_SIZE bytes (512 bits) in the HW
92113
if (currentlen == 0){ // only change HW status is size if 0
93114
if(ctx->hhash_sha256.Phase == HAL_HASH_PHASE_READY) {
94115
/* Select the SHA256 or SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute
@@ -100,60 +121,65 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in
100121
}
101122
}
102123
ctx->hhash_sha256.Phase = HAL_HASH_PHASE_PROCESS;
103-
} else if (currentlen < (MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len)) {
124+
} else if (currentlen < (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len)) {
104125
// only buffurize
105126
memcpy(ctx->sbuf + ctx->sbuf_len, input, currentlen);
106127
ctx->sbuf_len += currentlen;
107128
} else {
108129
// fill buffer and process it
109-
memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_SHA256_BLOCK_SIZE-ctx->sbuf_len));
110-
currentlen -= (MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len);
130+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len));
131+
currentlen -= (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len);
111132
mbedtls_sha256_process(ctx, ctx->sbuf);
112-
// now process every input as long as it is %4 bytes
113-
size_t iter = currentlen / 4;
114-
if (ctx->is224 == 0) {
115-
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4)) != 0) {
116-
//return 1; // Return error code here
117-
}
118-
} else {
119-
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + MBEDTLS_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * 4)) != 0) {
120-
//return 1; // Return error code here
133+
// Process every input as long as it is %64 bytes, ie 512 bits
134+
size_t iter = currentlen / ST_SHA256_BLOCK_SIZE;
135+
if (iter !=0) {
136+
if (ctx->is224 == 0) {
137+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + ST_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA256_BLOCK_SIZE)) != 0) {
138+
return; // Return error code here
139+
}
140+
} else {
141+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + ST_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA256_BLOCK_SIZE)) != 0) {
142+
return; // Return error code here
143+
}
121144
}
122145
}
123-
// sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
124-
ctx->sbuf_len = currentlen % 4;
146+
// sbuf is completely accumulated, now copy up to 63 remaining bytes
147+
ctx->sbuf_len = currentlen % ST_SHA256_BLOCK_SIZE;
125148
if (ctx->sbuf_len !=0) {
126-
memcpy(ctx->sbuf, input + iter, ctx->sbuf_len);
149+
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
127150
}
128151
}
152+
st_sha256_save_hw_context(ctx);
129153
}
130154

131155
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
132156
{
157+
st_sha256_restore_hw_context(ctx);
133158
if (ctx->sbuf_len > 0) {
134159
if (ctx->is224 == 0) {
135160
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
136-
//return 1; // Return error code here
161+
return; // Return error code here
137162
}
138163
} else {
139164
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
140-
//return 1; // Return error code here
165+
return; // Return error code here
141166
}
142167
}
143168
}
144-
mbedtls_zeroize(ctx->sbuf, MBEDTLS_SHA256_BLOCK_SIZE);
169+
mbedtls_zeroize(ctx->sbuf, ST_SHA256_BLOCK_SIZE);
145170
ctx->sbuf_len = 0;
146171
__HAL_HASH_START_DIGEST();
147172

148173
if (ctx->is224 == 0) {
149174
if (HAL_HASHEx_SHA256_Finish(&ctx->hhash_sha256, output, 10) != 0) {
150-
//return 1; // Return error code here
175+
return; // Return error code here
151176
}
152177
} else {
153178
if (HAL_HASHEx_SHA224_Finish(&ctx->hhash_sha256, output, 10) != 0) {
154-
//return 1; // Return error code here
179+
return; // Return error code here
155180
}
156181
}
182+
st_sha256_save_hw_context(ctx);
157183
}
158184

159185
#endif /*MBEDTLS_SHA256_ALT*/

features/mbedtls/targets/TARGET_STM/sha256_alt.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@
3131
extern "C" {
3232
#endif
3333

34-
#define MBEDTLS_SHA256_BLOCK_SIZE ((size_t)(64)) // must be a multiple of 4
34+
#define ST_SHA256_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes
3535
/**
3636
* \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
37+
* \note HAL_HASH_SHA256_Accumulate will accumulate 512 bits packets, unless it is the last call to the function
38+
* A ST_SHA256_BLOCK_SIZE bytes buffer is used to save values and handle the processing
39+
* ST_SHA256_BLOCK_SIZE bytes per ST_SHA256_BLOCK_SIZE bytes
4040
* If sha256_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA256_Finish
4141
*/
4242
typedef struct
4343
{
4444
int is224; /*!< 0 => SHA-256, else SHA-224 */
4545
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 */
46+
unsigned char sbuf[ST_SHA256_BLOCK_SIZE]; /*!< ST_SHA256_BLOCK_SIZE buffer to store values so that algorithm is called once the buffer is filled */
4747
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */
48+
uint32_t ctx_save_cr;
49+
uint32_t ctx_save_str;
50+
uint32_t ctx_save_csr[38];
4851
}
4952
mbedtls_sha256_context;
5053

@@ -98,7 +101,7 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in
98101
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
99102

100103
/* Internal use */
101-
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[MBEDTLS_SHA256_BLOCK_SIZE] );
104+
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] );
102105

103106
#ifdef __cplusplus
104107
}

0 commit comments

Comments
 (0)