Fold CRYPTO_hwrand and CRYPTO_have_hwrand together.

Since the caller must check for CRYPTO_hwrand failures anyway, there's not much
point in doing the CRYPTO_have_hwrand check externally.

(As a bonus, CRYPTO_hwrand no longer compiles to abort() on ARM, so linker
deduplicating won't confuse Chrome's crash reporter...)

Change-Id: I2191d835fbda5b70812f14cd9a873a5e35c30c6d
Reviewed-on: https://boringssl-review.googlesource.com/5630
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2015-08-06 19:26:27 -04:00 committed by Adam Langley
parent b2d987b47c
commit 1be2ec6756
3 changed files with 15 additions and 18 deletions

View File

@ -15,23 +15,28 @@
#include <openssl/rand.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/cpu.h>
#include "internal.h"
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM)
int CRYPTO_have_hwrand(void) {
return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
}
/* These functions are defined in asm/rdrand-x86_64.pl */
extern int CRYPTO_rdrand(uint8_t out[8]);
extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
static int have_rdrand(void) {
return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
}
int CRYPTO_hwrand(uint8_t *buf, size_t len) {
if (!have_rdrand()) {
return 0;
}
const size_t len_multiple8 = len & ~7;
if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
return 0;
@ -53,12 +58,8 @@ int CRYPTO_hwrand(uint8_t *buf, size_t len) {
#else
int CRYPTO_have_hwrand(void) {
int CRYPTO_hwrand(uint8_t *buf, size_t len) {
return 0;
}
void CRYPTO_hwrand(uint8_t *buf, size_t len) {
abort();
}
#endif

View File

@ -24,13 +24,10 @@ extern "C" {
* system. */
void CRYPTO_sysrand(uint8_t *buf, size_t len);
/* CRYPTO_have_hwrand returns one iff |CRYPTO_hwrand| can be called to generate
* hardware entropy. */
int CRYPTO_have_hwrand(void);
/* CRYPTO_hwrand fills |len| bytes at |buf| with entropy from the hardware.
* This function can only be called if |CRYPTO_have_hwrand| returns one.
* It returns one on success or zero on hardware failure. */
* This function can only be called if |CRYPTO_have_hwrand| returns one. It
* returns one on success or zero on hardware failure or if hardware support is
* unavailable. */
int CRYPTO_hwrand(uint8_t *buf, size_t len);

View File

@ -75,8 +75,7 @@ int RAND_bytes(uint8_t *buf, size_t len) {
return 1;
}
if (!CRYPTO_have_hwrand() ||
!CRYPTO_hwrand(buf, len)) {
if (!CRYPTO_hwrand(buf, len)) {
/* Without a hardware RNG to save us from address-space duplication, the OS
* entropy is used directly. */
CRYPTO_sysrand(buf, len);