301c642
diff -up openssl-1.1.1a/crypto/fips/fips.c.fips-post-rand openssl-1.1.1a/crypto/fips/fips.c
301c642
--- openssl-1.1.1a/crypto/fips/fips.c.fips-post-rand	2019-01-15 14:14:07.813360637 +0100
301c642
+++ openssl-1.1.1a/crypto/fips/fips.c	2019-01-15 14:14:07.838360173 +0100
06bb120
@@ -68,6 +68,7 @@
06bb120
 
06bb120
 # include <openssl/fips.h>
06bb120
 # include "internal/thread_once.h"
06bb120
+# include "internal/rand_int.h"
06bb120
 
06bb120
 # ifndef PATH_MAX
06bb120
 #  define PATH_MAX 1024
06bb120
@@ -76,6 +77,7 @@
06bb120
 static int fips_selftest_fail = 0;
06bb120
 static int fips_mode = 0;
06bb120
 static int fips_started = 0;
06bb120
+static int fips_post = 0;
06bb120
 
06bb120
 static int fips_is_owning_thread(void);
06bb120
 static int fips_set_owning_thread(void);
06bb120
@@ -158,6 +160,11 @@ void fips_set_selftest_fail(void)
06bb120
     fips_selftest_fail = 1;
06bb120
 }
06bb120
 
06bb120
+int fips_in_post(void)
06bb120
+{
06bb120
+    return fips_post;
06bb120
+}
06bb120
+
06bb120
 /* we implement what libfipscheck does ourselves */
06bb120
 
06bb120
 static int
06bb120
@@ -445,6 +452,8 @@ int FIPS_module_mode_set(int onoff)
06bb120
         }
06bb120
 # endif
06bb120
 
06bb120
+        fips_post = 1;
06bb120
+
06bb120
         if (!FIPS_selftest()) {
06bb120
             fips_selftest_fail = 1;
06bb120
             ret = 0;
06bb120
@@ -459,7 +468,12 @@ int FIPS_module_mode_set(int onoff)
06bb120
             goto end;
06bb120
         }
06bb120
 
06bb120
+        fips_post = 0;
06bb120
+
06bb120
         fips_set_mode(onoff);
06bb120
+        /* force RNG reseed with entropy from getrandom() on next call */
06bb120
+        rand_fork();
06bb120
+
06bb120
         ret = 1;
06bb120
         goto end;
06bb120
     }
301c642
diff -up openssl-1.1.1a/crypto/include/internal/fips_int.h.fips-post-rand openssl-1.1.1a/crypto/include/internal/fips_int.h
301c642
--- openssl-1.1.1a/crypto/include/internal/fips_int.h.fips-post-rand	2019-01-15 14:14:07.821360489 +0100
301c642
+++ openssl-1.1.1a/crypto/include/internal/fips_int.h	2019-01-15 14:14:07.838360173 +0100
06bb120
@@ -76,6 +76,8 @@ int FIPS_selftest_hmac(void);
06bb120
 int FIPS_selftest_drbg(void);
06bb120
 int FIPS_selftest_cmac(void);
06bb120
 
06bb120
+int fips_in_post(void);
06bb120
+
06bb120
 int fips_pkey_signature_test(EVP_PKEY *pkey,
06bb120
                                  const unsigned char *tbs, int tbslen,
06bb120
                                  const unsigned char *kat,
301c642
diff -up openssl-1.1.1a/crypto/rand/rand_unix.c.fips-post-rand openssl-1.1.1a/crypto/rand/rand_unix.c
301c642
--- openssl-1.1.1a/crypto/rand/rand_unix.c.fips-post-rand	2018-11-20 14:35:38.000000000 +0100
301c642
+++ openssl-1.1.1a/crypto/rand/rand_unix.c	2019-01-15 14:17:22.416748544 +0100
06bb120
@@ -16,10 +16,12 @@
06bb120
 #include <openssl/rand.h>
06bb120
 #include "rand_lcl.h"
06bb120
 #include "internal/rand_int.h"
06bb120
+#include "internal/fips_int.h"
06bb120
 #include <stdio.h>
06bb120
 #include "internal/dso.h"
06bb120
 #if defined(__linux)
06bb120
 # include <sys/syscall.h>
06bb120
+# include <sys/random.h>
06bb120
 #endif
06bb120
 #if defined(__FreeBSD__)
06bb120
 # include <sys/types.h>
301c642
@@ -258,7 +260,7 @@ static ssize_t sysctl_random(char *buf,
06bb120
  * syscall_random(): Try to get random data using a system call
06bb120
  * returns the number of bytes returned in buf, or < 0 on error.
06bb120
  */
06bb120
-static ssize_t syscall_random(void *buf, size_t buflen)
06bb120
+static ssize_t syscall_random(void *buf, size_t buflen, int nonblock)
06bb120
 {
06bb120
     /*
06bb120
      * Note: 'buflen' equals the size of the buffer which is used by the
301c642
@@ -280,6 +282,7 @@ static ssize_t syscall_random(void *buf,
06bb120
      * - Linux since 3.17 with glibc 2.25
06bb120
      * - FreeBSD since 12.0 (1200061)
06bb120
      */
06bb120
+#  if 0
06bb120
 #  if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
06bb120
     extern int getentropy(void *buffer, size_t length) __attribute__((weak));
06bb120
 
301c642
@@ -301,10 +304,10 @@ static ssize_t syscall_random(void *buf,
06bb120
     if (p_getentropy.p != NULL)
06bb120
         return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
06bb120
 #  endif
06bb120
-
06bb120
+#  endif
06bb120
     /* Linux supports this since version 3.17 */
06bb120
 #  if defined(__linux) && defined(SYS_getrandom)
06bb120
-    return syscall(SYS_getrandom, buf, buflen, 0);
06bb120
+    return syscall(SYS_getrandom, buf, buflen, nonblock?GRND_NONBLOCK:0);
06bb120
 #  elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
06bb120
     return sysctl_random(buf, buflen);
06bb120
 #  else
301c642
@@ -454,8 +457,10 @@ size_t rand_pool_acquire_entropy(RAND_PO
06bb120
     size_t bytes_needed;
06bb120
     size_t entropy_available = 0;
06bb120
     unsigned char *buffer;
06bb120
-
301c642
 #   if defined(OPENSSL_RAND_SEED_GETRANDOM)
06bb120
+    int in_post;
06bb120
+
06bb120
+    for (in_post = fips_in_post(); in_post >= 0; --in_post) {
06bb120
     {
06bb120
         ssize_t bytes;
06bb120
         /* Maximum allowed number of consecutive unsuccessful attempts */
301c642
@@ -464,7 +469,7 @@ size_t rand_pool_acquire_entropy(RAND_PO
06bb120
         bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
06bb120
         while (bytes_needed != 0 && attempts-- > 0) {
06bb120
             buffer = rand_pool_add_begin(pool, bytes_needed);
06bb120
-            bytes = syscall_random(buffer, bytes_needed);
06bb120
+            bytes = syscall_random(buffer, bytes_needed, in_post);
06bb120
             if (bytes > 0) {
06bb120
                 rand_pool_add_end(pool, bytes, 8 * bytes);
06bb120
                 bytes_needed -= bytes;
301c642
@@ -496,8 +501,10 @@ size_t rand_pool_acquire_entropy(RAND_PO
06bb120
             int attempts = 3;
06bb120
             const int fd = get_random_device(i);
06bb120
 
06bb120
-            if (fd == -1)
06bb120
+            if (fd == -1) {
06bb120
+                OPENSSL_showfatal("Random device %s cannot be opened.\n", random_device_paths[i]);
06bb120
                 continue;
06bb120
+            }
06bb120
 
06bb120
             while (bytes_needed != 0 && attempts-- > 0) {
06bb120
                 buffer = rand_pool_add_begin(pool, bytes_needed);
301c642
@@ -557,7 +564,9 @@ size_t rand_pool_acquire_entropy(RAND_PO
06bb120
         }
06bb120
     }
06bb120
 #   endif
06bb120
-
06bb120
+#   ifdef OPENSSL_RAND_SEED_GETRANDOM
06bb120
+    }
06bb120
+#   endif
06bb120
     return rand_pool_entropy_available(pool);
06bb120
 #  endif
06bb120
 }