test: update ECDSA and SM2 internal tests in line with the fake_random change

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/14341)
This commit is contained in:
Pauli 2021-02-27 11:57:13 +10:00
parent 5a11de50a4
commit fccdb61aee
2 changed files with 16 additions and 7 deletions

View File

@ -25,18 +25,21 @@
# include "internal/nelem.h" # include "internal/nelem.h"
# include "ecdsatest.h" # include "ecdsatest.h"
static fake_random_generate_cb fbytes;
static const char *numbers[2]; static const char *numbers[2];
static size_t crv_len = 0; static size_t crv_len = 0;
static EC_builtin_curve *curves = NULL; static EC_builtin_curve *curves = NULL;
static OSSL_PROVIDER *fake_rand = 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; int ret = 0;
static int fbytes_counter = 0; static int fbytes_counter = 0;
BIGNUM *tmp = NULL; BIGNUM *tmp = NULL;
fake_rand_set_callback(NULL); fake_rand_set_callback(ctx, NULL);
if (!TEST_ptr(tmp = BN_new()) if (!TEST_ptr(tmp = BN_new())
|| !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers)) || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
@ -114,7 +117,7 @@ static int x9_62_tests(int n)
goto err; goto err;
/* public key must match KAT */ /* 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)) if (!TEST_true(EC_KEY_generate_key(key))
|| !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED, || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED,
&pbuf, NULL)) &pbuf, NULL))
@ -124,7 +127,7 @@ static int x9_62_tests(int n)
goto err; goto err;
/* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */ /* 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)) if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
|| !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len, || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
kinv, rp, key)) kinv, rp, key))

View File

@ -28,12 +28,16 @@
# include "crypto/sm2.h" # include "crypto/sm2.h"
static fake_random_generate_cb get_faked_bytes;
static OSSL_PROVIDER *fake_rand = NULL; static OSSL_PROVIDER *fake_rand = NULL;
static uint8_t *fake_rand_bytes = NULL; static uint8_t *fake_rand_bytes = NULL;
static size_t fake_rand_bytes_offset = 0; static size_t fake_rand_bytes_offset = 0;
static size_t fake_rand_size = 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)) if (!TEST_ptr(fake_rand_bytes) || !TEST_size_t_gt(fake_rand_size, 0))
return 0; return 0;
@ -56,14 +60,16 @@ static int start_fake_rand(const char *hex_bytes)
return 0; return 0;
/* use own random function */ /* use own random function */
fake_rand_set_callback(get_faked_bytes); fake_rand_set_callback(RAND_get0_private(NULL), get_faked_bytes);
fake_rand_set_callback(RAND_get0_public(NULL), get_faked_bytes);
return 1; return 1;
} }
static void restore_rand(void) static void restore_rand(void)
{ {
fake_rand_set_callback(NULL); fake_rand_set_callback(RAND_get0_private(NULL), NULL);
fake_rand_set_callback(RAND_get0_public(NULL), NULL);
OPENSSL_free(fake_rand_bytes); OPENSSL_free(fake_rand_bytes);
fake_rand_bytes = NULL; fake_rand_bytes = NULL;
fake_rand_bytes_offset = 0; fake_rand_bytes_offset = 0;