From 76c8a1bf7e70fc496f91c592088ee3a0c3517fe2 Mon Sep 17 00:00:00 2001 From: Mahesh Mahadevan Date: Mon, 26 Feb 2018 15:09:17 -0600 Subject: [PATCH] LPC546XX: Add TRNG support Signed-off-by: Mahesh Mahadevan --- .../TARGET_LPC/objects.h | 8 ++- .../TARGET_LPC/trng_api.c | 59 +++++++++++++++++++ targets/targets.json | 2 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/trng_api.c diff --git a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/objects.h b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/objects.h index a7e19cf2d8..cc03c06278 100644 --- a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/objects.h +++ b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/objects.h @@ -58,12 +58,18 @@ struct spi_s { uint8_t bits; }; -#if DEVICE_FLASH +#if defined(DEVICE_FLASH) struct flash_s { uint8_t dummy; }; #endif +#if defined(DEVICE_TRNG) +struct trng_s { + uint8_t dummy; +}; +#endif + #include "gpio_object.h" #ifdef __cplusplus diff --git a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/trng_api.c b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/trng_api.c new file mode 100644 index 0000000000..0a91355b59 --- /dev/null +++ b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/trng_api.c @@ -0,0 +1,59 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "trng_api.h" + +#if defined(DEVICE_TRNG) +#include "fsl_rng.h" + +void trng_init(trng_t *obj) +{ +} + +void trng_free(trng_t *obj) +{ +} + +int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) +{ + uint32_t skip; + uint32_t data; + size_t idx = 0; + int i; + + /* Get Random data */ + while (idx < length) { + + data = RNG_GetRandomData(); + + for (i = 0; ((i < 4) && (idx < length)); i++) { + output[idx++] = (data >> (i * 8)) & 0xFF; + } + + /* Skip next 32 random numbers for better entropy */ + for (skip = 0; skip < 32; skip++) { + RNG_GetRandomData(); + } + } + + *output_length = idx; + + /* Zeroize to avoid leakage of entropy on the stack. Also ensure this is not removed by compiler optimizations */ + *((volatile uint32_t*) &data) = 0; + + return (idx == length ? 0 : -1); +} +#endif diff --git a/targets/targets.json b/targets/targets.json index edfe8ce3e4..42d6c472f6 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -747,7 +747,7 @@ "is_disk_virtual": true, "macros": ["CPU_LPC54628J512ET180", "FSL_RTOS_MBED"], "inherits": ["Target"], - "device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH"], + "device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH", "TRNG"], "features": ["LWIP"], "device_name" : "LPC54628J512ET180" },