test: update tests to use the fake random number generator
authorPauli <ppzgs1@gmail.com>
Wed, 17 Feb 2021 01:55:13 +0000 (11:55 +1000)
committerPauli <ppzgs1@gmail.com>
Tue, 23 Feb 2021 13:24:41 +0000 (23:24 +1000)
Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13652)

test/ecdsatest.c
test/sm2_internal_test.c

index 8340d289124c9de57a6676bfd6ccafe0a5ffe994..d03eb6f01e011de4796ccee069d2610871ed920b 100644 (file)
 # include "internal/nelem.h"
 # include "ecdsatest.h"
 
-/* functions to change the RAND_METHOD */
-static int fbytes(unsigned char *buf, int num);
-
-static RAND_METHOD fake_rand;
-static const RAND_METHOD *old_rand;
-static int use_fake = 0;
 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 change_rand(void)
-{
-    /* save old rand method */
-    if (!TEST_ptr(old_rand = RAND_get_rand_method()))
-        return 0;
-
-    fake_rand = *old_rand;
-    /* use own random function */
-    fake_rand.bytes = fbytes;
-    /* set new RAND_METHOD */
-    if (!TEST_true(RAND_set_rand_method(&fake_rand)))
-        return 0;
-    return 1;
-}
-
-static int restore_rand(void)
-{
-    if (!TEST_true(RAND_set_rand_method(old_rand)))
-        return 0;
-    return 1;
-}
-
-static int fbytes(unsigned char *buf, int num)
+static int fbytes(unsigned char *buf, size_t num)
 {
     int ret = 0;
     static int fbytes_counter = 0;
     BIGNUM *tmp = NULL;
 
-    if (use_fake == 0)
-        return old_rand->bytes(buf, num);
-
-    use_fake = 0;
+    fake_rand_set_callback(NULL);
 
     if (!TEST_ptr(tmp = BN_new())
         || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
@@ -140,13 +110,11 @@ static int x9_62_tests(int n)
         || !TEST_ptr(r = BN_new())
         || !TEST_ptr(s = BN_new())
         || !TEST_true(BN_hex2bn(&r, r_in))
-        || !TEST_true(BN_hex2bn(&s, s_in))
-        /* swap the RNG source */
-        || !TEST_true(change_rand()))
+        || !TEST_true(BN_hex2bn(&s, s_in)))
         goto err;
 
     /* public key must match KAT */
-    use_fake = 1;
+    fake_rand_set_callback(&fbytes);
     if (!TEST_true(EC_KEY_generate_key(key))
         || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED,
                                              &pbuf, NULL))
@@ -156,7 +124,7 @@ static int x9_62_tests(int n)
         goto err;
 
     /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */
-    use_fake = 1;
+    fake_rand_set_callback(&fbytes);
     if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
         || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
                                                   kinv, rp, key))
@@ -173,10 +141,6 @@ static int x9_62_tests(int n)
     ret = 1;
 
  err:
-    /* restore the RNG source */
-    if (!TEST_true(restore_rand()))
-        ret = 0;
-
     OPENSSL_free(message);
     OPENSSL_free(pbuf);
     OPENSSL_free(qbuf);
@@ -393,11 +357,17 @@ int setup_tests(void)
 #ifdef OPENSSL_NO_EC
     TEST_note("Elliptic curves are disabled.");
 #else
+    fake_rand = fake_rand_start(NULL);
+    if (fake_rand == NULL)
+        return 0;
+
     /* get a list of all internal curves */
     crv_len = EC_get_builtin_curves(NULL, 0);
     if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
-        || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
+        || !TEST_true(EC_get_builtin_curves(curves, crv_len))) {
+        fake_rand_finish(fake_rand);
         return 0;
+    }
     ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
 # ifndef OPENSSL_NO_SM2
     ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
@@ -410,6 +380,7 @@ int setup_tests(void)
 void cleanup_tests(void)
 {
 #ifndef OPENSSL_NO_EC
+    fake_rand_finish(fake_rand);
     OPENSSL_free(curves);
 #endif
 }
index 7dae25cf9f389615f14d8e512af8a699c65a7a8e..77b76e64f8992589824194dcc104d9a6853cc9bb 100644 (file)
 
 # include "crypto/sm2.h"
 
-static RAND_METHOD fake_rand;
-static const RAND_METHOD *saved_rand;
-
+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, int num)
+static int get_faked_bytes(unsigned char *buf, size_t num)
 {
-    if (fake_rand_bytes == NULL)
-        return saved_rand->bytes(buf, num);
-
-    if (!TEST_size_t_gt(fake_rand_size, 0))
+    if (!TEST_ptr(fake_rand_bytes) || !TEST_size_t_gt(fake_rand_size, 0))
         return 0;
 
     while (num-- > 0) {
@@ -54,32 +49,24 @@ static int get_faked_bytes(unsigned char *buf, int num)
 
 static int start_fake_rand(const char *hex_bytes)
 {
-    /* save old rand method */
-    if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
-        return 0;
-
-    fake_rand = *saved_rand;
-    /* use own random function */
-    fake_rand.bytes = get_faked_bytes;
-
-    fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
+    OPENSSL_free(fake_rand_bytes);
     fake_rand_bytes_offset = 0;
     fake_rand_size = strlen(hex_bytes) / 2;
-
-    /* set new RAND_METHOD */
-    if (!TEST_true(RAND_set_rand_method(&fake_rand)))
+    if (!TEST_ptr(fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL)))
         return 0;
+
+    /* use own random function */
+    fake_rand_set_callback(get_faked_bytes);
     return 1;
+
 }
 
-static int restore_rand(void)
+static void restore_rand(void)
 {
+    fake_rand_set_callback(NULL);
     OPENSSL_free(fake_rand_bytes);
     fake_rand_bytes = NULL;
     fake_rand_bytes_offset = 0;
-    if (!TEST_true(RAND_set_rand_method(saved_rand)))
-        return 0;
-    return 1;
 }
 
 static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
@@ -375,8 +362,19 @@ int setup_tests(void)
 #ifdef OPENSSL_NO_SM2
     TEST_note("SM2 is disabled.");
 #else
+    fake_rand = fake_rand_start(NULL);
+    if (fake_rand == NULL)
+        return 0;
+
     ADD_TEST(sm2_crypt_test);
     ADD_TEST(sm2_sig_test);
 #endif
     return 1;
 }
+
+void cleanup_tests(void)
+{
+#ifdef OPENSSL_NO_SM2
+    fake_rand_finish(fake_rand);
+#endif
+}