Skip to content

Commit a39ac60

Browse files
Merge pull request #4159 from adustm/STM_sha256_F439ZI
NUCLEO_F439ZI/mbedtls: add SHA256 hw_acceleration
2 parents 4fc4405 + ec72ac0 commit a39ac60

File tree

4 files changed

+500
-0
lines changed

4 files changed

+500
-0
lines changed

TESTS/mbedtls/multi/main.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
const unsigned char test_buf2[] = {"abcdefghijklmnopqrstuvwxyz012345678901234567890123456789"};
71+
72+
// sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
73+
const unsigned char test_sum1[] =
74+
{ 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
75+
0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
76+
0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
77+
0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 };
78+
// sha256_output_values for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
79+
const unsigned char test_sum2[] =
80+
{ 0x50, 0xEA, 0x82, 0x5D, 0x96, 0x84, 0xF4, 0x22,
81+
0x9C, 0xA2, 0x9F, 0x1F, 0xEC, 0x51, 0x15, 0x93,
82+
0xE2, 0x81, 0xE4, 0x6A, 0x14, 0x0D, 0x81, 0xE0,
83+
0x00, 0x5F, 0x8F, 0x68, 0x86, 0x69, 0xA0, 0x6C};
84+
// sha256_output_values for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdefghijklmnopqrstuvwxyz012345678901234567890123456789
85+
const unsigned char test_sum3[] =
86+
{ 0x6D, 0x5D, 0xDB, 0x5F, 0x4A, 0x94, 0xAB, 0x7E,
87+
0x5C, 0xF7, 0x9A, 0xD8, 0x3F, 0x58, 0xD3, 0x97,
88+
0xFE, 0x79, 0xFB, 0x0D, 0x79, 0xB2, 0x0D, 0x22,
89+
0xFF, 0x95, 0x9F, 0x04, 0xA2, 0xE4, 0x6C, 0x68};
90+
unsigned char outsum1[32], outsum2[32], outsum3[32];
91+
int i;
92+
93+
mbedtls_sha256_context ctx1;
94+
mbedtls_sha256_context ctx2;
95+
mbedtls_sha256_context ctx3;
96+
printf("test sha256_multi\n");
97+
//Init both contexts
98+
mbedtls_sha256_init( &ctx1);
99+
mbedtls_sha256_init( &ctx2);
100+
mbedtls_sha256_init( &ctx3);
101+
//Start both contexts
102+
mbedtls_sha256_starts( &ctx1, 0);
103+
mbedtls_sha256_starts( &ctx2, 0);
104+
105+
printf("upd ctx1\n");
106+
mbedtls_sha256_update( &ctx1, test_buf, 56 );
107+
printf("upd ctx2\n");
108+
mbedtls_sha256_update( &ctx2, test_buf, 66 );
109+
printf("finish ctx1\n");
110+
mbedtls_sha256_finish( &ctx1, outsum1 );
111+
printf("upd ctx2\n");
112+
mbedtls_sha256_update( &ctx2, test_buf+66, 46 );
113+
printf("clone ctx2 in ctx3\n");
114+
mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2);
115+
printf("free ctx1\n");
116+
mbedtls_sha256_free( &ctx1 );
117+
printf("upd ctx2\n");
118+
mbedtls_sha256_update( &ctx2, test_buf+112, 56 );
119+
printf("upd ctx3 with different values than ctx2\n");
120+
mbedtls_sha256_update( &ctx3, test_buf2, 56 );
121+
printf("finish ctx2\n");
122+
mbedtls_sha256_finish( &ctx2, outsum2 );
123+
printf("finish ctx3\n");
124+
mbedtls_sha256_finish( &ctx3, outsum3 );
125+
printf("free ctx2\n");
126+
mbedtls_sha256_free( &ctx2 );
127+
printf("free ctx3\n");
128+
mbedtls_sha256_free( &ctx3 );
129+
130+
printf("\nreceived result ctx1 : ");
131+
for (i=0;i<32;i++) { printf("%02X",outsum1[i]);}
132+
printf("\nawaited result : 248D6A61D20638B8E5C026930C3E6039A33CE45964FF216F6ECEDD19DB06C1\n"); // for abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
133+
printf("\nreceived result ctx2 : ");
134+
for (i=0;i<32;i++) { printf("%02X",outsum2[i]);}
135+
printf("\nawaited result : 50EA825D9684F4229CA29F1FEC511593E281E46A140D81E0005F8F688669A06C\n"); // for 3*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
136+
printf("\nreceived result ctx3 : ");
137+
for (i=0;i<32;i++) { printf("%02X",outsum3[i]);}
138+
printf("\nawaited result : 6D5DDB5F4A94AB7E5CF79AD83F58D397FE79FB0D79B20D22FF959F04A2E46C68\n"); // for 2*abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq+3*0123456789
139+
printf("\nend of test sha256\n");
140+
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum1, test_sum1,32);
141+
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum2, test_sum2,32);
142+
TEST_ASSERT_EQUAL_UINT8_ARRAY(outsum3, test_sum3,32);
143+
}
144+
#endif //MBEDTLS_SHA256_C
145+
146+
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
147+
greentea_case_failure_abort_handler(source, reason);
148+
return STATUS_CONTINUE;
149+
}
150+
151+
Case cases[] = {
152+
#if defined(MBEDTLS_SHA256_C)
153+
Case("Crypto: sha256_split", test_case_sha256_split, greentea_failure_handler),
154+
Case("Crypto: sha256_multi", test_case_sha256_multi, greentea_failure_handler),
155+
#endif
156+
};
157+
158+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
159+
GREENTEA_SETUP(10, "default_auto");
160+
return greentea_test_setup_handler(number_of_cases);
161+
}
162+
163+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
164+
165+
int main() {
166+
Harness::run(specification);
167+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#define MBEDTLS_DEVICE_H
2222

2323
#define MBEDTLS_AES_ALT
24+
25+
#define MBEDTLS_SHA256_ALT
26+
2427
#define MBEDTLS_SHA1_ALT
2528

2629
#endif /* MBEDTLS_DEVICE_H */
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
* sha256_alt.c for SHA256 HASH
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+
#include "mbedtls/sha256.h"
21+
22+
#if defined(MBEDTLS_SHA256_ALT)
23+
#include "mbedtls/platform.h"
24+
25+
/* Implementation that should never be optimized out by the compiler */
26+
static void mbedtls_zeroize( void *v, size_t n ) {
27+
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
28+
}
29+
30+
static int st_sha256_restore_hw_context(mbedtls_sha256_context *ctx)
31+
{
32+
uint32_t i;
33+
uint32_t tickstart;
34+
/* allow multi-instance of HASH use: save context for HASH HW module CR */
35+
/* Check that there is no HASH activity on going */
36+
tickstart = HAL_GetTick();
37+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
38+
if ((HAL_GetTick() - tickstart) > ST_SHA256_TIMEOUT) {
39+
return 0; // timeout: HASH processor is busy
40+
}
41+
}
42+
HASH->STR = ctx->ctx_save_str;
43+
HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT);
44+
for (i=0;i<38;i++) {
45+
HASH->CSR[i] = ctx->ctx_save_csr[i];
46+
}
47+
return 1;
48+
}
49+
50+
static int st_sha256_save_hw_context(mbedtls_sha256_context *ctx)
51+
{
52+
uint32_t i;
53+
uint32_t tickstart;
54+
/* Check that there is no HASH activity on going */
55+
tickstart = HAL_GetTick();
56+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
57+
if ((HAL_GetTick() - tickstart) > ST_SHA256_TIMEOUT) {
58+
return 0; // timeout: HASH processor is busy
59+
}
60+
}
61+
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
62+
ctx->ctx_save_cr = HASH->CR;
63+
ctx->ctx_save_str = HASH->STR;
64+
for (i=0;i<38;i++) {
65+
ctx->ctx_save_csr[i] = HASH->CSR[i];
66+
}
67+
return 1;
68+
}
69+
70+
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
71+
{
72+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
73+
74+
/* Enable HASH clock */
75+
__HAL_RCC_HASH_CLK_ENABLE();
76+
}
77+
78+
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
79+
{
80+
if( ctx == NULL )
81+
return;
82+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
83+
}
84+
85+
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
86+
const mbedtls_sha256_context *src )
87+
{
88+
*dst = *src;
89+
}
90+
91+
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
92+
{
93+
/* HASH IP initialization */
94+
if (HAL_HASH_DeInit(&ctx->hhash_sha256) == HAL_ERROR) {
95+
// error found to be returned
96+
return;
97+
}
98+
99+
ctx->is224 = is224;
100+
/* HASH Configuration */
101+
ctx->hhash_sha256.Init.DataType = HASH_DATATYPE_8B;
102+
if (HAL_HASH_Init(&ctx->hhash_sha256) == HAL_ERROR) {
103+
// error found to be returned
104+
return;
105+
}
106+
if (st_sha256_save_hw_context(ctx) != 1) {
107+
return; // return HASH_BUSY timeout Error here
108+
}
109+
}
110+
111+
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] )
112+
{
113+
if (st_sha256_restore_hw_context(ctx) != 1) {
114+
return; // Return HASH_BUSY timout error here
115+
}
116+
if (ctx->is224 == 0) {
117+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) {
118+
return; // Return error code
119+
}
120+
} else {
121+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) {
122+
return; // Return error code
123+
}
124+
}
125+
126+
if (st_sha256_save_hw_context(ctx) != 1) {
127+
return; // return HASH_BUSY timeout Error here
128+
}
129+
}
130+
131+
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen )
132+
{
133+
size_t currentlen = ilen;
134+
if (st_sha256_restore_hw_context(ctx) != 1) {
135+
return; // Return HASH_BUSY timout error here
136+
}
137+
138+
// store mechanism to accumulate ST_SHA256_BLOCK_SIZE bytes (512 bits) in the HW
139+
if (currentlen == 0) { // only change HW status is size if 0
140+
if(ctx->hhash_sha256.Phase == HAL_HASH_PHASE_READY) {
141+
/* Select the SHA256 or SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute
142+
the message digest of a new message */
143+
if (ctx->is224 == 0) {
144+
HASH->CR |= HASH_ALGOSELECTION_SHA256 | HASH_CR_INIT;
145+
} else {
146+
HASH->CR |= HASH_ALGOSELECTION_SHA224 | HASH_CR_INIT;
147+
}
148+
}
149+
ctx->hhash_sha256.Phase = HAL_HASH_PHASE_PROCESS;
150+
} else if (currentlen < (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len)) {
151+
// only buffurize
152+
memcpy(ctx->sbuf + ctx->sbuf_len, input, currentlen);
153+
ctx->sbuf_len += currentlen;
154+
} else {
155+
// fill buffer and process it
156+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len));
157+
currentlen -= (ST_SHA256_BLOCK_SIZE - ctx->sbuf_len);
158+
mbedtls_sha256_process(ctx, ctx->sbuf);
159+
// Process every input as long as it is %64 bytes, ie 512 bits
160+
size_t iter = currentlen / ST_SHA256_BLOCK_SIZE;
161+
if (iter !=0) {
162+
if (ctx->is224 == 0) {
163+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + ST_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA256_BLOCK_SIZE)) != 0) {
164+
return; // Return error code here
165+
}
166+
} else {
167+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *)(input + ST_SHA256_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA256_BLOCK_SIZE)) != 0) {
168+
return; // Return error code here
169+
}
170+
}
171+
}
172+
// sbuf is completely accumulated, now copy up to 63 remaining bytes
173+
ctx->sbuf_len = currentlen % ST_SHA256_BLOCK_SIZE;
174+
if (ctx->sbuf_len !=0) {
175+
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
176+
}
177+
}
178+
if (st_sha256_save_hw_context(ctx) != 1) {
179+
return; // return HASH_BUSY timeout Error here
180+
}
181+
}
182+
183+
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
184+
{
185+
if (st_sha256_restore_hw_context(ctx) != 1) {
186+
return; // Return HASH_BUSY timout error here
187+
}
188+
if (ctx->sbuf_len > 0) {
189+
if (ctx->is224 == 0) {
190+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
191+
return; // Return error code here
192+
}
193+
} else {
194+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
195+
return; // Return error code here
196+
}
197+
}
198+
}
199+
mbedtls_zeroize(ctx->sbuf, ST_SHA256_BLOCK_SIZE);
200+
ctx->sbuf_len = 0;
201+
__HAL_HASH_START_DIGEST();
202+
203+
if (ctx->is224 == 0) {
204+
if (HAL_HASHEx_SHA256_Finish(&ctx->hhash_sha256, output, 10) != 0) {
205+
return; // Return error code here
206+
}
207+
} else {
208+
if (HAL_HASHEx_SHA224_Finish(&ctx->hhash_sha256, output, 10) != 0) {
209+
return; // Return error code here
210+
}
211+
}
212+
if (st_sha256_save_hw_context(ctx) != 1) {
213+
return; // return HASH_BUSY timeout Error here
214+
}
215+
}
216+
217+
#endif /*MBEDTLS_SHA256_ALT*/

0 commit comments

Comments
 (0)