@@ -147,12 +147,13 @@ This library contains implementation for the random number generator.
147147#elif defined(WOLFSSL_IMXRT1170_CAAM )
148148#elif defined(CY_USING_HAL ) && defined(COMPONENT_WOLFSSL )
149149 #include "cyhal_trng.h" /* Infineon/Cypress HAL RNG implementation */
150- #elif defined(WOLFSSL_GETRANDOM )
151- #include <errno.h>
152- #include <sys/random.h>
153150#elif defined(WOLFSSL_MAX3266X ) || defined(WOLFSSL_MAX3266X_OLD )
154151 #include "wolfssl/wolfcrypt/port/maxim/max3266x.h"
155152#else
153+ #if defined(WOLFSSL_GETRANDOM ) || defined(HAVE_GETRANDOM )
154+ #include <errno.h>
155+ #include <sys/random.h>
156+ #endif
156157 /* include headers that may be needed to get good seed */
157158 #include <fcntl.h>
158159 #ifndef EBSNET
@@ -306,7 +307,11 @@ This library contains implementation for the random number generator.
306307
307308#ifdef WC_RNG_SEED_CB
308309
310+ #ifndef HAVE_FIPS
311+ static wc_RngSeed_Cb seedCb = wc_GenerateSeed ;
312+ #else
309313static wc_RngSeed_Cb seedCb = NULL ;
314+ #endif
310315
311316int wc_SetSeed_Cb (wc_RngSeed_Cb cb )
312317{
@@ -3971,37 +3976,6 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
39713976 return wc_MXC_TRNG_Random (output , sz );
39723977 }
39733978
3974- #elif defined(WOLFSSL_GETRANDOM )
3975-
3976- /* getrandom() was added to the Linux kernel in version 3.17.
3977- * Added to glibc in version 2.25. */
3978- int wc_GenerateSeed (OS_Seed * os , byte * output , word32 sz )
3979- {
3980- int ret = 0 ;
3981- (void )os ;
3982-
3983- while (sz ) {
3984- int len ;
3985-
3986- errno = 0 ;
3987- len = (int )getrandom (output , sz , 0 );
3988- if (len == -1 ) {
3989- if (errno == EINTR ) {
3990- /* interrupted, call getrandom again */
3991- continue ;
3992- }
3993- else {
3994- ret = READ_RAN_E ;
3995- }
3996- break ;
3997- }
3998-
3999- sz -= len ;
4000- output += len ;
4001- }
4002- return ret ;
4003- }
4004-
40053979#elif defined(CY_USING_HAL ) && defined(COMPONENT_WOLFSSL )
40063980
40073981 /* Infineon/Cypress HAL RNG implementation */
@@ -4137,6 +4111,43 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
41374111 }
41384112 #endif /* HAVE_INTEL_RDSEED || HAVE_AMD_RDSEED */
41394113
4114+ #if defined(WOLFSSL_GETRANDOM ) || defined(HAVE_GETRANDOM )
4115+ {
4116+ word32 grSz = sz ;
4117+ byte * grOutput = output ;
4118+
4119+ while (grSz ) {
4120+ ssize_t len ;
4121+
4122+ errno = 0 ;
4123+ len = getrandom (grOutput , grSz , 0 );
4124+ if (len == -1 ) {
4125+ if (errno == EINTR ) {
4126+ /* interrupted, call getrandom again */
4127+ continue ;
4128+ }
4129+ else {
4130+ ret = READ_RAN_E ;
4131+ }
4132+ break ;
4133+ }
4134+
4135+ grSz -= (word32 )len ;
4136+ grOutput += len ;
4137+ }
4138+ if (ret == 0 )
4139+ return ret ;
4140+ #ifdef FORCE_FAILURE_GETRANDOM
4141+ /* don't fallback to /dev/urandom */
4142+ return ret ;
4143+ #else
4144+ /* reset error and fallback to using /dev/urandom */
4145+ ret = 0 ;
4146+ #endif
4147+ }
4148+ #endif
4149+
4150+ #ifndef NO_FILESYSTEM
41404151 #ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
41414152 os -> fd = open ("/dev/urandom" , O_RDONLY );
41424153 #if defined(DEBUG_WOLFSSL )
@@ -4176,6 +4187,9 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
41764187 }
41774188 }
41784189 close (os -> fd );
4190+ #else
4191+ ret = NOT_COMPILED_IN ;
4192+ #endif /* NO_FILESYSTEM */
41794193
41804194 return ret ;
41814195 }
0 commit comments