From acff0e8781653671a516617fde9656f2588fd29b Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 25 Feb 2021 08:54:30 -0800 Subject: [PATCH 1/3] Fix for InTime RTOS v5. The `arc4random_buf` wasn't added until v6, so opting to use `arc4random`. ZD 11760. --- wolfcrypt/src/random.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 15127b084..230abb83c 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -2236,19 +2236,24 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) #elif defined(INTIME_RTOS) int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) { - int ret = 0; - - (void)os; + uint32_t rand; + word32 len = sizeof(rand); if (output == NULL) { return BUFFER_E; } - /* Note: Investigate better solution */ - /* no return to check */ - arc4random_buf(output, sz); + while (sz > 0) { + if (sz < len) + len = sz; + rand = arc4random(); /* returns 32-bits of random */ + XMEMCPY(output, &rand, len); + output += len; + sz -= len; + } + (void)os; - return ret; + return 0; } #elif defined(WOLFSSL_WICED) From 8c1a93d9e186fcd73c0cc2bf78ab31e72690b6e6 Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 1 Mar 2021 09:23:19 -0800 Subject: [PATCH 2/3] Using "rand()" to seed our PRNG as its available on all INTIME RTOS versions. --- wolfcrypt/src/random.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 230abb83c..cc1e33dee 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -2236,8 +2236,8 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) #elif defined(INTIME_RTOS) int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) { - uint32_t rand; - word32 len = sizeof(rand); + uint32_t randval; + word32 len = sizeof(randval); if (output == NULL) { return BUFFER_E; @@ -2246,8 +2246,8 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) while (sz > 0) { if (sz < len) len = sz; - rand = arc4random(); /* returns 32-bits of random */ - XMEMCPY(output, &rand, len); + randval = rand(); /* returns 32-bits of random */ + XMEMCPY(output, &randval, len); output += len; sz -= len; } From 3752347f1489d9e8208a38d073e443a0b5ef4391 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 2 Mar 2021 15:04:01 -0800 Subject: [PATCH 3/3] Improve the random logic for the INTIME RTOS RNG. --- wolfcrypt/src/random.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index cc1e33dee..bd637173f 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -2237,16 +2237,27 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) { uint32_t randval; - word32 len = sizeof(randval); + word32 len; if (output == NULL) { return BUFFER_E; } + #ifdef INTIMEVER + /* If INTIMEVER exists then it is INTIME RTOS v6 or later */ + #define INTIME_RAND_FUNC arc4random + len = 4; + #else + /* v5 and older */ + #define INTIME_RAND_FUNC rand + srand(time(0)); + len = 2; /* don't use all 31 returned bits */ + #endif + while (sz > 0) { if (sz < len) len = sz; - randval = rand(); /* returns 32-bits of random */ + randval = INTIME_RAND_FUNC(); XMEMCPY(output, &randval, len); output += len; sz -= len;