Skip to content

Commit 5385556

Browse files
committed
NUCLEO_F429ZI/mbedtls: add SHA256 hw_acceleration
1 parent 3541939 commit 5385556

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222

2323
#define MBEDTLS_AES_ALT
2424

25+
#define MBEDTLS_SHA256_ALT
2526

2627
#endif /* MBEDTLS_DEVICE_H */
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
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 = v; while( n-- ) *p++ = 0;
27+
}
28+
29+
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
30+
{
31+
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
32+
33+
/* Enable HASH clock */
34+
__HAL_RCC_HASH_CLK_ENABLE();
35+
}
36+
37+
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
38+
{
39+
if( ctx == NULL )
40+
return;
41+
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+
48+
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
49+
}
50+
51+
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
52+
const mbedtls_sha256_context *src )
53+
{
54+
*dst = *src;
55+
}
56+
57+
/*
58+
* SHA-256 context setup
59+
*/
60+
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
61+
{
62+
/* 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);
68+
69+
ctx->is224 = is224;
70+
}
71+
72+
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
73+
{
74+
if (ctx->is224 == 0)
75+
HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, 64);
76+
else
77+
HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, 64);
78+
}
79+
80+
/*
81+
* SHA-256 process buffer
82+
*/
83+
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen )
84+
{
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);
89+
}
90+
91+
/*
92+
* SHA-256 final digest
93+
*/
94+
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
95+
{
96+
__HAL_HASH_START_DIGEST();
97+
98+
if (ctx->is224 == 0)
99+
HAL_HASHEx_SHA256_Finish(&ctx->hhash_sha256, output, 10);
100+
else
101+
HAL_HASHEx_SHA224_Finish(&ctx->hhash_sha256, output, 10);
102+
}
103+
104+
#endif /*MBEDTLS_SHA256_ALT*/
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* sha256_alt.h SHA-256 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_SHA256_ALT_H
21+
#define MBEDTLS_SHA256_ALT_H
22+
23+
#if defined (MBEDTLS_SHA256_ALT)
24+
#include "mbedtls/platform.h"
25+
#include "mbedtls/config.h"
26+
27+
#include "cmsis.h"
28+
#include <string.h>
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
/**
34+
* \brief SHA-256 context structure
35+
*/
36+
typedef struct
37+
{
38+
int is224; /*!< 0 => SHA-256, else SHA-224 */
39+
HASH_HandleTypeDef hhash_sha256;
40+
}
41+
mbedtls_sha256_context;
42+
43+
/**
44+
* \brief Initialize SHA-256 context
45+
*
46+
* \param ctx SHA-256 context to be initialized
47+
*/
48+
void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
49+
50+
/**
51+
* \brief Clear SHA-256 context
52+
*
53+
* \param ctx SHA-256 context to be cleared
54+
*/
55+
void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
56+
57+
/**
58+
* \brief Clone (the state of) a SHA-256 context
59+
*
60+
* \param dst The destination context
61+
* \param src The context to be cloned
62+
*/
63+
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
64+
const mbedtls_sha256_context *src );
65+
66+
/**
67+
* \brief SHA-256 context setup
68+
*
69+
* \param ctx context to be initialized
70+
* \param is224 0 = use SHA256, 1 = use SHA224
71+
*/
72+
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
73+
74+
/**
75+
* \brief SHA-256 process buffer
76+
*
77+
* \param ctx SHA-256 context
78+
* \param input buffer holding the data
79+
* \param ilen length of the input data
80+
*/
81+
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
82+
size_t ilen );
83+
84+
/**
85+
* \brief SHA-256 final digest
86+
*
87+
* \param ctx SHA-256 context
88+
* \param output SHA-224/256 checksum result
89+
*/
90+
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
91+
92+
/* Internal use */
93+
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
94+
95+
#ifdef __cplusplus
96+
}
97+
#endif
98+
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+
125+
#endif /* MBEDTLS_SHA256_ALT */
126+
127+
#endif /* sha1_alt.h */

0 commit comments

Comments
 (0)