Skip to content

Commit ddf65b7

Browse files
committed
mbedtls/F439ZI: add md5 HASH feature
1 parent 1f88490 commit ddf65b7

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222

2323
#define MBEDTLS_AES_ALT
2424

25+
#define MBEDTLS_MD5_ALT
2526

27+
#define MBEDTLS_MD5_C
2628
#endif /* MBEDTLS_DEVICE_H */
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* MD5 hw acceleration
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+
21+
#include "mbedtls/md5.h"
22+
23+
#if defined(MBEDTLS_MD5_ALT)
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+
void mbedtls_md5_init( mbedtls_md5_context *ctx )
31+
{
32+
memset( ctx, 0, sizeof( mbedtls_md5_context ) );
33+
34+
/* Enable HASH clock */
35+
__HAL_RCC_HASH_CLK_ENABLE();
36+
}
37+
38+
void mbedtls_md5_free( mbedtls_md5_context *ctx )
39+
{
40+
if( ctx == NULL )
41+
return;
42+
43+
/* Force the HASH Periheral Clock Reset */
44+
__HAL_RCC_HASH_FORCE_RESET();
45+
46+
/* Release the HASH Periheral Clock Reset */
47+
__HAL_RCC_HASH_RELEASE_RESET();
48+
49+
mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
50+
}
51+
52+
void mbedtls_md5_clone( mbedtls_md5_context *dst,
53+
const mbedtls_md5_context *src )
54+
{
55+
*dst = *src;
56+
}
57+
58+
/*
59+
* MD5 context setup
60+
*/
61+
void mbedtls_md5_starts( mbedtls_md5_context *ctx )
62+
{
63+
/* HASH IP initialization */
64+
HAL_HASH_DeInit(&ctx->hhash_md5);
65+
66+
/* HASH Configuration */
67+
ctx->hhash_md5.Init.DataType = HASH_DATATYPE_8B;
68+
HAL_HASH_Init(&ctx->hhash_md5);
69+
}
70+
71+
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] )
72+
{
73+
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, 64);
74+
}
75+
76+
/*
77+
* MD5 process buffer
78+
*/
79+
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
80+
{
81+
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)input, ilen);
82+
}
83+
84+
/*
85+
* MD5 final digest
86+
*/
87+
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
88+
{
89+
__HAL_HASH_START_DIGEST();
90+
91+
HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10);
92+
}
93+
94+
#endif /* MBEDTLS_MD5_ALT */
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* \file md5_alt.h
3+
*
4+
* \brief MD5 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_MD5_ALT_H
23+
#define MBEDTLS_MD5_ALT_H
24+
25+
#if defined(MBEDTLS_MD5_ALT)
26+
#include "mbedtls/platform.h"
27+
#include "mbedtls/config.h"
28+
29+
#include "cmsis.h"
30+
#include <string.h>
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
/**
37+
* \brief MD5 context structure
38+
*/
39+
typedef struct
40+
{
41+
uint32_t total[2]; /*!< number of bytes processed */
42+
uint32_t state[4]; /*!< intermediate digest state */
43+
unsigned char buffer[64]; /*!< data block being processed */
44+
HASH_HandleTypeDef hhash_md5;
45+
}
46+
mbedtls_md5_context;
47+
48+
/**
49+
* \brief Initialize MD5 context
50+
*
51+
* \param ctx MD5 context to be initialized
52+
*/
53+
void mbedtls_md5_init( mbedtls_md5_context *ctx );
54+
55+
/**
56+
* \brief Clear MD5 context
57+
*
58+
* \param ctx MD5 context to be cleared
59+
*/
60+
void mbedtls_md5_free( mbedtls_md5_context *ctx );
61+
62+
/**
63+
* \brief Clone (the state of) an MD5 context
64+
*
65+
* \param dst The destination context
66+
* \param src The context to be cloned
67+
*/
68+
void mbedtls_md5_clone( mbedtls_md5_context *dst,
69+
const mbedtls_md5_context *src );
70+
71+
/**
72+
* \brief MD5 context setup
73+
*
74+
* \param ctx context to be initialized
75+
*/
76+
void mbedtls_md5_starts( mbedtls_md5_context *ctx );
77+
78+
/**
79+
* \brief MD5 process buffer
80+
*
81+
* \param ctx MD5 context
82+
* \param input buffer holding the data
83+
* \param ilen length of the input data
84+
*/
85+
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen );
86+
87+
/**
88+
* \brief MD5 final digest
89+
*
90+
* \param ctx MD5 context
91+
* \param output MD5 checksum result
92+
*/
93+
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] );
94+
95+
/* Internal use */
96+
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] );
97+
98+
#ifdef __cplusplus
99+
}
100+
#endif
101+
102+
#ifdef __cplusplus
103+
extern "C" {
104+
#endif
105+
106+
/**
107+
* \brief Output = MD5( input buffer )
108+
*
109+
* \param input buffer holding the data
110+
* \param ilen length of the input data
111+
* \param output MD5 checksum result
112+
*/
113+
void mbedtls_md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
114+
115+
/**
116+
* \brief Checkup routine
117+
*
118+
* \return 0 if successful, or 1 if the test failed
119+
*/
120+
int mbedtls_md5_self_test( int verbose );
121+
122+
#ifdef __cplusplus
123+
}
124+
#endif
125+
126+
#endif /* MBEDTLS_MD5_ALT */
127+
128+
#endif /* md5_alt.h */

0 commit comments

Comments
 (0)