| 
									
										
										
										
											2002-12-05 01:38:40 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-03-11 21:27:36 +08:00
										 |  |  |  * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2017-06-15 22:16:46 +08:00
										 |  |  |  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved | 
					
						
							| 
									
										
										
										
											2002-02-14 02:21:51 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:05:25 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:20:24 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											2002-02-14 02:21:51 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-05-18 02:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-28 13:14:18 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Low level APIs are deprecated for public use, but still ok for internal use. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include "internal/deprecated.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-11 07:09:27 +08:00
										 |  |  | #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
 | 
					
						
							| 
									
										
										
										
											2018-10-08 05:14:08 +08:00
										 |  |  | #include "testutil.h"
 | 
					
						
							| 
									
										
										
										
											2003-03-21 07:22:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 07:35:08 +08:00
										 |  |  | #ifndef OPENSSL_NO_EC
 | 
					
						
							| 
									
										
										
										
											2002-02-14 02:21:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | # include <openssl/evp.h>
 | 
					
						
							|  |  |  | # include <openssl/bn.h>
 | 
					
						
							| 
									
										
										
										
											2015-10-28 03:39:32 +08:00
										 |  |  | # include <openssl/ec.h>
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | # include <openssl/rand.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  | # include "internal/nelem.h"
 | 
					
						
							|  |  |  | # include "ecdsatest.h"
 | 
					
						
							| 
									
										
										
										
											2002-03-14 17:52:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 09:57:13 +08:00
										 |  |  | static fake_random_generate_cb fbytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  | static const char *numbers[2]; | 
					
						
							|  |  |  | static size_t crv_len = 0; | 
					
						
							|  |  |  | static EC_builtin_curve *curves = NULL; | 
					
						
							| 
									
										
										
										
											2021-02-17 09:55:13 +08:00
										 |  |  | static OSSL_PROVIDER *fake_rand = NULL; | 
					
						
							| 
									
										
										
										
											2002-12-05 01:38:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 09:57:13 +08:00
										 |  |  | static int fbytes(unsigned char *buf, size_t num, ossl_unused const char *name, | 
					
						
							|  |  |  |                   EVP_RAND_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-04-19 02:34:43 +08:00
										 |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     static int fbytes_counter = 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     BIGNUM *tmp = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 09:57:13 +08:00
										 |  |  |     fake_rand_set_callback(ctx, NULL); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     if (!TEST_ptr(tmp = BN_new()) | 
					
						
							|  |  |  |         || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers)) | 
					
						
							|  |  |  |         || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter])) | 
					
						
							|  |  |  |         /* tmp might need leading zeros so pad it out */ | 
					
						
							|  |  |  |         || !TEST_int_le(BN_num_bytes(tmp), num) | 
					
						
							| 
									
										
										
										
											2021-11-01 15:00:54 +08:00
										 |  |  |         || !TEST_int_gt(BN_bn2binpad(tmp, buf, num), 0)) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers); | 
					
						
							|  |  |  |     ret = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     BN_free(tmp); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2002-12-05 01:38:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  | /*-
 | 
					
						
							|  |  |  |  * This function hijacks the RNG to feed it the chosen ECDSA key and nonce. | 
					
						
							|  |  |  |  * The ECDSA KATs are from: | 
					
						
							|  |  |  |  * - the X9.62 draft (4) | 
					
						
							|  |  |  |  * - NIST CAVP (720) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * It uses the low-level ECDSA_sign_setup instead of EVP to control the RNG. | 
					
						
							|  |  |  |  * NB: This is not how applications should use ECDSA; this is only for testing. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Tests the library can successfully: | 
					
						
							|  |  |  |  * - generate public keys that matches those KATs | 
					
						
							|  |  |  |  * - create ECDSA signatures that match those KATs | 
					
						
							|  |  |  |  * - accept those signatures as valid | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int x9_62_tests(int n) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     int nid, md_nid, ret = 0; | 
					
						
							|  |  |  |     const char *r_in = NULL, *s_in = NULL, *tbs = NULL; | 
					
						
							|  |  |  |     unsigned char *pbuf = NULL, *qbuf = NULL, *message = NULL; | 
					
						
							|  |  |  |     unsigned char digest[EVP_MAX_MD_SIZE]; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     unsigned int dgst_len = 0; | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     long q_len, msg_len = 0; | 
					
						
							|  |  |  |     size_t p_len; | 
					
						
							|  |  |  |     EVP_MD_CTX *mctx = NULL; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     EC_KEY *key = NULL; | 
					
						
							|  |  |  |     ECDSA_SIG *signature = NULL; | 
					
						
							|  |  |  |     BIGNUM *r = NULL, *s = NULL; | 
					
						
							|  |  |  |     BIGNUM *kinv = NULL, *rp = NULL; | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     const BIGNUM *sig_r = NULL, *sig_s = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     nid = ecdsa_cavs_kats[n].nid; | 
					
						
							|  |  |  |     md_nid = ecdsa_cavs_kats[n].md_nid; | 
					
						
							|  |  |  |     r_in = ecdsa_cavs_kats[n].r; | 
					
						
							|  |  |  |     s_in = ecdsa_cavs_kats[n].s; | 
					
						
							|  |  |  |     tbs = ecdsa_cavs_kats[n].msg; | 
					
						
							|  |  |  |     numbers[0] = ecdsa_cavs_kats[n].d; | 
					
						
							|  |  |  |     numbers[1] = ecdsa_cavs_kats[n].k; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-14 04:34:56 +08:00
										 |  |  | #ifdef FIPS_MODULE
 | 
					
						
							| 
									
										
										
										
											2019-06-05 06:24:16 +08:00
										 |  |  |     if (EC_curve_nid2nist(nid) == NULL) | 
					
						
							|  |  |  |         return TEST_skip("skip non approved curves"); | 
					
						
							| 
									
										
										
										
											2020-04-14 04:34:56 +08:00
										 |  |  | #endif /* FIPS_MODULE */
 | 
					
						
							| 
									
										
										
										
											2019-06-05 06:24:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     if (!TEST_ptr(mctx = EVP_MD_CTX_new()) | 
					
						
							|  |  |  |         /* get the message digest */ | 
					
						
							|  |  |  |         || !TEST_ptr(message = OPENSSL_hexstr2buf(tbs, &msg_len)) | 
					
						
							|  |  |  |         || !TEST_true(EVP_DigestInit_ex(mctx, EVP_get_digestbynid(md_nid), NULL)) | 
					
						
							|  |  |  |         || !TEST_true(EVP_DigestUpdate(mctx, message, msg_len)) | 
					
						
							|  |  |  |         || !TEST_true(EVP_DigestFinal_ex(mctx, digest, &dgst_len)) | 
					
						
							|  |  |  |         /* create the key */ | 
					
						
							|  |  |  |         || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid)) | 
					
						
							|  |  |  |         /* load KAT variables */ | 
					
						
							|  |  |  |         || !TEST_ptr(r = BN_new()) | 
					
						
							|  |  |  |         || !TEST_ptr(s = BN_new()) | 
					
						
							|  |  |  |         || !TEST_true(BN_hex2bn(&r, r_in)) | 
					
						
							| 
									
										
										
										
											2021-02-17 09:55:13 +08:00
										 |  |  |         || !TEST_true(BN_hex2bn(&s, s_in))) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* public key must match KAT */ | 
					
						
							| 
									
										
										
										
											2021-02-27 09:57:13 +08:00
										 |  |  |     fake_rand_set_callback(RAND_get0_private(NULL), &fbytes); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     if (!TEST_true(EC_KEY_generate_key(key)) | 
					
						
							|  |  |  |         || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED, | 
					
						
							|  |  |  |                                              &pbuf, NULL)) | 
					
						
							|  |  |  |         || !TEST_ptr(qbuf = OPENSSL_hexstr2buf(ecdsa_cavs_kats[n].Q, &q_len)) | 
					
						
							|  |  |  |         || !TEST_int_eq(q_len, p_len) | 
					
						
							|  |  |  |         || !TEST_mem_eq(qbuf, q_len, pbuf, p_len)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */ | 
					
						
							| 
									
										
										
										
											2021-02-27 09:57:13 +08:00
										 |  |  |     fake_rand_set_callback(RAND_get0_private(NULL), &fbytes); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)) | 
					
						
							|  |  |  |         || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len, | 
					
						
							|  |  |  |                                                   kinv, rp, key)) | 
					
						
							|  |  |  |         /* verify the signature */ | 
					
						
							|  |  |  |         || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2017-04-19 02:34:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* compare the created signature with the expected signature */ | 
					
						
							| 
									
										
										
										
											2016-06-10 05:09:48 +08:00
										 |  |  |     ECDSA_SIG_get0(signature, &sig_r, &sig_s); | 
					
						
							| 
									
										
										
										
											2017-05-08 10:09:41 +08:00
										 |  |  |     if (!TEST_BN_eq(sig_r, r) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_BN_eq(sig_s, s)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ret = 1; | 
					
						
							| 
									
										
										
										
											2017-04-19 02:34:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |  err: | 
					
						
							|  |  |  |     OPENSSL_free(message); | 
					
						
							|  |  |  |     OPENSSL_free(pbuf); | 
					
						
							|  |  |  |     OPENSSL_free(qbuf); | 
					
						
							| 
									
										
										
										
											2015-03-26 06:35:24 +08:00
										 |  |  |     EC_KEY_free(key); | 
					
						
							| 
									
										
										
										
											2015-05-02 02:37:16 +08:00
										 |  |  |     ECDSA_SIG_free(signature); | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     BN_free(r); | 
					
						
							|  |  |  |     BN_free(s); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     EVP_MD_CTX_free(mctx); | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     BN_clear_free(kinv); | 
					
						
							|  |  |  |     BN_clear_free(rp); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2002-02-14 02:21:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  | /*-
 | 
					
						
							|  |  |  |  * Positive and negative ECDSA testing through EVP interface: | 
					
						
							|  |  |  |  * - EVP_DigestSign (this is the one-shot version) | 
					
						
							|  |  |  |  * - EVP_DigestVerify | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Tests the library can successfully: | 
					
						
							|  |  |  |  * - create a key | 
					
						
							|  |  |  |  * - create a signature | 
					
						
							|  |  |  |  * - accept that signature | 
					
						
							|  |  |  |  * - reject that signature with a different public key | 
					
						
							|  |  |  |  * - reject that signature if its length is not correct | 
					
						
							|  |  |  |  * - reject that signature after modifying the message | 
					
						
							|  |  |  |  * - accept that signature after un-modifying the message | 
					
						
							|  |  |  |  * - reject that signature after modifying the signature | 
					
						
							|  |  |  |  * - accept that signature after un-modifying the signature | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  | static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* With the SM2 key type, the SM2 ID is mandatory */ | 
					
						
							|  |  |  |     static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' }; | 
					
						
							|  |  |  |     EVP_PKEY_CTX *pctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |     if (!TEST_ptr(pctx = EVP_MD_CTX_get_pkey_ctx(mctx)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, sm2_id, sizeof(sm2_id)), 0)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int test_builtin(int n, int as) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     EC_KEY *eckey_neg = NULL, *eckey = NULL; | 
					
						
							|  |  |  |     unsigned char dirt, offset, tbs[128]; | 
					
						
							|  |  |  |     unsigned char *sig = NULL; | 
					
						
							| 
									
										
										
										
											2021-03-20 01:45:43 +08:00
										 |  |  |     EVP_PKEY *pkey_neg = NULL, *pkey = NULL, *dup_pk = NULL; | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     EVP_MD_CTX *mctx = NULL; | 
					
						
							|  |  |  |     size_t sig_len; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     int nid, ret = 0; | 
					
						
							| 
									
										
										
										
											2019-03-04 11:37:39 +08:00
										 |  |  |     int temp; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     nid = curves[n].nid; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     /* skip built-in curves where ord(G) is not prime */ | 
					
						
							|  |  |  |     if (nid == NID_ipsec4 || nid == NID_ipsec3) { | 
					
						
							|  |  |  |         TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid)); | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 23:25:49 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * skip SM2 curve if 'as' is equal to EVP_PKEY_EC or, skip all curves | 
					
						
							|  |  |  |      * except SM2 curve if 'as' is equal to EVP_PKEY_SM2 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (nid == NID_sm2 && as == EVP_PKEY_EC) { | 
					
						
							|  |  |  |         TEST_info("skipped: EC key type unsupported for curve %s", | 
					
						
							|  |  |  |                   OBJ_nid2sn(nid)); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } else if (nid != NID_sm2 && as == EVP_PKEY_SM2) { | 
					
						
							|  |  |  |         TEST_info("skipped: SM2 key type unsupported for curve %s", | 
					
						
							|  |  |  |                   OBJ_nid2sn(nid)); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |     TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid), | 
					
						
							|  |  |  |               as == EVP_PKEY_EC ? "EC" : "SM2"); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(mctx = EVP_MD_CTX_new()) | 
					
						
							|  |  |  |         /* get some random message data */ | 
					
						
							|  |  |  |         || !TEST_true(RAND_bytes(tbs, sizeof(tbs))) | 
					
						
							|  |  |  |         /* real key */ | 
					
						
							|  |  |  |         || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid)) | 
					
						
							|  |  |  |         || !TEST_true(EC_KEY_generate_key(eckey)) | 
					
						
							|  |  |  |         || !TEST_ptr(pkey = EVP_PKEY_new()) | 
					
						
							|  |  |  |         || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)) | 
					
						
							|  |  |  |         /* fake key for negative testing */ | 
					
						
							|  |  |  |         || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid)) | 
					
						
							|  |  |  |         || !TEST_true(EC_KEY_generate_key(eckey_neg)) | 
					
						
							|  |  |  |         || !TEST_ptr(pkey_neg = EVP_PKEY_new()) | 
					
						
							| 
									
										
										
										
											2020-09-10 10:36:00 +08:00
										 |  |  |         || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg, NULL)) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 01:45:43 +08:00
										 |  |  |     if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)) | 
					
						
							|  |  |  |         || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 11:37:39 +08:00
										 |  |  |     temp = ECDSA_size(eckey); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 11:37:39 +08:00
										 |  |  |     if (!TEST_int_ge(temp, 0) | 
					
						
							|  |  |  |         || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp)) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         /* create a signature */ | 
					
						
							|  |  |  |         || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey)) | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs))) | 
					
						
							|  |  |  |         || !TEST_int_le(sig_len, ECDSA_size(eckey)) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         /* negative test, verify with wrong key, 0 return */ | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg)) | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg)) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         /* negative test, verify with wrong signature length, -1 return */ | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         /* positive test, verify with correct key, 1 return */ | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx))) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* muck with the message, test it fails with 0 return */ | 
					
						
							|  |  |  |     tbs[0] ^= 1; | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) | 
					
						
							|  |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx))) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     /* un-muck and test it verifies */ | 
					
						
							|  |  |  |     tbs[0] ^= 1; | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) | 
					
						
							|  |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx))) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*-
 | 
					
						
							|  |  |  |      * Muck with the ECDSA signature. The DER encoding is one of: | 
					
						
							|  |  |  |      * - 30 LL 02 .. | 
					
						
							|  |  |  |      * - 30 81 LL 02 .. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - Sometimes this mucks with the high level DER sequence wrapper: | 
					
						
							|  |  |  |      *   in that case, DER-parsing of the whole signature should fail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - Sometimes this mucks with the DER-encoding of ECDSA.r: | 
					
						
							|  |  |  |      *   in that case, DER-parsing of ECDSA.r should fail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - Sometimes this mucks with the DER-encoding of ECDSA.s: | 
					
						
							|  |  |  |      *   in that case, DER-parsing of ECDSA.s should fail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - Sometimes this mucks with ECDSA.r: | 
					
						
							|  |  |  |      *   in that case, the signature verification should fail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - Sometimes this mucks with ECDSA.s: | 
					
						
							|  |  |  |      *   in that case, the signature verification should fail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * The usual case is changing the integer value of ECDSA.r or ECDSA.s. | 
					
						
							|  |  |  |      * Because the ratio of DER overhead to signature bytes is small. | 
					
						
							|  |  |  |      * So most of the time it will be one of the last two cases. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * In any case, EVP_PKEY_verify should not return 1 for valid. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     offset = tbs[0] % sig_len; | 
					
						
							|  |  |  |     dirt = tbs[1] ? tbs[1] : 1; | 
					
						
							|  |  |  |     sig[offset] ^= dirt; | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) | 
					
						
							|  |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx))) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     /* un-muck and test it verifies */ | 
					
						
							|  |  |  |     sig[offset] ^= dirt; | 
					
						
							| 
									
										
										
										
											2020-03-11 06:10:29 +08:00
										 |  |  |     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) | 
					
						
							|  |  |  |         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) | 
					
						
							|  |  |  |         || !TEST_true(EVP_MD_CTX_reset(mctx))) | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     ret = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     EVP_PKEY_free(pkey); | 
					
						
							|  |  |  |     EVP_PKEY_free(pkey_neg); | 
					
						
							| 
									
										
										
										
											2021-03-20 01:45:43 +08:00
										 |  |  |     EVP_PKEY_free(dup_pk); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     EVP_MD_CTX_free(mctx); | 
					
						
							|  |  |  |     OPENSSL_free(sig); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int test_builtin_as_ec(int n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return test_builtin(n, EVP_PKEY_EC); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 01:29:37 +08:00
										 |  |  | # ifndef OPENSSL_NO_SM2
 | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  | static int test_builtin_as_sm2(int n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return test_builtin(n, EVP_PKEY_SM2); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-05 01:29:37 +08:00
										 |  |  | # endif
 | 
					
						
							|  |  |  | #endif /* OPENSSL_NO_EC */
 | 
					
						
							| 
									
										
										
										
											2002-02-14 02:21:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-18 09:48:27 +08:00
										 |  |  | int setup_tests(void) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-06-22 07:35:08 +08:00
										 |  |  | #ifdef OPENSSL_NO_EC
 | 
					
						
							|  |  |  |     TEST_note("Elliptic curves are disabled."); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2021-02-17 09:55:13 +08:00
										 |  |  |     fake_rand = fake_rand_start(NULL); | 
					
						
							|  |  |  |     if (fake_rand == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     /* get a list of all internal curves */ | 
					
						
							|  |  |  |     crv_len = EC_get_builtin_curves(NULL, 0); | 
					
						
							|  |  |  |     if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len)) | 
					
						
							| 
									
										
										
										
											2021-02-17 09:55:13 +08:00
										 |  |  |         || !TEST_true(EC_get_builtin_curves(curves, crv_len))) { | 
					
						
							|  |  |  |         fake_rand_finish(fake_rand); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2021-02-17 09:55:13 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |     ADD_ALL_TESTS(test_builtin_as_ec, crv_len); | 
					
						
							| 
									
										
										
										
											2020-02-05 01:29:37 +08:00
										 |  |  | # ifndef OPENSSL_NO_SM2
 | 
					
						
							| 
									
										
										
										
											2020-01-25 01:14:53 +08:00
										 |  |  |     ADD_ALL_TESTS(test_builtin_as_sm2, crv_len); | 
					
						
							| 
									
										
										
										
											2020-02-05 01:29:37 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats)); | 
					
						
							| 
									
										
										
										
											2017-06-22 07:35:08 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-07-18 09:48:27 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void cleanup_tests(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifndef OPENSSL_NO_EC
 | 
					
						
							| 
									
										
										
										
											2021-02-17 09:55:13 +08:00
										 |  |  |     fake_rand_finish(fake_rand); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:35 +08:00
										 |  |  |     OPENSSL_free(curves); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } |