random.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953
  1. /* random.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. DESCRIPTION
  23. This library contains implementation for the random number generator.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #include <wolfssl/wolfcrypt/error-crypt.h>
  30. /* on HPUX 11 you may need to install /dev/random see
  31. http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I
  32. */
  33. #if defined(HAVE_FIPS) && \
  34. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  35. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  36. #define FIPS_NO_WRAPPERS
  37. #ifdef USE_WINDOWS_API
  38. #pragma code_seg(".fipsA$c")
  39. #pragma const_seg(".fipsB$c")
  40. #endif
  41. #endif
  42. #include <wolfssl/wolfcrypt/random.h>
  43. #include <wolfssl/wolfcrypt/cpuid.h>
  44. /* If building for old FIPS. */
  45. #if defined(HAVE_FIPS) && \
  46. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  47. int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz)
  48. {
  49. return GenerateSeed(os, seed, sz);
  50. }
  51. int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId)
  52. {
  53. (void)heap;
  54. (void)devId;
  55. return InitRng_fips(rng);
  56. }
  57. WOLFSSL_ABI
  58. int wc_InitRng(WC_RNG* rng)
  59. {
  60. return InitRng_fips(rng);
  61. }
  62. int wc_RNG_GenerateBlock(WC_RNG* rng, byte* b, word32 sz)
  63. {
  64. return RNG_GenerateBlock_fips(rng, b, sz);
  65. }
  66. int wc_RNG_GenerateByte(WC_RNG* rng, byte* b)
  67. {
  68. return RNG_GenerateByte(rng, b);
  69. }
  70. #ifdef HAVE_HASHDRBG
  71. int wc_FreeRng(WC_RNG* rng)
  72. {
  73. return FreeRng_fips(rng);
  74. }
  75. int wc_RNG_HealthTest(int reseed, const byte* seedA, word32 seedASz,
  76. const byte* seedB, word32 seedBSz,
  77. byte* output, word32 outputSz)
  78. {
  79. return RNG_HealthTest_fips(reseed, seedA, seedASz,
  80. seedB, seedBSz, output, outputSz);
  81. }
  82. #endif /* HAVE_HASHDRBG */
  83. #else /* else build without fips, or for new fips */
  84. #ifndef WC_NO_RNG /* if not FIPS and RNG is disabled then do not compile */
  85. #include <wolfssl/wolfcrypt/sha256.h>
  86. #ifdef WOLF_CRYPTO_CB
  87. #include <wolfssl/wolfcrypt/cryptocb.h>
  88. #endif
  89. #ifdef NO_INLINE
  90. #include <wolfssl/wolfcrypt/misc.h>
  91. #else
  92. #define WOLFSSL_MISC_INCLUDED
  93. #include <wolfcrypt/src/misc.c>
  94. #endif
  95. #if defined(WOLFSSL_SGX)
  96. #include <sgx_trts.h>
  97. #elif defined(USE_WINDOWS_API)
  98. #ifndef _WIN32_WINNT
  99. #define _WIN32_WINNT 0x0400
  100. #endif
  101. #include <windows.h>
  102. #include <wincrypt.h>
  103. #elif defined(HAVE_WNR)
  104. #include <wnr.h>
  105. #include <wolfssl/wolfcrypt/logging.h>
  106. wolfSSL_Mutex wnr_mutex; /* global netRandom mutex */
  107. int wnr_timeout = 0; /* entropy timeout, milliseconds */
  108. int wnr_mutex_init = 0; /* flag for mutex init */
  109. wnr_context* wnr_ctx; /* global netRandom context */
  110. #elif defined(FREESCALE_KSDK_2_0_TRNG)
  111. #include "fsl_trng.h"
  112. #elif defined(FREESCALE_KSDK_2_0_RNGA)
  113. #include "fsl_rnga.h"
  114. #elif defined(WOLFSSL_WICED)
  115. #include "wiced_crypto.h"
  116. #elif defined(WOLFSSL_NETBURNER)
  117. #include <predef.h>
  118. #include <basictypes.h>
  119. #include <random.h>
  120. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  121. #include "wolfssl/wolfcrypt/port/xilinx/xil-versal-trng.h"
  122. #elif defined(NO_DEV_RANDOM)
  123. #elif defined(CUSTOM_RAND_GENERATE)
  124. #elif defined(CUSTOM_RAND_GENERATE_BLOCK)
  125. #elif defined(CUSTOM_RAND_GENERATE_SEED)
  126. #elif defined(WOLFSSL_GENSEED_FORTEST)
  127. #elif defined(WOLFSSL_MDK_ARM)
  128. #elif defined(WOLFSSL_IAR_ARM)
  129. #elif defined(WOLFSSL_ROWLEY_ARM)
  130. #elif defined(WOLFSSL_EMBOS)
  131. #elif defined(WOLFSSL_DEOS)
  132. #elif defined(MICRIUM)
  133. #elif defined(WOLFSSL_NUCLEUS)
  134. #elif defined(WOLFSSL_PB)
  135. #elif defined(WOLFSSL_ZEPHYR)
  136. #elif defined(WOLFSSL_TELIT_M2MB)
  137. #elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_TRNG)
  138. #elif defined(WOLFSSL_GETRANDOM)
  139. #include <errno.h>
  140. #include <sys/random.h>
  141. #else
  142. /* include headers that may be needed to get good seed */
  143. #include <fcntl.h>
  144. #ifndef EBSNET
  145. #include <unistd.h>
  146. #endif
  147. #endif
  148. #if defined(WOLFSSL_SILABS_SE_ACCEL)
  149. #include <wolfssl/wolfcrypt/port/silabs/silabs_random.h>
  150. #endif
  151. #if defined(WOLFSSL_IOTSAFE) && defined(HAVE_IOTSAFE_HWRNG)
  152. #include <wolfssl/wolfcrypt/port/iotsafe/iotsafe.h>
  153. #endif
  154. #if defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_RNG)
  155. #include <wolfssl/wolfcrypt/port/psa/psa.h>
  156. #endif
  157. #if defined(HAVE_INTEL_RDRAND) || defined(HAVE_INTEL_RDSEED) || \
  158. defined(HAVE_AMD_RDSEED)
  159. static word32 intel_flags = 0;
  160. static void wc_InitRng_IntelRD(void)
  161. {
  162. intel_flags = cpuid_get_flags();
  163. }
  164. #if (defined(HAVE_INTEL_RDSEED) || defined(HAVE_AMD_RDSEED)) && \
  165. !defined(WOLFSSL_LINUXKM)
  166. static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz);
  167. #endif
  168. #ifdef HAVE_INTEL_RDRAND
  169. static int wc_GenerateRand_IntelRD(OS_Seed* os, byte* output, word32 sz);
  170. #endif
  171. #ifdef USE_WINDOWS_API
  172. #define USE_INTEL_INTRINSICS
  173. #elif !defined __GNUC__ || defined __clang__ || __GNUC__ > 4
  174. #define USE_INTEL_INTRINSICS
  175. #else
  176. #undef USE_INTEL_INTRINSICS
  177. #endif
  178. #ifdef USE_INTEL_INTRINSICS
  179. #include <immintrin.h>
  180. /* Before clang 7 or GCC 9, immintrin.h did not define _rdseed64_step() */
  181. #ifndef HAVE_INTEL_RDSEED
  182. #elif defined __clang__ && __clang_major__ > 6
  183. #elif !defined __GNUC__
  184. #elif __GNUC__ > 8
  185. #else
  186. #ifndef __clang__
  187. #pragma GCC push_options
  188. #pragma GCC target("rdseed")
  189. #else
  190. #define __RDSEED__
  191. #endif
  192. #include <x86intrin.h>
  193. #ifndef __clang__
  194. #pragma GCC pop_options
  195. #endif
  196. #endif
  197. #endif /* USE_WINDOWS_API */
  198. #endif
  199. /* Start NIST DRBG code */
  200. #ifdef HAVE_HASHDRBG
  201. #define OUTPUT_BLOCK_LEN (WC_SHA256_DIGEST_SIZE)
  202. #define MAX_REQUEST_LEN (0x10000)
  203. #define RESEED_INTERVAL WC_RESEED_INTERVAL
  204. /* The security strength for the RNG is the target number of bits of
  205. * entropy you are looking for in a seed. */
  206. #ifndef RNG_SECURITY_STRENGTH
  207. /* SHA-256 requires a minimum of 256-bits of entropy. */
  208. #define RNG_SECURITY_STRENGTH (256)
  209. #endif
  210. #ifndef ENTROPY_SCALE_FACTOR
  211. /* The entropy scale factor should be the whole number inverse of the
  212. * minimum bits of entropy per bit of NDRNG output. */
  213. #if defined(HAVE_AMD_RDSEED)
  214. /* This will yield a SEED_SZ of 16kb. Since nonceSz will be 0,
  215. * we'll add an additional 8kb on top. */
  216. #define ENTROPY_SCALE_FACTOR (512)
  217. #elif defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)
  218. /* The value of 2 applies to Intel's RDSEED which provides about
  219. * 0.5 bits minimum of entropy per bit. The value of 4 gives a
  220. * conservative margin for FIPS. */
  221. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  222. (HAVE_FIPS_VERSION >= 2)
  223. #define ENTROPY_SCALE_FACTOR (2*4)
  224. #else
  225. /* Not FIPS, but Intel RDSEED, only double. */
  226. #define ENTROPY_SCALE_FACTOR (2)
  227. #endif
  228. #elif defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  229. (HAVE_FIPS_VERSION >= 2)
  230. /* If doing a FIPS build without a specific scale factor, default
  231. * to 4. This will give 1024 bits of entropy. More is better, but
  232. * more is also slower. */
  233. #define ENTROPY_SCALE_FACTOR (4)
  234. #else
  235. /* Setting the default to 1. */
  236. #define ENTROPY_SCALE_FACTOR (1)
  237. #endif
  238. #endif
  239. #ifndef SEED_BLOCK_SZ
  240. /* The seed block size, is the size of the output of the underlying NDRNG.
  241. * This value is used for testing the output of the NDRNG. */
  242. #if defined(HAVE_AMD_RDSEED)
  243. /* AMD's RDSEED instruction works in 128-bit blocks read 64-bits
  244. * at a time. */
  245. #define SEED_BLOCK_SZ (sizeof(word64)*2)
  246. #elif defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)
  247. /* RDSEED outputs in blocks of 64-bits. */
  248. #define SEED_BLOCK_SZ sizeof(word64)
  249. #else
  250. /* Setting the default to 4. */
  251. #define SEED_BLOCK_SZ 4
  252. #endif
  253. #endif
  254. #define SEED_SZ (RNG_SECURITY_STRENGTH*ENTROPY_SCALE_FACTOR/8)
  255. /* The maximum seed size will be the seed size plus a seed block for the
  256. * test, and an additional half of the seed size. This additional half
  257. * is in case the user does not supply a nonce. A nonce will be obtained
  258. * from the NDRNG. */
  259. #define MAX_SEED_SZ (SEED_SZ + SEED_SZ/2 + SEED_BLOCK_SZ)
  260. #ifdef WC_RNG_SEED_CB
  261. static wc_RngSeed_Cb seedCb = NULL;
  262. int wc_SetSeed_Cb(wc_RngSeed_Cb cb)
  263. {
  264. seedCb = cb;
  265. return 0;
  266. }
  267. #endif
  268. /* Internal return codes */
  269. #define DRBG_SUCCESS 0
  270. #define DRBG_FAILURE 1
  271. #define DRBG_NEED_RESEED 2
  272. #define DRBG_CONT_FAILURE 3
  273. #define DRBG_NO_SEED_CB 4
  274. /* RNG health states */
  275. #define DRBG_NOT_INIT 0
  276. #define DRBG_OK 1
  277. #define DRBG_FAILED 2
  278. #define DRBG_CONT_FAILED 3
  279. #define RNG_HEALTH_TEST_CHECK_SIZE (WC_SHA256_DIGEST_SIZE * 4)
  280. /* Verify max gen block len */
  281. #if RNG_MAX_BLOCK_LEN > MAX_REQUEST_LEN
  282. #error RNG_MAX_BLOCK_LEN is larger than NIST DBRG max request length
  283. #endif
  284. enum {
  285. drbgInitC = 0,
  286. drbgReseed = 1,
  287. drbgGenerateW = 2,
  288. drbgGenerateH = 3,
  289. drbgInitV = 4
  290. };
  291. typedef struct DRBG_internal DRBG_internal;
  292. static int wc_RNG_HealthTestLocal(int reseed);
  293. /* Hash Derivation Function */
  294. /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
  295. static int Hash_df(DRBG_internal* drbg, byte* out, word32 outSz, byte type,
  296. const byte* inA, word32 inASz,
  297. const byte* inB, word32 inBSz)
  298. {
  299. int ret = DRBG_FAILURE;
  300. byte ctr;
  301. int i;
  302. int len;
  303. word32 bits = (outSz * 8); /* reverse byte order */
  304. #ifdef WOLFSSL_SMALL_STACK_CACHE
  305. wc_Sha256* sha = &drbg->sha256;
  306. #else
  307. wc_Sha256 sha[1];
  308. #endif
  309. #ifdef WC_ASYNC_ENABLE_SHA256
  310. WC_DECLARE_VAR(digest, byte, WC_SHA256_DIGEST_SIZE, drbg->heap);
  311. if (digest == NULL)
  312. return MEMORY_E;
  313. #else
  314. byte digest[WC_SHA256_DIGEST_SIZE];
  315. #endif
  316. (void)drbg;
  317. #ifdef WC_ASYNC_ENABLE_SHA256
  318. if (digest == NULL)
  319. return DRBG_FAILURE;
  320. #endif
  321. #ifdef LITTLE_ENDIAN_ORDER
  322. bits = ByteReverseWord32(bits);
  323. #endif
  324. len = (outSz / OUTPUT_BLOCK_LEN)
  325. + ((outSz % OUTPUT_BLOCK_LEN) ? 1 : 0);
  326. ctr = 1;
  327. for (i = 0; i < len; i++) {
  328. #ifndef WOLFSSL_SMALL_STACK_CACHE
  329. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  330. ret = wc_InitSha256_ex(sha, drbg->heap, drbg->devId);
  331. #else
  332. ret = wc_InitSha256(sha);
  333. #endif
  334. if (ret != 0)
  335. break;
  336. #endif
  337. ret = wc_Sha256Update(sha, &ctr, sizeof(ctr));
  338. if (ret == 0) {
  339. ctr++;
  340. ret = wc_Sha256Update(sha, (byte*)&bits, sizeof(bits));
  341. }
  342. if (ret == 0) {
  343. /* churning V is the only string that doesn't have the type added */
  344. if (type != drbgInitV)
  345. ret = wc_Sha256Update(sha, &type, sizeof(type));
  346. }
  347. if (ret == 0)
  348. ret = wc_Sha256Update(sha, inA, inASz);
  349. if (ret == 0) {
  350. if (inB != NULL && inBSz > 0)
  351. ret = wc_Sha256Update(sha, inB, inBSz);
  352. }
  353. if (ret == 0)
  354. ret = wc_Sha256Final(sha, digest);
  355. #ifndef WOLFSSL_SMALL_STACK_CACHE
  356. wc_Sha256Free(sha);
  357. #endif
  358. if (ret == 0) {
  359. if (outSz > OUTPUT_BLOCK_LEN) {
  360. XMEMCPY(out, digest, OUTPUT_BLOCK_LEN);
  361. outSz -= OUTPUT_BLOCK_LEN;
  362. out += OUTPUT_BLOCK_LEN;
  363. }
  364. else {
  365. XMEMCPY(out, digest, outSz);
  366. }
  367. }
  368. }
  369. ForceZero(digest, WC_SHA256_DIGEST_SIZE);
  370. #ifdef WC_ASYNC_ENABLE_SHA256
  371. WC_FREE_VAR(digest, drbg->heap);
  372. #endif
  373. return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
  374. }
  375. /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
  376. static int Hash_DRBG_Reseed(DRBG_internal* drbg, const byte* seed, word32 seedSz)
  377. {
  378. byte newV[DRBG_SEED_LEN];
  379. if (drbg == NULL) {
  380. return DRBG_FAILURE;
  381. }
  382. XMEMSET(newV, 0, DRBG_SEED_LEN);
  383. if (Hash_df(drbg, newV, sizeof(newV), drbgReseed,
  384. drbg->V, sizeof(drbg->V), seed, seedSz) != DRBG_SUCCESS) {
  385. return DRBG_FAILURE;
  386. }
  387. XMEMCPY(drbg->V, newV, sizeof(drbg->V));
  388. ForceZero(newV, sizeof(newV));
  389. if (Hash_df(drbg, drbg->C, sizeof(drbg->C), drbgInitC, drbg->V,
  390. sizeof(drbg->V), NULL, 0) != DRBG_SUCCESS) {
  391. return DRBG_FAILURE;
  392. }
  393. drbg->reseedCtr = 1;
  394. drbg->lastBlock = 0;
  395. drbg->matchCount = 0;
  396. return DRBG_SUCCESS;
  397. }
  398. /* Returns: DRBG_SUCCESS and DRBG_FAILURE or BAD_FUNC_ARG on fail */
  399. int wc_RNG_DRBG_Reseed(WC_RNG* rng, const byte* seed, word32 seedSz)
  400. {
  401. if (rng == NULL || seed == NULL) {
  402. return BAD_FUNC_ARG;
  403. }
  404. if (rng->drbg == NULL) {
  405. #if defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)
  406. if (IS_INTEL_RDRAND(intel_flags)) {
  407. /* using RDRAND not DRBG, so return success */
  408. return 0;
  409. }
  410. return BAD_FUNC_ARG;
  411. #endif
  412. }
  413. return Hash_DRBG_Reseed((DRBG_internal *)rng->drbg, seed, seedSz);
  414. }
  415. static WC_INLINE void array_add_one(byte* data, word32 dataSz)
  416. {
  417. int i;
  418. for (i = dataSz - 1; i >= 0; i--)
  419. {
  420. data[i]++;
  421. if (data[i] != 0) break;
  422. }
  423. }
  424. /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
  425. static int Hash_gen(DRBG_internal* drbg, byte* out, word32 outSz, const byte* V)
  426. {
  427. int ret = DRBG_FAILURE;
  428. byte data[DRBG_SEED_LEN];
  429. int i;
  430. int len;
  431. word32 checkBlock;
  432. #ifdef WOLFSSL_SMALL_STACK_CACHE
  433. wc_Sha256* sha = &drbg->sha256;
  434. #else
  435. wc_Sha256 sha[1];
  436. #endif
  437. #ifdef WC_ASYNC_ENABLE_SHA256
  438. WC_DECLARE_VAR(digest, byte, WC_SHA256_DIGEST_SIZE, drbg->heap);
  439. if (digest == NULL)
  440. return MEMORY_E;
  441. #else
  442. byte digest[WC_SHA256_DIGEST_SIZE];
  443. #endif
  444. /* Special case: outSz is 0 and out is NULL. wc_Generate a block to save for
  445. * the continuous test. */
  446. if (outSz == 0) outSz = 1;
  447. len = (outSz / OUTPUT_BLOCK_LEN) + ((outSz % OUTPUT_BLOCK_LEN) ? 1 : 0);
  448. XMEMCPY(data, V, sizeof(data));
  449. for (i = 0; i < len; i++) {
  450. #ifndef WOLFSSL_SMALL_STACK_CACHE
  451. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  452. ret = wc_InitSha256_ex(sha, drbg->heap, drbg->devId);
  453. #else
  454. ret = wc_InitSha256(sha);
  455. #endif
  456. if (ret == 0)
  457. #endif
  458. ret = wc_Sha256Update(sha, data, sizeof(data));
  459. if (ret == 0)
  460. ret = wc_Sha256Final(sha, digest);
  461. #ifndef WOLFSSL_SMALL_STACK_CACHE
  462. wc_Sha256Free(sha);
  463. #endif
  464. if (ret == 0) {
  465. XMEMCPY(&checkBlock, digest, sizeof(word32));
  466. if (drbg->reseedCtr > 1 && checkBlock == drbg->lastBlock) {
  467. if (drbg->matchCount == 1) {
  468. return DRBG_CONT_FAILURE;
  469. }
  470. else {
  471. if (i == (len-1)) {
  472. len++;
  473. }
  474. drbg->matchCount = 1;
  475. }
  476. }
  477. else {
  478. drbg->matchCount = 0;
  479. drbg->lastBlock = checkBlock;
  480. }
  481. if (out != NULL && outSz != 0) {
  482. if (outSz >= OUTPUT_BLOCK_LEN) {
  483. XMEMCPY(out, digest, OUTPUT_BLOCK_LEN);
  484. outSz -= OUTPUT_BLOCK_LEN;
  485. out += OUTPUT_BLOCK_LEN;
  486. array_add_one(data, DRBG_SEED_LEN);
  487. }
  488. else {
  489. XMEMCPY(out, digest, outSz);
  490. outSz = 0;
  491. }
  492. }
  493. }
  494. else {
  495. /* wc_Sha256Update or wc_Sha256Final returned error */
  496. break;
  497. }
  498. }
  499. ForceZero(data, sizeof(data));
  500. #ifdef WC_ASYNC_ENABLE_SHA256
  501. WC_FREE_VAR(digest, drbg->heap);
  502. #endif
  503. return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
  504. }
  505. static WC_INLINE void array_add(byte* d, word32 dLen, const byte* s, word32 sLen)
  506. {
  507. word16 carry = 0;
  508. if (dLen > 0 && sLen > 0 && dLen >= sLen) {
  509. int sIdx, dIdx;
  510. dIdx = dLen - 1;
  511. for (sIdx = sLen - 1; sIdx >= 0; sIdx--) {
  512. carry += (word16)d[dIdx] + (word16)s[sIdx];
  513. d[dIdx] = (byte)carry;
  514. carry >>= 8;
  515. dIdx--;
  516. }
  517. for (; dIdx >= 0; dIdx--) {
  518. carry += (word16)d[dIdx];
  519. d[dIdx] = (byte)carry;
  520. carry >>= 8;
  521. }
  522. }
  523. }
  524. /* Returns: DRBG_SUCCESS, DRBG_NEED_RESEED, or DRBG_FAILURE */
  525. static int Hash_DRBG_Generate(DRBG_internal* drbg, byte* out, word32 outSz)
  526. {
  527. int ret;
  528. #ifdef WOLFSSL_SMALL_STACK_CACHE
  529. wc_Sha256* sha = &drbg->sha256;
  530. #else
  531. wc_Sha256 sha[1];
  532. #endif
  533. byte type;
  534. word32 reseedCtr;
  535. if (drbg == NULL) {
  536. return DRBG_FAILURE;
  537. }
  538. if (drbg->reseedCtr == RESEED_INTERVAL) {
  539. return DRBG_NEED_RESEED;
  540. } else {
  541. #ifdef WC_ASYNC_ENABLE_SHA256
  542. WC_DECLARE_VAR(digest, byte, WC_SHA256_DIGEST_SIZE, drbg->heap);
  543. if (digest == NULL)
  544. return MEMORY_E;
  545. #else
  546. byte digest[WC_SHA256_DIGEST_SIZE];
  547. #endif
  548. type = drbgGenerateH;
  549. reseedCtr = drbg->reseedCtr;
  550. ret = Hash_gen(drbg, out, outSz, drbg->V);
  551. if (ret == DRBG_SUCCESS) {
  552. #ifndef WOLFSSL_SMALL_STACK_CACHE
  553. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  554. ret = wc_InitSha256_ex(sha, drbg->heap, drbg->devId);
  555. #else
  556. ret = wc_InitSha256(sha);
  557. #endif
  558. if (ret == 0)
  559. #endif
  560. ret = wc_Sha256Update(sha, &type, sizeof(type));
  561. if (ret == 0)
  562. ret = wc_Sha256Update(sha, drbg->V, sizeof(drbg->V));
  563. if (ret == 0)
  564. ret = wc_Sha256Final(sha, digest);
  565. #ifndef WOLFSSL_SMALL_STACK_CACHE
  566. wc_Sha256Free(sha);
  567. #endif
  568. if (ret == 0) {
  569. array_add(drbg->V, sizeof(drbg->V), digest, WC_SHA256_DIGEST_SIZE);
  570. array_add(drbg->V, sizeof(drbg->V), drbg->C, sizeof(drbg->C));
  571. #ifdef LITTLE_ENDIAN_ORDER
  572. reseedCtr = ByteReverseWord32(reseedCtr);
  573. #endif
  574. array_add(drbg->V, sizeof(drbg->V),
  575. (byte*)&reseedCtr, sizeof(reseedCtr));
  576. ret = DRBG_SUCCESS;
  577. }
  578. drbg->reseedCtr++;
  579. }
  580. ForceZero(digest, WC_SHA256_DIGEST_SIZE);
  581. #ifdef WC_ASYNC_ENABLE_SHA256
  582. WC_FREE_VAR(digest, drbg->heap);
  583. #endif
  584. }
  585. return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
  586. }
  587. /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
  588. static int Hash_DRBG_Instantiate(DRBG_internal* drbg, const byte* seed, word32 seedSz,
  589. const byte* nonce, word32 nonceSz,
  590. void* heap, int devId)
  591. {
  592. int ret = DRBG_FAILURE;
  593. XMEMSET(drbg, 0, sizeof(DRBG_internal));
  594. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  595. drbg->heap = heap;
  596. drbg->devId = devId;
  597. #else
  598. (void)heap;
  599. (void)devId;
  600. #endif
  601. #ifdef WOLFSSL_SMALL_STACK_CACHE
  602. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  603. ret = wc_InitSha256_ex(&drbg->sha256, drbg->heap, drbg->devId);
  604. #else
  605. ret = wc_InitSha256(&drbg->sha256);
  606. #endif
  607. if (ret != 0)
  608. return ret;
  609. #endif
  610. if (Hash_df(drbg, drbg->V, sizeof(drbg->V), drbgInitV, seed, seedSz,
  611. nonce, nonceSz) == DRBG_SUCCESS &&
  612. Hash_df(drbg, drbg->C, sizeof(drbg->C), drbgInitC, drbg->V,
  613. sizeof(drbg->V), NULL, 0) == DRBG_SUCCESS) {
  614. drbg->reseedCtr = 1;
  615. drbg->lastBlock = 0;
  616. drbg->matchCount = 0;
  617. ret = DRBG_SUCCESS;
  618. }
  619. return ret;
  620. }
  621. /* Returns: DRBG_SUCCESS or DRBG_FAILURE */
  622. static int Hash_DRBG_Uninstantiate(DRBG_internal* drbg)
  623. {
  624. word32 i;
  625. int compareSum = 0;
  626. byte* compareDrbg = (byte*)drbg;
  627. #ifdef WOLFSSL_SMALL_STACK_CACHE
  628. wc_Sha256Free(&drbg->sha256);
  629. #endif
  630. ForceZero(drbg, sizeof(DRBG_internal));
  631. for (i = 0; i < sizeof(DRBG_internal); i++)
  632. compareSum |= compareDrbg[i] ^ 0;
  633. return (compareSum == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
  634. }
  635. int wc_RNG_TestSeed(const byte* seed, word32 seedSz)
  636. {
  637. int ret = 0;
  638. /* Check the seed for duplicate words. */
  639. word32 seedIdx = 0;
  640. word32 scratchSz = min(SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ);
  641. while (seedIdx < seedSz - SEED_BLOCK_SZ) {
  642. if (ConstantCompare(seed + seedIdx,
  643. seed + seedIdx + scratchSz,
  644. scratchSz) == 0) {
  645. ret = DRBG_CONT_FAILURE;
  646. }
  647. seedIdx += SEED_BLOCK_SZ;
  648. scratchSz = min(SEED_BLOCK_SZ, (seedSz - seedIdx));
  649. }
  650. return ret;
  651. }
  652. #endif /* HAVE_HASHDRBG */
  653. /* End NIST DRBG Code */
  654. static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
  655. void* heap, int devId)
  656. {
  657. int ret = 0;
  658. #ifdef HAVE_HASHDRBG
  659. word32 seedSz = SEED_SZ + SEED_BLOCK_SZ;
  660. #endif
  661. (void)nonce;
  662. (void)nonceSz;
  663. if (rng == NULL)
  664. return BAD_FUNC_ARG;
  665. if (nonce == NULL && nonceSz != 0)
  666. return BAD_FUNC_ARG;
  667. #ifdef WOLFSSL_HEAP_TEST
  668. rng->heap = (void*)WOLFSSL_HEAP_TEST;
  669. (void)heap;
  670. #else
  671. rng->heap = heap;
  672. #endif
  673. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  674. rng->devId = devId;
  675. #if defined(WOLF_CRYPTO_CB)
  676. rng->seed.devId = devId;
  677. #endif
  678. #else
  679. (void)devId;
  680. #endif
  681. #ifdef HAVE_HASHDRBG
  682. /* init the DBRG to known values */
  683. rng->drbg = NULL;
  684. rng->status = DRBG_NOT_INIT;
  685. #endif
  686. #if defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND) || \
  687. defined(HAVE_AMD_RDSEED)
  688. /* init the intel RD seed and/or rand */
  689. wc_InitRng_IntelRD();
  690. #endif
  691. /* configure async RNG source if available */
  692. #ifdef WOLFSSL_ASYNC_CRYPT
  693. ret = wolfAsync_DevCtxInit(&rng->asyncDev, WOLFSSL_ASYNC_MARKER_RNG,
  694. rng->heap, rng->devId);
  695. if (ret != 0)
  696. return ret;
  697. #endif
  698. #ifdef HAVE_INTEL_RDRAND
  699. /* if CPU supports RDRAND, use it directly and by-pass DRBG init */
  700. if (IS_INTEL_RDRAND(intel_flags))
  701. return 0;
  702. #endif
  703. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  704. ret = wc_VersalTrngInit(nonce, nonceSz);
  705. if (ret)
  706. return ret;
  707. #endif
  708. #ifdef CUSTOM_RAND_GENERATE_BLOCK
  709. ret = 0; /* success */
  710. #else
  711. #ifdef HAVE_HASHDRBG
  712. if (nonceSz == 0)
  713. seedSz = MAX_SEED_SZ;
  714. if (wc_RNG_HealthTestLocal(0) == 0) {
  715. #ifdef WC_ASYNC_ENABLE_SHA256
  716. WC_DECLARE_VAR(seed, byte, MAX_SEED_SZ, rng->heap);
  717. if (seed == NULL)
  718. return MEMORY_E;
  719. #else
  720. byte seed[MAX_SEED_SZ];
  721. #endif
  722. #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
  723. rng->drbg =
  724. (struct DRBG*)XMALLOC(sizeof(DRBG_internal), rng->heap,
  725. DYNAMIC_TYPE_RNG);
  726. if (rng->drbg == NULL) {
  727. ret = MEMORY_E;
  728. rng->status = DRBG_FAILED;
  729. }
  730. #else
  731. rng->drbg = (struct DRBG*)&rng->drbg_data;
  732. #endif
  733. if (ret == 0) {
  734. #ifdef WC_RNG_SEED_CB
  735. if (seedCb == NULL) {
  736. ret = DRBG_NO_SEED_CB;
  737. }
  738. else {
  739. ret = seedCb(&rng->seed, seed, seedSz);
  740. if (ret != 0) {
  741. ret = DRBG_FAILURE;
  742. }
  743. }
  744. #else
  745. ret = wc_GenerateSeed(&rng->seed, seed, seedSz);
  746. #endif
  747. if (ret == 0)
  748. ret = wc_RNG_TestSeed(seed, seedSz);
  749. else {
  750. ret = DRBG_FAILURE;
  751. rng->status = DRBG_FAILED;
  752. }
  753. if (ret == DRBG_SUCCESS)
  754. ret = Hash_DRBG_Instantiate((DRBG_internal *)rng->drbg,
  755. seed + SEED_BLOCK_SZ, seedSz - SEED_BLOCK_SZ,
  756. nonce, nonceSz, rng->heap, devId);
  757. if (ret != DRBG_SUCCESS) {
  758. #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
  759. XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
  760. #endif
  761. rng->drbg = NULL;
  762. }
  763. }
  764. ForceZero(seed, seedSz);
  765. #ifdef WC_ASYNC_ENABLE_SHA256
  766. WC_FREE_VAR(seed, rng->heap);
  767. #endif
  768. }
  769. else
  770. ret = DRBG_CONT_FAILURE;
  771. if (ret == DRBG_SUCCESS) {
  772. #ifdef WOLFSSL_CHECK_MEM_ZERO
  773. #ifdef HAVE_HASHDRBG
  774. struct DRBG_internal* drbg = (struct DRBG_internal*)rng->drbg;
  775. wc_MemZero_Add("DRBG V", &drbg->V, sizeof(drbg->V));
  776. wc_MemZero_Add("DRBG C", &drbg->C, sizeof(drbg->C));
  777. #endif
  778. #endif
  779. rng->status = DRBG_OK;
  780. ret = 0;
  781. }
  782. else if (ret == DRBG_CONT_FAILURE) {
  783. rng->status = DRBG_CONT_FAILED;
  784. ret = DRBG_CONT_FIPS_E;
  785. }
  786. else if (ret == DRBG_FAILURE) {
  787. rng->status = DRBG_FAILED;
  788. ret = RNG_FAILURE_E;
  789. }
  790. else {
  791. rng->status = DRBG_FAILED;
  792. }
  793. #endif /* HAVE_HASHDRBG */
  794. #endif /* CUSTOM_RAND_GENERATE_BLOCK */
  795. return ret;
  796. }
  797. WOLFSSL_ABI
  798. WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap)
  799. {
  800. WC_RNG* rng;
  801. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG);
  802. if (rng) {
  803. int error = _InitRng(rng, nonce, nonceSz, heap, INVALID_DEVID) != 0;
  804. if (error) {
  805. XFREE(rng, heap, DYNAMIC_TYPE_RNG);
  806. rng = NULL;
  807. }
  808. }
  809. return rng;
  810. }
  811. WOLFSSL_ABI
  812. void wc_rng_free(WC_RNG* rng)
  813. {
  814. if (rng) {
  815. void* heap = rng->heap;
  816. wc_FreeRng(rng);
  817. ForceZero(rng, sizeof(WC_RNG));
  818. XFREE(rng, heap, DYNAMIC_TYPE_RNG);
  819. (void)heap;
  820. }
  821. }
  822. WOLFSSL_ABI
  823. int wc_InitRng(WC_RNG* rng)
  824. {
  825. return _InitRng(rng, NULL, 0, NULL, INVALID_DEVID);
  826. }
  827. int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId)
  828. {
  829. return _InitRng(rng, NULL, 0, heap, devId);
  830. }
  831. int wc_InitRngNonce(WC_RNG* rng, byte* nonce, word32 nonceSz)
  832. {
  833. return _InitRng(rng, nonce, nonceSz, NULL, INVALID_DEVID);
  834. }
  835. int wc_InitRngNonce_ex(WC_RNG* rng, byte* nonce, word32 nonceSz,
  836. void* heap, int devId)
  837. {
  838. return _InitRng(rng, nonce, nonceSz, heap, devId);
  839. }
  840. /* place a generated block in output */
  841. WOLFSSL_ABI
  842. int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
  843. {
  844. int ret;
  845. if (rng == NULL || output == NULL)
  846. return BAD_FUNC_ARG;
  847. if (sz == 0)
  848. return 0;
  849. #ifdef WOLF_CRYPTO_CB
  850. if (rng->devId != INVALID_DEVID) {
  851. ret = wc_CryptoCb_RandomBlock(rng, output, sz);
  852. if (ret != CRYPTOCB_UNAVAILABLE)
  853. return ret;
  854. /* fall-through when unavailable */
  855. }
  856. #endif
  857. #ifdef HAVE_INTEL_RDRAND
  858. if (IS_INTEL_RDRAND(intel_flags))
  859. return wc_GenerateRand_IntelRD(NULL, output, sz);
  860. #endif
  861. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_TRNG)
  862. return silabs_GenerateRand(output, sz);
  863. #endif
  864. #if defined(WOLFSSL_ASYNC_CRYPT)
  865. if (rng->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RNG) {
  866. /* these are blocking */
  867. #ifdef HAVE_CAVIUM
  868. return NitroxRngGenerateBlock(rng, output, sz);
  869. #elif defined(HAVE_INTEL_QA) && defined(QAT_ENABLE_RNG)
  870. return IntelQaDrbg(&rng->asyncDev, output, sz);
  871. #else
  872. /* simulator not supported */
  873. #endif
  874. }
  875. #endif
  876. #ifdef CUSTOM_RAND_GENERATE_BLOCK
  877. XMEMSET(output, 0, sz);
  878. ret = CUSTOM_RAND_GENERATE_BLOCK(output, sz);
  879. #else
  880. #ifdef HAVE_HASHDRBG
  881. if (sz > RNG_MAX_BLOCK_LEN)
  882. return BAD_FUNC_ARG;
  883. if (rng->status != DRBG_OK)
  884. return RNG_FAILURE_E;
  885. ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz);
  886. if (ret == DRBG_NEED_RESEED) {
  887. if (wc_RNG_HealthTestLocal(1) == 0) {
  888. byte newSeed[SEED_SZ + SEED_BLOCK_SZ];
  889. ret = wc_GenerateSeed(&rng->seed, newSeed,
  890. SEED_SZ + SEED_BLOCK_SZ);
  891. if (ret != 0)
  892. ret = DRBG_FAILURE;
  893. else
  894. ret = wc_RNG_TestSeed(newSeed, SEED_SZ + SEED_BLOCK_SZ);
  895. if (ret == DRBG_SUCCESS)
  896. ret = Hash_DRBG_Reseed((DRBG_internal *)rng->drbg,
  897. newSeed + SEED_BLOCK_SZ, SEED_SZ);
  898. if (ret == DRBG_SUCCESS)
  899. ret = Hash_DRBG_Generate((DRBG_internal *)rng->drbg, output, sz);
  900. ForceZero(newSeed, sizeof(newSeed));
  901. }
  902. else
  903. ret = DRBG_CONT_FAILURE;
  904. }
  905. if (ret == DRBG_SUCCESS) {
  906. ret = 0;
  907. }
  908. else if (ret == DRBG_CONT_FAILURE) {
  909. ret = DRBG_CONT_FIPS_E;
  910. rng->status = DRBG_CONT_FAILED;
  911. }
  912. else {
  913. ret = RNG_FAILURE_E;
  914. rng->status = DRBG_FAILED;
  915. }
  916. #else
  917. /* if we get here then there is an RNG configuration error */
  918. ret = RNG_FAILURE_E;
  919. #endif /* HAVE_HASHDRBG */
  920. #endif /* CUSTOM_RAND_GENERATE_BLOCK */
  921. return ret;
  922. }
  923. int wc_RNG_GenerateByte(WC_RNG* rng, byte* b)
  924. {
  925. return wc_RNG_GenerateBlock(rng, b, 1);
  926. }
  927. int wc_FreeRng(WC_RNG* rng)
  928. {
  929. int ret = 0;
  930. if (rng == NULL)
  931. return BAD_FUNC_ARG;
  932. #if defined(WOLFSSL_ASYNC_CRYPT)
  933. wolfAsync_DevCtxFree(&rng->asyncDev, WOLFSSL_ASYNC_MARKER_RNG);
  934. #endif
  935. #ifdef HAVE_HASHDRBG
  936. if (rng->drbg != NULL) {
  937. if (Hash_DRBG_Uninstantiate((DRBG_internal *)rng->drbg) != DRBG_SUCCESS)
  938. ret = RNG_FAILURE_E;
  939. #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
  940. XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
  941. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  942. wc_MemZero_Check(rng->drbg, sizeof(DRBG_internal));
  943. #endif
  944. rng->drbg = NULL;
  945. }
  946. rng->status = DRBG_NOT_INIT;
  947. #endif /* HAVE_HASHDRBG */
  948. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL\
  949. /* don't overwrite previously set error */
  950. if (wc_VersalTrngReset() && !ret)
  951. ret = WC_HW_E;
  952. #endif
  953. return ret;
  954. }
  955. #ifdef HAVE_HASHDRBG
  956. int wc_RNG_HealthTest(int reseed, const byte* seedA, word32 seedASz,
  957. const byte* seedB, word32 seedBSz,
  958. byte* output, word32 outputSz)
  959. {
  960. return wc_RNG_HealthTest_ex(reseed, NULL, 0,
  961. seedA, seedASz, seedB, seedBSz,
  962. output, outputSz,
  963. NULL, INVALID_DEVID);
  964. }
  965. int wc_RNG_HealthTest_ex(int reseed, const byte* nonce, word32 nonceSz,
  966. const byte* seedA, word32 seedASz,
  967. const byte* seedB, word32 seedBSz,
  968. byte* output, word32 outputSz,
  969. void* heap, int devId)
  970. {
  971. int ret = -1;
  972. DRBG_internal* drbg;
  973. #ifndef WOLFSSL_SMALL_STACK
  974. DRBG_internal drbg_var;
  975. #endif
  976. if (seedA == NULL || output == NULL) {
  977. return BAD_FUNC_ARG;
  978. }
  979. if (reseed != 0 && seedB == NULL) {
  980. return BAD_FUNC_ARG;
  981. }
  982. if (outputSz != RNG_HEALTH_TEST_CHECK_SIZE) {
  983. return ret;
  984. }
  985. #ifdef WOLFSSL_SMALL_STACK
  986. drbg = (DRBG_internal*)XMALLOC(sizeof(DRBG_internal), NULL, DYNAMIC_TYPE_RNG);
  987. if (drbg == NULL) {
  988. return MEMORY_E;
  989. }
  990. #else
  991. drbg = &drbg_var;
  992. #endif
  993. if (Hash_DRBG_Instantiate(drbg, seedA, seedASz, nonce, nonceSz,
  994. heap, devId) != 0) {
  995. goto exit_rng_ht;
  996. }
  997. if (reseed) {
  998. if (Hash_DRBG_Reseed(drbg, seedB, seedBSz) != 0) {
  999. goto exit_rng_ht;
  1000. }
  1001. }
  1002. /* This call to generate is prescribed by the NIST DRBGVS
  1003. * procedure. The results are thrown away. The known
  1004. * answer test checks the second block of DRBG out of
  1005. * the generator to ensure the internal state is updated
  1006. * as expected. */
  1007. if (Hash_DRBG_Generate(drbg, output, outputSz) != 0) {
  1008. goto exit_rng_ht;
  1009. }
  1010. if (Hash_DRBG_Generate(drbg, output, outputSz) != 0) {
  1011. goto exit_rng_ht;
  1012. }
  1013. /* Mark success */
  1014. ret = 0;
  1015. exit_rng_ht:
  1016. /* This is safe to call even if Hash_DRBG_Instantiate fails */
  1017. if (Hash_DRBG_Uninstantiate(drbg) != 0) {
  1018. ret = -1;
  1019. }
  1020. #ifdef WOLFSSL_SMALL_STACK
  1021. XFREE(drbg, NULL, DYNAMIC_TYPE_RNG);
  1022. #endif
  1023. return ret;
  1024. }
  1025. const FLASH_QUALIFIER byte seedA_data[] = {
  1026. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  1027. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  1028. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  1029. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  1030. };
  1031. const FLASH_QUALIFIER byte reseedSeedA_data[] = {
  1032. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  1033. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  1034. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  1035. };
  1036. const FLASH_QUALIFIER byte outputA_data[] = {
  1037. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  1038. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  1039. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  1040. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  1041. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  1042. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  1043. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  1044. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  1045. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  1046. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  1047. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  1048. };
  1049. const FLASH_QUALIFIER byte seedB_data[] = {
  1050. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  1051. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  1052. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, /* nonce next */
  1053. 0x85, 0x81, 0xf9, 0x31, 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d,
  1054. 0xdb, 0xcb, 0xcc, 0x2e
  1055. };
  1056. const FLASH_QUALIFIER byte outputB_data[] = {
  1057. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  1058. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  1059. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  1060. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  1061. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  1062. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  1063. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  1064. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  1065. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  1066. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  1067. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  1068. };
  1069. static int wc_RNG_HealthTestLocal(int reseed)
  1070. {
  1071. int ret = 0;
  1072. #ifdef WOLFSSL_SMALL_STACK
  1073. byte* check;
  1074. #else
  1075. byte check[RNG_HEALTH_TEST_CHECK_SIZE];
  1076. #endif
  1077. #ifdef WOLFSSL_SMALL_STACK
  1078. check = (byte*)XMALLOC(RNG_HEALTH_TEST_CHECK_SIZE, NULL,
  1079. DYNAMIC_TYPE_TMP_BUFFER);
  1080. if (check == NULL) {
  1081. return MEMORY_E;
  1082. }
  1083. #endif
  1084. if (reseed) {
  1085. #ifdef WOLFSSL_USE_FLASHMEM
  1086. byte* seedA = (byte*)XMALLOC(sizeof(seedA_data), NULL,
  1087. DYNAMIC_TYPE_TMP_BUFFER);
  1088. byte* reseedSeedA = (byte*)XMALLOC(sizeof(reseedSeedA_data), NULL,
  1089. DYNAMIC_TYPE_TMP_BUFFER);
  1090. byte* outputA = (byte*)XMALLOC(sizeof(outputA_data), NULL,
  1091. DYNAMIC_TYPE_TMP_BUFFER);
  1092. if (!seedA || !reseedSeedA || !outputA) {
  1093. XFREE(seedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1094. XFREE(reseedSeedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1095. XFREE(outputA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1096. ret = MEMORY_E;
  1097. }
  1098. else {
  1099. XMEMCPY_P(seedA, seedA_data, sizeof(seedA_data));
  1100. XMEMCPY_P(reseedSeedA, reseedSeedA_data, sizeof(reseedSeedA_data));
  1101. XMEMCPY_P(outputA, outputA_data, sizeof(outputA_data));
  1102. #else
  1103. const byte* seedA = seedA_data;
  1104. const byte* reseedSeedA = reseedSeedA_data;
  1105. const byte* outputA = outputA_data;
  1106. #endif
  1107. ret = wc_RNG_HealthTest(1, seedA, sizeof(seedA_data),
  1108. reseedSeedA, sizeof(reseedSeedA_data),
  1109. check, RNG_HEALTH_TEST_CHECK_SIZE);
  1110. if (ret == 0) {
  1111. if (ConstantCompare(check, outputA,
  1112. RNG_HEALTH_TEST_CHECK_SIZE) != 0)
  1113. ret = -1;
  1114. }
  1115. #ifdef WOLFSSL_USE_FLASHMEM
  1116. XFREE(seedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1117. XFREE(reseedSeedA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1118. XFREE(outputA, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1119. }
  1120. #endif
  1121. }
  1122. else {
  1123. #ifdef WOLFSSL_USE_FLASHMEM
  1124. byte* seedB = (byte*)XMALLOC(sizeof(seedB_data), NULL,
  1125. DYNAMIC_TYPE_TMP_BUFFER);
  1126. byte* outputB = (byte*)XMALLOC(sizeof(outputB_data), NULL,
  1127. DYNAMIC_TYPE_TMP_BUFFER);
  1128. if (!seedB || !outputB) {
  1129. XFREE(seedB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1130. XFREE(outputB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1131. ret = MEMORY_E;
  1132. }
  1133. else {
  1134. XMEMCPY_P(seedB, seedB_data, sizeof(seedB_data));
  1135. XMEMCPY_P(outputB, outputB_data, sizeof(outputB_data));
  1136. #else
  1137. const byte* seedB = seedB_data;
  1138. const byte* outputB = outputB_data;
  1139. #endif
  1140. ret = wc_RNG_HealthTest(0, seedB, sizeof(seedB_data),
  1141. NULL, 0,
  1142. check, RNG_HEALTH_TEST_CHECK_SIZE);
  1143. if (ret == 0) {
  1144. if (ConstantCompare(check, outputB,
  1145. RNG_HEALTH_TEST_CHECK_SIZE) != 0)
  1146. ret = -1;
  1147. }
  1148. /* The previous test cases use a large seed instead of a seed and nonce.
  1149. * seedB is actually from a test case with a seed and nonce, and
  1150. * just concatenates them. The pivot point between seed and nonce is
  1151. * byte 32, feed them into the health test separately. */
  1152. if (ret == 0) {
  1153. ret = wc_RNG_HealthTest_ex(0,
  1154. seedB + 32, sizeof(seedB_data) - 32,
  1155. seedB, 32,
  1156. NULL, 0,
  1157. check, RNG_HEALTH_TEST_CHECK_SIZE,
  1158. NULL, INVALID_DEVID);
  1159. if (ret == 0) {
  1160. if (ConstantCompare(check, outputB, sizeof(outputB_data)) != 0)
  1161. ret = -1;
  1162. }
  1163. }
  1164. #ifdef WOLFSSL_USE_FLASHMEM
  1165. XFREE(seedB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1166. XFREE(outputB, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1167. }
  1168. #endif
  1169. }
  1170. #ifdef WOLFSSL_SMALL_STACK
  1171. XFREE(check, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1172. #endif
  1173. return ret;
  1174. }
  1175. #endif /* HAVE_HASHDRBG */
  1176. #ifdef HAVE_WNR
  1177. /*
  1178. * Init global Whitewood netRandom context
  1179. * Returns 0 on success, negative on error
  1180. */
  1181. int wc_InitNetRandom(const char* configFile, wnr_hmac_key hmac_cb, int timeout)
  1182. {
  1183. if (configFile == NULL || timeout < 0)
  1184. return BAD_FUNC_ARG;
  1185. if (wnr_mutex_init > 0) {
  1186. WOLFSSL_MSG("netRandom context already created, skipping");
  1187. return 0;
  1188. }
  1189. if (wc_InitMutex(&wnr_mutex) != 0) {
  1190. WOLFSSL_MSG("Bad Init Mutex wnr_mutex");
  1191. return BAD_MUTEX_E;
  1192. }
  1193. wnr_mutex_init = 1;
  1194. if (wc_LockMutex(&wnr_mutex) != 0) {
  1195. WOLFSSL_MSG("Bad Lock Mutex wnr_mutex");
  1196. return BAD_MUTEX_E;
  1197. }
  1198. /* store entropy timeout */
  1199. wnr_timeout = timeout;
  1200. /* create global wnr_context struct */
  1201. if (wnr_create(&wnr_ctx) != WNR_ERROR_NONE) {
  1202. WOLFSSL_MSG("Error creating global netRandom context");
  1203. return RNG_FAILURE_E;
  1204. }
  1205. /* load config file */
  1206. if (wnr_config_loadf(wnr_ctx, (char*)configFile) != WNR_ERROR_NONE) {
  1207. WOLFSSL_MSG("Error loading config file into netRandom context");
  1208. wnr_destroy(wnr_ctx);
  1209. wnr_ctx = NULL;
  1210. return RNG_FAILURE_E;
  1211. }
  1212. /* create/init polling mechanism */
  1213. if (wnr_poll_create() != WNR_ERROR_NONE) {
  1214. WOLFSSL_MSG("Error initializing netRandom polling mechanism");
  1215. wnr_destroy(wnr_ctx);
  1216. wnr_ctx = NULL;
  1217. return RNG_FAILURE_E;
  1218. }
  1219. /* validate config, set HMAC callback (optional) */
  1220. if (wnr_setup(wnr_ctx, hmac_cb) != WNR_ERROR_NONE) {
  1221. WOLFSSL_MSG("Error setting up netRandom context");
  1222. wnr_destroy(wnr_ctx);
  1223. wnr_ctx = NULL;
  1224. wnr_poll_destroy();
  1225. return RNG_FAILURE_E;
  1226. }
  1227. wc_UnLockMutex(&wnr_mutex);
  1228. return 0;
  1229. }
  1230. /*
  1231. * Free global Whitewood netRandom context
  1232. * Returns 0 on success, negative on error
  1233. */
  1234. int wc_FreeNetRandom(void)
  1235. {
  1236. if (wnr_mutex_init > 0) {
  1237. if (wc_LockMutex(&wnr_mutex) != 0) {
  1238. WOLFSSL_MSG("Bad Lock Mutex wnr_mutex");
  1239. return BAD_MUTEX_E;
  1240. }
  1241. if (wnr_ctx != NULL) {
  1242. wnr_destroy(wnr_ctx);
  1243. wnr_ctx = NULL;
  1244. }
  1245. wnr_poll_destroy();
  1246. wc_UnLockMutex(&wnr_mutex);
  1247. wc_FreeMutex(&wnr_mutex);
  1248. wnr_mutex_init = 0;
  1249. }
  1250. return 0;
  1251. }
  1252. #endif /* HAVE_WNR */
  1253. #if defined(HAVE_INTEL_RDRAND) || defined(HAVE_INTEL_RDSEED) || \
  1254. defined(HAVE_AMD_RDSEED)
  1255. #ifdef WOLFSSL_ASYNC_CRYPT
  1256. /* need more retries if multiple cores */
  1257. #define INTELRD_RETRY (32 * 8)
  1258. #else
  1259. #define INTELRD_RETRY 32
  1260. #endif
  1261. #if defined(HAVE_INTEL_RDSEED) || defined(HAVE_AMD_RDSEED)
  1262. #ifndef USE_INTEL_INTRINSICS
  1263. /* return 0 on success */
  1264. static WC_INLINE int IntelRDseed64(word64* seed)
  1265. {
  1266. unsigned char ok;
  1267. __asm__ volatile("rdseed %0; setc %1":"=r"(*seed), "=qm"(ok));
  1268. return (ok) ? 0 : -1;
  1269. }
  1270. #else /* USE_INTEL_INTRINSICS */
  1271. /* The compiler Visual Studio uses does not allow inline assembly.
  1272. * It does allow for Intel intrinsic functions. */
  1273. /* return 0 on success */
  1274. # ifdef __GNUC__
  1275. __attribute__((target("rdseed")))
  1276. # endif
  1277. static WC_INLINE int IntelRDseed64(word64* seed)
  1278. {
  1279. int ok;
  1280. ok = _rdseed64_step((unsigned long long*) seed);
  1281. return (ok) ? 0 : -1;
  1282. }
  1283. #endif /* USE_INTEL_INTRINSICS */
  1284. /* return 0 on success */
  1285. static WC_INLINE int IntelRDseed64_r(word64* rnd)
  1286. {
  1287. int i;
  1288. for (i = 0; i < INTELRD_RETRY; i++) {
  1289. if (IntelRDseed64(rnd) == 0)
  1290. return 0;
  1291. }
  1292. return -1;
  1293. }
  1294. #ifndef WOLFSSL_LINUXKM
  1295. /* return 0 on success */
  1296. static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz)
  1297. {
  1298. int ret;
  1299. word64 rndTmp;
  1300. (void)os;
  1301. if (!IS_INTEL_RDSEED(intel_flags))
  1302. return -1;
  1303. for (; (sz / sizeof(word64)) > 0; sz -= sizeof(word64),
  1304. output += sizeof(word64)) {
  1305. ret = IntelRDseed64_r((word64*)output);
  1306. if (ret != 0)
  1307. return ret;
  1308. }
  1309. if (sz == 0)
  1310. return 0;
  1311. /* handle unaligned remainder */
  1312. ret = IntelRDseed64_r(&rndTmp);
  1313. if (ret != 0)
  1314. return ret;
  1315. XMEMCPY(output, &rndTmp, sz);
  1316. ForceZero(&rndTmp, sizeof(rndTmp));
  1317. return 0;
  1318. }
  1319. #endif
  1320. #endif /* HAVE_INTEL_RDSEED || HAVE_AMD_RDSEED */
  1321. #ifdef HAVE_INTEL_RDRAND
  1322. #ifndef USE_INTEL_INTRINSICS
  1323. /* return 0 on success */
  1324. static WC_INLINE int IntelRDrand64(word64 *rnd)
  1325. {
  1326. unsigned char ok;
  1327. __asm__ volatile("rdrand %0; setc %1":"=r"(*rnd), "=qm"(ok));
  1328. return (ok) ? 0 : -1;
  1329. }
  1330. #else /* USE_INTEL_INTRINSICS */
  1331. /* The compiler Visual Studio uses does not allow inline assembly.
  1332. * It does allow for Intel intrinsic functions. */
  1333. /* return 0 on success */
  1334. # ifdef __GNUC__
  1335. __attribute__((target("rdrnd")))
  1336. # endif
  1337. static WC_INLINE int IntelRDrand64(word64 *rnd)
  1338. {
  1339. int ok;
  1340. ok = _rdrand64_step((unsigned long long*) rnd);
  1341. return (ok) ? 0 : -1;
  1342. }
  1343. #endif /* USE_INTEL_INTRINSICS */
  1344. /* return 0 on success */
  1345. static WC_INLINE int IntelRDrand64_r(word64 *rnd)
  1346. {
  1347. int i;
  1348. for (i = 0; i < INTELRD_RETRY; i++) {
  1349. if (IntelRDrand64(rnd) == 0)
  1350. return 0;
  1351. }
  1352. return -1;
  1353. }
  1354. /* return 0 on success */
  1355. static int wc_GenerateRand_IntelRD(OS_Seed* os, byte* output, word32 sz)
  1356. {
  1357. int ret;
  1358. word64 rndTmp;
  1359. (void)os;
  1360. if (!IS_INTEL_RDRAND(intel_flags))
  1361. return -1;
  1362. for (; (sz / sizeof(word64)) > 0; sz -= sizeof(word64),
  1363. output += sizeof(word64)) {
  1364. ret = IntelRDrand64_r((word64 *)output);
  1365. if (ret != 0)
  1366. return ret;
  1367. }
  1368. if (sz == 0)
  1369. return 0;
  1370. /* handle unaligned remainder */
  1371. ret = IntelRDrand64_r(&rndTmp);
  1372. if (ret != 0)
  1373. return ret;
  1374. XMEMCPY(output, &rndTmp, sz);
  1375. return 0;
  1376. }
  1377. #endif /* HAVE_INTEL_RDRAND */
  1378. #endif /* HAVE_INTEL_RDRAND || HAVE_INTEL_RDSEED || HAVE_AMD_RDSEED */
  1379. /* Begin wc_GenerateSeed Implementations */
  1380. #if defined(CUSTOM_RAND_GENERATE_SEED)
  1381. /* Implement your own random generation function
  1382. * Return 0 to indicate success
  1383. * int rand_gen_seed(byte* output, word32 sz);
  1384. * #define CUSTOM_RAND_GENERATE_SEED rand_gen_seed */
  1385. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1386. {
  1387. (void)os; /* Suppress unused arg warning */
  1388. return CUSTOM_RAND_GENERATE_SEED(output, sz);
  1389. }
  1390. #elif defined(CUSTOM_RAND_GENERATE_SEED_OS)
  1391. /* Implement your own random generation function,
  1392. * which includes OS_Seed.
  1393. * Return 0 to indicate success
  1394. * int rand_gen_seed(OS_Seed* os, byte* output, word32 sz);
  1395. * #define CUSTOM_RAND_GENERATE_SEED_OS rand_gen_seed */
  1396. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1397. {
  1398. return CUSTOM_RAND_GENERATE_SEED_OS(os, output, sz);
  1399. }
  1400. #elif defined(CUSTOM_RAND_GENERATE)
  1401. /* Implement your own random generation function
  1402. * word32 rand_gen(void);
  1403. * #define CUSTOM_RAND_GENERATE rand_gen */
  1404. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1405. {
  1406. word32 i = 0;
  1407. (void)os;
  1408. while (i < sz)
  1409. {
  1410. /* If not aligned or there is odd/remainder */
  1411. if( (i + sizeof(CUSTOM_RAND_TYPE)) > sz ||
  1412. ((wc_ptr_t)&output[i] % sizeof(CUSTOM_RAND_TYPE)) != 0
  1413. ) {
  1414. /* Single byte at a time */
  1415. output[i++] = (byte)CUSTOM_RAND_GENERATE();
  1416. }
  1417. else {
  1418. /* Use native 8, 16, 32 or 64 copy instruction */
  1419. *((CUSTOM_RAND_TYPE*)&output[i]) = CUSTOM_RAND_GENERATE();
  1420. i += sizeof(CUSTOM_RAND_TYPE);
  1421. }
  1422. }
  1423. return 0;
  1424. }
  1425. #elif defined(WOLFSSL_SGX)
  1426. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1427. {
  1428. int ret = !SGX_SUCCESS;
  1429. int i, read_max = 10;
  1430. for (i = 0; i < read_max && ret != SGX_SUCCESS; i++) {
  1431. ret = sgx_read_rand(output, sz);
  1432. }
  1433. (void)os;
  1434. return (ret == SGX_SUCCESS) ? 0 : 1;
  1435. }
  1436. #elif defined(USE_WINDOWS_API)
  1437. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1438. {
  1439. #ifdef WOLF_CRYPTO_CB
  1440. int ret;
  1441. if (os != NULL && os->devId != INVALID_DEVID) {
  1442. ret = wc_CryptoCb_RandomSeed(os, output, sz);
  1443. if (ret != CRYPTOCB_UNAVAILABLE)
  1444. return ret;
  1445. /* fall-through when unavailable */
  1446. }
  1447. #endif
  1448. #ifdef HAVE_INTEL_RDSEED
  1449. if (IS_INTEL_RDSEED(intel_flags)) {
  1450. if (!wc_GenerateSeed_IntelRD(NULL, output, sz)) {
  1451. /* success, we're done */
  1452. return 0;
  1453. }
  1454. #ifdef FORCE_FAILURE_RDSEED
  1455. /* don't fall back to CryptoAPI */
  1456. return READ_RAN_E;
  1457. #endif
  1458. }
  1459. #endif /* HAVE_INTEL_RDSEED */
  1460. if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
  1461. CRYPT_VERIFYCONTEXT))
  1462. return WINCRYPT_E;
  1463. if (!CryptGenRandom(os->handle, sz, output))
  1464. return CRYPTGEN_E;
  1465. CryptReleaseContext(os->handle, 0);
  1466. return 0;
  1467. }
  1468. #elif defined(HAVE_RTP_SYS) || defined(EBSNET)
  1469. #include "rtprand.h" /* rtp_rand () */
  1470. #include "rtptime.h" /* rtp_get_system_msec() */
  1471. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1472. {
  1473. word32 i;
  1474. rtp_srand(rtp_get_system_msec());
  1475. for (i = 0; i < sz; i++ ) {
  1476. output[i] = rtp_rand() % 256;
  1477. }
  1478. return 0;
  1479. }
  1480. #elif (defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC_RNG)) && \
  1481. !defined(WOLFSSL_PIC32MZ_RNG)
  1482. /* enable ATECC RNG unless using PIC32MZ one instead */
  1483. #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
  1484. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1485. {
  1486. int ret = 0;
  1487. (void)os;
  1488. if (output == NULL) {
  1489. return BUFFER_E;
  1490. }
  1491. ret = atmel_get_random_number(sz, output);
  1492. return ret;
  1493. }
  1494. #elif defined(MICROCHIP_PIC32)
  1495. #ifdef MICROCHIP_MPLAB_HARMONY
  1496. #ifdef MICROCHIP_MPLAB_HARMONY_3
  1497. #include "system/time/sys_time.h"
  1498. #define PIC32_SEED_COUNT SYS_TIME_CounterGet
  1499. #else
  1500. #define PIC32_SEED_COUNT _CP0_GET_COUNT
  1501. #endif
  1502. #else
  1503. #if !defined(WOLFSSL_MICROCHIP_PIC32MZ)
  1504. #include <peripheral/timer.h>
  1505. #endif
  1506. extern word32 ReadCoreTimer(void);
  1507. #define PIC32_SEED_COUNT ReadCoreTimer
  1508. #endif
  1509. #ifdef WOLFSSL_PIC32MZ_RNG
  1510. #include "xc.h"
  1511. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1512. {
  1513. int i;
  1514. byte rnd[8];
  1515. word32 *rnd32 = (word32 *)rnd;
  1516. word32 size = sz;
  1517. byte* op = output;
  1518. #if ((__PIC32_FEATURE_SET0 == 'E') && (__PIC32_FEATURE_SET1 == 'C'))
  1519. RNGNUMGEN1 = _CP0_GET_COUNT();
  1520. RNGPOLY1 = _CP0_GET_COUNT();
  1521. RNGPOLY2 = _CP0_GET_COUNT();
  1522. RNGNUMGEN2 = _CP0_GET_COUNT();
  1523. #else
  1524. /* All others can be seeded from the TRNG */
  1525. RNGCONbits.TRNGMODE = 1;
  1526. RNGCONbits.TRNGEN = 1;
  1527. while (RNGCNT < 64);
  1528. RNGCONbits.LOAD = 1;
  1529. while (RNGCONbits.LOAD == 1);
  1530. while (RNGCNT < 64);
  1531. RNGPOLY2 = RNGSEED2;
  1532. RNGPOLY1 = RNGSEED1;
  1533. #endif
  1534. RNGCONbits.PLEN = 0x40;
  1535. RNGCONbits.PRNGEN = 1;
  1536. for (i=0; i<5; i++) { /* wait for RNGNUMGEN ready */
  1537. volatile int x, y;
  1538. x = RNGNUMGEN1;
  1539. y = RNGNUMGEN2;
  1540. (void)x;
  1541. (void)y;
  1542. }
  1543. do {
  1544. rnd32[0] = RNGNUMGEN1;
  1545. rnd32[1] = RNGNUMGEN2;
  1546. for(i=0; i<8; i++, op++) {
  1547. *op = rnd[i];
  1548. size --;
  1549. if(size==0)break;
  1550. }
  1551. } while(size);
  1552. return 0;
  1553. }
  1554. #else /* WOLFSSL_PIC32MZ_RNG */
  1555. /* uses the core timer, in nanoseconds to seed srand */
  1556. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1557. {
  1558. int i;
  1559. srand(PIC32_SEED_COUNT() * 25);
  1560. for (i = 0; i < sz; i++ ) {
  1561. output[i] = rand() % 256;
  1562. if ( (i % 8) == 7)
  1563. srand(PIC32_SEED_COUNT() * 25);
  1564. }
  1565. return 0;
  1566. }
  1567. #endif /* WOLFSSL_PIC32MZ_RNG */
  1568. #elif defined(FREESCALE_K70_RNGA) || defined(FREESCALE_RNGA)
  1569. /*
  1570. * wc_Generates a RNG seed using the Random Number Generator Accelerator
  1571. * on the Kinetis K70. Documentation located in Chapter 37 of
  1572. * K70 Sub-Family Reference Manual (see Note 3 in the README for link).
  1573. */
  1574. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1575. {
  1576. word32 i;
  1577. /* turn on RNGA module */
  1578. #if defined(SIM_SCGC3_RNGA_MASK)
  1579. SIM_SCGC3 |= SIM_SCGC3_RNGA_MASK;
  1580. #endif
  1581. #if defined(SIM_SCGC6_RNGA_MASK)
  1582. /* additionally needed for at least K64F */
  1583. SIM_SCGC6 |= SIM_SCGC6_RNGA_MASK;
  1584. #endif
  1585. /* set SLP bit to 0 - "RNGA is not in sleep mode" */
  1586. RNG_CR &= ~RNG_CR_SLP_MASK;
  1587. /* set HA bit to 1 - "security violations masked" */
  1588. RNG_CR |= RNG_CR_HA_MASK;
  1589. /* set GO bit to 1 - "output register loaded with data" */
  1590. RNG_CR |= RNG_CR_GO_MASK;
  1591. for (i = 0; i < sz; i++) {
  1592. /* wait for RNG FIFO to be full */
  1593. while((RNG_SR & RNG_SR_OREG_LVL(0xF)) == 0) {}
  1594. /* get value */
  1595. output[i] = RNG_OR;
  1596. }
  1597. return 0;
  1598. }
  1599. #elif defined(FREESCALE_K53_RNGB) || defined(FREESCALE_RNGB)
  1600. /*
  1601. * wc_Generates a RNG seed using the Random Number Generator (RNGB)
  1602. * on the Kinetis K53. Documentation located in Chapter 33 of
  1603. * K53 Sub-Family Reference Manual (see note in the README for link).
  1604. */
  1605. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1606. {
  1607. int i;
  1608. /* turn on RNGB module */
  1609. SIM_SCGC3 |= SIM_SCGC3_RNGB_MASK;
  1610. /* reset RNGB */
  1611. RNG_CMD |= RNG_CMD_SR_MASK;
  1612. /* FIFO generate interrupt, return all zeros on underflow,
  1613. * set auto reseed */
  1614. RNG_CR |= (RNG_CR_FUFMOD_MASK | RNG_CR_AR_MASK);
  1615. /* gen seed, clear interrupts, clear errors */
  1616. RNG_CMD |= (RNG_CMD_GS_MASK | RNG_CMD_CI_MASK | RNG_CMD_CE_MASK);
  1617. /* wait for seeding to complete */
  1618. while ((RNG_SR & RNG_SR_SDN_MASK) == 0) {}
  1619. for (i = 0; i < sz; i++) {
  1620. /* wait for a word to be available from FIFO */
  1621. while((RNG_SR & RNG_SR_FIFO_LVL_MASK) == 0) {}
  1622. /* get value */
  1623. output[i] = RNG_OUT;
  1624. }
  1625. return 0;
  1626. }
  1627. #elif defined(FREESCALE_KSDK_2_0_TRNG)
  1628. #ifndef TRNG0
  1629. #define TRNG0 TRNG
  1630. #endif
  1631. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1632. {
  1633. status_t status;
  1634. status = TRNG_GetRandomData(TRNG0, output, sz);
  1635. (void)os;
  1636. if (status == kStatus_Success)
  1637. {
  1638. return(0);
  1639. }
  1640. return RAN_BLOCK_E;
  1641. }
  1642. #elif defined(FREESCALE_KSDK_2_0_RNGA)
  1643. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1644. {
  1645. status_t status;
  1646. status = RNGA_GetRandomData(RNG, output, sz);
  1647. (void)os;
  1648. if (status == kStatus_Success)
  1649. {
  1650. return(0);
  1651. }
  1652. return RAN_BLOCK_E;
  1653. }
  1654. #elif defined(FREESCALE_RNGA)
  1655. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1656. {
  1657. status_t status;
  1658. status = RNGA_GetRandomData(RNG, output, sz);
  1659. (void)os;
  1660. if (status == kStatus_Success)
  1661. {
  1662. return(0);
  1663. }
  1664. return RAN_BLOCK_E;
  1665. }
  1666. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) || \
  1667. defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS)
  1668. /*
  1669. * Fallback to USE_TEST_GENSEED if a FREESCALE platform did not match any
  1670. * of the TRNG/RNGA/RNGB support
  1671. */
  1672. #define USE_TEST_GENSEED
  1673. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  1674. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1675. {
  1676. (void)os;
  1677. return silabs_GenerateRand(output, sz);
  1678. }
  1679. #elif defined(STM32_RNG)
  1680. /* Generate a RNG seed using the hardware random number generator
  1681. * on the STM32F2/F4/F7/L4. */
  1682. #ifdef WOLFSSL_STM32_CUBEMX
  1683. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1684. {
  1685. int ret;
  1686. RNG_HandleTypeDef hrng;
  1687. word32 i = 0;
  1688. (void)os;
  1689. ret = wolfSSL_CryptHwMutexLock();
  1690. if (ret != 0) {
  1691. return ret;
  1692. }
  1693. /* enable RNG clock source */
  1694. __HAL_RCC_RNG_CLK_ENABLE();
  1695. /* enable RNG peripheral */
  1696. XMEMSET(&hrng, 0, sizeof(hrng));
  1697. hrng.Instance = RNG;
  1698. HAL_RNG_Init(&hrng);
  1699. while (i < sz) {
  1700. /* If not aligned or there is odd/remainder */
  1701. if( (i + sizeof(word32)) > sz ||
  1702. ((wc_ptr_t)&output[i] % sizeof(word32)) != 0
  1703. ) {
  1704. /* Single byte at a time */
  1705. uint32_t tmpRng = 0;
  1706. if (HAL_RNG_GenerateRandomNumber(&hrng, &tmpRng) != HAL_OK) {
  1707. wolfSSL_CryptHwMutexUnLock();
  1708. return RAN_BLOCK_E;
  1709. }
  1710. output[i++] = (byte)tmpRng;
  1711. }
  1712. else {
  1713. /* Use native 32 instruction */
  1714. if (HAL_RNG_GenerateRandomNumber(&hrng, (uint32_t*)&output[i]) != HAL_OK) {
  1715. wolfSSL_CryptHwMutexUnLock();
  1716. return RAN_BLOCK_E;
  1717. }
  1718. i += sizeof(word32);
  1719. }
  1720. }
  1721. HAL_RNG_DeInit(&hrng);
  1722. wolfSSL_CryptHwMutexUnLock();
  1723. return 0;
  1724. }
  1725. #elif defined(WOLFSSL_STM32F427_RNG) || defined(WOLFSSL_STM32_RNG_NOLIB)
  1726. /* Generate a RNG seed using the hardware RNG on the STM32F427
  1727. * directly, following steps outlined in STM32F4 Reference
  1728. * Manual (Chapter 24) for STM32F4xx family. */
  1729. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1730. {
  1731. int ret;
  1732. word32 i;
  1733. (void)os;
  1734. ret = wolfSSL_CryptHwMutexLock();
  1735. if (ret != 0) {
  1736. return ret;
  1737. }
  1738. /* enable RNG peripheral clock */
  1739. RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
  1740. /* enable RNG interrupt, set IE bit in RNG->CR register */
  1741. RNG->CR |= RNG_CR_IE;
  1742. /* enable RNG, set RNGEN bit in RNG->CR. Activates RNG,
  1743. * RNG_LFSR, and error detector */
  1744. RNG->CR |= RNG_CR_RNGEN;
  1745. /* verify no errors, make sure SEIS and CEIS bits are 0
  1746. * in RNG->SR register */
  1747. if (RNG->SR & (RNG_SR_SECS | RNG_SR_CECS)) {
  1748. wolfSSL_CryptHwMutexUnLock();
  1749. return RNG_FAILURE_E;
  1750. }
  1751. for (i = 0; i < sz; i++) {
  1752. /* wait until RNG number is ready */
  1753. while ((RNG->SR & RNG_SR_DRDY) == 0) { }
  1754. /* get value */
  1755. output[i] = RNG->DR;
  1756. }
  1757. wolfSSL_CryptHwMutexUnLock();
  1758. return 0;
  1759. }
  1760. #else
  1761. /* Generate a RNG seed using the STM32 Standard Peripheral Library */
  1762. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1763. {
  1764. int ret;
  1765. word32 i;
  1766. (void)os;
  1767. ret = wolfSSL_CryptHwMutexLock();
  1768. if (ret != 0) {
  1769. return ret;
  1770. }
  1771. /* enable RNG clock source */
  1772. RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);
  1773. /* reset RNG */
  1774. RNG_DeInit();
  1775. /* enable RNG peripheral */
  1776. RNG_Cmd(ENABLE);
  1777. /* verify no errors with RNG_CLK or Seed */
  1778. if (RNG_GetFlagStatus(RNG_FLAG_SECS | RNG_FLAG_CECS) != RESET) {
  1779. wolfSSL_CryptHwMutexUnLock();
  1780. return RNG_FAILURE_E;
  1781. }
  1782. for (i = 0; i < sz; i++) {
  1783. /* wait until RNG number is ready */
  1784. while (RNG_GetFlagStatus(RNG_FLAG_DRDY) == RESET) { }
  1785. /* get value */
  1786. output[i] = RNG_GetRandomNumber();
  1787. }
  1788. wolfSSL_CryptHwMutexUnLock();
  1789. return 0;
  1790. }
  1791. #endif /* WOLFSSL_STM32_CUBEMX */
  1792. #elif defined(WOLFSSL_TIRTOS)
  1793. #warning "potential for not enough entropy, currently being used for testing"
  1794. #include <xdc/runtime/Timestamp.h>
  1795. #include <stdlib.h>
  1796. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1797. {
  1798. int i;
  1799. srand(xdc_runtime_Timestamp_get32());
  1800. for (i = 0; i < sz; i++ ) {
  1801. output[i] = rand() % 256;
  1802. if ((i % 8) == 7) {
  1803. srand(xdc_runtime_Timestamp_get32());
  1804. }
  1805. }
  1806. return 0;
  1807. }
  1808. #elif defined(WOLFSSL_PB)
  1809. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1810. {
  1811. word32 i;
  1812. for (i = 0; i < sz; i++)
  1813. output[i] = UTL_Rand();
  1814. (void)os;
  1815. return 0;
  1816. }
  1817. #elif defined(WOLFSSL_NUCLEUS)
  1818. #include "nucleus.h"
  1819. #include "kernel/plus_common.h"
  1820. #warning "potential for not enough entropy, currently being used for testing"
  1821. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1822. {
  1823. int i;
  1824. srand(NU_Get_Time_Stamp());
  1825. for (i = 0; i < sz; i++ ) {
  1826. output[i] = rand() % 256;
  1827. if ((i % 8) == 7) {
  1828. srand(NU_Get_Time_Stamp());
  1829. }
  1830. }
  1831. return 0;
  1832. }
  1833. #elif defined(WOLFSSL_DEOS) && !defined(CUSTOM_RAND_GENERATE)
  1834. #include "stdlib.h"
  1835. #warning "potential for not enough entropy, currently being used for testing Deos"
  1836. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1837. {
  1838. int i;
  1839. int seed = XTIME(0);
  1840. (void)os;
  1841. for (i = 0; i < sz; i++ ) {
  1842. output[i] = rand_r(&seed) % 256;
  1843. if ((i % 8) == 7) {
  1844. seed = XTIME(0);
  1845. rand_r(&seed);
  1846. }
  1847. }
  1848. return 0;
  1849. }
  1850. #elif defined(WOLFSSL_VXWORKS)
  1851. #ifdef WOLFSSL_VXWORKS_6_x
  1852. #include "stdlib.h"
  1853. #warning "potential for not enough entropy, currently being used for testing"
  1854. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1855. {
  1856. int i;
  1857. unsigned int seed = (unsigned int)XTIME(0);
  1858. (void)os;
  1859. for (i = 0; i < sz; i++ ) {
  1860. output[i] = rand_r(&seed) % 256;
  1861. if ((i % 8) == 7) {
  1862. seed = (unsigned int)XTIME(0);
  1863. rand_r(&seed);
  1864. }
  1865. }
  1866. return 0;
  1867. }
  1868. #else
  1869. #include <randomNumGen.h>
  1870. #include <tickLib.h>
  1871. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) {
  1872. STATUS status = ERROR;
  1873. RANDOM_NUM_GEN_STATUS r_status = RANDOM_NUM_GEN_ERROR;
  1874. _Vx_ticks_t seed = 0;
  1875. #ifdef VXWORKS_SIM
  1876. /* cannot generate true entropy with VxWorks simulator */
  1877. #warning "not enough entropy, simulator for testing only"
  1878. int i = 0;
  1879. for (i = 0; i < 1000; i++) {
  1880. randomAddTimeStamp();
  1881. }
  1882. #endif
  1883. /*
  1884. wolfSSL can request 52 Bytes of random bytes. We need to add
  1885. buffer to the entropy pool to ensure we can get more than 32 Bytes.
  1886. Because VxWorks has entropy limits (ENTROPY_MIN and ENTROPY_MAX)
  1887. defined as 256 and 1024 bits, see randomSWNumGenLib.c.
  1888. randStatus() can return the following status:
  1889. RANDOM_NUM_GEN_NO_ENTROPY when entropy is 0
  1890. RANDOM_NUM_GEN_ERROR, entropy is not initialized
  1891. RANDOM_NUM_GEN_NOT_ENOUGH_ENTROPY if entropy < 32 Bytes
  1892. RANDOM_NUM_GEN_ENOUGH_ENTROPY if entropy is between 32 and 128 Bytes
  1893. RANDOM_NUM_GEN_MAX_ENTROPY if entropy is greater than 128 Bytes
  1894. */
  1895. do {
  1896. seed = tickGet();
  1897. status = randAdd(&seed, sizeof(_Vx_ticks_t), 2);
  1898. if (status == OK)
  1899. r_status = randStatus();
  1900. } while (r_status != RANDOM_NUM_GEN_MAX_ENTROPY &&
  1901. r_status != RANDOM_NUM_GEN_ERROR && status == OK);
  1902. if (r_status == RANDOM_NUM_GEN_ERROR)
  1903. return RNG_FAILURE_E;
  1904. status = randBytes (output, sz);
  1905. if (status == ERROR) {
  1906. return RNG_FAILURE_E;
  1907. }
  1908. return 0;
  1909. }
  1910. #endif
  1911. #elif defined(WOLFSSL_NRF51) || defined(WOLFSSL_NRF5x)
  1912. #include "app_error.h"
  1913. #include "nrf_drv_rng.h"
  1914. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1915. {
  1916. int remaining = sz, length, pos = 0;
  1917. word32 err_code;
  1918. byte available;
  1919. static byte initialized = 0;
  1920. (void)os;
  1921. /* Make sure RNG is running */
  1922. if (!initialized) {
  1923. err_code = nrf_drv_rng_init(NULL);
  1924. if (err_code != NRF_SUCCESS && err_code != NRF_ERROR_INVALID_STATE
  1925. #ifdef NRF_ERROR_MODULE_ALREADY_INITIALIZED
  1926. && err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED
  1927. #endif
  1928. ) {
  1929. return -1;
  1930. }
  1931. initialized = 1;
  1932. }
  1933. while (remaining > 0) {
  1934. available = 0;
  1935. nrf_drv_rng_bytes_available(&available); /* void func */
  1936. length = (remaining < available) ? remaining : available;
  1937. if (length > 0) {
  1938. err_code = nrf_drv_rng_rand(&output[pos], length);
  1939. if (err_code != NRF_SUCCESS) {
  1940. break;
  1941. }
  1942. remaining -= length;
  1943. pos += length;
  1944. }
  1945. }
  1946. return (err_code == NRF_SUCCESS) ? 0 : -1;
  1947. }
  1948. #elif defined(HAVE_WNR)
  1949. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1950. {
  1951. if (os == NULL || output == NULL || wnr_ctx == NULL ||
  1952. wnr_timeout < 0) {
  1953. return BAD_FUNC_ARG;
  1954. }
  1955. if (wnr_mutex_init == 0) {
  1956. WOLFSSL_MSG("netRandom context must be created before use");
  1957. return RNG_FAILURE_E;
  1958. }
  1959. if (wc_LockMutex(&wnr_mutex) != 0) {
  1960. WOLFSSL_MSG("Bad Lock Mutex wnr_mutex");
  1961. return BAD_MUTEX_E;
  1962. }
  1963. if (wnr_get_entropy(wnr_ctx, wnr_timeout, output, sz, sz) !=
  1964. WNR_ERROR_NONE)
  1965. return RNG_FAILURE_E;
  1966. wc_UnLockMutex(&wnr_mutex);
  1967. return 0;
  1968. }
  1969. #elif defined(INTIME_RTOS)
  1970. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  1971. {
  1972. uint32_t randval;
  1973. word32 len;
  1974. if (output == NULL) {
  1975. return BUFFER_E;
  1976. }
  1977. #ifdef INTIMEVER
  1978. /* If INTIMEVER exists then it is INTIME RTOS v6 or later */
  1979. #define INTIME_RAND_FUNC arc4random
  1980. len = 4;
  1981. #else
  1982. /* v5 and older */
  1983. #define INTIME_RAND_FUNC rand
  1984. srand(time(0));
  1985. len = 2; /* don't use all 31 returned bits */
  1986. #endif
  1987. while (sz > 0) {
  1988. if (sz < len)
  1989. len = sz;
  1990. randval = INTIME_RAND_FUNC();
  1991. XMEMCPY(output, &randval, len);
  1992. output += len;
  1993. sz -= len;
  1994. }
  1995. (void)os;
  1996. return 0;
  1997. }
  1998. #elif defined(WOLFSSL_WICED)
  1999. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2000. {
  2001. int ret;
  2002. (void)os;
  2003. if (output == NULL || UINT16_MAX < sz) {
  2004. return BUFFER_E;
  2005. }
  2006. if ((ret = wiced_crypto_get_random((void*) output, sz) )
  2007. != WICED_SUCCESS) {
  2008. return ret;
  2009. }
  2010. return ret;
  2011. }
  2012. #elif defined(WOLFSSL_NETBURNER)
  2013. #warning using NetBurner pseudo random GetRandomByte for seed
  2014. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2015. {
  2016. word32 i;
  2017. (void)os;
  2018. if (output == NULL) {
  2019. return BUFFER_E;
  2020. }
  2021. for (i = 0; i < sz; i++) {
  2022. output[i] = GetRandomByte();
  2023. /* check if was a valid random number */
  2024. if (!RandomValid())
  2025. return RNG_FAILURE_E;
  2026. }
  2027. return 0;
  2028. }
  2029. #elif defined(IDIRECT_DEV_RANDOM)
  2030. extern int getRandom( int sz, unsigned char *output );
  2031. int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2032. {
  2033. int num_bytes_returned = 0;
  2034. num_bytes_returned = getRandom( (int) sz, (unsigned char *) output );
  2035. return 0;
  2036. }
  2037. #elif (defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
  2038. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_QNX_CAAM))
  2039. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  2040. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2041. {
  2042. unsigned int args[4] = {0};
  2043. CAAM_BUFFER buf[1];
  2044. int ret = 0;
  2045. int times = 1000, i; /* 1000 is an arbitrary number chosen */
  2046. word32 idx = 0;
  2047. (void)os;
  2048. if (output == NULL) {
  2049. return BUFFER_E;
  2050. }
  2051. /* Check Waiting to make sure entropy is ready */
  2052. for (i = 0; i < times; i++) {
  2053. buf[0].BufferType = DataBuffer | LastBuffer;
  2054. buf[0].TheAddress = (CAAM_ADDRESS)(output + idx);
  2055. buf[0].Length = ((sz - idx) < WC_CAAM_MAX_ENTROPY)?
  2056. sz - idx : WC_CAAM_MAX_ENTROPY;
  2057. args[0] = buf[0].Length;
  2058. ret = wc_caamAddAndWait(buf, 1, args, CAAM_ENTROPY);
  2059. if (ret == 0) {
  2060. idx += buf[0].Length;
  2061. if (idx == sz)
  2062. break;
  2063. }
  2064. /* driver could be waiting for entropy */
  2065. if (ret != RAN_BLOCK_E && ret != 0) {
  2066. return ret;
  2067. }
  2068. usleep(100);
  2069. }
  2070. if (i == times && ret != 0) {
  2071. return RNG_FAILURE_E;
  2072. }
  2073. else { /* Success case */
  2074. ret = 0;
  2075. }
  2076. return ret;
  2077. }
  2078. #elif defined(WOLFSSL_APACHE_MYNEWT)
  2079. #include <stdlib.h>
  2080. #include "os/os_time.h"
  2081. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2082. {
  2083. int i;
  2084. srand(os_time_get());
  2085. for (i = 0; i < sz; i++ ) {
  2086. output[i] = rand() % 256;
  2087. if ((i % 8) == 7) {
  2088. srand(os_time_get());
  2089. }
  2090. }
  2091. return 0;
  2092. }
  2093. #elif defined(WOLFSSL_ESPIDF)
  2094. /* Espressif */
  2095. #if defined(WOLFSSL_ESPWROOM32) || defined(WOLFSSL_ESPWROOM32SE)
  2096. /* Espressif ESP32 */
  2097. #include <esp_system.h>
  2098. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2099. {
  2100. word32 rand;
  2101. while (sz > 0) {
  2102. word32 len = sizeof(rand);
  2103. if (sz < len)
  2104. len = sz;
  2105. /* Get one random 32-bit word from hw RNG */
  2106. rand = esp_random( );
  2107. XMEMCPY(output, &rand, len);
  2108. output += len;
  2109. sz -= len;
  2110. }
  2111. return 0;
  2112. }
  2113. #elif defined(WOLFSSL_ESP8266)
  2114. /* Espressif ESP8266 */
  2115. #include <esp_system.h>
  2116. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2117. {
  2118. word32 rand;
  2119. while (sz > 0) {
  2120. word32 len = sizeof(rand);
  2121. if (sz < len)
  2122. len = sz;
  2123. /* Get one random 32-bit word from hw RNG */
  2124. rand = esp_random( );
  2125. XMEMCPY(output, &rand, len);
  2126. output += len;
  2127. sz -= len;
  2128. }
  2129. return 0;
  2130. }
  2131. #endif /* end WOLFSSL_ESPWROOM32 */
  2132. #elif defined(WOLFSSL_LINUXKM)
  2133. #include <linux/random.h>
  2134. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2135. {
  2136. (void)os;
  2137. get_random_bytes(output, sz);
  2138. return 0;
  2139. }
  2140. #elif defined(WOLFSSL_RENESAS_TSIP)
  2141. #if defined(WOLFSSL_RENESA_TSIP_IAREWRX)
  2142. #include "r_bsp/mcu/all/r_rx_compiler.h"
  2143. #endif
  2144. #include "r_bsp/platform.h"
  2145. #include "r_tsip_rx_if.h"
  2146. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2147. {
  2148. int ret = 0;
  2149. word32 buffer[4];
  2150. while (sz > 0) {
  2151. word32 len = sizeof(buffer);
  2152. if (sz < len) {
  2153. len = sz;
  2154. }
  2155. /* return 4 words random number*/
  2156. ret = R_TSIP_GenerateRandomNumber((uint32_t*)buffer);
  2157. if(ret == TSIP_SUCCESS) {
  2158. XMEMCPY(output, &buffer, len);
  2159. output += len;
  2160. sz -= len;
  2161. } else
  2162. return ret;
  2163. }
  2164. return ret;
  2165. }
  2166. #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
  2167. #include "r_sce.h"
  2168. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2169. {
  2170. int ret = 0;
  2171. word32 buffer[4];
  2172. while (sz > 0) {
  2173. word32 len = sizeof(buffer);
  2174. if (sz < len) {
  2175. len = sz;
  2176. }
  2177. /* return 4 words random number*/
  2178. ret = R_SCE_RandomNumberGenerate(buffer);
  2179. if(ret == FSP_SUCCESS) {
  2180. XMEMCPY(output, &buffer, len);
  2181. output += len;
  2182. sz -= len;
  2183. } else
  2184. return ret;
  2185. }
  2186. return ret;
  2187. }
  2188. #elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_TRNG)
  2189. #include "hal_data.h"
  2190. #ifndef WOLFSSL_SCE_TRNG_HANDLE
  2191. #define WOLFSSL_SCE_TRNG_HANDLE g_sce_trng
  2192. #endif
  2193. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2194. {
  2195. word32 ret;
  2196. word32 blocks;
  2197. word32 len = sz;
  2198. ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->open(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
  2199. WOLFSSL_SCE_TRNG_HANDLE.p_cfg);
  2200. if (ret != SSP_SUCCESS && ret != SSP_ERR_CRYPTO_ALREADY_OPEN) {
  2201. /* error opening TRNG driver */
  2202. return -1;
  2203. }
  2204. blocks = sz / sizeof(word32);
  2205. if (blocks > 0) {
  2206. ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
  2207. (word32*)output, blocks);
  2208. if (ret != SSP_SUCCESS) {
  2209. return -1;
  2210. }
  2211. }
  2212. len = len - (blocks * sizeof(word32));
  2213. if (len > 0) {
  2214. word32 tmp;
  2215. if (len > sizeof(word32)) {
  2216. return -1;
  2217. }
  2218. ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->read(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl,
  2219. (word32*)&tmp, 1);
  2220. if (ret != SSP_SUCCESS) {
  2221. return -1;
  2222. }
  2223. XMEMCPY(output + (blocks * sizeof(word32)), (byte*)&tmp, len);
  2224. }
  2225. ret = WOLFSSL_SCE_TRNG_HANDLE.p_api->close(WOLFSSL_SCE_TRNG_HANDLE.p_ctrl);
  2226. if (ret != SSP_SUCCESS) {
  2227. /* error opening TRNG driver */
  2228. return -1;
  2229. }
  2230. return 0;
  2231. }
  2232. #elif defined(CUSTOM_RAND_GENERATE_BLOCK)
  2233. /* #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc
  2234. * extern int myRngFunc(byte* output, word32 sz);
  2235. */
  2236. #elif defined(WOLFSSL_SAFERTOS) || defined(WOLFSSL_LEANPSK) || \
  2237. defined(WOLFSSL_IAR_ARM) || defined(WOLFSSL_MDK_ARM) || \
  2238. defined(WOLFSSL_uITRON4) || defined(WOLFSSL_uTKERNEL2) || \
  2239. defined(WOLFSSL_LPC43xx) || defined(NO_STM32_RNG) || \
  2240. defined(MBED) || defined(WOLFSSL_EMBOS) || \
  2241. defined(WOLFSSL_GENSEED_FORTEST) || defined(WOLFSSL_CHIBIOS) || \
  2242. defined(WOLFSSL_CONTIKI) || defined(WOLFSSL_AZSPHERE)
  2243. /* these platforms do not have a default random seed and
  2244. you'll need to implement your own wc_GenerateSeed or define via
  2245. CUSTOM_RAND_GENERATE_BLOCK */
  2246. #define USE_TEST_GENSEED
  2247. #elif defined(WOLFSSL_ZEPHYR)
  2248. #include <random/rand32.h>
  2249. #ifndef _POSIX_C_SOURCE
  2250. #include <posix/time.h>
  2251. #else
  2252. #include <sys/time.h>
  2253. #endif
  2254. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2255. {
  2256. sys_rand_get(output, sz);
  2257. return 0;
  2258. }
  2259. #elif defined(WOLFSSL_TELIT_M2MB)
  2260. #include "stdlib.h"
  2261. static long get_timestamp(void) {
  2262. long myTime = 0;
  2263. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  2264. if (fd >= 0) {
  2265. M2MB_RTC_TIMEVAL_T timeval;
  2266. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  2267. myTime = timeval.msec;
  2268. m2mb_rtc_close(fd);
  2269. }
  2270. return myTime;
  2271. }
  2272. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2273. {
  2274. int i;
  2275. srand(get_timestamp());
  2276. for (i = 0; i < sz; i++ ) {
  2277. output[i] = rand() % 256;
  2278. if ((i % 8) == 7) {
  2279. srand(get_timestamp());
  2280. }
  2281. }
  2282. return 0;
  2283. }
  2284. #elif defined(WOLFSSL_SE050)
  2285. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  2286. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz){
  2287. int ret = 0;
  2288. (void)os;
  2289. if (output == NULL) {
  2290. return BUFFER_E;
  2291. }
  2292. ret = wolfSSL_CryptHwMutexLock();
  2293. if (ret == 0) {
  2294. ret = se050_get_random_number(sz, output);
  2295. wolfSSL_CryptHwMutexUnLock();
  2296. }
  2297. return ret;
  2298. }
  2299. #elif defined(DOLPHIN_EMULATOR)
  2300. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2301. {
  2302. word32 i;
  2303. (void)os;
  2304. srand(time(NULL));
  2305. for (i = 0; i < sz; i++)
  2306. output[i] = (byte)rand();
  2307. return 0;
  2308. }
  2309. #elif defined(WOLFSSL_GETRANDOM)
  2310. /* getrandom() was added to the Linux kernel in version 3.17.
  2311. * Added to glibc in version 2.25. */
  2312. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2313. {
  2314. int ret = 0;
  2315. int len = 0;
  2316. (void)os;
  2317. while (sz) {
  2318. errno = 0;
  2319. len = (int)getrandom(output, sz, 0);
  2320. if (len == -1) {
  2321. if (errno == EINTR) {
  2322. /* interrupted, call getrandom again */
  2323. continue;
  2324. }
  2325. else {
  2326. ret = READ_RAN_E;
  2327. }
  2328. break;
  2329. }
  2330. sz -= len;
  2331. output += len;
  2332. }
  2333. return ret;
  2334. }
  2335. #elif defined(NO_DEV_RANDOM)
  2336. #error "you need to write an os specific wc_GenerateSeed() here"
  2337. /*
  2338. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2339. {
  2340. return 0;
  2341. }
  2342. */
  2343. #else
  2344. /* may block */
  2345. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2346. {
  2347. int ret = 0;
  2348. if (os == NULL) {
  2349. return BAD_FUNC_ARG;
  2350. }
  2351. #ifdef WOLF_CRYPTO_CB
  2352. if (os->devId != INVALID_DEVID) {
  2353. ret = wc_CryptoCb_RandomSeed(os, output, sz);
  2354. if (ret != CRYPTOCB_UNAVAILABLE)
  2355. return ret;
  2356. /* fall-through when unavailable */
  2357. ret = 0; /* reset error code */
  2358. }
  2359. #endif
  2360. #if defined(HAVE_INTEL_RDSEED) || defined(HAVE_AMD_RDSEED)
  2361. if (IS_INTEL_RDSEED(intel_flags)) {
  2362. ret = wc_GenerateSeed_IntelRD(NULL, output, sz);
  2363. if (ret == 0) {
  2364. /* success, we're done */
  2365. return ret;
  2366. }
  2367. #ifdef FORCE_FAILURE_RDSEED
  2368. /* don't fallback to /dev/urandom */
  2369. return ret;
  2370. #else
  2371. /* reset error and fallback to using /dev/urandom */
  2372. ret = 0;
  2373. #endif
  2374. }
  2375. #endif /* HAVE_INTEL_RDSEED || HAVE_AMD_RDSEED */
  2376. #ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
  2377. os->fd = open("/dev/urandom", O_RDONLY);
  2378. if (os->fd == -1)
  2379. #endif
  2380. {
  2381. /* may still have /dev/random */
  2382. os->fd = open("/dev/random", O_RDONLY);
  2383. if (os->fd == -1)
  2384. return OPEN_RAN_E;
  2385. }
  2386. while (sz) {
  2387. int len = (int)read(os->fd, output, sz);
  2388. if (len == -1) {
  2389. ret = READ_RAN_E;
  2390. break;
  2391. }
  2392. sz -= len;
  2393. output += len;
  2394. if (sz) {
  2395. #if defined(BLOCKING) || defined(WC_RNG_BLOCKING)
  2396. sleep(0); /* context switch */
  2397. #else
  2398. ret = RAN_BLOCK_E;
  2399. break;
  2400. #endif
  2401. }
  2402. }
  2403. close(os->fd);
  2404. return ret;
  2405. }
  2406. #endif
  2407. #ifdef USE_TEST_GENSEED
  2408. #ifndef _MSC_VER
  2409. #warning "write a real random seed!!!!, just for testing now"
  2410. #else
  2411. #pragma message("Warning: write a real random seed!!!!, just for testing now")
  2412. #endif
  2413. int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  2414. {
  2415. word32 i;
  2416. for (i = 0; i < sz; i++ )
  2417. output[i] = i;
  2418. (void)os;
  2419. return 0;
  2420. }
  2421. #endif
  2422. /* End wc_GenerateSeed */
  2423. #if defined(CUSTOM_RAND_GENERATE_BLOCK) && defined(WOLFSSL_KCAPI)
  2424. #include <fcntl.h>
  2425. int wc_hwrng_generate_block(byte *output, word32 sz)
  2426. {
  2427. int fd;
  2428. int len;
  2429. int ret = 0;
  2430. fd = open("/dev/hwrng", O_RDONLY);
  2431. if (fd == -1)
  2432. return OPEN_RAN_E;
  2433. while(sz)
  2434. {
  2435. len = (int)read(fd, output, sz);
  2436. if (len == -1)
  2437. {
  2438. ret = READ_RAN_E;
  2439. break;
  2440. }
  2441. sz -= len;
  2442. output += len;
  2443. }
  2444. close(fd);
  2445. return ret;
  2446. }
  2447. #endif
  2448. #endif /* WC_NO_RNG */
  2449. #endif /* HAVE_FIPS */