Skip to content

Commit cb78341

Browse files
authored
Merge pull request #7586 from kareem-wolfssl/gh7197
Keep RNG seed file descriptor open until the RNG is freed.
2 parents 5fa0681 + 17b6ce7 commit cb78341

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7835,7 +7835,7 @@ fi
78357835
if test "$ENABLED_HAPROXY" = "yes"
78367836
then
78377837
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_HAPROXY -DOPENSSL_COMPATIBLE_DEFAULTS"
7838-
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SIGNER_DER_CERT"
7838+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SIGNER_DER_CERT -DWOLFSSL_KEEP_RNG_SEED_FD_OPEN"
78397839
# --enable-all defines its own DEFAULT_MAX_CLASSIC_ASYM_KEY_BITS
78407840
if test -z "$DEFAULT_MAX_CLASSIC_ASYM_KEY_BITS"
78417841
then

wolfcrypt/src/random.c

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

wolfssl/wolfcrypt/random.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ struct OS_Seed {
156156
ProviderHandle handle;
157157
#else
158158
int fd;
159+
#if defined(WOLFSSL_KEEP_RNG_SEED_FD_OPEN)
160+
WC_BITFIELD seedFdOpen:1;
161+
WC_BITFIELD keepSeedFdOpen:1;
162+
#endif
159163
#endif
160164
#if defined(WOLF_CRYPTO_CB)
161165
int devId;

wolfssl/wolfcrypt/wc_port.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
10101010
#define XSEEK_SET FS_SEEK_SET
10111011
#define XSEEK_END FS_SEEK_END
10121012
#define XBADFILE NULL
1013+
#define XBADFD (-1)
10131014
#define XFGETS(b,s,f) -2 /* Not ported yet */
10141015

10151016
#define XSTAT fs_stat
@@ -1119,6 +1120,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
11191120
#define XSEEK_SET SEEK_SET
11201121
#define XSEEK_END SEEK_END
11211122
#define XBADFILE NULL
1123+
#define XBADFD (-1)
11221124
#define XFGETS fgets
11231125
#define XFPRINTF fprintf
11241126
#define XFFLUSH fflush

0 commit comments

Comments
 (0)