Skip to content

Commit 0a2a48c

Browse files
Merge pull request #4157 from adustm/STM_sha1_F439ZI
NUCLEO_F439ZI/mbedtls: add SHA1 hw_acceleration
2 parents d103979 + e63912f commit 0a2a48c

File tree

3 files changed

+299
-1
lines changed

3 files changed

+299
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
#define MBEDTLS_DEVICE_H
2222

2323
#define MBEDTLS_AES_ALT
24-
24+
#define MBEDTLS_SHA1_ALT
2525

2626
#endif /* MBEDTLS_DEVICE_H */
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* sha1_alt.c for SHA1 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/sha1.h"
21+
#if defined(MBEDTLS_SHA1_ALT)
22+
#include "mbedtls/platform.h"
23+
24+
/* Implementation that should never be optimized out by the compiler */
25+
static void mbedtls_zeroize( void *v, size_t n ) {
26+
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
27+
}
28+
29+
static int st_sha1_restore_hw_context(mbedtls_sha1_context *ctx)
30+
{
31+
uint32_t i;
32+
uint32_t tickstart;
33+
/* allow multi-instance of HASH use: save context for HASH HW module CR */
34+
/* Check that there is no HASH activity on going */
35+
tickstart = HAL_GetTick();
36+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
37+
if ((HAL_GetTick() - tickstart) > ST_SHA1_TIMEOUT) {
38+
return 0; // timeout: HASH processor is busy
39+
}
40+
}
41+
HASH->STR = ctx->ctx_save_str;
42+
HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT);
43+
for (i=0;i<38;i++) {
44+
HASH->CSR[i] = ctx->ctx_save_csr[i];
45+
}
46+
return 1;
47+
}
48+
49+
static int st_sha1_save_hw_context(mbedtls_sha1_context *ctx)
50+
{
51+
uint32_t i;
52+
uint32_t tickstart;
53+
/* Check that there is no HASH activity on going */
54+
tickstart = HAL_GetTick();
55+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
56+
if ((HAL_GetTick() - tickstart) > ST_SHA1_TIMEOUT) {
57+
return 0; // timeout: HASH processor is busy
58+
}
59+
}
60+
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
61+
ctx->ctx_save_cr = HASH->CR;
62+
ctx->ctx_save_str = HASH->STR;
63+
for (i=0;i<38;i++) {
64+
ctx->ctx_save_csr[i] = HASH->CSR[i];
65+
}
66+
return 1;
67+
}
68+
69+
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
70+
{
71+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
72+
73+
/* Enable HASH clock */
74+
__HAL_RCC_HASH_CLK_ENABLE();
75+
76+
}
77+
78+
void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
79+
{
80+
if( ctx == NULL )
81+
return;
82+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
83+
}
84+
85+
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
86+
const mbedtls_sha1_context *src )
87+
{
88+
*dst = *src;
89+
}
90+
91+
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
92+
{
93+
/* Deinitializes the HASH peripheral */
94+
if (HAL_HASH_DeInit(&ctx->hhash_sha1) == HAL_ERROR) {
95+
// error found to be returned
96+
return;
97+
}
98+
99+
/* HASH Configuration */
100+
ctx->hhash_sha1.Init.DataType = HASH_DATATYPE_8B;
101+
if (HAL_HASH_Init(&ctx->hhash_sha1) == HAL_ERROR) {
102+
// error found to be returned
103+
return;
104+
}
105+
if (st_sha1_save_hw_context(ctx) != 1) {
106+
return; // return HASH_BUSY timeout Error here
107+
}
108+
}
109+
110+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] )
111+
{
112+
if (st_sha1_restore_hw_context(ctx) != 1) {
113+
return; // Return HASH_BUSY timout error here
114+
}
115+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, ST_SHA1_BLOCK_SIZE) != 0) {
116+
return; // Return error code
117+
}
118+
119+
if (st_sha1_save_hw_context(ctx) != 1) {
120+
return; // return HASH_BUSY timeout Error here
121+
}
122+
}
123+
124+
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
125+
{
126+
size_t currentlen = ilen;
127+
if (st_sha1_restore_hw_context(ctx) != 1) {
128+
return; // Return HASH_BUSY timout error here
129+
}
130+
131+
// store mechanism to accumulate ST_SHA1_BLOCK_SIZE bytes (512 bits) in the HW
132+
if (currentlen == 0){ // only change HW status is size if 0
133+
if(ctx->hhash_sha1.Phase == HAL_HASH_PHASE_READY) {
134+
/* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute
135+
the message digest of a new message */
136+
HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT;
137+
}
138+
ctx->hhash_sha1.Phase = HAL_HASH_PHASE_PROCESS;
139+
} else if (currentlen < (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len)) {
140+
// only buffurize
141+
memcpy(ctx->sbuf + ctx->sbuf_len, input, currentlen);
142+
ctx->sbuf_len += currentlen;
143+
} else {
144+
// fill buffer and process it
145+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len));
146+
currentlen -= (ST_SHA1_BLOCK_SIZE - ctx->sbuf_len);
147+
mbedtls_sha1_process(ctx, ctx->sbuf);
148+
// Process every input as long as it is %64 bytes, ie 512 bits
149+
size_t iter = currentlen / ST_SHA1_BLOCK_SIZE;
150+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)(input + ST_SHA1_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_SHA1_BLOCK_SIZE)) != 0) {
151+
return; // Return error code here
152+
}
153+
// sbuf is completely accumulated, now copy up to 63 remaining bytes
154+
ctx->sbuf_len = currentlen % ST_SHA1_BLOCK_SIZE;
155+
if (ctx->sbuf_len !=0) {
156+
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
157+
}
158+
}
159+
if (st_sha1_save_hw_context(ctx) != 1) {
160+
return; // return HASH_BUSY timeout Error here
161+
}
162+
}
163+
164+
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
165+
{
166+
if (st_sha1_restore_hw_context(ctx) != 1) {
167+
return; // Return HASH_BUSY timout error here
168+
}
169+
170+
if (ctx->sbuf_len > 0) {
171+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) {
172+
return; // Return error code here
173+
}
174+
}
175+
mbedtls_zeroize(ctx->sbuf, ST_SHA1_BLOCK_SIZE);
176+
ctx->sbuf_len = 0;
177+
__HAL_HASH_START_DIGEST();
178+
179+
if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10) != 0){
180+
return; // error code to be returned
181+
}
182+
if (st_sha1_save_hw_context(ctx) != 1) {
183+
return; // return HASH_BUSY timeout Error here
184+
}
185+
}
186+
187+
#endif /*MBEDTLS_SHA1_ALT*/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* \file sha1_alt.h
3+
*
4+
* \brief SHA1 hw acceleration (hash function)
5+
*
6+
* Copyright (C) 2017, STMicroelectronics
7+
* SPDX-License-Identifier: Apache-2.0
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
10+
* not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
*/
22+
#ifndef MBEDTLS_SHA1_ALT_H
23+
#define MBEDTLS_SHA1_ALT_H
24+
25+
#if defined (MBEDTLS_SHA1_ALT)
26+
27+
28+
#include "cmsis.h"
29+
#include <string.h>
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
#define ST_SHA1_BLOCK_SIZE ((size_t) 64) // HW handles 512 bits, ie 64 bytes
36+
#define ST_SHA1_TIMEOUT ((uint32_t) 3)
37+
/**
38+
* \brief SHA-1 context structure
39+
* \note HAL_HASH_SHA1_Accumulate will accumulate 512 bits packets, unless it is the last call to the function
40+
* A ST_SHA1_BLOCK_SIZE bytes buffer is used to save values and handle the processing
41+
* multiples of ST_SHA1_BLOCK_SIZE bytes
42+
* If SHA1_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA1_Finish
43+
*/
44+
typedef struct
45+
{
46+
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 */
47+
unsigned char sbuf_len; /*!< number of bytes remaining in sbuf to be processed */
48+
HASH_HandleTypeDef hhash_sha1; /*!< ST HAL HASH struct */
49+
uint32_t ctx_save_cr;
50+
uint32_t ctx_save_str;
51+
uint32_t ctx_save_csr[38];
52+
}
53+
mbedtls_sha1_context;
54+
55+
/**
56+
* \brief Initialize SHA-1 context
57+
*
58+
* \param ctx SHA-1 context to be initialized
59+
*/
60+
void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
61+
62+
/**
63+
* \brief Clear SHA-1 context
64+
*
65+
* \param ctx SHA-1 context to be cleared
66+
*/
67+
void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
68+
69+
/**
70+
* \brief Clone (the state of) a SHA-1 context
71+
*
72+
* \param dst The destination context
73+
* \param src The context to be cloned
74+
*/
75+
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
76+
const mbedtls_sha1_context *src );
77+
78+
/**
79+
* \brief SHA-1 context setup
80+
*
81+
* \param ctx context to be initialized
82+
*/
83+
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
84+
85+
/**
86+
* \brief SHA-1 process buffer
87+
*
88+
* \param ctx SHA-1 context
89+
* \param input buffer holding the data
90+
* \param ilen length of the input data
91+
*/
92+
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen );
93+
94+
/**
95+
* \brief SHA-1 final digest
96+
*
97+
* \param ctx SHA-1 context
98+
* \param output SHA-1 checksum result
99+
*/
100+
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] );
101+
102+
/* Internal use */
103+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] );
104+
105+
#ifdef __cplusplus
106+
}
107+
#endif
108+
109+
#endif /* MBEDTLS_SHA1_ALT */
110+
111+
#endif /* sha1_alt.h */

0 commit comments

Comments
 (0)