Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -7835,7 +7835,7 @@ fi
if test "$ENABLED_HAPROXY" = "yes"
then
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_HAPROXY -DOPENSSL_COMPATIBLE_DEFAULTS"
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SIGNER_DER_CERT"
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SIGNER_DER_CERT -DWOLFSSL_KEEP_RNG_SEED_FD_OPEN"
# --enable-all defines its own DEFAULT_MAX_CLASSIC_ASYM_KEY_BITS
if test -z "$DEFAULT_MAX_CLASSIC_ASYM_KEY_BITS"
then
Expand Down
73 changes: 63 additions & 10 deletions wolfcrypt/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,11 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
}
#endif

#if defined(WOLFSSL_KEEP_RNG_SEED_FD_OPEN) && !defined(USE_WINDOWS_API)
if (!rng->seed.seedFdOpen)
rng->seed.fd = XBADFD;
#endif

#ifdef CUSTOM_RAND_GENERATE_BLOCK
ret = 0; /* success */
#else
Expand Down Expand Up @@ -1304,6 +1309,15 @@ int wc_FreeRng(WC_RNG* rng)
ret = WC_HW_E;
#endif

#if defined(WOLFSSL_KEEP_RNG_SEED_FD_OPEN) && defined(XCLOSE) && \
!defined(USE_WINDOWS_API)
if(rng->seed.seedFdOpen && rng->seed.fd != XBADFD) {
XCLOSE(rng->seed.fd);
rng->seed.fd = XBADFD;
rng->seed.seedFdOpen = 0;
}
#endif

return ret;
}

Expand Down Expand Up @@ -3483,25 +3497,55 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#endif

#ifndef NO_FILESYSTEM
#ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
#ifdef WOLFSSL_KEEP_RNG_SEED_FD_OPEN
if (!os->seedFdOpen)
{
#ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
os->fd = open("/dev/urandom", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/urandom.");
#endif /* DEBUG_WOLFSSL */
if (os->fd == XBADFD)
#endif /* NO_DEV_URANDOM */
{
/* may still have /dev/random */
os->fd = open("/dev/random", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/random.");
#endif /* DEBUG_WOLFSSL */
if (os->fd == XBADFD)
return OPEN_RAN_E;
else {
os->keepSeedFdOpen = 0;
os->seedFdOpen = 1;
}
}
else {
os->keepSeedFdOpen = 1;
os->seedFdOpen = 1;
}
}
#else /* WOLFSSL_KEEP_RNG_SEED_FD_OPEN */
#ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
os->fd = open("/dev/urandom", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/urandom.");
#endif
if (os->fd == -1)
#endif
#endif /* DEBUG_WOLFSSL */
if (os->fd == XBADFD)
#endif /* !NO_DEV_URANDOM */
{
/* may still have /dev/random */
os->fd = open("/dev/random", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/random.");
#endif
if (os->fd == -1)
#endif /* DEBUG_WOLFSSL */
if (os->fd == XBADFD)
return OPEN_RAN_E;
}
#endif /* WOLFSSL_KEEP_RNG_SEED_FD_OPEN */
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("rnd read...");
#endif
#endif /* DEBUG_WOLFSSL */
while (sz) {
int len = (int)read(os->fd, output, sz);
if (len == -1) {
Expand All @@ -3518,11 +3562,20 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#else
ret = RAN_BLOCK_E;
break;
#endif
#endif /* BLOCKING || WC_RNG_BLOCKING */
}
}
#ifdef WOLFSSL_KEEP_RNG_SEED_FD_OPEN
if (!os->keepSeedFdOpen && os->seedFdOpen)
{
close(os->fd);
os->fd = -1;
os->seedFdOpen = 0;
}
#else
close(os->fd);
#else
#endif /* WOLFSSL_KEEP_RNG_SEED_FD_OPEN */
#else /* NO_FILESYSTEM */
(void)output;
(void)sz;
ret = NOT_COMPILED_IN;
Expand Down
4 changes: 4 additions & 0 deletions wolfssl/wolfcrypt/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ struct OS_Seed {
ProviderHandle handle;
#else
int fd;
#if defined(WOLFSSL_KEEP_RNG_SEED_FD_OPEN)
WC_BITFIELD seedFdOpen:1;
WC_BITFIELD keepSeedFdOpen:1;
#endif
#endif
#if defined(WOLF_CRYPTO_CB)
int devId;
Expand Down
2 changes: 2 additions & 0 deletions wolfssl/wolfcrypt/wc_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
#define XSEEK_SET FS_SEEK_SET
#define XSEEK_END FS_SEEK_END
#define XBADFILE NULL
#define XBADFD (-1)
#define XFGETS(b,s,f) -2 /* Not ported yet */

#define XSTAT fs_stat
Expand Down Expand Up @@ -1119,6 +1120,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
#define XSEEK_SET SEEK_SET
#define XSEEK_END SEEK_END
#define XBADFILE NULL
#define XBADFD (-1)
#define XFGETS fgets
#define XFPRINTF fprintf
#define XFFLUSH fflush
Expand Down