Fix the Windows fuzzer build

OPENSSL_WINDOWS doesn't *quite* imply that crypto/rand_extra/windows.c
is used, thanks to fuzzer mode.

The sea of ifdefs here is becoming quite a mess, so I've added
OPENSSL_RAND_* resolve the dispatch in one place. Perhaps later we
should also we can also simplify this by just including
CRYPTO_init_sysrand and CRYPTO_sysrand_if_available in all the C files.
But that'll be easier to do when Trusty's RNG is moved in tree.

While I'm here, fold some of the ifdefs in windows.c together.

Change-Id: Ic9c21c5c943a409ebb1d77f27daea1eeb9422e9d
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/61085
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: Bob Beck <bbe@google.com>
chromium-stable
David Benjamin 1 year ago committed by Boringssl LUCI CQ
parent a905bbb52a
commit a369247817
  1. 21
      crypto/fipsmodule/rand/internal.h
  2. 4
      crypto/fipsmodule/rand/urandom.c
  3. 6
      crypto/fipsmodule/rand/urandom_test.cc
  4. 7
      crypto/rand_extra/deterministic.c
  5. 8
      crypto/rand_extra/fuchsia.c
  6. 48
      crypto/rand_extra/windows.c

@ -26,9 +26,16 @@ extern "C" {
#endif
#if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_FUCHSIA) && \
!defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) && !defined(OPENSSL_TRUSTY)
#define OPENSSL_URANDOM
#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
#define OPENSSL_RAND_DETERMINISTIC
#elif defined(OPENSSL_FUCHSIA)
#define OPENSSL_RAND_FUCHSIA
#elif defined(OPENSSL_TRUSTY)
// Trusty's PRNG file is, for now, maintained outside the tree.
#elif defined(OPENSSL_WINDOWS)
#define OPENSSL_RAND_WINDOWS
#else
#define OPENSSL_RAND_URANDOM
#endif
// RAND_bytes_with_additional_data samples from the RNG after mixing 32 bytes
@ -70,15 +77,15 @@ void CRYPTO_sysrand(uint8_t *buf, size_t len);
// depending on the vendor's configuration.
void CRYPTO_sysrand_for_seed(uint8_t *buf, size_t len);
#if defined(OPENSSL_URANDOM) || defined(OPENSSL_WINDOWS)
#if defined(OPENSSL_RAND_URANDOM) || defined(OPENSSL_RAND_WINDOWS)
// CRYPTO_init_sysrand initializes long-lived resources needed to draw entropy
// from the operating system.
void CRYPTO_init_sysrand(void);
#else
OPENSSL_INLINE void CRYPTO_init_sysrand(void) {}
#endif // defined(OPENSSL_URANDOM) || defined(OPENSSL_WINDOWS)
#endif // defined(OPENSSL_RAND_URANDOM) || defined(OPENSSL_RAND_WINDOWS)
#if defined(OPENSSL_URANDOM)
#if defined(OPENSSL_RAND_URANDOM)
// CRYPTO_sysrand_if_available fills |len| bytes at |buf| with entropy from the
// operating system, or early /dev/urandom data, and returns 1, _if_ the entropy
// pool is initialized or if getrandom() is not available and not in FIPS mode.
@ -90,7 +97,7 @@ OPENSSL_INLINE int CRYPTO_sysrand_if_available(uint8_t *buf, size_t len) {
CRYPTO_sysrand(buf, len);
return 1;
}
#endif // defined(OPENSSL_URANDOM)
#endif // defined(OPENSSL_RAND_URANDOM)
// rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has
// been enabled via |RAND_enable_fork_unsafe_buffering|.

@ -20,7 +20,7 @@
#include "internal.h"
#if defined(OPENSSL_URANDOM)
#if defined(OPENSSL_RAND_URANDOM)
#include <assert.h>
#include <errno.h>
@ -352,4 +352,4 @@ int CRYPTO_sysrand_if_available(uint8_t *out, size_t requested) {
}
}
#endif // OPENSSL_URANDOM
#endif // OPENSSL_RAND_URANDOM

@ -22,9 +22,9 @@
#include "getrandom_fillin.h"
#include "internal.h"
#if (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(BORINGSSL_SHARED_LIBRARY) && \
defined(OPENSSL_URANDOM) && defined(USE_NR_getrandom)
#if (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(BORINGSSL_SHARED_LIBRARY) && defined(OPENSSL_RAND_URANDOM) && \
defined(USE_NR_getrandom)
#include <elf.h>
#include <linux/random.h>

@ -14,14 +14,15 @@
#include <openssl/rand.h>
#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
#include "../fipsmodule/rand/internal.h"
#if defined(OPENSSL_RAND_DETERMINISTIC)
#include <string.h>
#include <openssl/chacha.h>
#include "../internal.h"
#include "../fipsmodule/rand/internal.h"
// g_num_calls is the number of calls to |CRYPTO_sysrand| that have occurred.
@ -53,4 +54,4 @@ void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) {
CRYPTO_sysrand(out, requested);
}
#endif // BORINGSSL_UNSAFE_DETERMINISTIC_MODE
#endif // OPENSSL_RAND_DETERMINISTIC

@ -14,15 +14,15 @@
#include <openssl/rand.h>
#if defined(OPENSSL_FUCHSIA) && !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
#include "../fipsmodule/rand/internal.h"
#if defined(OPENSSL_RAND_FUCHSIA)
#include <limits.h>
#include <stdlib.h>
#include <zircon/syscalls.h>
#include "../fipsmodule/rand/internal.h"
void CRYPTO_sysrand(uint8_t *out, size_t requested) {
zx_cprng_draw(out, requested);
}
@ -31,4 +31,4 @@ void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) {
CRYPTO_sysrand(out, requested);
}
#endif // OPENSSL_FUCHSIA && !BORINGSSL_UNSAFE_DETERMINISTIC_MODE
#endif // OPENSSL_RAND_FUCHSIA

@ -14,7 +14,9 @@
#include <openssl/rand.h>
#if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
#include "../fipsmodule/rand/internal.h"
#if defined(OPENSSL_RAND_WINDOWS)
#include <limits.h>
#include <stdlib.h>
@ -31,12 +33,29 @@ OPENSSL_MSVC_PRAGMA(comment(lib, "bcrypt.lib"))
OPENSSL_MSVC_PRAGMA(warning(pop))
#include "../fipsmodule/rand/internal.h"
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
void CRYPTO_init_sysrand(void) {}
void CRYPTO_sysrand(uint8_t *out, size_t requested) {
while (requested > 0) {
ULONG output_bytes_this_pass = ULONG_MAX;
if (requested < output_bytes_this_pass) {
output_bytes_this_pass = (ULONG)requested;
}
if (!BCRYPT_SUCCESS(BCryptGenRandom(
/*hAlgorithm=*/NULL, out, output_bytes_this_pass,
BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
abort();
}
requested -= output_bytes_this_pass;
out += output_bytes_this_pass;
}
}
#else
// See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng
typedef BOOL (WINAPI *ProcessPrngFunction)(PBYTE pbData, SIZE_T cbData);
static ProcessPrngFunction g_processprng_fn = NULL;
@ -56,26 +75,8 @@ void CRYPTO_init_sysrand(void) {
static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
CRYPTO_once(&once, init_processprng);
}
#endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP
void CRYPTO_sysrand(uint8_t *out, size_t requested) {
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
while (requested > 0) {
ULONG output_bytes_this_pass = ULONG_MAX;
if (requested < output_bytes_this_pass) {
output_bytes_this_pass = (ULONG)requested;
}
if (!BCRYPT_SUCCESS(BCryptGenRandom(
/*hAlgorithm=*/NULL, out, output_bytes_this_pass,
BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
abort();
}
requested -= output_bytes_this_pass;
out += output_bytes_this_pass;
}
return;
#else
CRYPTO_init_sysrand();
// On non-UWP configurations, use ProcessPrng instead of BCryptGenRandom
// to avoid accessing resources that may be unavailable inside the
@ -83,11 +84,12 @@ void CRYPTO_sysrand(uint8_t *out, size_t requested) {
if (!g_processprng_fn(out, requested)) {
abort();
}
#endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP
}
#endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP
void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) {
CRYPTO_sysrand(out, requested);
}
#endif // OPENSSL_WINDOWS && !BORINGSSL_UNSAFE_DETERMINISTIC_MODE
#endif // OPENSSL_RAND_WINDOWS

Loading…
Cancel
Save