1*e0c4386eSCy Schubert /* 2*e0c4386eSCy Schubert * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved. 3*e0c4386eSCy Schubert * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved 4*e0c4386eSCy Schubert * 5*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use 6*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy 7*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at 8*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html 9*e0c4386eSCy Schubert */ 10*e0c4386eSCy Schubert 11*e0c4386eSCy Schubert /* 12*e0c4386eSCy Schubert * Low level APIs are deprecated for public use, but still ok for internal use. 13*e0c4386eSCy Schubert */ 14*e0c4386eSCy Schubert #include "internal/deprecated.h" 15*e0c4386eSCy Schubert 16*e0c4386eSCy Schubert #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */ 17*e0c4386eSCy Schubert #include "testutil.h" 18*e0c4386eSCy Schubert 19*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC 20*e0c4386eSCy Schubert 21*e0c4386eSCy Schubert # include <openssl/evp.h> 22*e0c4386eSCy Schubert # include <openssl/bn.h> 23*e0c4386eSCy Schubert # include <openssl/ec.h> 24*e0c4386eSCy Schubert # include <openssl/rand.h> 25*e0c4386eSCy Schubert # include "internal/nelem.h" 26*e0c4386eSCy Schubert # include "ecdsatest.h" 27*e0c4386eSCy Schubert 28*e0c4386eSCy Schubert static fake_random_generate_cb fbytes; 29*e0c4386eSCy Schubert 30*e0c4386eSCy Schubert static const char *numbers[2]; 31*e0c4386eSCy Schubert static size_t crv_len = 0; 32*e0c4386eSCy Schubert static EC_builtin_curve *curves = NULL; 33*e0c4386eSCy Schubert static OSSL_PROVIDER *fake_rand = NULL; 34*e0c4386eSCy Schubert 35*e0c4386eSCy Schubert static int fbytes(unsigned char *buf, size_t num, ossl_unused const char *name, 36*e0c4386eSCy Schubert EVP_RAND_CTX *ctx) 37*e0c4386eSCy Schubert { 38*e0c4386eSCy Schubert int ret = 0; 39*e0c4386eSCy Schubert static int fbytes_counter = 0; 40*e0c4386eSCy Schubert BIGNUM *tmp = NULL; 41*e0c4386eSCy Schubert 42*e0c4386eSCy Schubert fake_rand_set_callback(ctx, NULL); 43*e0c4386eSCy Schubert 44*e0c4386eSCy Schubert if (!TEST_ptr(tmp = BN_new()) 45*e0c4386eSCy Schubert || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers)) 46*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter])) 47*e0c4386eSCy Schubert /* tmp might need leading zeros so pad it out */ 48*e0c4386eSCy Schubert || !TEST_int_le(BN_num_bytes(tmp), num) 49*e0c4386eSCy Schubert || !TEST_int_gt(BN_bn2binpad(tmp, buf, num), 0)) 50*e0c4386eSCy Schubert goto err; 51*e0c4386eSCy Schubert 52*e0c4386eSCy Schubert fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers); 53*e0c4386eSCy Schubert ret = 1; 54*e0c4386eSCy Schubert err: 55*e0c4386eSCy Schubert BN_free(tmp); 56*e0c4386eSCy Schubert return ret; 57*e0c4386eSCy Schubert } 58*e0c4386eSCy Schubert 59*e0c4386eSCy Schubert /*- 60*e0c4386eSCy Schubert * This function hijacks the RNG to feed it the chosen ECDSA key and nonce. 61*e0c4386eSCy Schubert * The ECDSA KATs are from: 62*e0c4386eSCy Schubert * - the X9.62 draft (4) 63*e0c4386eSCy Schubert * - NIST CAVP (720) 64*e0c4386eSCy Schubert * 65*e0c4386eSCy Schubert * It uses the low-level ECDSA_sign_setup instead of EVP to control the RNG. 66*e0c4386eSCy Schubert * NB: This is not how applications should use ECDSA; this is only for testing. 67*e0c4386eSCy Schubert * 68*e0c4386eSCy Schubert * Tests the library can successfully: 69*e0c4386eSCy Schubert * - generate public keys that matches those KATs 70*e0c4386eSCy Schubert * - create ECDSA signatures that match those KATs 71*e0c4386eSCy Schubert * - accept those signatures as valid 72*e0c4386eSCy Schubert */ 73*e0c4386eSCy Schubert static int x9_62_tests(int n) 74*e0c4386eSCy Schubert { 75*e0c4386eSCy Schubert int nid, md_nid, ret = 0; 76*e0c4386eSCy Schubert const char *r_in = NULL, *s_in = NULL, *tbs = NULL; 77*e0c4386eSCy Schubert unsigned char *pbuf = NULL, *qbuf = NULL, *message = NULL; 78*e0c4386eSCy Schubert unsigned char digest[EVP_MAX_MD_SIZE]; 79*e0c4386eSCy Schubert unsigned int dgst_len = 0; 80*e0c4386eSCy Schubert long q_len, msg_len = 0; 81*e0c4386eSCy Schubert size_t p_len; 82*e0c4386eSCy Schubert EVP_MD_CTX *mctx = NULL; 83*e0c4386eSCy Schubert EC_KEY *key = NULL; 84*e0c4386eSCy Schubert ECDSA_SIG *signature = NULL; 85*e0c4386eSCy Schubert BIGNUM *r = NULL, *s = NULL; 86*e0c4386eSCy Schubert BIGNUM *kinv = NULL, *rp = NULL; 87*e0c4386eSCy Schubert const BIGNUM *sig_r = NULL, *sig_s = NULL; 88*e0c4386eSCy Schubert 89*e0c4386eSCy Schubert nid = ecdsa_cavs_kats[n].nid; 90*e0c4386eSCy Schubert md_nid = ecdsa_cavs_kats[n].md_nid; 91*e0c4386eSCy Schubert r_in = ecdsa_cavs_kats[n].r; 92*e0c4386eSCy Schubert s_in = ecdsa_cavs_kats[n].s; 93*e0c4386eSCy Schubert tbs = ecdsa_cavs_kats[n].msg; 94*e0c4386eSCy Schubert numbers[0] = ecdsa_cavs_kats[n].d; 95*e0c4386eSCy Schubert numbers[1] = ecdsa_cavs_kats[n].k; 96*e0c4386eSCy Schubert 97*e0c4386eSCy Schubert TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid)); 98*e0c4386eSCy Schubert 99*e0c4386eSCy Schubert #ifdef FIPS_MODULE 100*e0c4386eSCy Schubert if (EC_curve_nid2nist(nid) == NULL) 101*e0c4386eSCy Schubert return TEST_skip("skip non approved curves"); 102*e0c4386eSCy Schubert #endif /* FIPS_MODULE */ 103*e0c4386eSCy Schubert 104*e0c4386eSCy Schubert if (!TEST_ptr(mctx = EVP_MD_CTX_new()) 105*e0c4386eSCy Schubert /* get the message digest */ 106*e0c4386eSCy Schubert || !TEST_ptr(message = OPENSSL_hexstr2buf(tbs, &msg_len)) 107*e0c4386eSCy Schubert || !TEST_true(EVP_DigestInit_ex(mctx, EVP_get_digestbynid(md_nid), NULL)) 108*e0c4386eSCy Schubert || !TEST_true(EVP_DigestUpdate(mctx, message, msg_len)) 109*e0c4386eSCy Schubert || !TEST_true(EVP_DigestFinal_ex(mctx, digest, &dgst_len)) 110*e0c4386eSCy Schubert /* create the key */ 111*e0c4386eSCy Schubert || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid)) 112*e0c4386eSCy Schubert /* load KAT variables */ 113*e0c4386eSCy Schubert || !TEST_ptr(r = BN_new()) 114*e0c4386eSCy Schubert || !TEST_ptr(s = BN_new()) 115*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&r, r_in)) 116*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&s, s_in))) 117*e0c4386eSCy Schubert goto err; 118*e0c4386eSCy Schubert 119*e0c4386eSCy Schubert /* public key must match KAT */ 120*e0c4386eSCy Schubert fake_rand_set_callback(RAND_get0_private(NULL), &fbytes); 121*e0c4386eSCy Schubert if (!TEST_true(EC_KEY_generate_key(key)) 122*e0c4386eSCy Schubert || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED, 123*e0c4386eSCy Schubert &pbuf, NULL)) 124*e0c4386eSCy Schubert || !TEST_ptr(qbuf = OPENSSL_hexstr2buf(ecdsa_cavs_kats[n].Q, &q_len)) 125*e0c4386eSCy Schubert || !TEST_int_eq(q_len, p_len) 126*e0c4386eSCy Schubert || !TEST_mem_eq(qbuf, q_len, pbuf, p_len)) 127*e0c4386eSCy Schubert goto err; 128*e0c4386eSCy Schubert 129*e0c4386eSCy Schubert /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */ 130*e0c4386eSCy Schubert fake_rand_set_callback(RAND_get0_private(NULL), &fbytes); 131*e0c4386eSCy Schubert if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)) 132*e0c4386eSCy Schubert || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len, 133*e0c4386eSCy Schubert kinv, rp, key)) 134*e0c4386eSCy Schubert /* verify the signature */ 135*e0c4386eSCy Schubert || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1)) 136*e0c4386eSCy Schubert goto err; 137*e0c4386eSCy Schubert 138*e0c4386eSCy Schubert /* compare the created signature with the expected signature */ 139*e0c4386eSCy Schubert ECDSA_SIG_get0(signature, &sig_r, &sig_s); 140*e0c4386eSCy Schubert if (!TEST_BN_eq(sig_r, r) 141*e0c4386eSCy Schubert || !TEST_BN_eq(sig_s, s)) 142*e0c4386eSCy Schubert goto err; 143*e0c4386eSCy Schubert 144*e0c4386eSCy Schubert ret = 1; 145*e0c4386eSCy Schubert 146*e0c4386eSCy Schubert err: 147*e0c4386eSCy Schubert OPENSSL_free(message); 148*e0c4386eSCy Schubert OPENSSL_free(pbuf); 149*e0c4386eSCy Schubert OPENSSL_free(qbuf); 150*e0c4386eSCy Schubert EC_KEY_free(key); 151*e0c4386eSCy Schubert ECDSA_SIG_free(signature); 152*e0c4386eSCy Schubert BN_free(r); 153*e0c4386eSCy Schubert BN_free(s); 154*e0c4386eSCy Schubert EVP_MD_CTX_free(mctx); 155*e0c4386eSCy Schubert BN_clear_free(kinv); 156*e0c4386eSCy Schubert BN_clear_free(rp); 157*e0c4386eSCy Schubert return ret; 158*e0c4386eSCy Schubert } 159*e0c4386eSCy Schubert 160*e0c4386eSCy Schubert /*- 161*e0c4386eSCy Schubert * Positive and negative ECDSA testing through EVP interface: 162*e0c4386eSCy Schubert * - EVP_DigestSign (this is the one-shot version) 163*e0c4386eSCy Schubert * - EVP_DigestVerify 164*e0c4386eSCy Schubert * 165*e0c4386eSCy Schubert * Tests the library can successfully: 166*e0c4386eSCy Schubert * - create a key 167*e0c4386eSCy Schubert * - create a signature 168*e0c4386eSCy Schubert * - accept that signature 169*e0c4386eSCy Schubert * - reject that signature with a different public key 170*e0c4386eSCy Schubert * - reject that signature if its length is not correct 171*e0c4386eSCy Schubert * - reject that signature after modifying the message 172*e0c4386eSCy Schubert * - accept that signature after un-modifying the message 173*e0c4386eSCy Schubert * - reject that signature after modifying the signature 174*e0c4386eSCy Schubert * - accept that signature after un-modifying the signature 175*e0c4386eSCy Schubert */ 176*e0c4386eSCy Schubert static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey) 177*e0c4386eSCy Schubert { 178*e0c4386eSCy Schubert /* With the SM2 key type, the SM2 ID is mandatory */ 179*e0c4386eSCy Schubert static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' }; 180*e0c4386eSCy Schubert EVP_PKEY_CTX *pctx; 181*e0c4386eSCy Schubert 182*e0c4386eSCy Schubert if (!TEST_ptr(pctx = EVP_MD_CTX_get_pkey_ctx(mctx)) 183*e0c4386eSCy Schubert || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, sm2_id, sizeof(sm2_id)), 0)) 184*e0c4386eSCy Schubert return 0; 185*e0c4386eSCy Schubert return 1; 186*e0c4386eSCy Schubert } 187*e0c4386eSCy Schubert 188*e0c4386eSCy Schubert static int test_builtin(int n, int as) 189*e0c4386eSCy Schubert { 190*e0c4386eSCy Schubert EC_KEY *eckey_neg = NULL, *eckey = NULL; 191*e0c4386eSCy Schubert unsigned char dirt, offset, tbs[128]; 192*e0c4386eSCy Schubert unsigned char *sig = NULL; 193*e0c4386eSCy Schubert EVP_PKEY *pkey_neg = NULL, *pkey = NULL, *dup_pk = NULL; 194*e0c4386eSCy Schubert EVP_MD_CTX *mctx = NULL; 195*e0c4386eSCy Schubert size_t sig_len; 196*e0c4386eSCy Schubert int nid, ret = 0; 197*e0c4386eSCy Schubert int temp; 198*e0c4386eSCy Schubert 199*e0c4386eSCy Schubert nid = curves[n].nid; 200*e0c4386eSCy Schubert 201*e0c4386eSCy Schubert /* skip built-in curves where ord(G) is not prime */ 202*e0c4386eSCy Schubert if (nid == NID_ipsec4 || nid == NID_ipsec3) { 203*e0c4386eSCy Schubert TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid)); 204*e0c4386eSCy Schubert return 1; 205*e0c4386eSCy Schubert } 206*e0c4386eSCy Schubert 207*e0c4386eSCy Schubert /* 208*e0c4386eSCy Schubert * skip SM2 curve if 'as' is equal to EVP_PKEY_EC or, skip all curves 209*e0c4386eSCy Schubert * except SM2 curve if 'as' is equal to EVP_PKEY_SM2 210*e0c4386eSCy Schubert */ 211*e0c4386eSCy Schubert if (nid == NID_sm2 && as == EVP_PKEY_EC) { 212*e0c4386eSCy Schubert TEST_info("skipped: EC key type unsupported for curve %s", 213*e0c4386eSCy Schubert OBJ_nid2sn(nid)); 214*e0c4386eSCy Schubert return 1; 215*e0c4386eSCy Schubert } else if (nid != NID_sm2 && as == EVP_PKEY_SM2) { 216*e0c4386eSCy Schubert TEST_info("skipped: SM2 key type unsupported for curve %s", 217*e0c4386eSCy Schubert OBJ_nid2sn(nid)); 218*e0c4386eSCy Schubert return 1; 219*e0c4386eSCy Schubert } 220*e0c4386eSCy Schubert 221*e0c4386eSCy Schubert TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid), 222*e0c4386eSCy Schubert as == EVP_PKEY_EC ? "EC" : "SM2"); 223*e0c4386eSCy Schubert 224*e0c4386eSCy Schubert if (!TEST_ptr(mctx = EVP_MD_CTX_new()) 225*e0c4386eSCy Schubert /* get some random message data */ 226*e0c4386eSCy Schubert || !TEST_int_gt(RAND_bytes(tbs, sizeof(tbs)), 0) 227*e0c4386eSCy Schubert /* real key */ 228*e0c4386eSCy Schubert || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid)) 229*e0c4386eSCy Schubert || !TEST_true(EC_KEY_generate_key(eckey)) 230*e0c4386eSCy Schubert || !TEST_ptr(pkey = EVP_PKEY_new()) 231*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)) 232*e0c4386eSCy Schubert /* fake key for negative testing */ 233*e0c4386eSCy Schubert || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid)) 234*e0c4386eSCy Schubert || !TEST_true(EC_KEY_generate_key(eckey_neg)) 235*e0c4386eSCy Schubert || !TEST_ptr(pkey_neg = EVP_PKEY_new()) 236*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg, NULL)) 237*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg))) 238*e0c4386eSCy Schubert goto err; 239*e0c4386eSCy Schubert 240*e0c4386eSCy Schubert if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)) 241*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1)) 242*e0c4386eSCy Schubert goto err; 243*e0c4386eSCy Schubert 244*e0c4386eSCy Schubert temp = ECDSA_size(eckey); 245*e0c4386eSCy Schubert 246*e0c4386eSCy Schubert if (!TEST_int_ge(temp, 0) 247*e0c4386eSCy Schubert || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp)) 248*e0c4386eSCy Schubert /* create a signature */ 249*e0c4386eSCy Schubert || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey)) 250*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) 251*e0c4386eSCy Schubert || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs))) 252*e0c4386eSCy Schubert || !TEST_int_le(sig_len, ECDSA_size(eckey)) 253*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx)) 254*e0c4386eSCy Schubert /* negative test, verify with wrong key, 0 return */ 255*e0c4386eSCy Schubert || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg)) 256*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg)) 257*e0c4386eSCy Schubert || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0) 258*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx)) 259*e0c4386eSCy Schubert /* negative test, verify with wrong signature length, -1 return */ 260*e0c4386eSCy Schubert || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) 261*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) 262*e0c4386eSCy Schubert || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1) 263*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx)) 264*e0c4386eSCy Schubert /* positive test, verify with correct key, 1 return */ 265*e0c4386eSCy Schubert || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) 266*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) 267*e0c4386eSCy Schubert || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) 268*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx))) 269*e0c4386eSCy Schubert goto err; 270*e0c4386eSCy Schubert 271*e0c4386eSCy Schubert /* muck with the message, test it fails with 0 return */ 272*e0c4386eSCy Schubert tbs[0] ^= 1; 273*e0c4386eSCy Schubert if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) 274*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) 275*e0c4386eSCy Schubert || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0) 276*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx))) 277*e0c4386eSCy Schubert goto err; 278*e0c4386eSCy Schubert /* un-muck and test it verifies */ 279*e0c4386eSCy Schubert tbs[0] ^= 1; 280*e0c4386eSCy Schubert if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) 281*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) 282*e0c4386eSCy Schubert || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) 283*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx))) 284*e0c4386eSCy Schubert goto err; 285*e0c4386eSCy Schubert 286*e0c4386eSCy Schubert /*- 287*e0c4386eSCy Schubert * Muck with the ECDSA signature. The DER encoding is one of: 288*e0c4386eSCy Schubert * - 30 LL 02 .. 289*e0c4386eSCy Schubert * - 30 81 LL 02 .. 290*e0c4386eSCy Schubert * 291*e0c4386eSCy Schubert * - Sometimes this mucks with the high level DER sequence wrapper: 292*e0c4386eSCy Schubert * in that case, DER-parsing of the whole signature should fail. 293*e0c4386eSCy Schubert * 294*e0c4386eSCy Schubert * - Sometimes this mucks with the DER-encoding of ECDSA.r: 295*e0c4386eSCy Schubert * in that case, DER-parsing of ECDSA.r should fail. 296*e0c4386eSCy Schubert * 297*e0c4386eSCy Schubert * - Sometimes this mucks with the DER-encoding of ECDSA.s: 298*e0c4386eSCy Schubert * in that case, DER-parsing of ECDSA.s should fail. 299*e0c4386eSCy Schubert * 300*e0c4386eSCy Schubert * - Sometimes this mucks with ECDSA.r: 301*e0c4386eSCy Schubert * in that case, the signature verification should fail. 302*e0c4386eSCy Schubert * 303*e0c4386eSCy Schubert * - Sometimes this mucks with ECDSA.s: 304*e0c4386eSCy Schubert * in that case, the signature verification should fail. 305*e0c4386eSCy Schubert * 306*e0c4386eSCy Schubert * The usual case is changing the integer value of ECDSA.r or ECDSA.s. 307*e0c4386eSCy Schubert * Because the ratio of DER overhead to signature bytes is small. 308*e0c4386eSCy Schubert * So most of the time it will be one of the last two cases. 309*e0c4386eSCy Schubert * 310*e0c4386eSCy Schubert * In any case, EVP_PKEY_verify should not return 1 for valid. 311*e0c4386eSCy Schubert */ 312*e0c4386eSCy Schubert offset = tbs[0] % sig_len; 313*e0c4386eSCy Schubert dirt = tbs[1] ? tbs[1] : 1; 314*e0c4386eSCy Schubert sig[offset] ^= dirt; 315*e0c4386eSCy Schubert if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) 316*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) 317*e0c4386eSCy Schubert || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) 318*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx))) 319*e0c4386eSCy Schubert goto err; 320*e0c4386eSCy Schubert /* un-muck and test it verifies */ 321*e0c4386eSCy Schubert sig[offset] ^= dirt; 322*e0c4386eSCy Schubert if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) 323*e0c4386eSCy Schubert || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) 324*e0c4386eSCy Schubert || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) 325*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_reset(mctx))) 326*e0c4386eSCy Schubert goto err; 327*e0c4386eSCy Schubert 328*e0c4386eSCy Schubert ret = 1; 329*e0c4386eSCy Schubert err: 330*e0c4386eSCy Schubert EVP_PKEY_free(pkey); 331*e0c4386eSCy Schubert EVP_PKEY_free(pkey_neg); 332*e0c4386eSCy Schubert EVP_PKEY_free(dup_pk); 333*e0c4386eSCy Schubert EVP_MD_CTX_free(mctx); 334*e0c4386eSCy Schubert OPENSSL_free(sig); 335*e0c4386eSCy Schubert return ret; 336*e0c4386eSCy Schubert } 337*e0c4386eSCy Schubert 338*e0c4386eSCy Schubert static int test_builtin_as_ec(int n) 339*e0c4386eSCy Schubert { 340*e0c4386eSCy Schubert return test_builtin(n, EVP_PKEY_EC); 341*e0c4386eSCy Schubert } 342*e0c4386eSCy Schubert 343*e0c4386eSCy Schubert # ifndef OPENSSL_NO_SM2 344*e0c4386eSCy Schubert static int test_builtin_as_sm2(int n) 345*e0c4386eSCy Schubert { 346*e0c4386eSCy Schubert return test_builtin(n, EVP_PKEY_SM2); 347*e0c4386eSCy Schubert } 348*e0c4386eSCy Schubert # endif 349*e0c4386eSCy Schubert 350*e0c4386eSCy Schubert static int test_ecdsa_sig_NULL(void) 351*e0c4386eSCy Schubert { 352*e0c4386eSCy Schubert int ret; 353*e0c4386eSCy Schubert unsigned int siglen; 354*e0c4386eSCy Schubert unsigned char dgst[128] = { 0 }; 355*e0c4386eSCy Schubert EC_KEY *eckey = NULL; 356*e0c4386eSCy Schubert 357*e0c4386eSCy Schubert ret = TEST_ptr(eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)) 358*e0c4386eSCy Schubert && TEST_int_eq(EC_KEY_generate_key(eckey), 1) 359*e0c4386eSCy Schubert && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen, eckey), 1) 360*e0c4386eSCy Schubert && TEST_int_gt(siglen, 0); 361*e0c4386eSCy Schubert EC_KEY_free(eckey); 362*e0c4386eSCy Schubert return ret; 363*e0c4386eSCy Schubert } 364*e0c4386eSCy Schubert 365*e0c4386eSCy Schubert #endif /* OPENSSL_NO_EC */ 366*e0c4386eSCy Schubert 367*e0c4386eSCy Schubert int setup_tests(void) 368*e0c4386eSCy Schubert { 369*e0c4386eSCy Schubert #ifdef OPENSSL_NO_EC 370*e0c4386eSCy Schubert TEST_note("Elliptic curves are disabled."); 371*e0c4386eSCy Schubert #else 372*e0c4386eSCy Schubert fake_rand = fake_rand_start(NULL); 373*e0c4386eSCy Schubert if (fake_rand == NULL) 374*e0c4386eSCy Schubert return 0; 375*e0c4386eSCy Schubert 376*e0c4386eSCy Schubert /* get a list of all internal curves */ 377*e0c4386eSCy Schubert crv_len = EC_get_builtin_curves(NULL, 0); 378*e0c4386eSCy Schubert if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len)) 379*e0c4386eSCy Schubert || !TEST_true(EC_get_builtin_curves(curves, crv_len))) { 380*e0c4386eSCy Schubert fake_rand_finish(fake_rand); 381*e0c4386eSCy Schubert return 0; 382*e0c4386eSCy Schubert } 383*e0c4386eSCy Schubert ADD_ALL_TESTS(test_builtin_as_ec, crv_len); 384*e0c4386eSCy Schubert ADD_TEST(test_ecdsa_sig_NULL); 385*e0c4386eSCy Schubert # ifndef OPENSSL_NO_SM2 386*e0c4386eSCy Schubert ADD_ALL_TESTS(test_builtin_as_sm2, crv_len); 387*e0c4386eSCy Schubert # endif 388*e0c4386eSCy Schubert ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats)); 389*e0c4386eSCy Schubert #endif 390*e0c4386eSCy Schubert return 1; 391*e0c4386eSCy Schubert } 392*e0c4386eSCy Schubert 393*e0c4386eSCy Schubert void cleanup_tests(void) 394*e0c4386eSCy Schubert { 395*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC 396*e0c4386eSCy Schubert fake_rand_finish(fake_rand); 397*e0c4386eSCy Schubert OPENSSL_free(curves); 398*e0c4386eSCy Schubert #endif 399*e0c4386eSCy Schubert } 400