Skip to content

Commit ea43bcb

Browse files
Keep RNG seed file descriptor open until the RNG is freed.
1 parent a103f5a commit ea43bcb

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

wolfcrypt/src/random.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,12 @@ int wc_FreeRng(WC_RNG* rng)
13731373
ret = WC_HW_E;
13741374
#endif
13751375

1376+
#ifndef USE_WINDOWS_API
1377+
if(rng->seed.fd != 0 && rng->seed.fd != -1) {
1378+
close(rng->seed.fd);
1379+
}
1380+
#endif
1381+
13761382
return ret;
13771383
}
13781384

@@ -3553,20 +3559,22 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
35533559

35543560
#ifndef NO_FILESYSTEM
35553561
#ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
3556-
os->fd = open("/dev/urandom", O_RDONLY);
3562+
if (os->fd == 0 || os->fd == -1) {
3563+
os->fd = open("/dev/urandom", O_RDONLY);
3564+
#if defined(DEBUG_WOLFSSL)
3565+
WOLFSSL_MSG("opened /dev/urandom.");
3566+
#endif
3567+
if (os->fd == -1)
3568+
#endif
3569+
{
3570+
/* may still have /dev/random */
3571+
os->fd = open("/dev/random", O_RDONLY);
35573572
#if defined(DEBUG_WOLFSSL)
3558-
WOLFSSL_MSG("opened /dev/urandom.");
3573+
WOLFSSL_MSG("opened /dev/random.");
35593574
#endif
3560-
if (os->fd == -1)
3561-
#endif
3562-
{
3563-
/* may still have /dev/random */
3564-
os->fd = open("/dev/random", O_RDONLY);
3565-
#if defined(DEBUG_WOLFSSL)
3566-
WOLFSSL_MSG("opened /dev/random.");
3567-
#endif
3568-
if (os->fd == -1)
3569-
return OPEN_RAN_E;
3575+
if (os->fd == -1)
3576+
return OPEN_RAN_E;
3577+
}
35703578
}
35713579
#if defined(DEBUG_WOLFSSL)
35723580
WOLFSSL_MSG("rnd read...");
@@ -3590,7 +3598,6 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
35903598
#endif
35913599
}
35923600
}
3593-
close(os->fd);
35943601
#else
35953602
(void)output;
35963603
(void)sz;

0 commit comments

Comments
 (0)