From 02fd613a8164263c55e1897d965f47f741cd1142 Mon Sep 17 00:00:00 2001 From: Martin Kojtal Date: Fri, 16 Sep 2016 13:25:27 +0100 Subject: [PATCH] k66f: add RNG HAL implementation --- hal/targets.json | 2 +- .../{entropy_hardware_poll.c => rng_api.c} | 49 +++++++++---------- 2 files changed, 24 insertions(+), 27 deletions(-) rename hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/{entropy_hardware_poll.c => rng_api.c} (69%) diff --git a/hal/targets.json b/hal/targets.json index bde86d0f4f..46f98efa50 100644 --- a/hal/targets.json +++ b/hal/targets.json @@ -603,7 +603,7 @@ "inherits": ["Target"], "progen": {"target": "frdm-k66f"}, "detect_code": ["0311"], - "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"], + "device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "RNG"], "release_versions": ["2", "5"] }, "NUCLEO_F030R8": { diff --git a/hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/entropy_hardware_poll.c b/hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/rng_api.c similarity index 69% rename from hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/entropy_hardware_poll.c rename to hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/rng_api.c index f834e5fb82..7c25673912 100644 --- a/hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/entropy_hardware_poll.c +++ b/hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/rng_api.c @@ -26,12 +26,25 @@ #include "cmsis.h" #include "fsl_common.h" #include "fsl_clock.h" +#include "rng_api.h" + +void rng_init(rng_t *obj) +{ + CLOCK_EnableClock(kCLOCK_Rnga0); + CLOCK_DisableClock(kCLOCK_Rnga0); + CLOCK_EnableClock(kCLOCK_Rnga0); +} + +void rng_free(rng_t *obj) +{ + CLOCK_DisableClock(kCLOCK_Rnga0); +} /* * Get one byte of entropy from the RNG, assuming it is up and running. * As recommended (34.1.1), get only one bit of each output. */ -static void rng_get_byte( unsigned char *byte ) +static void rng_get_byte(unsigned char *byte) { size_t bit; @@ -43,41 +56,25 @@ static void rng_get_byte( unsigned char *byte ) } } -/* - * Get len bytes of entropy from the hardware RNG. - */ -int mbedtls_hardware_poll( void *data, - unsigned char *output, size_t len, size_t *olen ) +int rng_get_numbers(rng_t *obj, uint8_t *output, size_t length, size_t *output_length) { size_t i; int ret; - ((void) data); - - CLOCK_EnableClock( kCLOCK_Rnga0 ); - CLOCK_DisableClock( kCLOCK_Rnga0 ); - CLOCK_EnableClock( kCLOCK_Rnga0 ); /* Set "Interrupt Mask", "High Assurance" and "Go", * unset "Clear interrupt" and "Sleep" */ RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK; - for( i = 0; i < len; i++ ) - rng_get_byte( output + i ); - - /* Just be extra sure that we didn't do it wrong */ - if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 ) - { - ret = -1; - goto cleanup; + for (i = 0; i < length; i++) { + rng_get_byte(output + i); } - *olen = len; - ret = 0; + /* Just be extra sure that we didn't do it wrong */ + if ((RNG->SR & RNG_SR_SECV_MASK) != 0) { + return -1; + } -cleanup: - /* Disable clock to save power - assume we're the only users of RNG */ - CLOCK_DisableClock( kCLOCK_Rnga0 ); + *output_length = length; - return( ret ); + return 0; } -