Skip to content

Commit 4c50ec2

Browse files
committed
Add entropy functions for STM32F4 and STM32F7 family
1 parent 0f2566b commit 4c50ec2

File tree

3 files changed

+170
-1
lines changed

3 files changed

+170
-1
lines changed

hal/targets.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@
772772
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
773773
"inherits": ["Target"],
774774
"progen": {"target": "nucleo-f410rb"},
775+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
775776
"detect_code": ["0740"],
776777
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
777778
"release_versions": ["2", "5"]
@@ -808,7 +809,8 @@
808809
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI"],
809810
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
810811
"progen": {"target": "nucleo-f429zi"},
811-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
812+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
813+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
812814
"detect_code": ["0796"],
813815
"release_versions": ["2", "5"]
814816
},
@@ -854,6 +856,7 @@
854856
"extra_labels": ["STM", "STM32F7", "STM32F746", "STM32F746ZG"],
855857
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
856858
"default_toolchain": "ARM",
859+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
857860
"progen": {
858861
"target": "nucleo-f746zg",
859862
"iar": {
@@ -872,6 +875,7 @@
872875
"extra_labels": ["STM", "STM32F7", "STM32F767", "STM32F767ZI"],
873876
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
874877
"default_toolchain": "ARM",
878+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
875879
"progen": {"target": "nucleo-f767zi"},
876880
"detect_code": ["0818"],
877881
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
@@ -1037,6 +1041,7 @@
10371041
"core": "Cortex-M4F",
10381042
"default_toolchain": "ARM",
10391043
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI"],
1044+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
10401045
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10411046
"progen": {"target": "disco-f429zi"},
10421047
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
@@ -1049,6 +1054,7 @@
10491054
"extra_labels": ["STM", "STM32F4", "STM32F469", "STM32F469NI"],
10501055
"supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"],
10511056
"inherits": ["Target"],
1057+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
10521058
"progen": {"target": "disco-f469ni"},
10531059
"detect_code": ["0788"],
10541060
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
@@ -1070,6 +1076,7 @@
10701076
"extra_labels": ["STM", "STM32F7", "STM32F746", "STM32F746NG"],
10711077
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10721078
"default_toolchain": "ARM",
1079+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
10731080
"progen": {"target": "disco-f746ng"},
10741081
"detect_code": ["0815"],
10751082
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Hardware entropy collector for the STM32F4 family
3+
*
4+
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
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+
22+
#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
23+
defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
24+
defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F469xx) ||\
25+
defined(STM32F479xx)
26+
27+
#include <stdlib.h>
28+
#include "cmsis.h"
29+
30+
/* RNG handler declaration */
31+
RNG_HandleTypeDef RngHandle;
32+
33+
/*
34+
* Get one byte of entropy from the RNG, assuming it is up and running.
35+
*/
36+
static void rng_get_byte( unsigned char *byte )
37+
{
38+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
39+
}
40+
41+
/*
42+
* Get len bytes of entropy from the hardware RNG.
43+
*/
44+
int mbedtls_hardware_poll( void *data,
45+
unsigned char *output, size_t len, size_t *olen )
46+
{
47+
size_t i;
48+
int ret;
49+
((void) data);
50+
51+
/* RNG Peripheral clock enable */
52+
__HAL_RCC_RNG_CLK_ENABLE();
53+
54+
/* Initialize RNG instance */
55+
RngHandle.Instance = RNG;
56+
HAL_RNG_Init(&RngHandle);
57+
58+
/* Get Random byte */
59+
for( i = 0; i < len; i++ ){
60+
rng_get_byte( output + i );
61+
printf("output %i: %i\n",i,(int)*(output+i));
62+
63+
}
64+
/* Just be extra sure that we didn't do it wrong */
65+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 )
66+
{
67+
ret = -1;
68+
goto cleanup;
69+
}
70+
71+
*olen = len;
72+
ret = 0;
73+
74+
cleanup:
75+
/*Disable the RNG peripheral */
76+
HAL_RNG_DeInit(&RngHandle);
77+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
78+
__HAL_RCC_RNG_CLK_DISABLE();
79+
80+
81+
return( ret );
82+
}
83+
#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
84+
STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx */
85+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Hardware entropy collector for the STM32F7 family
3+
*
4+
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
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+
22+
#include <stdlib.h>
23+
#include "cmsis.h"
24+
25+
/* RNG handler declaration */
26+
RNG_HandleTypeDef RngHandle;
27+
28+
/*
29+
* Get one byte of entropy from the RNG, assuming it is up and running.
30+
*/
31+
static void rng_get_byte( unsigned char *byte )
32+
{
33+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
34+
}
35+
36+
/*
37+
* Get len bytes of entropy from the hardware RNG.
38+
*/
39+
int mbedtls_hardware_poll( void *data,
40+
unsigned char *output, size_t len, size_t *olen )
41+
{
42+
size_t i;
43+
int ret;
44+
((void) data);
45+
46+
/* RNG Peripheral clock enable */
47+
__HAL_RCC_RNG_CLK_ENABLE();
48+
49+
/* Initialize RNG instance */
50+
RngHandle.Instance = RNG;
51+
HAL_RNG_Init(&RngHandle);
52+
53+
54+
/* Get Random byte */
55+
for( i = 0; i < len; i++ )
56+
rng_get_byte( output + i );
57+
58+
/* Just be extra sure that we didn't do it wrong */
59+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 )
60+
{
61+
ret = -1;
62+
goto cleanup;
63+
}
64+
65+
*olen = len;
66+
ret = 0;
67+
68+
cleanup:
69+
/*Disable the RNG peripheral */
70+
HAL_RNG_DeInit(&RngHandle);
71+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
72+
__HAL_RCC_RNG_CLK_DISABLE();
73+
74+
75+
return( ret );
76+
}
77+

0 commit comments

Comments
 (0)