2 * Copyright 2002-2020 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
12 * Low level APIs are deprecated for public use, but still ok for internal use.
14 #include "internal/deprecated.h"
16 #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
21 # include <openssl/evp.h>
22 # include <openssl/bn.h>
23 # include <openssl/ec.h>
24 # include <openssl/rand.h>
25 # include "internal/nelem.h"
26 # include "ecdsatest.h"
28 /* functions to change the RAND_METHOD */
29 static int fbytes(unsigned char *buf
, int num
);
31 static RAND_METHOD fake_rand
;
32 static const RAND_METHOD
*old_rand
;
33 static int use_fake
= 0;
34 static const char *numbers
[2];
35 static size_t crv_len
= 0;
36 static EC_builtin_curve
*curves
= NULL
;
38 static int change_rand(void)
40 /* save old rand method */
41 if (!TEST_ptr(old_rand
= RAND_get_rand_method()))
44 fake_rand
= *old_rand
;
45 /* use own random function */
46 fake_rand
.bytes
= fbytes
;
47 /* set new RAND_METHOD */
48 if (!TEST_true(RAND_set_rand_method(&fake_rand
)))
53 static int restore_rand(void)
55 if (!TEST_true(RAND_set_rand_method(old_rand
)))
60 static int fbytes(unsigned char *buf
, int num
)
63 static int fbytes_counter
= 0;
67 return old_rand
->bytes(buf
, num
);
71 if (!TEST_ptr(tmp
= BN_new())
72 || !TEST_int_lt(fbytes_counter
, OSSL_NELEM(numbers
))
73 || !TEST_true(BN_hex2bn(&tmp
, numbers
[fbytes_counter
]))
74 /* tmp might need leading zeros so pad it out */
75 || !TEST_int_le(BN_num_bytes(tmp
), num
)
76 || !TEST_true(BN_bn2binpad(tmp
, buf
, num
)))
79 fbytes_counter
= (fbytes_counter
+ 1) % OSSL_NELEM(numbers
);
87 * This function hijacks the RNG to feed it the chosen ECDSA key and nonce.
88 * The ECDSA KATs are from:
89 * - the X9.62 draft (4)
92 * It uses the low-level ECDSA_sign_setup instead of EVP to control the RNG.
93 * NB: This is not how applications should use ECDSA; this is only for testing.
95 * Tests the library can successfully:
96 * - generate public keys that matches those KATs
97 * - create ECDSA signatures that match those KATs
98 * - accept those signatures as valid
100 static int x9_62_tests(int n
)
102 int nid
, md_nid
, ret
= 0;
103 const char *r_in
= NULL
, *s_in
= NULL
, *tbs
= NULL
;
104 unsigned char *pbuf
= NULL
, *qbuf
= NULL
, *message
= NULL
;
105 unsigned char digest
[EVP_MAX_MD_SIZE
];
106 unsigned int dgst_len
= 0;
107 long q_len
, msg_len
= 0;
109 EVP_MD_CTX
*mctx
= NULL
;
111 ECDSA_SIG
*signature
= NULL
;
112 BIGNUM
*r
= NULL
, *s
= NULL
;
113 BIGNUM
*kinv
= NULL
, *rp
= NULL
;
114 const BIGNUM
*sig_r
= NULL
, *sig_s
= NULL
;
116 nid
= ecdsa_cavs_kats
[n
].nid
;
117 md_nid
= ecdsa_cavs_kats
[n
].md_nid
;
118 r_in
= ecdsa_cavs_kats
[n
].r
;
119 s_in
= ecdsa_cavs_kats
[n
].s
;
120 tbs
= ecdsa_cavs_kats
[n
].msg
;
121 numbers
[0] = ecdsa_cavs_kats
[n
].d
;
122 numbers
[1] = ecdsa_cavs_kats
[n
].k
;
124 TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid
));
127 if (EC_curve_nid2nist(nid
) == NULL
)
128 return TEST_skip("skip non approved curves");
129 #endif /* FIPS_MODULE */
131 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())
132 /* get the message digest */
133 || !TEST_ptr(message
= OPENSSL_hexstr2buf(tbs
, &msg_len
))
134 || !TEST_true(EVP_DigestInit_ex(mctx
, EVP_get_digestbynid(md_nid
), NULL
))
135 || !TEST_true(EVP_DigestUpdate(mctx
, message
, msg_len
))
136 || !TEST_true(EVP_DigestFinal_ex(mctx
, digest
, &dgst_len
))
138 || !TEST_ptr(key
= EC_KEY_new_by_curve_name(nid
))
139 /* load KAT variables */
140 || !TEST_ptr(r
= BN_new())
141 || !TEST_ptr(s
= BN_new())
142 || !TEST_true(BN_hex2bn(&r
, r_in
))
143 || !TEST_true(BN_hex2bn(&s
, s_in
))
144 /* swap the RNG source */
145 || !TEST_true(change_rand()))
148 /* public key must match KAT */
150 if (!TEST_true(EC_KEY_generate_key(key
))
151 || !TEST_true(p_len
= EC_KEY_key2buf(key
, POINT_CONVERSION_UNCOMPRESSED
,
153 || !TEST_ptr(qbuf
= OPENSSL_hexstr2buf(ecdsa_cavs_kats
[n
].Q
, &q_len
))
154 || !TEST_int_eq(q_len
, p_len
)
155 || !TEST_mem_eq(qbuf
, q_len
, pbuf
, p_len
))
158 /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */
160 if (!TEST_true(ECDSA_sign_setup(key
, NULL
, &kinv
, &rp
))
161 || !TEST_ptr(signature
= ECDSA_do_sign_ex(digest
, dgst_len
,
163 /* verify the signature */
164 || !TEST_int_eq(ECDSA_do_verify(digest
, dgst_len
, signature
, key
), 1))
167 /* compare the created signature with the expected signature */
168 ECDSA_SIG_get0(signature
, &sig_r
, &sig_s
);
169 if (!TEST_BN_eq(sig_r
, r
)
170 || !TEST_BN_eq(sig_s
, s
))
176 /* restore the RNG source */
177 if (!TEST_true(restore_rand()))
180 OPENSSL_free(message
);
184 ECDSA_SIG_free(signature
);
187 EVP_MD_CTX_free(mctx
);
194 * Positive and negative ECDSA testing through EVP interface:
195 * - EVP_DigestSign (this is the one-shot version)
198 * Tests the library can successfully:
200 * - create a signature
201 * - accept that signature
202 * - reject that signature with a different public key
203 * - reject that signature if its length is not correct
204 * - reject that signature after modifying the message
205 * - accept that signature after un-modifying the message
206 * - reject that signature after modifying the signature
207 * - accept that signature after un-modifying the signature
209 static int set_sm2_id(EVP_MD_CTX
*mctx
, EVP_PKEY
*pkey
)
211 /* With the SM2 key type, the SM2 ID is mandatory */
212 static const char sm2_id
[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
215 if (!TEST_ptr(pctx
= EVP_MD_CTX_pkey_ctx(mctx
))
216 || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx
, sm2_id
, sizeof(sm2_id
)), 0))
221 static int test_builtin(int n
, int as
)
223 EC_KEY
*eckey_neg
= NULL
, *eckey
= NULL
;
224 unsigned char dirt
, offset
, tbs
[128];
225 unsigned char *sig
= NULL
;
226 EVP_PKEY
*pkey_neg
= NULL
, *pkey
= NULL
;
227 EVP_MD_CTX
*mctx
= NULL
;
234 /* skip built-in curves where ord(G) is not prime */
235 if (nid
== NID_ipsec4
|| nid
== NID_ipsec3
) {
236 TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid
));
241 * skip SM2 curve if 'as' is equal to EVP_PKEY_EC or, skip all curves
242 * except SM2 curve if 'as' is equal to EVP_PKEY_SM2
244 if (nid
== NID_sm2
&& as
== EVP_PKEY_EC
) {
245 TEST_info("skipped: EC key type unsupported for curve %s",
248 } else if (nid
!= NID_sm2
&& as
== EVP_PKEY_SM2
) {
249 TEST_info("skipped: SM2 key type unsupported for curve %s",
254 TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid
),
255 as
== EVP_PKEY_EC
? "EC" : "SM2");
257 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())
258 /* get some random message data */
259 || !TEST_true(RAND_bytes(tbs
, sizeof(tbs
)))
261 || !TEST_ptr(eckey
= EC_KEY_new_by_curve_name(nid
))
262 || !TEST_true(EC_KEY_generate_key(eckey
))
263 || !TEST_ptr(pkey
= EVP_PKEY_new())
264 || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey
, eckey
))
265 /* fake key for negative testing */
266 || !TEST_ptr(eckey_neg
= EC_KEY_new_by_curve_name(nid
))
267 || !TEST_true(EC_KEY_generate_key(eckey_neg
))
268 || !TEST_ptr(pkey_neg
= EVP_PKEY_new())
269 || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg
, NULL
))
270 || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg
, eckey_neg
)))
273 temp
= ECDSA_size(eckey
);
276 * |as| indicates how we want to treat the key, i.e. what sort of
277 * computation we want to do with it. The two choices are the key
278 * types EVP_PKEY_EC and EVP_PKEY_SM2. It's perfectly possible to
279 * switch back and forth between those two key types, regardless of
280 * curve, even though the default is to have EVP_PKEY_SM2 for the
281 * SM2 curve and EVP_PKEY_EC for all other curves.
283 if (!TEST_true(EVP_PKEY_set_alias_type(pkey
, as
))
284 || !TEST_true(EVP_PKEY_set_alias_type(pkey_neg
, as
)))
287 if (!TEST_int_ge(temp
, 0)
288 || !TEST_ptr(sig
= OPENSSL_malloc(sig_len
= (size_t)temp
))
289 /* create a signature */
290 || !TEST_true(EVP_DigestSignInit(mctx
, NULL
, NULL
, NULL
, pkey
))
291 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey
))
292 || !TEST_true(EVP_DigestSign(mctx
, sig
, &sig_len
, tbs
, sizeof(tbs
)))
293 || !TEST_int_le(sig_len
, ECDSA_size(eckey
))
294 || !TEST_true(EVP_MD_CTX_reset(mctx
))
295 /* negative test, verify with wrong key, 0 return */
296 || !TEST_true(EVP_DigestVerifyInit(mctx
, NULL
, NULL
, NULL
, pkey_neg
))
297 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey_neg
))
298 || !TEST_int_eq(EVP_DigestVerify(mctx
, sig
, sig_len
, tbs
, sizeof(tbs
)), 0)
299 || !TEST_true(EVP_MD_CTX_reset(mctx
))
300 /* negative test, verify with wrong signature length, -1 return */
301 || !TEST_true(EVP_DigestVerifyInit(mctx
, NULL
, NULL
, NULL
, pkey
))
302 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey
))
303 || !TEST_int_eq(EVP_DigestVerify(mctx
, sig
, sig_len
- 1, tbs
, sizeof(tbs
)), -1)
304 || !TEST_true(EVP_MD_CTX_reset(mctx
))
305 /* positive test, verify with correct key, 1 return */
306 || !TEST_true(EVP_DigestVerifyInit(mctx
, NULL
, NULL
, NULL
, pkey
))
307 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey
))
308 || !TEST_int_eq(EVP_DigestVerify(mctx
, sig
, sig_len
, tbs
, sizeof(tbs
)), 1)
309 || !TEST_true(EVP_MD_CTX_reset(mctx
)))
312 /* muck with the message, test it fails with 0 return */
314 if (!TEST_true(EVP_DigestVerifyInit(mctx
, NULL
, NULL
, NULL
, pkey
))
315 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey
))
316 || !TEST_int_eq(EVP_DigestVerify(mctx
, sig
, sig_len
, tbs
, sizeof(tbs
)), 0)
317 || !TEST_true(EVP_MD_CTX_reset(mctx
)))
319 /* un-muck and test it verifies */
321 if (!TEST_true(EVP_DigestVerifyInit(mctx
, NULL
, NULL
, NULL
, pkey
))
322 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey
))
323 || !TEST_int_eq(EVP_DigestVerify(mctx
, sig
, sig_len
, tbs
, sizeof(tbs
)), 1)
324 || !TEST_true(EVP_MD_CTX_reset(mctx
)))
328 * Muck with the ECDSA signature. The DER encoding is one of:
332 * - Sometimes this mucks with the high level DER sequence wrapper:
333 * in that case, DER-parsing of the whole signature should fail.
335 * - Sometimes this mucks with the DER-encoding of ECDSA.r:
336 * in that case, DER-parsing of ECDSA.r should fail.
338 * - Sometimes this mucks with the DER-encoding of ECDSA.s:
339 * in that case, DER-parsing of ECDSA.s should fail.
341 * - Sometimes this mucks with ECDSA.r:
342 * in that case, the signature verification should fail.
344 * - Sometimes this mucks with ECDSA.s:
345 * in that case, the signature verification should fail.
347 * The usual case is changing the integer value of ECDSA.r or ECDSA.s.
348 * Because the ratio of DER overhead to signature bytes is small.
349 * So most of the time it will be one of the last two cases.
351 * In any case, EVP_PKEY_verify should not return 1 for valid.
353 offset
= tbs
[0] % sig_len
;
354 dirt
= tbs
[1] ? tbs
[1] : 1;
356 if (!TEST_true(EVP_DigestVerifyInit(mctx
, NULL
, NULL
, NULL
, pkey
))
357 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey
))
358 || !TEST_int_ne(EVP_DigestVerify(mctx
, sig
, sig_len
, tbs
, sizeof(tbs
)), 1)
359 || !TEST_true(EVP_MD_CTX_reset(mctx
)))
361 /* un-muck and test it verifies */
363 if (!TEST_true(EVP_DigestVerifyInit(mctx
, NULL
, NULL
, NULL
, pkey
))
364 || (as
== EVP_PKEY_SM2
&& !set_sm2_id(mctx
, pkey
))
365 || !TEST_int_eq(EVP_DigestVerify(mctx
, sig
, sig_len
, tbs
, sizeof(tbs
)), 1)
366 || !TEST_true(EVP_MD_CTX_reset(mctx
)))
372 EVP_PKEY_free(pkey_neg
);
373 EVP_MD_CTX_free(mctx
);
378 static int test_builtin_as_ec(int n
)
380 return test_builtin(n
, EVP_PKEY_EC
);
383 # ifndef OPENSSL_NO_SM2
384 static int test_builtin_as_sm2(int n
)
386 return test_builtin(n
, EVP_PKEY_SM2
);
389 #endif /* OPENSSL_NO_EC */
391 int setup_tests(void)
394 TEST_note("Elliptic curves are disabled.");
396 /* get a list of all internal curves */
397 crv_len
= EC_get_builtin_curves(NULL
, 0);
398 if (!TEST_ptr(curves
= OPENSSL_malloc(sizeof(*curves
) * crv_len
))
399 || !TEST_true(EC_get_builtin_curves(curves
, crv_len
)))
401 ADD_ALL_TESTS(test_builtin_as_ec
, crv_len
);
402 # ifndef OPENSSL_NO_SM2
403 ADD_ALL_TESTS(test_builtin_as_sm2
, crv_len
);
405 ADD_ALL_TESTS(x9_62_tests
, OSSL_NELEM(ecdsa_cavs_kats
));
410 void cleanup_tests(void)
412 #ifndef OPENSSL_NO_EC
413 OPENSSL_free(curves
);