- Timestamp:
- Jun 22, 2021, 9:00:19 PM (3 years ago)
- 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 20 20 */ 21 21 22 22 /* 23 24 DESCRIPTION 25 This library contains implementation for the random number generator. 26 27 */ 23 28 #ifdef HAVE_CONFIG_H 24 29 #include <config.h> … … 165 170 #endif 166 171 172 #if defined(WOLFSSL_SILABS_SE_ACCEL) 173 #include <wolfssl/wolfcrypt/port/silabs/silabs_random.h> 174 #endif 175 167 176 168 177 #if defined(HAVE_INTEL_RDRAND) || defined(HAVE_INTEL_RDSEED) … … 172 181 intel_flags = cpuid_get_flags(); 173 182 } 174 #if def HAVE_INTEL_RDSEED183 #if defined(HAVE_INTEL_RDSEED) && !defined(WOLFSSL_LINUXKM) 175 184 static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz); 176 185 #endif … … 180 189 181 190 #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 182 199 #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 183 217 #endif /* USE_WINDOWS_API */ 184 218 #endif … … 280 314 }; 281 315 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 316 typedef struct DRBG_internal DRBG_internal; 298 317 299 318 static int wc_RNG_HealthTestLocal(int reseed); … … 301 320 /* Hash Derivation Function */ 302 321 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */ 303 static int Hash_df(DRBG * drbg, byte* out, word32 outSz, byte type,322 static int Hash_df(DRBG_internal* drbg, byte* out, word32 outSz, byte type, 304 323 const byte* inA, word32 inASz, 305 324 const byte* inB, word32 inBSz) … … 390 409 391 410 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */ 392 static int Hash_DRBG_Reseed(DRBG * drbg, const byte* seed, word32 seedSz)411 static int Hash_DRBG_Reseed(DRBG_internal* drbg, const byte* seed, word32 seedSz) 393 412 { 394 413 byte newV[DRBG_SEED_LEN]; … … 422 441 } 423 442 424 return Hash_DRBG_Reseed( rng->drbg, seed, seedSz);443 return Hash_DRBG_Reseed((DRBG_internal *)rng->drbg, seed, seedSz); 425 444 } 426 445 … … 437 456 438 457 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */ 439 static int Hash_gen(DRBG * drbg, byte* out, word32 outSz, const byte* V)458 static int Hash_gen(DRBG_internal* drbg, byte* out, word32 outSz, const byte* V) 440 459 { 441 460 int ret = DRBG_FAILURE; … … 488 507 } 489 508 else { 490 if (i == len) {509 if (i == (len-1)) { 491 510 len++; 492 511 } … … 512 531 } 513 532 } 533 else { 534 /* wc_Sha256Update or wc_Sha256Final returned error */ 535 break; 536 } 514 537 } 515 538 ForceZero(data, sizeof(data)); … … 529 552 int sIdx, dIdx; 530 553 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]; 534 556 d[dIdx] = (byte)carry; 535 557 carry >>= 8; … … 537 559 538 560 for (; carry != 0 && dIdx >= 0; dIdx--) { 539 carry += d[dIdx];561 carry += (word16)d[dIdx]; 540 562 d[dIdx] = (byte)carry; 541 563 carry >>= 8; … … 545 567 546 568 /* Returns: DRBG_SUCCESS, DRBG_NEED_RESEED, or DRBG_FAILURE */ 547 static int Hash_DRBG_Generate(DRBG * drbg, byte* out, word32 outSz)569 static int Hash_DRBG_Generate(DRBG_internal* drbg, byte* out, word32 outSz) 548 570 { 549 571 int ret; … … 611 633 612 634 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */ 613 static int Hash_DRBG_Instantiate(DRBG * drbg, const byte* seed, word32 seedSz,635 static int Hash_DRBG_Instantiate(DRBG_internal* drbg, const byte* seed, word32 seedSz, 614 636 const byte* nonce, word32 nonceSz, 615 637 void* heap, int devId) 616 638 { 617 int ret ;618 619 XMEMSET(drbg, 0, sizeof(DRBG ));639 int ret = DRBG_FAILURE; 640 641 XMEMSET(drbg, 0, sizeof(DRBG_internal)); 620 642 #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB) 621 643 drbg->heap = heap; … … 646 668 ret = DRBG_SUCCESS; 647 669 } 648 else {649 ret = DRBG_FAILURE;650 }651 670 652 671 return ret; … … 654 673 655 674 /* Returns: DRBG_SUCCESS or DRBG_FAILURE */ 656 static int Hash_DRBG_Uninstantiate(DRBG * drbg)675 static int Hash_DRBG_Uninstantiate(DRBG_internal* drbg) 657 676 { 658 677 word32 i; … … 664 683 #endif 665 684 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++) 669 688 compareSum |= compareDrbg[i] ^ 0; 670 689 … … 675 694 int wc_RNG_TestSeed(const byte* seed, word32 seedSz) 676 695 { 677 int ret = DRBG_SUCCESS;696 int ret = 0; 678 697 679 698 /* Check the seed for duplicate words. */ … … 701 720 void* heap, int devId) 702 721 { 703 int ret = RNG_FAILURE_E;722 int ret = 0; 704 723 #ifdef HAVE_HASHDRBG 705 724 word32 seedSz = SEED_SZ + SEED_BLOCK_SZ; … … 772 791 #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) 773 792 rng->drbg = 774 (struct DRBG*)XMALLOC(sizeof(DRBG ), rng->heap,793 (struct DRBG*)XMALLOC(sizeof(DRBG_internal), rng->heap, 775 794 DYNAMIC_TYPE_RNG); 776 #else777 /* 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 #endif783 784 795 if (rng->drbg == NULL) { 785 796 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) { 788 803 ret = wc_GenerateSeed(&rng->seed, seed, seedSz); 789 if (ret != 0) 804 if (ret == 0) 805 ret = wc_RNG_TestSeed(seed, seedSz); 806 else { 790 807 ret = DRBG_FAILURE; 791 else792 ret = wc_RNG_TestSeed(seed, seedSz);808 rng->status = DRBG_FAILED; 809 } 793 810 794 811 if (ret == DRBG_SUCCESS) 795 ret = Hash_DRBG_Instantiate(rng->drbg,812 ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg, 796 813 seed + SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ, 797 814 nonce, nonceSz, rng->heap, devId); … … 901 918 return BAD_FUNC_ARG; 902 919 920 if (sz == 0) 921 return 0; 922 903 923 #ifdef WOLF_CRYPTO_CB 904 924 if (rng->devId != INVALID_DEVID) { … … 913 933 if (IS_INTEL_RDRAND(intel_flags)) 914 934 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); 915 939 #endif 916 940 … … 940 964 return RNG_FAILURE_E; 941 965 942 ret = Hash_DRBG_Generate( rng->drbg, output, sz);966 ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz); 943 967 if (ret == DRBG_NEED_RESEED) { 944 968 if (wc_RNG_HealthTestLocal(1) == 0) { … … 953 977 954 978 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, 956 980 SEED_SZ); 957 981 if (ret == DRBG_SUCCESS) 958 ret = Hash_DRBG_Generate(rng->drbg, output, sz);982 ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz); 959 983 960 984 ForceZero(newSeed, sizeof(newSeed)); … … 1006 1030 #ifdef HAVE_HASHDRBG 1007 1031 if (rng->drbg != NULL) { 1008 if (Hash_DRBG_Uninstantiate(rng->drbg) != DRBG_SUCCESS)1032 if (Hash_DRBG_Uninstantiate((DRBG_internal *)rng->drbg) != DRBG_SUCCESS) 1009 1033 ret = RNG_FAILURE_E; 1010 1034 … … 1040 1064 { 1041 1065 int ret = -1; 1042 DRBG * drbg;1066 DRBG_internal* drbg; 1043 1067 #ifndef WOLFSSL_SMALL_STACK 1044 DRBG drbg_var;1068 DRBG_internal drbg_var; 1045 1069 #endif 1046 1070 … … 1058 1082 1059 1083 #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); 1061 1085 if (drbg == NULL) { 1062 1086 return MEMORY_E; … … 1108 1132 1109 1133 1110 const byte seedA[] = {1134 const FLASH_QUALIFIER byte seedA_data[] = { 1111 1135 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4, 1112 1136 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00, … … 1115 1139 }; 1116 1140 1117 const byte reseedSeedA[] = {1141 const FLASH_QUALIFIER byte reseedSeedA_data[] = { 1118 1142 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3, 1119 1143 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22, … … 1121 1145 }; 1122 1146 1123 const byte outputA[] = {1147 const FLASH_QUALIFIER byte outputA_data[] = { 1124 1148 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb, 1125 1149 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79, … … 1135 1159 }; 1136 1160 1137 const byte seedB[] = {1161 const FLASH_QUALIFIER byte seedB_data[] = { 1138 1162 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3, 1139 1163 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19, … … 1143 1167 }; 1144 1168 1145 const byte outputB[] = {1169 const FLASH_QUALIFIER byte outputB_data[] = { 1146 1170 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64, 1147 1171 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5, … … 1176 1200 1177 1201 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), 1180 1227 check, RNG_HEALTH_TEST_CHECK_SIZE); 1181 1228 if (ret == 0) { … … 1184 1231 ret = -1; 1185 1232 } 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 1186 1240 } 1187 1241 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), 1189 1261 NULL, 0, 1190 1262 check, RNG_HEALTH_TEST_CHECK_SIZE); … … 1201 1273 if (ret == 0) { 1202 1274 ret = wc_RNG_HealthTest_ex(0, 1203 seedB + 32, sizeof(seedB ) - 32,1275 seedB + 32, sizeof(seedB_data) - 32, 1204 1276 seedB, 32, 1205 1277 NULL, 0, … … 1207 1279 NULL, INVALID_DEVID); 1208 1280 if (ret == 0) { 1209 if (ConstantCompare(check, outputB, sizeof(outputB )) != 0)1281 if (ConstantCompare(check, outputB, sizeof(outputB_data)) != 0) 1210 1282 ret = -1; 1211 1283 } 1212 1284 } 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 1213 1291 } 1214 1292 … … 1332 1410 #ifdef HAVE_INTEL_RDSEED 1333 1411 1334 #ifndef USE_ WINDOWS_API1412 #ifndef USE_INTEL_INTRINSICS 1335 1413 1336 1414 /* return 0 on success */ … … 1343 1421 } 1344 1422 1345 #else /* USE_ WINDOWS_API*/1423 #else /* USE_INTEL_INTRINSICS */ 1346 1424 /* The compiler Visual Studio uses does not allow inline assembly. 1347 1425 * It does allow for Intel intrinsic functions. */ 1348 1426 1349 1427 /* return 0 on success */ 1428 # ifdef __GNUC__ 1429 __attribute__((target("rdseed"))) 1430 # endif 1350 1431 static WC_INLINE int IntelRDseed64(word64* seed) 1351 1432 { 1352 1433 int ok; 1353 1434 1354 ok = _rdseed64_step( seed);1435 ok = _rdseed64_step((unsigned long long*) seed); 1355 1436 return (ok) ? 0 : -1; 1356 1437 } 1357 1438 1358 #endif /* USE_ WINDOWS_API*/1439 #endif /* USE_INTEL_INTRINSICS */ 1359 1440 1360 1441 /* return 0 on success */ … … 1369 1450 } 1370 1451 1452 #ifndef WOLFSSL_LINUXKM 1371 1453 /* return 0 on success */ 1372 1454 static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz) … … 1399 1481 return 0; 1400 1482 } 1483 #endif 1401 1484 1402 1485 #endif /* HAVE_INTEL_RDSEED */ … … 1404 1487 #ifdef HAVE_INTEL_RDRAND 1405 1488 1406 #ifndef USE_ WINDOWS_API1489 #ifndef USE_INTEL_INTRINSICS 1407 1490 1408 1491 /* return 0 on success */ … … 1416 1499 } 1417 1500 1418 #else /* USE_ WINDOWS_API*/1501 #else /* USE_INTEL_INTRINSICS */ 1419 1502 /* The compiler Visual Studio uses does not allow inline assembly. 1420 1503 * It does allow for Intel intrinsic functions. */ 1421 1504 1422 1505 /* return 0 on success */ 1506 # ifdef __GNUC__ 1507 __attribute__((target("rdrnd"))) 1508 # endif 1423 1509 static WC_INLINE int IntelRDrand64(word64 *rnd) 1424 1510 { 1425 1511 int ok; 1426 1512 1427 ok = _rdrand64_step( rnd);1513 ok = _rdrand64_step((unsigned long long*) rnd); 1428 1514 1429 1515 return (ok) ? 0 : -1; 1430 1516 } 1431 1517 1432 #endif /* USE_ WINDOWS_API*/1518 #endif /* USE_INTEL_INTRINSICS */ 1433 1519 1434 1520 /* return 0 on success */ … … 1608 1694 } 1609 1695 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 } 1610 1714 1611 1715 #elif defined(MICROCHIP_PIC32) … … 1816 1920 #endif /* FREESCALE_K70_RNGA */ 1817 1921 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 1818 1929 #elif defined(STM32_RNG) 1819 1930 /* Generate a RNG seed using the hardware random number generator … … 1847 1958 ) { 1848 1959 /* Single byte at a time */ 1849 uint32_ttmpRng = 0;1960 word32 tmpRng = 0; 1850 1961 if (HAL_RNG_GenerateRandomNumber(&hrng, &tmpRng) != HAL_OK) { 1851 1962 wolfSSL_CryptHwMutexUnLock(); … … 1856 1967 else { 1857 1968 /* 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) { 1859 1970 wolfSSL_CryptHwMutexUnLock(); 1860 1971 return RAN_BLOCK_E; … … 2053 2164 } 2054 2165 2055 #elif defined(WOLFSSL_NRF51) 2166 #elif defined(WOLFSSL_NRF51) || defined(WOLFSSL_NRF5x) 2056 2167 #include "app_error.h" 2057 2168 #include "nrf_drv_rng.h" … … 2059 2170 { 2060 2171 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; 2063 2175 2064 2176 (void)os; 2065 2177 2066 2178 /* 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; 2070 2189 } 2071 2190 2072 2191 while (remaining > 0) { 2073 err_code = nrf_drv_rng_bytes_available(&available);2074 if (err_code == NRF_SUCCESS) {2075 2076 2077 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; 2080 2199 } 2081 } 2082 2083 if (err_code != NRF_SUCCESS) { 2084 break; 2200 remaining -= length; 2201 pos += length; 2085 2202 } 2086 2203 } … … 2115 2232 2116 2233 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;2134 2234 } 2135 2235 … … 2290 2390 #endif /* end WOLFSSL_ESPWROOM32 */ 2291 2391 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 2292 2403 #elif defined(WOLFSSL_RENESAS_TSIP) 2293 2404 #if defined(WOLFSSL_RENESA_TSIP_IAREWRX) 2294 2295 #endif 2296 2405 #include "r_bsp/mcu/all/r_rx_compiler.h" 2406 #endif 2407 #include "r_bsp/platform.h" 2297 2408 #include "r_tsip_rx_if.h" 2298 2409 2299 2410 int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) 2300 2411 { 2301 2412 int ret; 2302 uint32_tbuffer[4];2413 word32 buffer[4]; 2303 2414 2304 2415 while (sz > 0) { 2305 uint32_tlen = sizeof(buffer);2306 2416 word32 len = sizeof(buffer); 2417 2307 2418 if (sz < len) { 2308 2419 len = sz; … … 2329 2440 int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) 2330 2441 { 2331 uint32_tret;2332 uint32_tblocks;2333 word32 2442 word32 ret; 2443 word32 blocks; 2444 word32 len = sz; 2334 2445 2335 2446 ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->open(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl, … … 2340 2451 } 2341 2452 2342 blocks = sz / sizeof( uint32_t);2453 blocks = sz / sizeof(word32); 2343 2454 if (blocks > 0) { 2344 2455 ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl, 2345 (uint32_t*)output, blocks);2456 (word32*)output, blocks); 2346 2457 if (ret != SSP_SUCCESS) { 2347 2458 return -1; … … 2349 2460 } 2350 2461 2351 len = len - (blocks * sizeof( uint32_t));2462 len = len - (blocks * sizeof(word32)); 2352 2463 if (len > 0) { 2353 uint32_ttmp;2354 2355 if (len > sizeof( uint32_t)) {2464 word32 tmp; 2465 2466 if (len > sizeof(word32)) { 2356 2467 return -1; 2357 2468 } 2358 2469 ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl, 2359 ( uint32_t*)tmp, 1);2470 (word32*)tmp, 1); 2360 2471 if (ret != SSP_SUCCESS) { 2361 2472 return -1; 2362 2473 } 2363 XMEMCPY(output + (blocks * sizeof( uint32_t)), (byte*)&tmp, len);2474 XMEMCPY(output + (blocks * sizeof(word32)), (byte*)&tmp, len); 2364 2475 } 2365 2476
Note:
See TracChangeset
for help on using the changeset viewer.