Skip to content

Commit defa61d

Browse files
authored
Merge pull request #2253 from adustm/STM32F4_F7_entropy
[STM]Add RNG (random number generator) for STM32F4 STM32F7 families
2 parents f3d652b + 059c5a3 commit defa61d

File tree

3 files changed

+176
-5
lines changed

3 files changed

+176
-5
lines changed

hal/targets.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,10 +769,11 @@
769769
"supported_form_factors": ["ARDUINO", "MORPHO"],
770770
"core": "Cortex-M4F",
771771
"default_toolchain": "ARM",
772-
"extra_labels": ["STM", "STM32F4", "STM32F410RB"],
772+
"extra_labels": ["STM", "STM32F4", "STM32F410RB","STM32F410Rx"],
773773
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
774774
"inherits": ["Target"],
775775
"progen": {"target": "nucleo-f410rb"},
776+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
776777
"detect_code": ["0740"],
777778
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
778779
"release_versions": ["2", "5"]
@@ -806,10 +807,11 @@
806807
"inherits": ["Target"],
807808
"core": "Cortex-M4F",
808809
"default_toolchain": "ARM",
809-
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI"],
810+
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xx"],
810811
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
811812
"progen": {"target": "nucleo-f429zi"},
812-
"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"],
813+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
814+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
813815
"detect_code": ["0796"],
814816
"features": ["IPV4"],
815817
"release_versions": ["2", "5"]
@@ -856,6 +858,7 @@
856858
"extra_labels": ["STM", "STM32F7", "STM32F746", "STM32F746ZG"],
857859
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
858860
"default_toolchain": "ARM",
861+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
859862
"progen": {
860863
"target": "nucleo-f746zg",
861864
"iar": {
@@ -874,6 +877,7 @@
874877
"extra_labels": ["STM", "STM32F7", "STM32F767", "STM32F767ZI"],
875878
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
876879
"default_toolchain": "ARM",
880+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
877881
"progen": {"target": "nucleo-f767zi"},
878882
"detect_code": ["0818"],
879883
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
@@ -1039,7 +1043,8 @@
10391043
"inherits": ["Target"],
10401044
"core": "Cortex-M4F",
10411045
"default_toolchain": "ARM",
1042-
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI"],
1046+
"extra_labels": ["STM", "STM32F4", "STM32F429", "STM32F429ZI", "STM32F429xx"],
1047+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
10431048
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10441049
"progen": {"target": "disco-f429zi"},
10451050
"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,9 +1054,10 @@
10491054
"supported_form_factors": ["ARDUINO"],
10501055
"core": "Cortex-M4F",
10511056
"default_toolchain": "ARM",
1052-
"extra_labels": ["STM", "STM32F4", "STM32F469", "STM32F469NI"],
1057+
"extra_labels": ["STM", "STM32F4", "STM32F469", "STM32F469NI", "STM32F469xx"],
10531058
"supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"],
10541059
"inherits": ["Target"],
1060+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
10551061
"progen": {"target": "disco-f469ni"},
10561062
"detect_code": ["0788"],
10571063
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
@@ -1073,6 +1079,7 @@
10731079
"extra_labels": ["STM", "STM32F7", "STM32F746", "STM32F746NG"],
10741080
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10751081
"default_toolchain": "ARM",
1082+
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
10761083
"progen": {"target": "disco-f746ng"},
10771084
"detect_code": ["0815"],
10781085
"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(TARGET_STM32F405xx) || defined(TARGET_STM32F415xx) || defined(TARGET_STM32F407xx) || defined(TARGET_STM32F417xx) ||\
23+
defined(TARGET_STM32F427xx) || defined(TARGET_STM32F437xx) || defined(TARGET_STM32F429xx) || defined(TARGET_STM32F439xx) ||\
24+
defined(TARGET_STM32F410Tx) || defined(TARGET_STM32F410Cx) || defined(TARGET_STM32F410Rx) || defined(TARGET_STM32F469xx) ||\
25+
defined(TARGET_STM32F479xx)
26+
#include <stdlib.h>
27+
#include "cmsis.h"
28+
29+
/* RNG handler declaration */
30+
RNG_HandleTypeDef RngHandle;
31+
32+
33+
/** rng_get_byte
34+
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
35+
* @param pointer to the hardware generated random byte.
36+
*/
37+
static void rng_get_byte( unsigned char *byte )
38+
{
39+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
40+
}
41+
42+
43+
/** mbedtls_hardware_poll
44+
* @brief Get len bytes of entropy from the hardware RNG.
45+
* @param data pointer will be NULL
46+
* @param output pointer to the random generated bytes buffer
47+
* @param len input is the requested length of bytes to be generated
48+
* @param olen is the pointer to the length of bytes effectively generated
49+
* @returns 0 if the generation went well. -1 in case of error
50+
*/
51+
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
52+
{
53+
int ret;
54+
((void) data);
55+
56+
/* RNG Peripheral clock enable */
57+
__HAL_RCC_RNG_CLK_ENABLE();
58+
59+
/* Initialize RNG instance */
60+
RngHandle.Instance = RNG;
61+
HAL_RNG_Init(&RngHandle);
62+
63+
/* Get Random byte */
64+
for( uint32_t i = 0; i < len; i++ ){
65+
rng_get_byte( output + i );
66+
67+
}
68+
*olen = len;
69+
/* Just be extra sure that we didn't do it wrong */
70+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
71+
ret = -1;
72+
} else {
73+
ret = 0;
74+
}
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
/** rng_get_byte
30+
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
31+
* @param pointer to the hardware generated random byte.
32+
*/
33+
static void rng_get_byte( unsigned char *byte )
34+
{
35+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
36+
}
37+
38+
39+
/** mbedtls_hardware_poll
40+
* @brief Get len bytes of entropy from the hardware RNG.
41+
* @param data pointer will be NULL
42+
* @param output pointer to the random generated bytes buffer
43+
* @param len input is the requested length of bytes to be generated
44+
* @param olen is the pointer to the length of bytes effectively generated
45+
* @returns 0 if the generation went well. -1 in case of error
46+
*/
47+
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
48+
{
49+
int ret;
50+
((void) data);
51+
52+
/* RNG Peripheral clock enable */
53+
__HAL_RCC_RNG_CLK_ENABLE();
54+
55+
/* Initialize RNG instance */
56+
RngHandle.Instance = RNG;
57+
HAL_RNG_Init(&RngHandle);
58+
59+
/* Get Random byte */
60+
for( uint32_t i = 0; i < len; i++ ){
61+
rng_get_byte( output + i );
62+
63+
}
64+
*olen = len;
65+
/* Just be extra sure that we didn't do it wrong */
66+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
67+
ret = -1;
68+
} else {
69+
ret = 0;
70+
}
71+
/*Disable the RNG peripheral */
72+
HAL_RNG_DeInit(&RngHandle);
73+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
74+
__HAL_RCC_RNG_CLK_DISABLE();
75+
76+
77+
return( ret );
78+
}
79+

0 commit comments

Comments
 (0)