Change MBEDTLS_ENTROPY_ALT to device has TRNG

update code accordingly
pull/2611/head
adustm 2016-09-29 12:16:27 +02:00
parent 5069c73e8d
commit e189bc7cc8
8 changed files with 79 additions and 63 deletions

View File

@ -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 <stdlib.h>
#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

View File

@ -18,36 +18,24 @@
*
*/
#if defined(DEVICE_TRNG)
#include <stdlib.h>
#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

View File

@ -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 )

View File

@ -84,6 +84,10 @@ struct i2c_s {
I2CName i2c;
};
struct trng_s {
RNG_HandleTypeDef handle;
};
#include "common_objects.h"
#include "gpio_object.h"

View File

@ -90,6 +90,10 @@ struct can_s {
int index;
};
struct trng_s {
RNG_HandleTypeDef handle;
};
#include "common_objects.h"
#include "gpio_object.h"

View File

@ -90,6 +90,10 @@ struct can_s {
int index;
};
struct trng_s {
RNG_HandleTypeDef handle;
};
#include "gpio_object.h"
#include "common_objects.h"

View File

@ -90,6 +90,10 @@ struct can_s {
int index;
};
struct trng_s {
RNG_HandleTypeDef handle;
};
#include "common_objects.h"
#include "gpio_object.h"

View File

@ -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": {