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