]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 3687] ntp_crypto_rand RNG status not known
authorJuergen Perlinger <perlinger@ntp.org>
Sat, 24 Oct 2020 05:49:18 +0000 (07:49 +0200)
committerJuergen Perlinger <perlinger@ntp.org>
Sat, 24 Oct 2020 05:49:18 +0000 (07:49 +0200)
bk: 5f93c05epdh0wYNPlnUAUmT9Bv0Exg

ChangeLog
libntp/ntp_crypto_rnd.c

index eeceaa9f10cb57b56caf561eaab50a81ec861f63..e71f526796d8254225c1c76a51acdadb7e88a506 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+---
+* [Bug 3687] ntp_crypto_rand RNG status not known <perlinger@ntp.org>
+  - patch by Gerry Garvey
+
 ---
 (4.2.8p15) 2020/06/23 Released by Harlan Stenn <stenn@ntp.org>
 
index 2a4f91affad5f0a88d702bd15f6b4e42ccf899cd..6a1a1b8279275be383dd5af7f9cd35e053b69d3d 100644 (file)
@@ -26,12 +26,17 @@ int crypto_rand_init = 0;
 #else
 
 # ifndef HAVE_ARC4RANDOM_BUF
-static void
-arc4random_buf(void *buf, size_t nbytes);
+static int arc4random_stir(void);
+static void arc4random_buf(void *buf, size_t nbytes);
 
-void
-evutil_secure_rng_get_bytes(void *buf, size_t nbytes);
+int evutil_secure_rng_init(void);
+void evutil_secure_rng_get_bytes(void *buf, size_t nbytes);
 
+static int
+arc4random_stir(void)
+{
+       return(evutil_secure_rng_init());
+}
 static void
 arc4random_buf(void *buf, size_t nbytes)
 {
@@ -41,6 +46,8 @@ arc4random_buf(void *buf, size_t nbytes)
 # endif
 #endif
 
+int crypto_rand_ok = 0;
+
 /*
  * As of late 2014, here's how we plan to provide cryptographic-quality
  * random numbers:
@@ -71,11 +78,18 @@ ntp_crypto_srandom(
 {
 #ifdef USE_OPENSSL_CRYPTO_RAND
        if (!crypto_rand_init) {
-               RAND_poll();
+               if (RAND_poll())
+                       crypto_rand_ok = 1;
                crypto_rand_init = 1;
        }
 #else
-       /* No initialization needed for arc4random() */
+       /*
+        * Explicitly init arc4random to make sure it does seed OK. This
+        * is the only way we can tell if it can successfully get
+        * entropy from the system.
+        */
+       if (!arc4random_stir())
+               crypto_rand_ok = 1;
 #endif
 }
 
@@ -91,6 +105,9 @@ ntp_crypto_random_buf(
        size_t nbytes
        )
 {
+       if (!crypto_rand_ok)
+               return (-1);
+
 #ifdef USE_OPENSSL_CRYPTO_RAND
        int rc;