Skip to content

Commit b84e53b

Browse files
committed
NUCLEO_F429ZI/mbedtls: add SHA1 hw_acceleration
1 parent cc58a7f commit b84e53b

File tree

3 files changed

+278
-1
lines changed

3 files changed

+278
-1
lines changed

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

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

2323
#define MBEDTLS_AES_ALT
24+
#define MBEDTLS_SHA1_ALT
2425

25-
26+
#define MBEDTLS_SHA1_C
2627
#endif /* MBEDTLS_DEVICE_H */
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
22+
#if defined(MBEDTLS_SHA1_ALT)
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+
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
30+
{
31+
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
32+
33+
/* Enable HASH clock */
34+
__HAL_RCC_HASH_CLK_ENABLE();
35+
36+
ctx->flag=0;
37+
}
38+
39+
void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
40+
{
41+
if( ctx == NULL )
42+
return;
43+
44+
/* Force the HASH Periheral Clock Reset */
45+
__HAL_RCC_HASH_FORCE_RESET();
46+
47+
/* Release the HASH Periheral Clock Reset */
48+
__HAL_RCC_HASH_RELEASE_RESET();
49+
50+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
51+
}
52+
53+
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
54+
const mbedtls_sha1_context *src )
55+
{
56+
*dst = *src;
57+
}
58+
59+
/*
60+
* SHA-1 context setup
61+
*/
62+
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
63+
{
64+
/* Deinitializes the HASH peripheral */
65+
if (HAL_HASH_DeInit(&ctx->hhash_sha1) == HAL_ERROR) {
66+
// error found to be returned
67+
return;
68+
}
69+
70+
/* HASH Configuration */
71+
ctx->hhash_sha1.Init.DataType = HASH_DATATYPE_8B;
72+
if (HAL_HASH_Init(&ctx->hhash_sha1) == HAL_ERROR) {
73+
// error found to be returned
74+
return;
75+
}
76+
77+
ctx->flag=0;
78+
}
79+
80+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
81+
{
82+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, 64);
83+
}
84+
85+
/*
86+
* SHA-1 process buffer
87+
*/
88+
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
89+
{
90+
unsigned char *intermediate_buf=NULL;
91+
unsigned char modulus=0;
92+
unsigned char buf_len=0;
93+
94+
// Accumulate cannot be called for a size <4 unless it is the last call
95+
96+
modulus = ilen % 4;
97+
98+
if (ilen <4)
99+
{
100+
ctx->sbuf=malloc(ilen);
101+
memcpy(ctx->sbuf, input, ilen);
102+
ctx->flag = 1;
103+
ctx->sbuf_len=ilen;
104+
}
105+
else
106+
{
107+
if (modulus !=0)
108+
{
109+
buf_len = ilen - modulus;
110+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, buf_len);
111+
ctx->sbuf_len=modulus;
112+
ctx->sbuf=malloc(ctx->sbuf_len);
113+
memcpy(ctx->sbuf, input+buf_len, modulus);
114+
ctx->flag = 1;
115+
}
116+
else
117+
{
118+
if (ctx->flag==0)
119+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, ilen);
120+
else
121+
{
122+
intermediate_buf=malloc(ilen+ctx->sbuf_len);
123+
memcpy(intermediate_buf, ctx->sbuf, ctx->sbuf_len);
124+
memcpy(intermediate_buf+ctx->sbuf_len, input, ilen);
125+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, intermediate_buf, ilen+ctx->sbuf_len);
126+
ctx->flag=0;
127+
}
128+
}
129+
}
130+
}
131+
132+
/*
133+
* SHA-1 final digest
134+
*/
135+
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
136+
{
137+
if (ctx->flag == 1) {
138+
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len);
139+
ctx->flag=0;
140+
}
141+
142+
__HAL_HASH_START_DIGEST();
143+
144+
if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10)){
145+
// error code to be returned
146+
}
147+
}
148+
149+
#endif /*MBEDTLS_SHA1_ALT*/
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* sha1_alt.h SHA-1 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+
#ifndef MBEDTLS_SHA1_ALT_H
21+
#define MBEDTLS_SHA1_ALT_H
22+
23+
#if defined MBEDTLS_SHA1_ALT
24+
25+
#include "mbedtls/platform.h"
26+
#include "mbedtls/config.h"
27+
28+
#include "cmsis.h"
29+
#include <string.h>
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
/**
36+
* \brief SHA-1 context structure
37+
*/
38+
typedef struct
39+
{
40+
unsigned char *sbuf;
41+
unsigned char sbuf_len;
42+
HASH_HandleTypeDef hhash_sha1;
43+
int flag; /* flag to manage buffer constraint of crypto Hw */
44+
}
45+
mbedtls_sha1_context;
46+
47+
/**
48+
* \brief Initialize SHA-1 context
49+
*
50+
* \param ctx SHA-1 context to be initialized
51+
*/
52+
void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
53+
54+
/**
55+
* \brief Clear SHA-1 context
56+
*
57+
* \param ctx SHA-1 context to be cleared
58+
*/
59+
void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
60+
61+
/**
62+
* \brief Clone (the state of) a SHA-1 context
63+
*
64+
* \param dst The destination context
65+
* \param src The context to be cloned
66+
*/
67+
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
68+
const mbedtls_sha1_context *src );
69+
70+
/**
71+
* \brief SHA-1 context setup
72+
*
73+
* \param ctx context to be initialized
74+
*/
75+
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
76+
77+
/**
78+
* \brief SHA-1 process buffer
79+
*
80+
* \param ctx SHA-1 context
81+
* \param input buffer holding the data
82+
* \param ilen length of the input data
83+
*/
84+
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen );
85+
86+
/**
87+
* \brief SHA-1 final digest
88+
*
89+
* \param ctx SHA-1 context
90+
* \param output SHA-1 checksum result
91+
*/
92+
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] );
93+
94+
/* Internal use */
95+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] );
96+
97+
#ifdef __cplusplus
98+
}
99+
#endif
100+
101+
#ifdef __cplusplus
102+
extern "C" {
103+
#endif
104+
105+
/**
106+
* \brief Output = SHA-1( input buffer )
107+
*
108+
* \param input buffer holding the data
109+
* \param ilen length of the input data
110+
* \param output SHA-1 checksum result
111+
*/
112+
void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
113+
114+
/**
115+
* \brief Checkup routine
116+
*
117+
* \return 0 if successful, or 1 if the test failed
118+
*/
119+
int mbedtls_sha1_self_test( int verbose );
120+
121+
#ifdef __cplusplus
122+
}
123+
#endif
124+
125+
#endif /* MBEDTLS_SHA1_ALT */
126+
127+
#endif /* sha1_alt.h */

0 commit comments

Comments
 (0)