[openssl] master update

Dr. Paul Dale pauli at openssl.org
Tue Mar 2 03:24:26 UTC 2021


The branch master has been updated
       via  e1f946630f06c2d3a112022472bb13a1586f599f (commit)
       via  740582cfaffb26c60c72cdc789b39da5c7ec8c66 (commit)
       via  fccdb61aee9538268e2eecfdc5b1e31327803ee4 (commit)
       via  5a11de50a41054ed17d4280c39825a2bdaa96b96 (commit)
      from  0647162f6af7c2e0edb4c770bf501ad7e0302970 (commit)


- Log -----------------------------------------------------------------
commit e1f946630f06c2d3a112022472bb13a1586f599f
Author: Pauli <ppzgs1 at gmail.com>
Date:   Sat Feb 27 12:18:15 2021 +1000

    test: use the new set public and private together call
    
    Reviewed-by: Tomas Mraz <tomas at openssl.org>
    Reviewed-by: Shane Lontis <shane.lontis at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/14341)

commit 740582cfaffb26c60c72cdc789b39da5c7ec8c66
Author: Pauli <ppzgs1 at gmail.com>
Date:   Sat Feb 27 12:17:57 2021 +1000

    test: add utility function to set the fake random callback on both the public and private instances
    
    Reviewed-by: Tomas Mraz <tomas at openssl.org>
    Reviewed-by: Shane Lontis <shane.lontis at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/14341)

commit fccdb61aee9538268e2eecfdc5b1e31327803ee4
Author: Pauli <ppzgs1 at gmail.com>
Date:   Sat Feb 27 11:57:13 2021 +1000

    test: update ECDSA and SM2 internal tests in line with the fake_random change
    
    Reviewed-by: Tomas Mraz <tomas at openssl.org>
    Reviewed-by: Shane Lontis <shane.lontis at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/14341)

commit 5a11de50a41054ed17d4280c39825a2bdaa96b96
Author: Pauli <ppzgs1 at gmail.com>
Date:   Sat Feb 27 11:55:59 2021 +1000

    test: update test_random to create real contexts instead of sharing one
    
    Reviewed-by: Tomas Mraz <tomas at openssl.org>
    Reviewed-by: Shane Lontis <shane.lontis at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/14341)

-----------------------------------------------------------------------

Summary of changes:
 test/ecdsatest.c            | 11 +++++----
 test/sm2_internal_test.c    | 10 +++++---
 test/testutil.h             |  9 ++++++-
 test/testutil/fake_random.c | 58 ++++++++++++++++++++++++++++++++++-----------
 4 files changed, 66 insertions(+), 22 deletions(-)

diff --git a/test/ecdsatest.c b/test/ecdsatest.c
index d03eb6f01e..cf09419c94 100644
--- a/test/ecdsatest.c
+++ b/test/ecdsatest.c
@@ -25,18 +25,21 @@
 # include "internal/nelem.h"
 # include "ecdsatest.h"
 
+static fake_random_generate_cb fbytes;
+
 static const char *numbers[2];
 static size_t crv_len = 0;
 static EC_builtin_curve *curves = NULL;
 static OSSL_PROVIDER *fake_rand = NULL;
 
-static int fbytes(unsigned char *buf, size_t num)
+static int fbytes(unsigned char *buf, size_t num, ossl_unused const char *name,
+                  EVP_RAND_CTX *ctx)
 {
     int ret = 0;
     static int fbytes_counter = 0;
     BIGNUM *tmp = NULL;
 
-    fake_rand_set_callback(NULL);
+    fake_rand_set_callback(ctx, NULL);
 
     if (!TEST_ptr(tmp = BN_new())
         || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
@@ -114,7 +117,7 @@ static int x9_62_tests(int n)
         goto err;
 
     /* public key must match KAT */
-    fake_rand_set_callback(&fbytes);
+    fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
     if (!TEST_true(EC_KEY_generate_key(key))
         || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED,
                                              &pbuf, NULL))
@@ -124,7 +127,7 @@ static int x9_62_tests(int n)
         goto err;
 
     /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */
-    fake_rand_set_callback(&fbytes);
+    fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
     if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
         || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
                                                   kinv, rp, key))
diff --git a/test/sm2_internal_test.c b/test/sm2_internal_test.c
index aaa337b57b..6b80611dd2 100644
--- a/test/sm2_internal_test.c
+++ b/test/sm2_internal_test.c
@@ -28,12 +28,16 @@
 
 # include "crypto/sm2.h"
 
+static fake_random_generate_cb get_faked_bytes;
+
 static OSSL_PROVIDER *fake_rand = NULL;
 static uint8_t *fake_rand_bytes = NULL;
 static size_t fake_rand_bytes_offset = 0;
 static size_t fake_rand_size = 0;
 
-static int get_faked_bytes(unsigned char *buf, size_t num)
+static int get_faked_bytes(unsigned char *buf, size_t num,
+                           ossl_unused const char *name,
+                           ossl_unused EVP_RAND_CTX *ctx)
 {
     if (!TEST_ptr(fake_rand_bytes) || !TEST_size_t_gt(fake_rand_size, 0))
         return 0;
@@ -56,14 +60,14 @@ static int start_fake_rand(const char *hex_bytes)
         return 0;
 
     /* use own random function */
-    fake_rand_set_callback(get_faked_bytes);
+    fake_rand_set_public_private_callbacks(NULL, get_faked_bytes);
     return 1;
 
 }
 
 static void restore_rand(void)
 {
-    fake_rand_set_callback(NULL);
+    fake_rand_set_public_private_callbacks(NULL, NULL);
     OPENSSL_free(fake_rand_bytes);
     fake_rand_bytes = NULL;
     fake_rand_bytes_offset = 0;
diff --git a/test/testutil.h b/test/testutil.h
index 93c91a4a41..8457a2a384 100644
--- a/test/testutil.h
+++ b/test/testutil.h
@@ -567,9 +567,16 @@ uint32_t test_random(void);
 void test_random_seed(uint32_t sd);
 
 /* Fake non-secure random number generator */
+typedef int fake_random_generate_cb(unsigned char *out, size_t outlen,
+                                    const char *name, EVP_RAND_CTX *ctx);
+
 OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx);
 void fake_rand_finish(OSSL_PROVIDER *p);
-void fake_rand_set_callback(int (*cb)(unsigned char *out, size_t outlen));
+void fake_rand_set_callback(EVP_RAND_CTX *ctx,
+                            int (*cb)(unsigned char *out, size_t outlen,
+                                      const char *name, EVP_RAND_CTX *ctx));
+void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
+                                            fake_random_generate_cb *cb);
 
 /* Create a file path from a directory and a filename */
 char *test_mk_file_path(const char *dir, const char *file);
diff --git a/test/testutil/fake_random.c b/test/testutil/fake_random.c
index f8b97d2287..bdd48d394c 100644
--- a/test/testutil/fake_random.c
+++ b/test/testutil/fake_random.c
@@ -12,15 +12,17 @@
 #include <openssl/core_names.h>
 #include <openssl/rand.h>
 #include <openssl/provider.h>
+#include "../include/crypto/evp.h"
+#include "../../crypto/evp/evp_local.h"
 #include "../testutil.h"
 
 typedef struct {
-    int (*cb)(unsigned char *out, size_t outlen);
+    fake_random_generate_cb *cb;
     int state;
+    const char *name;
+    EVP_RAND_CTX *ctx;
 } FAKE_RAND;
 
-static FAKE_RAND fake_rand;
-
 static OSSL_FUNC_rand_newctx_fn fake_rand_newctx;
 static OSSL_FUNC_rand_freectx_fn fake_rand_freectx;
 static OSSL_FUNC_rand_instantiate_fn fake_rand_instantiate;
@@ -33,16 +35,16 @@ static OSSL_FUNC_rand_enable_locking_fn fake_rand_enable_locking;
 static void *fake_rand_newctx(void *provctx, void *parent,
                               const OSSL_DISPATCH *parent_dispatch)
 {
-    fake_rand.state = EVP_RAND_STATE_UNINITIALISED;
-    return &fake_rand;
+    FAKE_RAND *r = OPENSSL_zalloc(sizeof(*r));
+
+    if (r != NULL)
+        r->state = EVP_RAND_STATE_UNINITIALISED;
+    return r;
 }
 
 static void fake_rand_freectx(void *vrng)
 {
-    FAKE_RAND *frng = (FAKE_RAND *)vrng;
-
-    frng->cb = NULL;
-    frng->state = EVP_RAND_STATE_UNINITIALISED;
+    OPENSSL_free(vrng);
 }
 
 static int fake_rand_instantiate(void *vrng, ossl_unused unsigned int strength,
@@ -74,7 +76,7 @@ static int fake_rand_generate(void *vrng, unsigned char *out, size_t outlen,
     uint32_t r;
 
     if (frng->cb != NULL)
-        return (*frng->cb)(out, outlen);
+        return (*frng->cb)(out, outlen, frng->name, frng->ctx);
     while (outlen > 0) {
         r = test_random();
         l = outlen < sizeof(r) ? outlen : sizeof(r);
@@ -169,6 +171,20 @@ static int fake_rand_provider_init(const OSSL_CORE_HANDLE *handle,
     return 1;
 }
 
+static int check_rng(EVP_RAND_CTX *rng, const char *name)
+{
+    FAKE_RAND *f;
+
+    if (!TEST_ptr(rng)) {
+        TEST_info("random: %s", name);
+        return 0;
+    }
+    f = rng->data;
+    f->name = name;
+    f->ctx = rng;
+    return 1;
+}
+
 OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx)
 {
     OSSL_PROVIDER *p;
@@ -180,8 +196,9 @@ OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx)
         return NULL;
 
     /* Ensure that the fake rand is initialized. */
-    if (!TEST_ptr(RAND_get0_private(libctx))
-            || !TEST_ptr(RAND_get0_public(libctx))) {
+    if (!TEST_true(check_rng(RAND_get0_primary(libctx), "primary"))
+            || !TEST_true(check_rng(RAND_get0_private(libctx), "private"))
+            || !TEST_true(check_rng(RAND_get0_public(libctx), "public"))) {
         OSSL_PROVIDER_unload(p);
         return NULL;
     }
@@ -194,8 +211,21 @@ void fake_rand_finish(OSSL_PROVIDER *p)
     OSSL_PROVIDER_unload(p);
 }
 
-void fake_rand_set_callback(int (*cb)(unsigned char *out, size_t outlen))
+void fake_rand_set_callback(EVP_RAND_CTX *rng,
+                            int (*cb)(unsigned char *out, size_t outlen,
+                                      const char *name, EVP_RAND_CTX *ctx))
+{
+    if (rng != NULL)
+        ((FAKE_RAND *)rng->data)->cb = cb;
+}
+
+void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
+                                            int (*cb)(unsigned char *out,
+                                                      size_t outlen,
+                                                      const char *name,
+                                                      EVP_RAND_CTX *ctx))
 {
-    fake_rand.cb = cb;
+    fake_rand_set_callback(RAND_get0_private(libctx), cb);
+    fake_rand_set_callback(RAND_get0_public(libctx), cb);
 }
 


More information about the openssl-commits mailing list