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