@@ -855,6 +855,11 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
855855 }
856856#endif
857857
858+ #if defined(WOLFSSL_KEEP_RNG_SEED_FD_OPEN ) && !defined(USE_WINDOWS_API )
859+ if (!rng -> seed .seedFdOpen )
860+ rng -> seed .fd = XBADFD ;
861+ #endif
862+
858863#ifdef CUSTOM_RAND_GENERATE_BLOCK
859864 ret = 0 ; /* success */
860865#else
@@ -1359,6 +1364,15 @@ int wc_FreeRng(WC_RNG* rng)
13591364 ret = WC_HW_E ;
13601365#endif
13611366
1367+ #if defined(WOLFSSL_KEEP_RNG_SEED_FD_OPEN ) && defined(XCLOSE ) && \
1368+ !defined(USE_WINDOWS_API )
1369+ if (rng -> seed .seedFdOpen && rng -> seed .fd != XBADFD ) {
1370+ XCLOSE (rng -> seed .fd );
1371+ rng -> seed .fd = XBADFD ;
1372+ rng -> seed .seedFdOpen = 0 ;
1373+ }
1374+ #endif
1375+
13621376 return ret ;
13631377}
13641378
@@ -3553,25 +3567,55 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
35533567 #endif
35543568
35553569#ifndef NO_FILESYSTEM
3556- #ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
3570+ #ifdef WOLFSSL_KEEP_RNG_SEED_FD_OPEN
3571+ if (!os -> seedFdOpen )
3572+ {
3573+ #ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
3574+ os -> fd = open ("/dev/urandom" , O_RDONLY );
3575+ #if defined(DEBUG_WOLFSSL )
3576+ WOLFSSL_MSG ("opened /dev/urandom." );
3577+ #endif /* DEBUG_WOLFSSL */
3578+ if (os -> fd == XBADFD )
3579+ #endif /* NO_DEV_URANDOM */
3580+ {
3581+ /* may still have /dev/random */
3582+ os -> fd = open ("/dev/random" , O_RDONLY );
3583+ #if defined(DEBUG_WOLFSSL )
3584+ WOLFSSL_MSG ("opened /dev/random." );
3585+ #endif /* DEBUG_WOLFSSL */
3586+ if (os -> fd == XBADFD )
3587+ return OPEN_RAN_E ;
3588+ else {
3589+ os -> keepSeedFdOpen = 0 ;
3590+ os -> seedFdOpen = 1 ;
3591+ }
3592+ }
3593+ else {
3594+ os -> keepSeedFdOpen = 1 ;
3595+ os -> seedFdOpen = 1 ;
3596+ }
3597+ }
3598+ #else /* WOLFSSL_KEEP_RNG_SEED_FD_OPEN */
3599+ #ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
35573600 os -> fd = open ("/dev/urandom" , O_RDONLY );
35583601 #if defined(DEBUG_WOLFSSL )
35593602 WOLFSSL_MSG ("opened /dev/urandom." );
3560- #endif
3561- if (os -> fd == -1 )
3562- #endif
3603+ #endif /* DEBUG_WOLFSSL */
3604+ if (os -> fd == XBADFD )
3605+ #endif /* !NO_DEV_URANDOM */
35633606 {
35643607 /* may still have /dev/random */
35653608 os -> fd = open ("/dev/random" , O_RDONLY );
3566- #if defined(DEBUG_WOLFSSL )
3609+ #if defined(DEBUG_WOLFSSL )
35673610 WOLFSSL_MSG ("opened /dev/random." );
3568- #endif
3569- if (os -> fd == -1 )
3611+ #endif /* DEBUG_WOLFSSL */
3612+ if (os -> fd == XBADFD )
35703613 return OPEN_RAN_E ;
35713614 }
3615+ #endif /* WOLFSSL_KEEP_RNG_SEED_FD_OPEN */
35723616 #if defined(DEBUG_WOLFSSL )
35733617 WOLFSSL_MSG ("rnd read..." );
3574- #endif
3618+ #endif /* DEBUG_WOLFSSL */
35753619 while (sz ) {
35763620 int len = (int )read (os -> fd , output , sz );
35773621 if (len == -1 ) {
@@ -3588,11 +3632,20 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
35883632 #else
35893633 ret = RAN_BLOCK_E ;
35903634 break ;
3591- #endif
3635+ #endif /* BLOCKING || WC_RNG_BLOCKING */
35923636 }
35933637 }
3638+ #ifdef WOLFSSL_KEEP_RNG_SEED_FD_OPEN
3639+ if (!os -> keepSeedFdOpen && os -> seedFdOpen )
3640+ {
3641+ close (os -> fd );
3642+ os -> fd = -1 ;
3643+ os -> seedFdOpen = 0 ;
3644+ }
3645+ #else
35943646 close (os -> fd );
3595- #else
3647+ #endif /* WOLFSSL_KEEP_RNG_SEED_FD_OPEN */
3648+ #else /* NO_FILESYSTEM */
35963649 (void )output ;
35973650 (void )sz ;
35983651 ret = NOT_COMPILED_IN ;
0 commit comments