]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add support for RNDRRS Provider
authorOrr Toledano <otoledan@amazon.com>
Thu, 6 May 2021 21:32:49 +0000 (21:32 +0000)
committerTomas Mraz <tomas@openssl.org>
Thu, 16 Dec 2021 11:38:09 +0000 (12:38 +0100)
Create new provider for RNDRRS. Modify support for rand_cpu to default to
RDRAND/RDSEED on x86 and RNDRRS on aarch64.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15361)

crypto/info.c
providers/implementations/rands/seeding/build.info
providers/implementations/rands/seeding/rand_cpu_arm64.c [new file with mode: 0644]

index a106e8c885910ace331792b634fd375e765132dd..f3bef56b1369f063d153152b4097333b3995306c 100644 (file)
@@ -135,7 +135,11 @@ DEFINE_RUN_ONCE_STATIC(init_info_strings)
         add_seeds_string("stdsc");
 #endif
 #ifdef OPENSSL_RAND_SEED_RDCPU
+# ifdef __aarch64__
+        add_seeds_string("rndr ( rndrrs rndr )");
+# else
         add_seeds_string("rdrand ( rdseed rdrand )");
+# endif
 #endif
 #ifdef OPENSSL_RAND_SEED_LIBRANDOM
         add_seeds_string("C-library-random");
index 2788146ad42ad878a57eb1d3e3f219c8be3f6bf0..9c5eefee2d0c3c74178a5e6052a7384f724f89bc 100644 (file)
@@ -1,10 +1,15 @@
-$COMMON=rand_unix.c rand_win.c rand_tsc.c rand_cpu_x86.c
+$COMMON=rand_unix.c rand_win.c rand_tsc.c
 IF[{- $config{target} =~ /vxworks/i -}]
   $COMMON=$COMMON rand_vxworks.c
 ENDIF
 IF[{- $config{target} =~ /vms/i -}]
   $COMMON=$COMMON rand_vms.c
 ENDIF
+IF[{- !$disabled{asm} && $config{target} =~ '.*aarch64' -}]
+  $COMMON=$COMMON rand_cpu_arm64.c
+ELSE
+  $COMMON=$COMMON rand_cpu_x86.c
+ENDIF
 
 SOURCE[../../../libdefault.a]=$COMMON
 
diff --git a/providers/implementations/rands/seeding/rand_cpu_arm64.c b/providers/implementations/rands/seeding/rand_cpu_arm64.c
new file mode 100644 (file)
index 0000000..a8530e0
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include "internal/cryptlib.h"
+#include <openssl/opensslconf.h>
+#include "crypto/rand_pool.h"
+#include "prov/seeding.h"
+
+
+#ifdef OPENSSL_RAND_SEED_RDCPU
+#include "crypto/arm_arch.h"
+
+size_t OPENSSL_rndrrs_bytes(unsigned char *buf, size_t len);
+
+static size_t get_hardware_random_value(unsigned char *buf, size_t len);
+
+/*
+ * Acquire entropy using Arm-specific cpu instructions
+ *
+ * Uses the RNDRRS instruction. RNDR is never needed since
+ * RNDRRS will always be available if RNDR is an available
+ * instruction.
+ *
+ * Returns the total entropy count, if it exceeds the requested
+ * entropy count. Otherwise, returns an entropy count of 0.
+ */
+size_t ossl_prov_acquire_entropy_from_cpu(RAND_POOL *pool)
+{
+    size_t bytes_needed;
+    unsigned char *buffer;
+
+    bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
+    if (bytes_needed > 0) {
+        buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
+
+        if (buffer != NULL) {
+            if (get_hardware_random_value(buffer, bytes_needed) == bytes_needed)
+                ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
+            else
+                ossl_rand_pool_add_end(pool, 0, 0);
+        }
+    }
+
+    return ossl_rand_pool_entropy_available(pool);
+}
+
+static size_t get_hardware_random_value(unsigned char *buf, size_t len)
+{
+    /* Always use RNDRRS or nothing */
+    if (OPENSSL_armcap_P & ARMV8_RNG)  {
+        if (OPENSSL_rndrrs_bytes(buf, len) != len)
+            return 0;
+    } else {
+        return 0;
+    }
+    return len;
+}
+
+#else
+NON_EMPTY_TRANSLATION_UNIT
+#endif /* OPENSSL_RAND_SEED_RDCPU */