diff options
author | Pauli <ppzgs1@gmail.com> | 2021-02-17 11:55:13 +1000 |
---|---|---|
committer | Pauli <ppzgs1@gmail.com> | 2021-02-23 23:24:41 +1000 |
commit | 332a245c04dff95f81cfa1f77e0f8a935794f5ee (patch) | |
tree | cca7a471a6972697f6bbd5e637f1f09d99f4f2a1 /test/ecdsatest.c | |
parent | d994ce12058d80f1f04257c30f89d04d5f6399e1 (diff) | |
download | openssl-332a245c04dff95f81cfa1f77e0f8a935794f5ee.zip openssl-332a245c04dff95f81cfa1f77e0f8a935794f5ee.tar.gz openssl-332a245c04dff95f81cfa1f77e0f8a935794f5ee.tar.bz2 |
test: update tests to use the fake random number generator
Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13652)
Diffstat (limited to 'test/ecdsatest.c')
-rw-r--r-- | test/ecdsatest.c | 57 |
1 files changed, 14 insertions, 43 deletions
diff --git a/test/ecdsatest.c b/test/ecdsatest.c index 8340d28..d03eb6f 100644 --- a/test/ecdsatest.c +++ b/test/ecdsatest.c @@ -25,48 +25,18 @@ # 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 } |