Clean up references to FIPS
[openssl.git] / crypto / rand / md_rand.c
index 0d25aeb532b6e7a31b6ff60da78e5b77c8b13b31..8cbfae170f4ab700141d220ce89fe29efce61877 100644 (file)
 
 #include <openssl/err.h>
 
-#ifdef OPENSSL_FIPS
-# include <openssl/fips.h>
-#endif
+#include <internal/thread_once.h>
 
-#ifdef BN_DEBUG
+#if defined(BN_DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
 # define PREDICT
 #endif
 
@@ -85,10 +83,12 @@ static RAND_METHOD rand_meth = {
     rand_status
 };
 
-static void do_rand_lock_init(void)
+DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
 {
+    OPENSSL_init_crypto(0, NULL);
     rand_lock = CRYPTO_THREAD_lock_new();
     rand_tmp_lock = CRYPTO_THREAD_lock_new();
+    return rand_lock != NULL && rand_tmp_lock != NULL;
 }
 
 RAND_METHOD *RAND_OpenSSL(void)
@@ -122,6 +122,11 @@ static int rand_add(const void *buf, int num, double add)
     if (!num)
         return 1;
 
+#ifdef PREDICT
+    if (rand_predictable)
+        return 1;
+#endif
+
     /*
      * (Based on the rand(3) manpage)
      *
@@ -141,7 +146,8 @@ static int rand_add(const void *buf, int num, double add)
     if (m == NULL)
         goto err;
 
-    CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init);
+    if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
+        goto err;
 
     /* check if we already have the lock */
     if (crypto_lock_rand) {
@@ -302,7 +308,7 @@ static int rand_bytes(unsigned char *buf, int num, int pseudo)
 
 #ifdef PREDICT
     if (rand_predictable) {
-        static unsigned char val = 0;
+        unsigned char val = 1;
 
         for (i = 0; i < num; i++)
             buf[i] = val++;
@@ -339,7 +345,9 @@ static int rand_bytes(unsigned char *buf, int num, int pseudo)
      * global 'md'.
      */
 
-    CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init);
+    if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
+        goto err_mem;
+
     CRYPTO_THREAD_write_lock(rand_lock);
     /*
      * We could end up in an async engine while holding this lock so ensure
@@ -534,7 +542,9 @@ static int rand_status(void)
     int ret;
     int do_not_lock;
 
-    CRYPTO_THREAD_run_once(&rand_lock_init, do_rand_lock_init);
+    if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
+        return 0;
+
     cur = CRYPTO_THREAD_get_current_id();
     /*
      * check if we already have the lock (could happen if a RAND_poll()
@@ -644,9 +654,9 @@ void rand_hw_xor(unsigned char *buf, size_t num)
 
 #else
 
-static void rand_hw_seed(EVP_MD_CTX *ctx)
+static int rand_hw_seed(EVP_MD_CTX *ctx)
 {
-    return;
+    return 1;
 }
 
 void rand_hw_xor(unsigned char *buf, size_t num)