From dad88b4c8101db27c8b6a1304001a76033a229b0 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 17 Jan 2019 09:57:26 -0800 Subject: [PATCH] Improvements to the STM32L4 random generation code for improved performance and error handling. Added new `WOLFSSL_STM32_RNG_NOLIB` define to support generic STM32 series RNG without external ST library. --- wolfcrypt/src/random.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 9d90abeb8..66e940190 100755 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -1741,30 +1741,47 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) #elif defined(STM32_RNG) /* Generate a RNG seed using the hardware random number generator - * on the STM32F2/F4/F7. */ + * on the STM32F2/F4/F7/L4. */ #ifdef WOLFSSL_STM32_CUBEMX int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) { RNG_HandleTypeDef hrng; - int i; + word32 i = 0; (void)os; /* enable RNG clock source */ __HAL_RCC_RNG_CLK_ENABLE(); /* enable RNG peripheral */ + XMEMSET(&hrng, 0, sizeof(hrng)); hrng.Instance = RNG; HAL_RNG_Init(&hrng); - for (i = 0; i < (int)sz; i++) { - /* get value */ - output[i] = (byte)HAL_RNG_GetRandomNumber(&hrng); - } + while (i < sz) { + /* If not aligned or there is odd/remainder */ + if( (i + sizeof(word32)) > sz || + ((wolfssl_word)&output[i] % sizeof(word32)) != 0 + ) { + /* Single byte at a time */ + word32 tmpRng = 0; + if (HAL_RNG_GenerateRandomNumber(&hrng, &tmpRng) != HAL_OK) { + return RAN_BLOCK_E; + } + output[i++] = (byte)tmpRng; + } + else { + /* Use native 32 instruction */ + if (HAL_RNG_GenerateRandomNumber(&hrng, (word32*)&output[i]) != HAL_OK) { + return RAN_BLOCK_E; + } + i += sizeof(word32); + } + } - return 0; + return 0; } - #elif defined(WOLFSSL_STM32F427_RNG) + #elif defined(WOLFSSL_STM32F427_RNG) || defined(WOLFSSL_STM32_RNG_NOLIB) /* Generate a RNG seed using the hardware RNG on the STM32F427 * directly, following steps outlined in STM32F4 Reference