2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
11 #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
16 # include <openssl/evp.h>
17 # include <openssl/bn.h>
18 # include <openssl/ec.h>
19 # include <openssl/rand.h>
20 # include "internal/nelem.h"
21 # include "ecdsatest.h"
23 /* functions to change the RAND_METHOD */
24 static int fbytes(unsigned char *buf, int num);
26 static RAND_METHOD fake_rand;
27 static const RAND_METHOD *old_rand;
28 static int use_fake = 0;
29 static const char *numbers[2];
30 static size_t crv_len = 0;
31 static EC_builtin_curve *curves = NULL;
33 static int change_rand(void)
35 /* save old rand method */
36 if (!TEST_ptr(old_rand = RAND_get_rand_method()))
39 fake_rand = *old_rand;
40 /* use own random function */
41 fake_rand.bytes = fbytes;
42 /* set new RAND_METHOD */
43 if (!TEST_true(RAND_set_rand_method(&fake_rand)))
48 static int restore_rand(void)
50 if (!TEST_true(RAND_set_rand_method(old_rand)))
55 static int fbytes(unsigned char *buf, int num)
58 static int fbytes_counter = 0;
62 return old_rand->bytes(buf, num);
66 if (!TEST_ptr(tmp = BN_new())
67 || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
68 || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter]))
69 /* tmp might need leading zeros so pad it out */
70 || !TEST_int_le(BN_num_bytes(tmp), num)
71 || !TEST_true(BN_bn2binpad(tmp, buf, num)))
74 fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers);
82 * This function hijacks the RNG to feed it the chosen ECDSA key and nonce.
83 * The ECDSA KATs are from:
84 * - the X9.62 draft (4)
87 * It uses the low-level ECDSA_sign_setup instead of EVP to control the RNG.
88 * NB: This is not how applications should use ECDSA; this is only for testing.
90 * Tests the library can successfully:
91 * - generate public keys that matches those KATs
92 * - create ECDSA signatures that match those KATs
93 * - accept those signatures as valid
95 static int x9_62_tests(int n)
97 int nid, md_nid, ret = 0;
98 const char *r_in = NULL, *s_in = NULL, *tbs = NULL;
99 unsigned char *pbuf = NULL, *qbuf = NULL, *message = NULL;
100 unsigned char digest[EVP_MAX_MD_SIZE];
101 unsigned int dgst_len = 0;
102 long q_len, msg_len = 0;
104 EVP_MD_CTX *mctx = NULL;
106 ECDSA_SIG *signature = NULL;
107 BIGNUM *r = NULL, *s = NULL;
108 BIGNUM *kinv = NULL, *rp = NULL;
109 const BIGNUM *sig_r = NULL, *sig_s = NULL;
111 nid = ecdsa_cavs_kats[n].nid;
112 md_nid = ecdsa_cavs_kats[n].md_nid;
113 r_in = ecdsa_cavs_kats[n].r;
114 s_in = ecdsa_cavs_kats[n].s;
115 tbs = ecdsa_cavs_kats[n].msg;
116 numbers[0] = ecdsa_cavs_kats[n].d;
117 numbers[1] = ecdsa_cavs_kats[n].k;
119 TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid));
122 if (EC_curve_nid2nist(nid) == NULL)
123 return TEST_skip("skip non approved curves");
124 #endif /* FIPS_MODE */
126 if (!TEST_ptr(mctx = EVP_MD_CTX_new())
127 /* get the message digest */
128 || !TEST_ptr(message = OPENSSL_hexstr2buf(tbs, &msg_len))
129 || !TEST_true(EVP_DigestInit_ex(mctx, EVP_get_digestbynid(md_nid), NULL))
130 || !TEST_true(EVP_DigestUpdate(mctx, message, msg_len))
131 || !TEST_true(EVP_DigestFinal_ex(mctx, digest, &dgst_len))
133 || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid))
134 /* load KAT variables */
135 || !TEST_ptr(r = BN_new())
136 || !TEST_ptr(s = BN_new())
137 || !TEST_true(BN_hex2bn(&r, r_in))
138 || !TEST_true(BN_hex2bn(&s, s_in))
139 /* swap the RNG source */
140 || !TEST_true(change_rand()))
143 /* public key must match KAT */
145 if (!TEST_true(EC_KEY_generate_key(key))
146 || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED,
148 || !TEST_ptr(qbuf = OPENSSL_hexstr2buf(ecdsa_cavs_kats[n].Q, &q_len))
149 || !TEST_int_eq(q_len, p_len)
150 || !TEST_mem_eq(qbuf, q_len, pbuf, p_len))
153 /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */
155 if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
156 || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
158 /* verify the signature */
159 || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1))
162 /* compare the created signature with the expected signature */
163 ECDSA_SIG_get0(signature, &sig_r, &sig_s);
164 if (!TEST_BN_eq(sig_r, r)
165 || !TEST_BN_eq(sig_s, s))
171 /* restore the RNG source */
172 if (!TEST_true(restore_rand()))
175 OPENSSL_free(message);
179 ECDSA_SIG_free(signature);
182 EVP_MD_CTX_free(mctx);
189 * Positive and negative ECDSA testing through EVP interface:
190 * - EVP_DigestSign (this is the one-shot version)
193 * Tests the library can successfully:
195 * - create a signature
196 * - accept that signature
197 * - reject that signature with a different public key
198 * - reject that signature if its length is not correct
199 * - reject that signature after modifying the message
200 * - accept that signature after un-modifying the message
201 * - reject that signature after modifying the signature
202 * - accept that signature after un-modifying the signature
204 static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey)
206 /* With the SM2 key type, the SM2 ID is mandatory */
207 static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
210 if (!TEST_ptr(pctx = EVP_PKEY_CTX_new(pkey, NULL))
211 || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, sm2_id, sizeof(sm2_id)), 0))
213 EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
217 static int clean_sm2_id(EVP_MD_CTX *mctx)
221 if (!TEST_ptr(pctx = EVP_MD_CTX_pkey_ctx(mctx)))
223 EVP_PKEY_CTX_free(pctx);
227 static int test_builtin(int n, int as)
229 EC_KEY *eckey_neg = NULL, *eckey = NULL;
230 unsigned char dirt, offset, tbs[128];
231 unsigned char *sig = NULL;
232 EVP_PKEY *pkey_neg = NULL, *pkey = NULL;
233 EVP_MD_CTX *mctx = NULL;
240 /* skip built-in curves where ord(G) is not prime */
241 if (nid == NID_ipsec4 || nid == NID_ipsec3) {
242 TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid));
246 TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid),
247 as == EVP_PKEY_EC ? "EC" : "SM2");
249 if (!TEST_ptr(mctx = EVP_MD_CTX_new())
250 /* get some random message data */
251 || !TEST_true(RAND_bytes(tbs, sizeof(tbs)))
253 || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid))
254 || !TEST_true(EC_KEY_generate_key(eckey))
255 || !TEST_ptr(pkey = EVP_PKEY_new())
256 || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))
257 /* fake key for negative testing */
258 || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid))
259 || !TEST_true(EC_KEY_generate_key(eckey_neg))
260 || !TEST_ptr(pkey_neg = EVP_PKEY_new())
261 || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg)))
264 temp = ECDSA_size(eckey);
267 * |as| indicates how we want to treat the key, i.e. what sort of
268 * computation we want to do with it. The two choices are the key
269 * types EVP_PKEY_EC and EVP_PKEY_SM2. It's perfectly possible to
270 * switch back and forth between those two key types, regardless of
271 * curve, even though the default is to have EVP_PKEY_SM2 for the
272 * SM2 curve and EVP_PKEY_EC for all other curves.
274 if (!TEST_true(EVP_PKEY_set_alias_type(pkey, as))
275 || !TEST_true(EVP_PKEY_set_alias_type(pkey_neg, as)))
278 if (!TEST_int_ge(temp, 0)
279 || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp))
280 /* create a signature */
281 || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
282 || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey))
283 || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs)))
284 || !TEST_int_le(sig_len, ECDSA_size(eckey))
285 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
286 || !TEST_true(EVP_MD_CTX_reset(mctx))
287 /* negative test, verify with wrong key, 0 return */
288 || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg))
289 || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg))
290 || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
291 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
292 || !TEST_true(EVP_MD_CTX_reset(mctx))
293 /* negative test, verify with wrong signature length, -1 return */
294 || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
295 || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
296 || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1)
297 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
298 || !TEST_true(EVP_MD_CTX_reset(mctx))
299 /* positive test, verify with correct key, 1 return */
300 || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
301 || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
302 || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
303 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
304 || !TEST_true(EVP_MD_CTX_reset(mctx)))
307 /* muck with the message, test it fails with 0 return */
309 if ((as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
310 || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
311 || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
312 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
313 || !TEST_true(EVP_MD_CTX_reset(mctx)))
315 /* un-muck and test it verifies */
317 if ((as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
318 || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
319 || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
320 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
321 || !TEST_true(EVP_MD_CTX_reset(mctx)))
325 * Muck with the ECDSA signature. The DER encoding is one of:
329 * - Sometimes this mucks with the high level DER sequence wrapper:
330 * in that case, DER-parsing of the whole signature should fail.
332 * - Sometimes this mucks with the DER-encoding of ECDSA.r:
333 * in that case, DER-parsing of ECDSA.r should fail.
335 * - Sometimes this mucks with the DER-encoding of ECDSA.s:
336 * in that case, DER-parsing of ECDSA.s should fail.
338 * - Sometimes this mucks with ECDSA.r:
339 * in that case, the signature verification should fail.
341 * - Sometimes this mucks with ECDSA.s:
342 * in that case, the signature verification should fail.
344 * The usual case is changing the integer value of ECDSA.r or ECDSA.s.
345 * Because the ratio of DER overhead to signature bytes is small.
346 * So most of the time it will be one of the last two cases.
348 * In any case, EVP_PKEY_verify should not return 1 for valid.
350 offset = tbs[0] % sig_len;
351 dirt = tbs[1] ? tbs[1] : 1;
353 if ((as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
354 || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
355 || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
356 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
357 || !TEST_true(EVP_MD_CTX_reset(mctx)))
359 /* un-muck and test it verifies */
361 if ((as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
362 || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
363 || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
364 || (as == EVP_PKEY_SM2 && !clean_sm2_id(mctx))
365 || !TEST_true(EVP_MD_CTX_reset(mctx)))
371 EVP_PKEY_free(pkey_neg);
372 EVP_MD_CTX_free(mctx);
377 static int test_builtin_as_ec(int n)
379 return test_builtin(n, EVP_PKEY_EC);
382 static int test_builtin_as_sm2(int n)
384 return test_builtin(n, EVP_PKEY_SM2);
388 int setup_tests(void)
391 TEST_note("Elliptic curves are disabled.");
393 /* get a list of all internal curves */
394 crv_len = EC_get_builtin_curves(NULL, 0);
395 if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
396 || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
398 ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
399 ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
400 ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats));
405 void cleanup_tests(void)
407 #ifndef OPENSSL_NO_EC
408 OPENSSL_free(curves);