# 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))
|| !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))
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))
ret = 1;
err:
- /* restore the RNG source */
- if (!TEST_true(restore_rand()))
- ret = 0;
-
OPENSSL_free(message);
OPENSSL_free(pbuf);
OPENSSL_free(qbuf);
#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);
void cleanup_tests(void)
{
#ifndef OPENSSL_NO_EC
+ fake_rand_finish(fake_rand);
OPENSSL_free(curves);
#endif
}
# 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) {
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,
#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
+}