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