isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone

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

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