STM32F4/F7: Add RNG HAL implementation

pull/2765/head
Martin Kojtal 2016-09-19 11:52:46 +01:00
parent 02fd613a81
commit 72fac611f2
11 changed files with 84 additions and 65 deletions

View File

@ -795,7 +795,7 @@
"progen": {"target": "nucleo-f410rb"},
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT", "TRANSACTION_QUEUE_SIZE_SPI=2"],
"detect_code": ["0740"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "RNG"],
"release_versions": ["2", "5"]
},
"NUCLEO_F411RE": {
@ -1077,7 +1077,7 @@
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","DEVICE_RTC_LSI=1","TRANSACTION_QUEUE_SIZE_SPI=2"],
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
"progen": {"target": "disco-f429zi"},
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "RNG"],
"release_versions": ["2", "5"]
},
"DISCO_F469NI": {
@ -1090,7 +1090,7 @@
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","TRANSACTION_QUEUE_SIZE_SPI=2"],
"progen": {"target": "disco-f469ni"},
"detect_code": ["0788"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "RNG"],
"release_versions": ["2", "5"]
},
"DISCO_L053C8": {

View File

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

View File

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

View File

@ -65,6 +65,10 @@ struct dac_s {
uint8_t channel;
};
struct rng_s {
RNG_HandleTypeDef handle;
};
#include "common_objects.h"
#include "gpio_object.h"

View File

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

View File

@ -25,58 +25,52 @@
defined(TARGET_STM32F479xx)
#include <stdlib.h>
#include "cmsis.h"
/* RNG handler declaration */
RNG_HandleTypeDef RngHandle;
#include "rng_api.h"
/** rng_get_byte
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
* @param pointer to the hardware generated random byte.
*/
static void rng_get_byte( unsigned char *byte )
static void rng_get_byte(rng_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 rng_init(rng_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 rng_free(rng_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 rng_get_numbers(rng_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++ ){
rng_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 );
}

View File

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

View File

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

View File

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

View File

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

View File

@ -21,58 +21,52 @@
#include <stdlib.h>
#include "cmsis.h"
/* RNG handler declaration */
RNG_HandleTypeDef RngHandle;
#include "rng_api.h"
/** rng_get_byte
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
* @param pointer to the hardware generated random byte.
*/
static void rng_get_byte( unsigned char *byte )
static void rng_get_byte(rng_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 rng_init(rng_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 rng_free(rng_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 rng_get_numbers(rng_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++ ){
rng_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 );
}