isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone

Blame SOURCES/openssl-1.1.1-fips-post-rand.patch

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