Index: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/random.c
===================================================================
--- azure_iot_hub_f767zi/trunk/wolfssl-4.4.0/wolfcrypt/src/random.c	(revision 457)
+++ azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/random.c	(revision 464)
@@ -20,5 +20,10 @@
  */
 
-
+/*
+
+DESCRIPTION
+This library contains implementation for the random number generator.
+
+*/
 #ifdef HAVE_CONFIG_H
     #include <config.h>
@@ -165,4 +170,8 @@
 #endif
 
+#if defined(WOLFSSL_SILABS_SE_ACCEL)
+#include <wolfssl/wolfcrypt/port/silabs/silabs_random.h>
+#endif
+
 
 #if defined(HAVE_INTEL_RDRAND) || defined(HAVE_INTEL_RDSEED)
@@ -172,5 +181,5 @@
         intel_flags = cpuid_get_flags();
     }
-    #ifdef HAVE_INTEL_RDSEED
+    #if defined(HAVE_INTEL_RDSEED) && !defined(WOLFSSL_LINUXKM)
     static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz);
     #endif
@@ -180,5 +189,30 @@
 
 #ifdef USE_WINDOWS_API
+    #define USE_INTEL_INTRINSICS
+#elif !defined __GNUC__ || defined __clang__ || __GNUC__ > 4
+    #define USE_INTEL_INTRINSICS
+#else
+    #undef USE_INTEL_INTRINSICS
+#endif
+
+#ifdef USE_INTEL_INTRINSICS
     #include <immintrin.h>
+    /* Before clang 7 or GCC 9, immintrin.h did not define _rdseed64_step() */
+    #ifndef HAVE_INTEL_RDSEED
+    #elif defined __clang__ && __clang_major__ > 6
+    #elif !defined __GNUC__
+    #elif __GNUC__ > 8
+    #else
+        #ifndef __clang__
+            #pragma GCC push_options
+            #pragma GCC target("rdseed")
+        #else
+            #define __RDSEED__
+        #endif
+        #include <x86intrin.h>
+        #ifndef __clang__
+            #pragma GCC pop_options
+        #endif
+    #endif
 #endif /* USE_WINDOWS_API */
 #endif
@@ -280,20 +314,5 @@
 };
 
-/* NOTE: if DRBG struct is changed please update random.h drbg_data size */
-typedef struct DRBG {
-    word32 reseedCtr;
-    word32 lastBlock;
-    byte V[DRBG_SEED_LEN];
-    byte C[DRBG_SEED_LEN];
-#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
-    void* heap;
-    int devId;
-#endif
-    byte   matchCount;
-#ifdef WOLFSSL_SMALL_STACK_CACHE
-    wc_Sha256 sha256;
-#endif
-} DRBG;
-
+typedef struct DRBG_internal DRBG_internal;
 
 static int wc_RNG_HealthTestLocal(int reseed);
@@ -301,5 +320,5 @@
 /* Hash Derivation Function */
 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
-static int Hash_df(DRBG* drbg, byte* out, word32 outSz, byte type,
+static int Hash_df(DRBG_internal* drbg, byte* out, word32 outSz, byte type,
                                                   const byte* inA, word32 inASz,
                                                   const byte* inB, word32 inBSz)
@@ -390,5 +409,5 @@
 
 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
-static int Hash_DRBG_Reseed(DRBG* drbg, const byte* seed, word32 seedSz)
+static int Hash_DRBG_Reseed(DRBG_internal* drbg, const byte* seed, word32 seedSz)
 {
     byte newV[DRBG_SEED_LEN];
@@ -422,5 +441,5 @@
     }
 
-    return Hash_DRBG_Reseed(rng->drbg, seed, seedSz);
+    return Hash_DRBG_Reseed((DRBG_internal *)rng->drbg, seed, seedSz);
 }
 
@@ -437,5 +456,5 @@
 
 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
-static int Hash_gen(DRBG* drbg, byte* out, word32 outSz, const byte* V)
+static int Hash_gen(DRBG_internal* drbg, byte* out, word32 outSz, const byte* V)
 {
     int ret = DRBG_FAILURE;
@@ -488,5 +507,5 @@
                 }
                 else {
-                    if (i == len) {
+                    if (i == (len-1)) {
                         len++;
                     }
@@ -512,4 +531,8 @@
             }
         }
+        else {
+            /* wc_Sha256Update or wc_Sha256Final returned error */
+            break;
+        }
     }
     ForceZero(data, sizeof(data));
@@ -529,7 +552,6 @@
         int sIdx, dIdx;
 
-        for (sIdx = sLen - 1, dIdx = dLen - 1; sIdx >= 0; dIdx--, sIdx--)
-        {
-            carry += d[dIdx] + s[sIdx];
+        for (sIdx = sLen - 1, dIdx = dLen - 1; sIdx >= 0; dIdx--, sIdx--) {
+            carry += (word16)d[dIdx] + (word16)s[sIdx];
             d[dIdx] = (byte)carry;
             carry >>= 8;
@@ -537,5 +559,5 @@
 
         for (; carry != 0 && dIdx >= 0; dIdx--) {
-            carry += d[dIdx];
+            carry += (word16)d[dIdx];
             d[dIdx] = (byte)carry;
             carry >>= 8;
@@ -545,5 +567,5 @@
 
 /* Returns: DRBG_SUCCESS, DRBG_NEED_RESEED, or DRBG_FAILURE */
-static int Hash_DRBG_Generate(DRBG* drbg, byte* out, word32 outSz)
+static int Hash_DRBG_Generate(DRBG_internal* drbg, byte* out, word32 outSz)
 {
     int ret;
@@ -611,11 +633,11 @@
 
 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
-static int Hash_DRBG_Instantiate(DRBG* drbg, const byte* seed, word32 seedSz,
+static int Hash_DRBG_Instantiate(DRBG_internal* drbg, const byte* seed, word32 seedSz,
                                              const byte* nonce, word32 nonceSz,
                                              void* heap, int devId)
 {
-    int ret;
-
-    XMEMSET(drbg, 0, sizeof(DRBG));
+    int ret = DRBG_FAILURE;
+
+    XMEMSET(drbg, 0, sizeof(DRBG_internal));
 #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
     drbg->heap = heap;
@@ -646,7 +668,4 @@
         ret = DRBG_SUCCESS;
     }
-    else {
-        ret = DRBG_FAILURE;
-    }
 
     return ret;
@@ -654,5 +673,5 @@
 
 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
-static int Hash_DRBG_Uninstantiate(DRBG* drbg)
+static int Hash_DRBG_Uninstantiate(DRBG_internal* drbg)
 {
     word32 i;
@@ -664,7 +683,7 @@
 #endif
 
-    ForceZero(drbg, sizeof(DRBG));
-
-    for (i = 0; i < sizeof(DRBG); i++)
+    ForceZero(drbg, sizeof(DRBG_internal));
+
+    for (i = 0; i < sizeof(DRBG_internal); i++)
         compareSum |= compareDrbg[i] ^ 0;
 
@@ -675,5 +694,5 @@
 int wc_RNG_TestSeed(const byte* seed, word32 seedSz)
 {
-    int ret = DRBG_SUCCESS;
+    int ret = 0;
 
     /* Check the seed for duplicate words. */
@@ -701,5 +720,5 @@
                     void* heap, int devId)
 {
-    int ret = RNG_FAILURE_E;
+    int ret = 0;
 #ifdef HAVE_HASHDRBG
     word32 seedSz = SEED_SZ + SEED_BLOCK_SZ;
@@ -772,26 +791,24 @@
 #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
         rng->drbg =
-                (struct DRBG*)XMALLOC(sizeof(DRBG), rng->heap,
+                (struct DRBG*)XMALLOC(sizeof(DRBG_internal), rng->heap,
                                                           DYNAMIC_TYPE_RNG);
-#else
-        /* compile-time validation of drbg_data size */
-        typedef char drbg_data_test[sizeof(rng->drbg_data) >=
-                sizeof(struct DRBG) ? 1 : -1];
-        (void)sizeof(drbg_data_test);
-        rng->drbg = (struct DRBG*)rng->drbg_data;
-#endif
-
         if (rng->drbg == NULL) {
             ret = MEMORY_E;
-        }
-        else {
+            rng->status = DRBG_FAILED;
+        }
+#else
+        rng->drbg = (struct DRBG*)&rng->drbg_data;
+#endif
+        if (ret == 0) {
             ret = wc_GenerateSeed(&rng->seed, seed, seedSz);
-            if (ret != 0)
+            if (ret == 0)
+                ret = wc_RNG_TestSeed(seed, seedSz);
+            else {
                 ret = DRBG_FAILURE;
-            else
-                ret = wc_RNG_TestSeed(seed, seedSz);
+                rng->status = DRBG_FAILED;
+            }
 
             if (ret == DRBG_SUCCESS)
-                 ret = Hash_DRBG_Instantiate(rng->drbg,
+	      ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg,
                             seed + SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ,
                             nonce, nonceSz, rng->heap, devId);
@@ -901,4 +918,7 @@
         return BAD_FUNC_ARG;
 
+    if (sz == 0)
+        return 0; 
+
 #ifdef WOLF_CRYPTO_CB
     if (rng->devId != INVALID_DEVID) {
@@ -913,4 +933,8 @@
     if (IS_INTEL_RDRAND(intel_flags))
         return wc_GenerateRand_IntelRD(NULL, output, sz);
+#endif
+
+#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_TRNG)
+    return silabs_GenerateRand(output, sz);
 #endif
 
@@ -940,5 +964,5 @@
         return RNG_FAILURE_E;
 
-    ret = Hash_DRBG_Generate(rng->drbg, output, sz);
+    ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz);
     if (ret == DRBG_NEED_RESEED) {
         if (wc_RNG_HealthTestLocal(1) == 0) {
@@ -953,8 +977,8 @@
 
             if (ret == DRBG_SUCCESS)
-                ret = Hash_DRBG_Reseed(rng->drbg, newSeed + SEED_BLOCK_SZ,
+	      ret = Hash_DRBG_Reseed((DRBG_internal *)rng->drbg, newSeed + SEED_BLOCK_SZ,
                                        SEED_SZ);
             if (ret == DRBG_SUCCESS)
-                ret = Hash_DRBG_Generate(rng->drbg, output, sz);
+	      ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz);
 
             ForceZero(newSeed, sizeof(newSeed));
@@ -1006,5 +1030,5 @@
 #ifdef HAVE_HASHDRBG
     if (rng->drbg != NULL) {
-        if (Hash_DRBG_Uninstantiate(rng->drbg) != DRBG_SUCCESS)
+      if (Hash_DRBG_Uninstantiate((DRBG_internal *)rng->drbg) != DRBG_SUCCESS)
             ret = RNG_FAILURE_E;
 
@@ -1040,7 +1064,7 @@
 {
     int ret = -1;
-    DRBG* drbg;
+    DRBG_internal* drbg;
 #ifndef WOLFSSL_SMALL_STACK
-    DRBG  drbg_var;
+    DRBG_internal  drbg_var;
 #endif
 
@@ -1058,5 +1082,5 @@
 
 #ifdef WOLFSSL_SMALL_STACK
-    drbg = (DRBG*)XMALLOC(sizeof(DRBG), NULL, DYNAMIC_TYPE_RNG);
+    drbg = (DRBG_internal*)XMALLOC(sizeof(DRBG_internal), NULL, DYNAMIC_TYPE_RNG);
     if (drbg == NULL) {
         return MEMORY_E;
@@ -1108,5 +1132,5 @@
 
 
-const byte seedA[] = {
+const FLASH_QUALIFIER byte seedA_data[] = {
     0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
     0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
@@ -1115,5 +1139,5 @@
 };
 
-const byte reseedSeedA[] = {
+const FLASH_QUALIFIER byte reseedSeedA_data[] = {
     0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
     0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
@@ -1121,5 +1145,5 @@
 };
 
-const byte outputA[] = {
+const FLASH_QUALIFIER byte outputA_data[] = {
     0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
     0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
@@ -1135,5 +1159,5 @@
 };
 
-const byte seedB[] = {
+const FLASH_QUALIFIER byte seedB_data[] = {
     0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
     0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
@@ -1143,5 +1167,5 @@
 };
 
-const byte outputB[] = {
+const FLASH_QUALIFIER byte outputB_data[] = {
     0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
     0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
@@ -1176,6 +1200,29 @@
 
     if (reseed) {
-        ret = wc_RNG_HealthTest(1, seedA, sizeof(seedA),
-                                reseedSeedA, sizeof(reseedSeedA),
+#ifdef WOLFSSL_USE_FLASHMEM
+        byte* seedA = (byte*)XMALLOC(sizeof(seedA_data), NULL,
+                             DYNAMIC_TYPE_TMP_BUFFER);
+        byte* reseedSeedA = (byte*)XMALLOC(sizeof(reseedSeedA_data), NULL,
+                             DYNAMIC_TYPE_TMP_BUFFER);
+        byte* outputA = (byte*)XMALLOC(sizeof(outputA_data), NULL,
+                             DYNAMIC_TYPE_TMP_BUFFER);
+
+        if (!seedA || !reseedSeedA || !outputA) {
+            XFREE(seedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            XFREE(reseedSeedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            XFREE(outputA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            ret = MEMORY_E;
+        }
+        else {
+            XMEMCPY_P(seedA, seedA_data, sizeof(seedA_data));
+            XMEMCPY_P(reseedSeedA, reseedSeedA_data, sizeof(reseedSeedA_data));
+            XMEMCPY_P(outputA, outputA_data, sizeof(outputA_data));
+#else
+        const byte* seedA = seedA_data;
+        const byte* reseedSeedA = reseedSeedA_data;
+        const byte* outputA = outputA_data;
+#endif
+        ret = wc_RNG_HealthTest(1, seedA, sizeof(seedA_data),
+                                reseedSeedA, sizeof(reseedSeedA_data),
                                 check, RNG_HEALTH_TEST_CHECK_SIZE);
         if (ret == 0) {
@@ -1184,7 +1231,32 @@
                 ret = -1;
         }
+
+#ifdef WOLFSSL_USE_FLASHMEM
+            XFREE(seedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            XFREE(reseedSeedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            XFREE(outputA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+        }
+#endif
     }
     else {
-        ret = wc_RNG_HealthTest(0, seedB, sizeof(seedB),
+#ifdef WOLFSSL_USE_FLASHMEM
+        byte* seedB = (byte*)XMALLOC(sizeof(seedB_data), NULL,
+                             DYNAMIC_TYPE_TMP_BUFFER);
+        byte* outputB = (byte*)XMALLOC(sizeof(outputB_data), NULL,
+                               DYNAMIC_TYPE_TMP_BUFFER);
+
+        if (!seedB || !outputB) {
+            XFREE(seedB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            XFREE(outputB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            ret = MEMORY_E;
+        }
+        else {
+            XMEMCPY_P(seedB, seedB_data, sizeof(seedB_data));
+            XMEMCPY_P(outputB, outputB_data, sizeof(outputB_data));
+#else
+        const byte* seedB = seedB_data;
+        const byte* outputB = outputB_data;
+#endif
+        ret = wc_RNG_HealthTest(0, seedB, sizeof(seedB_data),
                                 NULL, 0,
                                 check, RNG_HEALTH_TEST_CHECK_SIZE);
@@ -1201,5 +1273,5 @@
         if (ret == 0) {
             ret = wc_RNG_HealthTest_ex(0,
-                                    seedB + 32, sizeof(seedB) - 32,
+                                    seedB + 32, sizeof(seedB_data) - 32,
                                     seedB, 32,
                                     NULL, 0,
@@ -1207,8 +1279,14 @@
                                     NULL, INVALID_DEVID);
             if (ret == 0) {
-                if (ConstantCompare(check, outputB, sizeof(outputB)) != 0)
+                if (ConstantCompare(check, outputB, sizeof(outputB_data)) != 0)
                     ret = -1;
             }
         }
+
+#ifdef WOLFSSL_USE_FLASHMEM
+            XFREE(seedB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+            XFREE(outputB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+        }
+#endif
     }
 
@@ -1332,5 +1410,5 @@
 #ifdef HAVE_INTEL_RDSEED
 
-#ifndef USE_WINDOWS_API
+#ifndef USE_INTEL_INTRINSICS
 
     /* return 0 on success */
@@ -1343,18 +1421,21 @@
     }
 
-#else /* USE_WINDOWS_API */
+#else /* USE_INTEL_INTRINSICS */
     /* The compiler Visual Studio uses does not allow inline assembly.
      * It does allow for Intel intrinsic functions. */
 
     /* return 0 on success */
+# ifdef __GNUC__
+    __attribute__((target("rdseed")))
+# endif
     static WC_INLINE int IntelRDseed64(word64* seed)
     {
         int ok;
 
-        ok = _rdseed64_step(seed);
+        ok = _rdseed64_step((unsigned long long*) seed);
         return (ok) ? 0 : -1;
     }
 
-#endif /* USE_WINDOWS_API */
+#endif /* USE_INTEL_INTRINSICS */
 
 /* return 0 on success */
@@ -1369,4 +1450,5 @@
 }
 
+#ifndef WOLFSSL_LINUXKM
 /* return 0 on success */
 static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz)
@@ -1399,4 +1481,5 @@
     return 0;
 }
+#endif
 
 #endif /* HAVE_INTEL_RDSEED */
@@ -1404,5 +1487,5 @@
 #ifdef HAVE_INTEL_RDRAND
 
-#ifndef USE_WINDOWS_API
+#ifndef USE_INTEL_INTRINSICS
 
 /* return 0 on success */
@@ -1416,19 +1499,22 @@
 }
 
-#else /* USE_WINDOWS_API */
+#else /* USE_INTEL_INTRINSICS */
     /* The compiler Visual Studio uses does not allow inline assembly.
      * It does allow for Intel intrinsic functions. */
 
 /* return 0 on success */
+# ifdef __GNUC__
+__attribute__((target("rdrnd")))
+# endif
 static WC_INLINE int IntelRDrand64(word64 *rnd)
 {
     int ok;
 
-    ok = _rdrand64_step(rnd);
+    ok = _rdrand64_step((unsigned long long*) rnd);
 
     return (ok) ? 0 : -1;
 }
 
-#endif /* USE_WINDOWS_API */
+#endif /* USE_INTEL_INTRINSICS */
 
 /* return 0 on success */
@@ -1608,4 +1694,22 @@
 }
 
+#elif (defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC_RNG)) && \
+      !defined(WOLFSSL_PIC32MZ_RNG)
+    /* enable ATECC RNG unless using PIC32MZ one instead */
+    #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
+
+    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
+    {
+        int ret = 0;
+
+        (void)os;
+        if (output == NULL) {
+            return BUFFER_E;
+        }
+
+        ret = atmel_get_random_number(sz, output);
+
+        return ret;
+    }
 
 #elif defined(MICROCHIP_PIC32)
@@ -1816,4 +1920,11 @@
     #endif /* FREESCALE_K70_RNGA */
 
+#elif defined(WOLFSSL_SILABS_SE_ACCEL)
+    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
+    {
+        (void)os;
+        return silabs_GenerateRand(output, sz);
+    }
+
 #elif defined(STM32_RNG)
      /* Generate a RNG seed using the hardware random number generator
@@ -1847,5 +1958,5 @@
             ) {
                 /* Single byte at a time */
-                uint32_t tmpRng = 0;
+                word32 tmpRng = 0;
                 if (HAL_RNG_GenerateRandomNumber(&hrng, &tmpRng) != HAL_OK) {
                     wolfSSL_CryptHwMutexUnLock();
@@ -1856,5 +1967,5 @@
             else {
                 /* Use native 32 instruction */
-                if (HAL_RNG_GenerateRandomNumber(&hrng, (uint32_t*)&output[i]) != HAL_OK) {
+                if (HAL_RNG_GenerateRandomNumber(&hrng, (word32*)&output[i]) != HAL_OK) {
                     wolfSSL_CryptHwMutexUnLock();
                     return RAN_BLOCK_E;
@@ -2053,5 +2164,5 @@
     }
 
-#elif defined(WOLFSSL_NRF51)
+#elif defined(WOLFSSL_NRF51) || defined(WOLFSSL_NRF5x)
     #include "app_error.h"
     #include "nrf_drv_rng.h"
@@ -2059,28 +2170,34 @@
     {
         int remaining = sz, length, pos = 0;
-        uint8_t available;
-        uint32_t err_code;
+        word32 err_code;
+        byte available;
+        static byte initialized = 0;
 
         (void)os;
 
         /* Make sure RNG is running */
-        err_code = nrf_drv_rng_init(NULL);
-        if (err_code != NRF_SUCCESS && err_code != NRF_ERROR_INVALID_STATE) {
-            return -1;
+        if (!initialized) {
+            err_code = nrf_drv_rng_init(NULL);
+            if (err_code != NRF_SUCCESS && err_code != NRF_ERROR_INVALID_STATE
+            #ifdef NRF_ERROR_MODULE_ALREADY_INITIALIZED
+                && err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED
+            #endif
+            ) {
+                return -1;
+            }
+            initialized = 1;
         }
 
         while (remaining > 0) {
-            err_code = nrf_drv_rng_bytes_available(&available);
-            if (err_code == NRF_SUCCESS) {
-                length = (remaining < available) ? remaining : available;
-                if (length > 0) {
-                    err_code = nrf_drv_rng_rand(&output[pos], length);
-                    remaining -= length;
-                    pos += length;
+            available = 0;
+            nrf_drv_rng_bytes_available(&available); /* void func */
+            length = (remaining < available) ? remaining : available;
+            if (length > 0) {
+                err_code = nrf_drv_rng_rand(&output[pos], length);
+                if (err_code != NRF_SUCCESS) {
+                    break;
                 }
-            }
-
-            if (err_code != NRF_SUCCESS) {
-                break;
+                remaining -= length;
+                pos += length;
             }
         }
@@ -2115,21 +2232,4 @@
 
         return 0;
-    }
-
-#elif defined(WOLFSSL_ATMEL)
-    #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
-
-    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
-    {
-        int ret = 0;
-
-        (void)os;
-        if (output == NULL) {
-            return BUFFER_E;
-        }
-
-        ret = atmel_get_random_number(sz, output);
-
-        return ret;
     }
 
@@ -2290,19 +2390,30 @@
     #endif /* end WOLFSSL_ESPWROOM32 */
 
+#elif defined(WOLFSSL_LINUXKM)
+    #include <linux/random.h>
+    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
+    {
+        (void)os;
+
+        get_random_bytes(output, sz);
+
+        return 0;
+    }
+
 #elif defined(WOLFSSL_RENESAS_TSIP)
 #if defined(WOLFSSL_RENESA_TSIP_IAREWRX)
-    #include "r_bsp/mcu/all/r_rx_compiler.h"
-#endif
-    #include "r_bsp/platform.h"
+   #include "r_bsp/mcu/all/r_rx_compiler.h"
+#endif
+   #include "r_bsp/platform.h"
     #include "r_tsip_rx_if.h"
-    
+
     int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
     {
         int ret;
-        uint32_t buffer[4];
+        word32 buffer[4];
 
         while (sz > 0) {
-            uint32_t len = sizeof(buffer);
-            
+            word32 len = sizeof(buffer);
+
             if (sz < len) {
                 len = sz;
@@ -2329,7 +2440,7 @@
     int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
     {
-        uint32_t ret;
-        uint32_t blocks;
-        word32   len = sz;
+        word32 ret;
+        word32 blocks;
+        word32 len = sz;
 
         ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->open(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
@@ -2340,8 +2451,8 @@
         }
 
-        blocks = sz / sizeof(uint32_t);
+        blocks = sz / sizeof(word32);
         if (blocks > 0) {
             ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
-                                                      (uint32_t*)output, blocks);
+                                                       (word32*)output, blocks);
             if (ret != SSP_SUCCESS) {
                 return -1;
@@ -2349,17 +2460,17 @@
         }
 
-        len = len - (blocks * sizeof(uint32_t));
+        len = len - (blocks * sizeof(word32));
         if (len > 0) {
-            uint32_t tmp;
-
-            if (len > sizeof(uint32_t)) {
+            word32 tmp;
+
+            if (len > sizeof(word32)) {
                 return -1;
             }
             ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
-                                                      (uint32_t*)tmp, 1);
+                                                      (word32*)tmp, 1);
             if (ret != SSP_SUCCESS) {
                 return -1;
             }
-            XMEMCPY(output + (blocks * sizeof(uint32_t)), (byte*)&tmp, len);
+            XMEMCPY(output + (blocks * sizeof(word32)), (byte*)&tmp, len);
         }
 
