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>
diff --git a/crypto/rand/hwrand.c b/crypto/rand/hwrand.c
index 5f81f09..f0bbccd 100644
--- a/crypto/rand/hwrand.c
+++ b/crypto/rand/hwrand.c
@@ -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 @@
 
 #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
diff --git a/crypto/rand/internal.h b/crypto/rand/internal.h
index 5e6ea11..50ecf4d 100644
--- a/crypto/rand/internal.h
+++ b/crypto/rand/internal.h
@@ -24,13 +24,10 @@
  * 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);
 
 
diff --git a/crypto/rand/rand.c b/crypto/rand/rand.c
index 3969520..e76a120 100644
--- a/crypto/rand/rand.c
+++ b/crypto/rand/rand.c
@@ -75,8 +75,7 @@
     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);