Skip to content

Commit e189bc7

Browse files
committed
Change MBEDTLS_ENTROPY_ALT to device has TRNG
update code accordingly
1 parent 5069c73 commit e189bc7

File tree

8 files changed

+79
-63
lines changed

8 files changed

+79
-63
lines changed

hal/targets/hal/TARGET_STM/TARGET_STM32L0/entropy_hardware_poll.c renamed to hal/targets/hal/TARGET_STM/TARGET_STM32L0/trng_api.c

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,66 +18,63 @@
1818
*
1919
*/
2020

21+
#if defined(DEVICE_TRNG)
2122

2223
#if defined (TARGET_STM32L052xx) || defined (TARGET_STM32L053xx) || defined (TARGET_STM32L062xx) || defined (TARGET_STM32L063xx) || \
2324
defined (TARGET_STM32L072xx) || defined (TARGET_STM32L073xx) || defined (TARGET_STM32L082xx) || defined (TARGET_STM32L083xx)
2425

2526
#include <stdlib.h>
2627
#include "cmsis.h"
28+
#include "trng_api.h"
2729

28-
/* RNG handler declaration */
29-
RNG_HandleTypeDef RngHandle;
30-
31-
32-
/** rng_get_byte
30+
/** trng_get_byte
3331
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
32+
* @param obj TRNG obj
3433
* @param pointer to the hardware generated random byte.
3534
*/
36-
static void rng_get_byte( unsigned char *byte )
35+
static void trng_get_byte(trng_t *obj, unsigned char *byte )
3736
{
38-
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
37+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle);
3938
}
4039

41-
42-
/** mbedtls_hardware_poll
43-
* @brief Get len bytes of entropy from the hardware RNG.
44-
* @param data pointer will be NULL
45-
* @param output pointer to the random generated bytes buffer
46-
* @param len input is the requested length of bytes to be generated
47-
* @param olen is the pointer to the length of bytes effectively generated
48-
* @returns 0 if the generation went well. -1 in case of error
49-
*/
50-
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
40+
void trng_init(trng_t *obj)
5141
{
52-
int ret;
53-
((void) data);
54-
5542
/* RNG Peripheral clock enable */
5643
__HAL_RCC_RNG_CLK_ENABLE();
5744

5845
/* Initialize RNG instance */
59-
RngHandle.Instance = RNG;
60-
HAL_RNG_Init(&RngHandle);
46+
obj->handle.Instance = RNG;
47+
HAL_RNG_Init(&obj->handle);
6148

62-
/* Get Random byte */
63-
for( uint32_t i = 0; i < len; i++ ){
64-
rng_get_byte( output + i );
49+
}
6550

51+
void trng_free(trng_t *obj)
52+
{
53+
/*Disable the RNG peripheral */
54+
HAL_RNG_DeInit(&obj->handle);
55+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
56+
__HAL_RCC_RNG_CLK_DISABLE();
57+
}
58+
59+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
60+
{
61+
int ret;
62+
63+
/* Get Random byte */
64+
for( uint32_t i = 0; i < length; i++ ){
65+
trng_get_byte(obj, output + i );
6666
}
67-
*olen = len;
67+
68+
*output_length = length;
6869
/* Just be extra sure that we didn't do it wrong */
69-
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
70+
if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) {
7071
ret = -1;
7172
} else {
7273
ret = 0;
7374
}
74-
/*Disable the RNG peripheral */
75-
HAL_RNG_DeInit(&RngHandle);
76-
/* RNG Peripheral clock disable - assume we're the only users of RNG */
77-
__HAL_RCC_RNG_CLK_DISABLE();
78-
7975

8076
return( ret );
8177
}
8278
#endif /* STM32L073RZ */
8379

80+
#endif

hal/targets/hal/TARGET_STM/TARGET_STM32L4/entropy_hardware_poll.c renamed to hal/targets/hal/TARGET_STM/TARGET_STM32L4/trng_api.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,24 @@
1818
*
1919
*/
2020

21+
#if defined(DEVICE_TRNG)
2122

2223
#include <stdlib.h>
2324
#include "cmsis.h"
25+
#include "trng_api.h"
2426

25-
/* RNG handler declaration */
26-
RNG_HandleTypeDef RngHandle;
27-
28-
29-
/** rng_get_byte
27+
/** trng_get_byte
3028
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
29+
* @param obj TRNG obj
3130
* @param pointer to the hardware generated random byte.
3231
*/
33-
static void rng_get_byte( unsigned char *byte )
32+
static void trng_get_byte(trng_t *obj, unsigned char *byte )
3433
{
35-
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
34+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle);
3635
}
3736

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 )
37+
void trng_init(trng_t *obj)
4838
{
49-
int ret;
50-
((void) data);
5139
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
5240

5341
/*Select PLLQ output as RNG clock source */
@@ -59,27 +47,38 @@ int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t
5947
__HAL_RCC_RNG_CLK_ENABLE();
6048

6149
/* Initialize RNG instance */
62-
RngHandle.Instance = RNG;
63-
HAL_RNG_Init(&RngHandle);
50+
obj->handle.Instance = RNG;
51+
HAL_RNG_Init(&obj->handle);
6452

65-
/* Get Random byte */
66-
for( uint32_t i = 0; i < len; i++ ){
67-
rng_get_byte( output + i );
53+
}
6854

55+
void trng_free(trng_t *obj)
56+
{
57+
/*Disable the RNG peripheral */
58+
HAL_RNG_DeInit(&obj->handle);
59+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
60+
__HAL_RCC_RNG_CLK_DISABLE();
61+
}
62+
63+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
64+
{
65+
int ret;
66+
67+
/* Get Random byte */
68+
for( uint32_t i = 0; i < length; i++ ){
69+
trng_get_byte(obj, output + i );
6970
}
70-
*olen = len;
71+
72+
*output_length = length;
7173
/* Just be extra sure that we didn't do it wrong */
72-
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
74+
if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) {
7375
ret = -1;
7476
} else {
7577
ret = 0;
7678
}
77-
/*Disable the RNG peripheral */
78-
HAL_RNG_DeInit(&RngHandle);
79-
/* RNG Peripheral clock disable - assume we're the only users of RNG */
80-
__HAL_RCC_RNG_CLK_DISABLE();
8179

8280
return( ret );
8381
}
8482

8583

84+
#endif

targets/TARGET_STM/TARGET_STM32F7/trng_api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
/** trng_get_byte
2828
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
29+
* @param obj TRNG obj
2930
* @param pointer to the hardware generated random byte.
3031
*/
3132
static void trng_get_byte(trng_t *obj, unsigned char *byte )

targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ struct i2c_s {
8484
I2CName i2c;
8585
};
8686

87+
struct trng_s {
88+
RNG_HandleTypeDef handle;
89+
};
90+
8791
#include "common_objects.h"
8892
#include "gpio_object.h"
8993

targets/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ struct can_s {
9090
int index;
9191
};
9292

93+
struct trng_s {
94+
RNG_HandleTypeDef handle;
95+
};
96+
9397
#include "common_objects.h"
9498
#include "gpio_object.h"
9599

targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L432KC/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ struct can_s {
9090
int index;
9191
};
9292

93+
struct trng_s {
94+
RNG_HandleTypeDef handle;
95+
};
96+
9397
#include "gpio_object.h"
9498
#include "common_objects.h"
9599

targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ struct can_s {
9090
int index;
9191
};
9292

93+
struct trng_s {
94+
RNG_HandleTypeDef handle;
95+
};
96+
9397
#include "common_objects.h"
9498
#include "gpio_object.h"
9599

targets/targets.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@
920920
"inherits": ["Target"],
921921
"detect_code": ["0760"],
922922
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
923+
"release_versions": ["2", "5"]
923924
"device_name": "STM32L073RZ"
924925
},
925926
"NUCLEO_L152RE": {
@@ -943,7 +944,7 @@
943944
"inherits": ["Target"],
944945
"detect_code": ["0770"],
945946
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "CAN", "STDIO_MESSAGES", "TRNG"],
946-
947+
"release_versions": ["2", "5"]
947948
"device_name" : "STM32L432KC"
948949
},
949950
"NUCLEO_L476RG": {
@@ -955,6 +956,7 @@
955956
"inherits": ["Target"],
956957
"detect_code": ["0765"],
957958
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
959+
"release_versions": ["2", "5"]
958960
"device_name": "stm32l476rg"
959961
},
960962
"STM32F3XX": {
@@ -1099,6 +1101,7 @@
10991101
"supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"],
11001102
"detect_code": ["0820"],
11011103
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
1104+
"release_versions": ["2", "5"]
11021105
"device_name": "stm32l476vg"
11031106
},
11041107
"MTS_MDOT_F405RG": {

0 commit comments

Comments
 (0)