Ignore:
Timestamp:
Jun 22, 2021, 9:00:19 PM (3 years ago)
Author:
coas-nagasima
Message:

WolfSSLとAzure IoT SDKを更新

Location:
azure_iot_hub_f767zi/trunk/wolfssl-4.7.0
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/random.c

    r457 r464  
    2020 */
    2121
    22 
     22/*
     23
     24DESCRIPTION
     25This library contains implementation for the random number generator.
     26
     27*/
    2328#ifdef HAVE_CONFIG_H
    2429    #include <config.h>
     
    165170#endif
    166171
     172#if defined(WOLFSSL_SILABS_SE_ACCEL)
     173#include <wolfssl/wolfcrypt/port/silabs/silabs_random.h>
     174#endif
     175
    167176
    168177#if defined(HAVE_INTEL_RDRAND) || defined(HAVE_INTEL_RDSEED)
     
    172181        intel_flags = cpuid_get_flags();
    173182    }
    174     #ifdef HAVE_INTEL_RDSEED
     183    #if defined(HAVE_INTEL_RDSEED) && !defined(WOLFSSL_LINUXKM)
    175184    static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz);
    176185    #endif
     
    180189
    181190#ifdef USE_WINDOWS_API
     191    #define USE_INTEL_INTRINSICS
     192#elif !defined __GNUC__ || defined __clang__ || __GNUC__ > 4
     193    #define USE_INTEL_INTRINSICS
     194#else
     195    #undef USE_INTEL_INTRINSICS
     196#endif
     197
     198#ifdef USE_INTEL_INTRINSICS
    182199    #include <immintrin.h>
     200    /* Before clang 7 or GCC 9, immintrin.h did not define _rdseed64_step() */
     201    #ifndef HAVE_INTEL_RDSEED
     202    #elif defined __clang__ && __clang_major__ > 6
     203    #elif !defined __GNUC__
     204    #elif __GNUC__ > 8
     205    #else
     206        #ifndef __clang__
     207            #pragma GCC push_options
     208            #pragma GCC target("rdseed")
     209        #else
     210            #define __RDSEED__
     211        #endif
     212        #include <x86intrin.h>
     213        #ifndef __clang__
     214            #pragma GCC pop_options
     215        #endif
     216    #endif
    183217#endif /* USE_WINDOWS_API */
    184218#endif
     
    280314};
    281315
    282 /* NOTE: if DRBG struct is changed please update random.h drbg_data size */
    283 typedef struct DRBG {
    284     word32 reseedCtr;
    285     word32 lastBlock;
    286     byte V[DRBG_SEED_LEN];
    287     byte C[DRBG_SEED_LEN];
    288 #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
    289     void* heap;
    290     int devId;
    291 #endif
    292     byte   matchCount;
    293 #ifdef WOLFSSL_SMALL_STACK_CACHE
    294     wc_Sha256 sha256;
    295 #endif
    296 } DRBG;
    297 
     316typedef struct DRBG_internal DRBG_internal;
    298317
    299318static int wc_RNG_HealthTestLocal(int reseed);
     
    301320/* Hash Derivation Function */
    302321/* Returns: DRBG_SUCCESS or DRBG_FAILURE */
    303 static int Hash_df(DRBG* drbg, byte* out, word32 outSz, byte type,
     322static int Hash_df(DRBG_internal* drbg, byte* out, word32 outSz, byte type,
    304323                                                  const byte* inA, word32 inASz,
    305324                                                  const byte* inB, word32 inBSz)
     
    390409
    391410/* Returns: DRBG_SUCCESS or DRBG_FAILURE */
    392 static int Hash_DRBG_Reseed(DRBG* drbg, const byte* seed, word32 seedSz)
     411static int Hash_DRBG_Reseed(DRBG_internal* drbg, const byte* seed, word32 seedSz)
    393412{
    394413    byte newV[DRBG_SEED_LEN];
     
    422441    }
    423442
    424     return Hash_DRBG_Reseed(rng->drbg, seed, seedSz);
     443    return Hash_DRBG_Reseed((DRBG_internal *)rng->drbg, seed, seedSz);
    425444}
    426445
     
    437456
    438457/* Returns: DRBG_SUCCESS or DRBG_FAILURE */
    439 static int Hash_gen(DRBG* drbg, byte* out, word32 outSz, const byte* V)
     458static int Hash_gen(DRBG_internal* drbg, byte* out, word32 outSz, const byte* V)
    440459{
    441460    int ret = DRBG_FAILURE;
     
    488507                }
    489508                else {
    490                     if (i == len) {
     509                    if (i == (len-1)) {
    491510                        len++;
    492511                    }
     
    512531            }
    513532        }
     533        else {
     534            /* wc_Sha256Update or wc_Sha256Final returned error */
     535            break;
     536        }
    514537    }
    515538    ForceZero(data, sizeof(data));
     
    529552        int sIdx, dIdx;
    530553
    531         for (sIdx = sLen - 1, dIdx = dLen - 1; sIdx >= 0; dIdx--, sIdx--)
    532         {
    533             carry += d[dIdx] + s[sIdx];
     554        for (sIdx = sLen - 1, dIdx = dLen - 1; sIdx >= 0; dIdx--, sIdx--) {
     555            carry += (word16)d[dIdx] + (word16)s[sIdx];
    534556            d[dIdx] = (byte)carry;
    535557            carry >>= 8;
     
    537559
    538560        for (; carry != 0 && dIdx >= 0; dIdx--) {
    539             carry += d[dIdx];
     561            carry += (word16)d[dIdx];
    540562            d[dIdx] = (byte)carry;
    541563            carry >>= 8;
     
    545567
    546568/* Returns: DRBG_SUCCESS, DRBG_NEED_RESEED, or DRBG_FAILURE */
    547 static int Hash_DRBG_Generate(DRBG* drbg, byte* out, word32 outSz)
     569static int Hash_DRBG_Generate(DRBG_internal* drbg, byte* out, word32 outSz)
    548570{
    549571    int ret;
     
    611633
    612634/* Returns: DRBG_SUCCESS or DRBG_FAILURE */
    613 static int Hash_DRBG_Instantiate(DRBG* drbg, const byte* seed, word32 seedSz,
     635static int Hash_DRBG_Instantiate(DRBG_internal* drbg, const byte* seed, word32 seedSz,
    614636                                             const byte* nonce, word32 nonceSz,
    615637                                             void* heap, int devId)
    616638{
    617     int ret;
    618 
    619     XMEMSET(drbg, 0, sizeof(DRBG));
     639    int ret = DRBG_FAILURE;
     640
     641    XMEMSET(drbg, 0, sizeof(DRBG_internal));
    620642#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
    621643    drbg->heap = heap;
     
    646668        ret = DRBG_SUCCESS;
    647669    }
    648     else {
    649         ret = DRBG_FAILURE;
    650     }
    651670
    652671    return ret;
     
    654673
    655674/* Returns: DRBG_SUCCESS or DRBG_FAILURE */
    656 static int Hash_DRBG_Uninstantiate(DRBG* drbg)
     675static int Hash_DRBG_Uninstantiate(DRBG_internal* drbg)
    657676{
    658677    word32 i;
     
    664683#endif
    665684
    666     ForceZero(drbg, sizeof(DRBG));
    667 
    668     for (i = 0; i < sizeof(DRBG); i++)
     685    ForceZero(drbg, sizeof(DRBG_internal));
     686
     687    for (i = 0; i < sizeof(DRBG_internal); i++)
    669688        compareSum |= compareDrbg[i] ^ 0;
    670689
     
    675694int wc_RNG_TestSeed(const byte* seed, word32 seedSz)
    676695{
    677     int ret = DRBG_SUCCESS;
     696    int ret = 0;
    678697
    679698    /* Check the seed for duplicate words. */
     
    701720                    void* heap, int devId)
    702721{
    703     int ret = RNG_FAILURE_E;
     722    int ret = 0;
    704723#ifdef HAVE_HASHDRBG
    705724    word32 seedSz = SEED_SZ + SEED_BLOCK_SZ;
     
    772791#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
    773792        rng->drbg =
    774                 (struct DRBG*)XMALLOC(sizeof(DRBG), rng->heap,
     793                (struct DRBG*)XMALLOC(sizeof(DRBG_internal), rng->heap,
    775794                                                          DYNAMIC_TYPE_RNG);
    776 #else
    777         /* compile-time validation of drbg_data size */
    778         typedef char drbg_data_test[sizeof(rng->drbg_data) >=
    779                 sizeof(struct DRBG) ? 1 : -1];
    780         (void)sizeof(drbg_data_test);
    781         rng->drbg = (struct DRBG*)rng->drbg_data;
    782 #endif
    783 
    784795        if (rng->drbg == NULL) {
    785796            ret = MEMORY_E;
    786         }
    787         else {
     797            rng->status = DRBG_FAILED;
     798        }
     799#else
     800        rng->drbg = (struct DRBG*)&rng->drbg_data;
     801#endif
     802        if (ret == 0) {
    788803            ret = wc_GenerateSeed(&rng->seed, seed, seedSz);
    789             if (ret != 0)
     804            if (ret == 0)
     805                ret = wc_RNG_TestSeed(seed, seedSz);
     806            else {
    790807                ret = DRBG_FAILURE;
    791             else
    792                 ret = wc_RNG_TestSeed(seed, seedSz);
     808                rng->status = DRBG_FAILED;
     809            }
    793810
    794811            if (ret == DRBG_SUCCESS)
    795                  ret = Hash_DRBG_Instantiate(rng->drbg,
     812              ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg,
    796813                            seed + SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ,
    797814                            nonce, nonceSz, rng->heap, devId);
     
    901918        return BAD_FUNC_ARG;
    902919
     920    if (sz == 0)
     921        return 0;
     922
    903923#ifdef WOLF_CRYPTO_CB
    904924    if (rng->devId != INVALID_DEVID) {
     
    913933    if (IS_INTEL_RDRAND(intel_flags))
    914934        return wc_GenerateRand_IntelRD(NULL, output, sz);
     935#endif
     936
     937#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_TRNG)
     938    return silabs_GenerateRand(output, sz);
    915939#endif
    916940
     
    940964        return RNG_FAILURE_E;
    941965
    942     ret = Hash_DRBG_Generate(rng->drbg, output, sz);
     966    ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz);
    943967    if (ret == DRBG_NEED_RESEED) {
    944968        if (wc_RNG_HealthTestLocal(1) == 0) {
     
    953977
    954978            if (ret == DRBG_SUCCESS)
    955                 ret = Hash_DRBG_Reseed(rng->drbg, newSeed + SEED_BLOCK_SZ,
     979              ret = Hash_DRBG_Reseed((DRBG_internal *)rng->drbg, newSeed + SEED_BLOCK_SZ,
    956980                                       SEED_SZ);
    957981            if (ret == DRBG_SUCCESS)
    958                 ret = Hash_DRBG_Generate(rng->drbg, output, sz);
     982              ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz);
    959983
    960984            ForceZero(newSeed, sizeof(newSeed));
     
    10061030#ifdef HAVE_HASHDRBG
    10071031    if (rng->drbg != NULL) {
    1008         if (Hash_DRBG_Uninstantiate(rng->drbg) != DRBG_SUCCESS)
     1032      if (Hash_DRBG_Uninstantiate((DRBG_internal *)rng->drbg) != DRBG_SUCCESS)
    10091033            ret = RNG_FAILURE_E;
    10101034
     
    10401064{
    10411065    int ret = -1;
    1042     DRBG* drbg;
     1066    DRBG_internal* drbg;
    10431067#ifndef WOLFSSL_SMALL_STACK
    1044     DRBG  drbg_var;
     1068    DRBG_internal  drbg_var;
    10451069#endif
    10461070
     
    10581082
    10591083#ifdef WOLFSSL_SMALL_STACK
    1060     drbg = (DRBG*)XMALLOC(sizeof(DRBG), NULL, DYNAMIC_TYPE_RNG);
     1084    drbg = (DRBG_internal*)XMALLOC(sizeof(DRBG_internal), NULL, DYNAMIC_TYPE_RNG);
    10611085    if (drbg == NULL) {
    10621086        return MEMORY_E;
     
    11081132
    11091133
    1110 const byte seedA[] = {
     1134const FLASH_QUALIFIER byte seedA_data[] = {
    11111135    0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
    11121136    0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
     
    11151139};
    11161140
    1117 const byte reseedSeedA[] = {
     1141const FLASH_QUALIFIER byte reseedSeedA_data[] = {
    11181142    0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
    11191143    0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
     
    11211145};
    11221146
    1123 const byte outputA[] = {
     1147const FLASH_QUALIFIER byte outputA_data[] = {
    11241148    0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
    11251149    0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
     
    11351159};
    11361160
    1137 const byte seedB[] = {
     1161const FLASH_QUALIFIER byte seedB_data[] = {
    11381162    0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
    11391163    0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
     
    11431167};
    11441168
    1145 const byte outputB[] = {
     1169const FLASH_QUALIFIER byte outputB_data[] = {
    11461170    0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
    11471171    0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
     
    11761200
    11771201    if (reseed) {
    1178         ret = wc_RNG_HealthTest(1, seedA, sizeof(seedA),
    1179                                 reseedSeedA, sizeof(reseedSeedA),
     1202#ifdef WOLFSSL_USE_FLASHMEM
     1203        byte* seedA = (byte*)XMALLOC(sizeof(seedA_data), NULL,
     1204                             DYNAMIC_TYPE_TMP_BUFFER);
     1205        byte* reseedSeedA = (byte*)XMALLOC(sizeof(reseedSeedA_data), NULL,
     1206                             DYNAMIC_TYPE_TMP_BUFFER);
     1207        byte* outputA = (byte*)XMALLOC(sizeof(outputA_data), NULL,
     1208                             DYNAMIC_TYPE_TMP_BUFFER);
     1209
     1210        if (!seedA || !reseedSeedA || !outputA) {
     1211            XFREE(seedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1212            XFREE(reseedSeedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1213            XFREE(outputA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1214            ret = MEMORY_E;
     1215        }
     1216        else {
     1217            XMEMCPY_P(seedA, seedA_data, sizeof(seedA_data));
     1218            XMEMCPY_P(reseedSeedA, reseedSeedA_data, sizeof(reseedSeedA_data));
     1219            XMEMCPY_P(outputA, outputA_data, sizeof(outputA_data));
     1220#else
     1221        const byte* seedA = seedA_data;
     1222        const byte* reseedSeedA = reseedSeedA_data;
     1223        const byte* outputA = outputA_data;
     1224#endif
     1225        ret = wc_RNG_HealthTest(1, seedA, sizeof(seedA_data),
     1226                                reseedSeedA, sizeof(reseedSeedA_data),
    11801227                                check, RNG_HEALTH_TEST_CHECK_SIZE);
    11811228        if (ret == 0) {
     
    11841231                ret = -1;
    11851232        }
     1233
     1234#ifdef WOLFSSL_USE_FLASHMEM
     1235            XFREE(seedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1236            XFREE(reseedSeedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1237            XFREE(outputA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1238        }
     1239#endif
    11861240    }
    11871241    else {
    1188         ret = wc_RNG_HealthTest(0, seedB, sizeof(seedB),
     1242#ifdef WOLFSSL_USE_FLASHMEM
     1243        byte* seedB = (byte*)XMALLOC(sizeof(seedB_data), NULL,
     1244                             DYNAMIC_TYPE_TMP_BUFFER);
     1245        byte* outputB = (byte*)XMALLOC(sizeof(outputB_data), NULL,
     1246                               DYNAMIC_TYPE_TMP_BUFFER);
     1247
     1248        if (!seedB || !outputB) {
     1249            XFREE(seedB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1250            XFREE(outputB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1251            ret = MEMORY_E;
     1252        }
     1253        else {
     1254            XMEMCPY_P(seedB, seedB_data, sizeof(seedB_data));
     1255            XMEMCPY_P(outputB, outputB_data, sizeof(outputB_data));
     1256#else
     1257        const byte* seedB = seedB_data;
     1258        const byte* outputB = outputB_data;
     1259#endif
     1260        ret = wc_RNG_HealthTest(0, seedB, sizeof(seedB_data),
    11891261                                NULL, 0,
    11901262                                check, RNG_HEALTH_TEST_CHECK_SIZE);
     
    12011273        if (ret == 0) {
    12021274            ret = wc_RNG_HealthTest_ex(0,
    1203                                     seedB + 32, sizeof(seedB) - 32,
     1275                                    seedB + 32, sizeof(seedB_data) - 32,
    12041276                                    seedB, 32,
    12051277                                    NULL, 0,
     
    12071279                                    NULL, INVALID_DEVID);
    12081280            if (ret == 0) {
    1209                 if (ConstantCompare(check, outputB, sizeof(outputB)) != 0)
     1281                if (ConstantCompare(check, outputB, sizeof(outputB_data)) != 0)
    12101282                    ret = -1;
    12111283            }
    12121284        }
     1285
     1286#ifdef WOLFSSL_USE_FLASHMEM
     1287            XFREE(seedB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1288            XFREE(outputB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     1289        }
     1290#endif
    12131291    }
    12141292
     
    13321410#ifdef HAVE_INTEL_RDSEED
    13331411
    1334 #ifndef USE_WINDOWS_API
     1412#ifndef USE_INTEL_INTRINSICS
    13351413
    13361414    /* return 0 on success */
     
    13431421    }
    13441422
    1345 #else /* USE_WINDOWS_API */
     1423#else /* USE_INTEL_INTRINSICS */
    13461424    /* The compiler Visual Studio uses does not allow inline assembly.
    13471425     * It does allow for Intel intrinsic functions. */
    13481426
    13491427    /* return 0 on success */
     1428# ifdef __GNUC__
     1429    __attribute__((target("rdseed")))
     1430# endif
    13501431    static WC_INLINE int IntelRDseed64(word64* seed)
    13511432    {
    13521433        int ok;
    13531434
    1354         ok = _rdseed64_step(seed);
     1435        ok = _rdseed64_step((unsigned long long*) seed);
    13551436        return (ok) ? 0 : -1;
    13561437    }
    13571438
    1358 #endif /* USE_WINDOWS_API */
     1439#endif /* USE_INTEL_INTRINSICS */
    13591440
    13601441/* return 0 on success */
     
    13691450}
    13701451
     1452#ifndef WOLFSSL_LINUXKM
    13711453/* return 0 on success */
    13721454static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz)
     
    13991481    return 0;
    14001482}
     1483#endif
    14011484
    14021485#endif /* HAVE_INTEL_RDSEED */
     
    14041487#ifdef HAVE_INTEL_RDRAND
    14051488
    1406 #ifndef USE_WINDOWS_API
     1489#ifndef USE_INTEL_INTRINSICS
    14071490
    14081491/* return 0 on success */
     
    14161499}
    14171500
    1418 #else /* USE_WINDOWS_API */
     1501#else /* USE_INTEL_INTRINSICS */
    14191502    /* The compiler Visual Studio uses does not allow inline assembly.
    14201503     * It does allow for Intel intrinsic functions. */
    14211504
    14221505/* return 0 on success */
     1506# ifdef __GNUC__
     1507__attribute__((target("rdrnd")))
     1508# endif
    14231509static WC_INLINE int IntelRDrand64(word64 *rnd)
    14241510{
    14251511    int ok;
    14261512
    1427     ok = _rdrand64_step(rnd);
     1513    ok = _rdrand64_step((unsigned long long*) rnd);
    14281514
    14291515    return (ok) ? 0 : -1;
    14301516}
    14311517
    1432 #endif /* USE_WINDOWS_API */
     1518#endif /* USE_INTEL_INTRINSICS */
    14331519
    14341520/* return 0 on success */
     
    16081694}
    16091695
     1696#elif (defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC_RNG)) && \
     1697      !defined(WOLFSSL_PIC32MZ_RNG)
     1698    /* enable ATECC RNG unless using PIC32MZ one instead */
     1699    #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
     1700
     1701    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
     1702    {
     1703        int ret = 0;
     1704
     1705        (void)os;
     1706        if (output == NULL) {
     1707            return BUFFER_E;
     1708        }
     1709
     1710        ret = atmel_get_random_number(sz, output);
     1711
     1712        return ret;
     1713    }
    16101714
    16111715#elif defined(MICROCHIP_PIC32)
     
    18161920    #endif /* FREESCALE_K70_RNGA */
    18171921
     1922#elif defined(WOLFSSL_SILABS_SE_ACCEL)
     1923    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
     1924    {
     1925        (void)os;
     1926        return silabs_GenerateRand(output, sz);
     1927    }
     1928
    18181929#elif defined(STM32_RNG)
    18191930     /* Generate a RNG seed using the hardware random number generator
     
    18471958            ) {
    18481959                /* Single byte at a time */
    1849                 uint32_t tmpRng = 0;
     1960                word32 tmpRng = 0;
    18501961                if (HAL_RNG_GenerateRandomNumber(&hrng, &tmpRng) != HAL_OK) {
    18511962                    wolfSSL_CryptHwMutexUnLock();
     
    18561967            else {
    18571968                /* Use native 32 instruction */
    1858                 if (HAL_RNG_GenerateRandomNumber(&hrng, (uint32_t*)&output[i]) != HAL_OK) {
     1969                if (HAL_RNG_GenerateRandomNumber(&hrng, (word32*)&output[i]) != HAL_OK) {
    18591970                    wolfSSL_CryptHwMutexUnLock();
    18601971                    return RAN_BLOCK_E;
     
    20532164    }
    20542165
    2055 #elif defined(WOLFSSL_NRF51)
     2166#elif defined(WOLFSSL_NRF51) || defined(WOLFSSL_NRF5x)
    20562167    #include "app_error.h"
    20572168    #include "nrf_drv_rng.h"
     
    20592170    {
    20602171        int remaining = sz, length, pos = 0;
    2061         uint8_t available;
    2062         uint32_t err_code;
     2172        word32 err_code;
     2173        byte available;
     2174        static byte initialized = 0;
    20632175
    20642176        (void)os;
    20652177
    20662178        /* Make sure RNG is running */
    2067         err_code = nrf_drv_rng_init(NULL);
    2068         if (err_code != NRF_SUCCESS && err_code != NRF_ERROR_INVALID_STATE) {
    2069             return -1;
     2179        if (!initialized) {
     2180            err_code = nrf_drv_rng_init(NULL);
     2181            if (err_code != NRF_SUCCESS && err_code != NRF_ERROR_INVALID_STATE
     2182            #ifdef NRF_ERROR_MODULE_ALREADY_INITIALIZED
     2183                && err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED
     2184            #endif
     2185            ) {
     2186                return -1;
     2187            }
     2188            initialized = 1;
    20702189        }
    20712190
    20722191        while (remaining > 0) {
    2073             err_code = nrf_drv_rng_bytes_available(&available);
    2074             if (err_code == NRF_SUCCESS) {
    2075                 length = (remaining < available) ? remaining : available;
    2076                 if (length > 0) {
    2077                     err_code = nrf_drv_rng_rand(&output[pos], length);
    2078                     remaining -= length;
    2079                     pos += length;
     2192            available = 0;
     2193            nrf_drv_rng_bytes_available(&available); /* void func */
     2194            length = (remaining < available) ? remaining : available;
     2195            if (length > 0) {
     2196                err_code = nrf_drv_rng_rand(&output[pos], length);
     2197                if (err_code != NRF_SUCCESS) {
     2198                    break;
    20802199                }
    2081             }
    2082 
    2083             if (err_code != NRF_SUCCESS) {
    2084                 break;
     2200                remaining -= length;
     2201                pos += length;
    20852202            }
    20862203        }
     
    21152232
    21162233        return 0;
    2117     }
    2118 
    2119 #elif defined(WOLFSSL_ATMEL)
    2120     #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
    2121 
    2122     int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
    2123     {
    2124         int ret = 0;
    2125 
    2126         (void)os;
    2127         if (output == NULL) {
    2128             return BUFFER_E;
    2129         }
    2130 
    2131         ret = atmel_get_random_number(sz, output);
    2132 
    2133         return ret;
    21342234    }
    21352235
     
    22902390    #endif /* end WOLFSSL_ESPWROOM32 */
    22912391
     2392#elif defined(WOLFSSL_LINUXKM)
     2393    #include <linux/random.h>
     2394    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
     2395    {
     2396        (void)os;
     2397
     2398        get_random_bytes(output, sz);
     2399
     2400        return 0;
     2401    }
     2402
    22922403#elif defined(WOLFSSL_RENESAS_TSIP)
    22932404#if defined(WOLFSSL_RENESA_TSIP_IAREWRX)
    2294     #include "r_bsp/mcu/all/r_rx_compiler.h"
    2295 #endif
    2296     #include "r_bsp/platform.h"
     2405   #include "r_bsp/mcu/all/r_rx_compiler.h"
     2406#endif
     2407   #include "r_bsp/platform.h"
    22972408    #include "r_tsip_rx_if.h"
    2298    
     2409
    22992410    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
    23002411    {
    23012412        int ret;
    2302         uint32_t buffer[4];
     2413        word32 buffer[4];
    23032414
    23042415        while (sz > 0) {
    2305             uint32_t len = sizeof(buffer);
    2306            
     2416            word32 len = sizeof(buffer);
     2417
    23072418            if (sz < len) {
    23082419                len = sz;
     
    23292440    int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
    23302441    {
    2331         uint32_t ret;
    2332         uint32_t blocks;
    2333         word32   len = sz;
     2442        word32 ret;
     2443        word32 blocks;
     2444        word32 len = sz;
    23342445
    23352446        ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->open(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
     
    23402451        }
    23412452
    2342         blocks = sz / sizeof(uint32_t);
     2453        blocks = sz / sizeof(word32);
    23432454        if (blocks > 0) {
    23442455            ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
    2345                                                       (uint32_t*)output, blocks);
     2456                                                       (word32*)output, blocks);
    23462457            if (ret != SSP_SUCCESS) {
    23472458                return -1;
     
    23492460        }
    23502461
    2351         len = len - (blocks * sizeof(uint32_t));
     2462        len = len - (blocks * sizeof(word32));
    23522463        if (len > 0) {
    2353             uint32_t tmp;
    2354 
    2355             if (len > sizeof(uint32_t)) {
     2464            word32 tmp;
     2465
     2466            if (len > sizeof(word32)) {
    23562467                return -1;
    23572468            }
    23582469            ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
    2359                                                       (uint32_t*)tmp, 1);
     2470                                                      (word32*)tmp, 1);
    23602471            if (ret != SSP_SUCCESS) {
    23612472                return -1;
    23622473            }
    2363             XMEMCPY(output + (blocks * sizeof(uint32_t)), (byte*)&tmp, len);
     2474            XMEMCPY(output + (blocks * sizeof(word32)), (byte*)&tmp, len);
    23642475        }
    23652476
Note: See TracChangeset for help on using the changeset viewer.