diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32L0/entropy_hardware_poll.c b/hal/targets/hal/TARGET_STM/TARGET_STM32L0/trng_api.c similarity index 63% rename from hal/targets/hal/TARGET_STM/TARGET_STM32L0/entropy_hardware_poll.c rename to hal/targets/hal/TARGET_STM/TARGET_STM32L0/trng_api.c index 611a5e3ff1..c8fd7dbe5f 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32L0/entropy_hardware_poll.c +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32L0/trng_api.c @@ -18,66 +18,63 @@ * */ +#if defined(DEVICE_TRNG) #if defined (TARGET_STM32L052xx) || defined (TARGET_STM32L053xx) || defined (TARGET_STM32L062xx) || defined (TARGET_STM32L063xx) || \ defined (TARGET_STM32L072xx) || defined (TARGET_STM32L073xx) || defined (TARGET_STM32L082xx) || defined (TARGET_STM32L083xx) #include #include "cmsis.h" +#include "trng_api.h" -/* RNG handler declaration */ -RNG_HandleTypeDef RngHandle; - - -/** rng_get_byte +/** trng_get_byte * @brief Get one byte of entropy from the RNG, assuming it is up and running. + * @param obj TRNG obj * @param pointer to the hardware generated random byte. */ -static void rng_get_byte( unsigned char *byte ) +static void trng_get_byte(trng_t *obj, unsigned char *byte ) { - *byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle); + *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle); } - -/** mbedtls_hardware_poll - * @brief Get len bytes of entropy from the hardware RNG. - * @param data pointer will be NULL - * @param output pointer to the random generated bytes buffer - * @param len input is the requested length of bytes to be generated - * @param olen is the pointer to the length of bytes effectively generated - * @returns 0 if the generation went well. -1 in case of error - */ -int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) +void trng_init(trng_t *obj) { - int ret; - ((void) data); - /* RNG Peripheral clock enable */ __HAL_RCC_RNG_CLK_ENABLE(); /* Initialize RNG instance */ - RngHandle.Instance = RNG; - HAL_RNG_Init(&RngHandle); + obj->handle.Instance = RNG; + HAL_RNG_Init(&obj->handle); + +} + +void trng_free(trng_t *obj) +{ + /*Disable the RNG peripheral */ + HAL_RNG_DeInit(&obj->handle); + /* RNG Peripheral clock disable - assume we're the only users of RNG */ + __HAL_RCC_RNG_CLK_DISABLE(); +} + +int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) +{ + int ret; /* Get Random byte */ - for( uint32_t i = 0; i < len; i++ ){ - rng_get_byte( output + i ); - + for( uint32_t i = 0; i < length; i++ ){ + trng_get_byte(obj, output + i ); } - *olen = len; + + *output_length = length; /* Just be extra sure that we didn't do it wrong */ - if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) { + if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) { ret = -1; } else { ret = 0; } - /*Disable the RNG peripheral */ - HAL_RNG_DeInit(&RngHandle); - /* RNG Peripheral clock disable - assume we're the only users of RNG */ - __HAL_RCC_RNG_CLK_DISABLE(); - return( ret ); } #endif /* STM32L073RZ */ +#endif diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/entropy_hardware_poll.c b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/trng_api.c similarity index 63% rename from hal/targets/hal/TARGET_STM/TARGET_STM32L4/entropy_hardware_poll.c rename to hal/targets/hal/TARGET_STM/TARGET_STM32L4/trng_api.c index e7a880f44b..64483b8f12 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/entropy_hardware_poll.c +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/trng_api.c @@ -18,36 +18,24 @@ * */ +#if defined(DEVICE_TRNG) #include #include "cmsis.h" +#include "trng_api.h" -/* RNG handler declaration */ -RNG_HandleTypeDef RngHandle; - - -/** rng_get_byte +/** trng_get_byte * @brief Get one byte of entropy from the RNG, assuming it is up and running. + * @param obj TRNG obj * @param pointer to the hardware generated random byte. */ -static void rng_get_byte( unsigned char *byte ) +static void trng_get_byte(trng_t *obj, unsigned char *byte ) { - *byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle); + *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle); } - -/** mbedtls_hardware_poll - * @brief Get len bytes of entropy from the hardware RNG. - * @param data pointer will be NULL - * @param output pointer to the random generated bytes buffer - * @param len input is the requested length of bytes to be generated - * @param olen is the pointer to the length of bytes effectively generated - * @returns 0 if the generation went well. -1 in case of error - */ -int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) +void trng_init(trng_t *obj) { - int ret; - ((void) data); RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; /*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 __HAL_RCC_RNG_CLK_ENABLE(); /* Initialize RNG instance */ - RngHandle.Instance = RNG; - HAL_RNG_Init(&RngHandle); + obj->handle.Instance = RNG; + HAL_RNG_Init(&obj->handle); + +} + +void trng_free(trng_t *obj) +{ + /*Disable the RNG peripheral */ + HAL_RNG_DeInit(&obj->handle); + /* RNG Peripheral clock disable - assume we're the only users of RNG */ + __HAL_RCC_RNG_CLK_DISABLE(); +} + +int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) +{ + int ret; /* Get Random byte */ - for( uint32_t i = 0; i < len; i++ ){ - rng_get_byte( output + i ); - + for( uint32_t i = 0; i < length; i++ ){ + trng_get_byte(obj, output + i ); } - *olen = len; + + *output_length = length; /* Just be extra sure that we didn't do it wrong */ - if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) { + if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) { ret = -1; } else { ret = 0; } - /*Disable the RNG peripheral */ - HAL_RNG_DeInit(&RngHandle); - /* RNG Peripheral clock disable - assume we're the only users of RNG */ - __HAL_RCC_RNG_CLK_DISABLE(); return( ret ); } +#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/trng_api.c b/targets/TARGET_STM/TARGET_STM32F7/trng_api.c index ef3595e9b1..f435798f36 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/trng_api.c +++ b/targets/TARGET_STM/TARGET_STM32F7/trng_api.c @@ -26,6 +26,7 @@ /** trng_get_byte * @brief Get one byte of entropy from the RNG, assuming it is up and running. + * @param obj TRNG obj * @param pointer to the hardware generated random byte. */ static void trng_get_byte(trng_t *obj, unsigned char *byte ) diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h index b464a5f2e7..3932d46128 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h @@ -84,6 +84,10 @@ struct i2c_s { I2CName i2c; }; +struct trng_s { + RNG_HandleTypeDef handle; +}; + #include "common_objects.h" #include "gpio_object.h" diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/objects.h index 7f1ca3c87d..20556881d8 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/objects.h @@ -90,6 +90,10 @@ struct can_s { int index; }; +struct trng_s { + RNG_HandleTypeDef handle; +}; + #include "common_objects.h" #include "gpio_object.h" diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L432KC/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L432KC/objects.h index 3aa688ef3a..c3407b008d 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L432KC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L432KC/objects.h @@ -90,6 +90,10 @@ struct can_s { int index; }; +struct trng_s { + RNG_HandleTypeDef handle; +}; + #include "gpio_object.h" #include "common_objects.h" diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/objects.h index 7f1ca3c87d..20556881d8 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/objects.h @@ -90,6 +90,10 @@ struct can_s { int index; }; +struct trng_s { + RNG_HandleTypeDef handle; +}; + #include "common_objects.h" #include "gpio_object.h" diff --git a/targets/targets.json b/targets/targets.json index e5acb852cd..04372b25b9 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -920,6 +920,7 @@ "inherits": ["Target"], "detect_code": ["0760"], "device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"], + "release_versions": ["2", "5"] "device_name": "STM32L073RZ" }, "NUCLEO_L152RE": { @@ -943,7 +944,7 @@ "inherits": ["Target"], "detect_code": ["0770"], "device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "CAN", "STDIO_MESSAGES", "TRNG"], - + "release_versions": ["2", "5"] "device_name" : "STM32L432KC" }, "NUCLEO_L476RG": { @@ -955,6 +956,7 @@ "inherits": ["Target"], "detect_code": ["0765"], "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"], + "release_versions": ["2", "5"] "device_name": "stm32l476rg" }, "STM32F3XX": { @@ -1099,6 +1101,7 @@ "supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"], "detect_code": ["0820"], "device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"], + "release_versions": ["2", "5"] "device_name": "stm32l476vg" }, "MTS_MDOT_F405RG": {