xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/ecdsatest.c (revision b46c97fed00dfbea5bf756ee3219c17ae32ec29b)
1c7da899bSchristos /*
2*b46c97feSchristos  * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.
3e0ea3921Schristos  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4c7da899bSchristos  *
58fbed61eSchristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
6c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
7c7da899bSchristos  * in the file LICENSE in the source distribution or at
8c7da899bSchristos  * https://www.openssl.org/source/license.html
9c7da899bSchristos  */
10c7da899bSchristos 
118fbed61eSchristos /*
128fbed61eSchristos  * Low level APIs are deprecated for public use, but still ok for internal use.
138fbed61eSchristos  */
148fbed61eSchristos #include "internal/deprecated.h"
158fbed61eSchristos 
16c7da899bSchristos #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
17e0ea3921Schristos #include "testutil.h"
18c7da899bSchristos 
19e0ea3921Schristos #ifndef OPENSSL_NO_EC
20c7da899bSchristos 
21c7da899bSchristos # include <openssl/evp.h>
22c7da899bSchristos # include <openssl/bn.h>
23c7da899bSchristos # include <openssl/ec.h>
24c7da899bSchristos # include <openssl/rand.h>
254261787cSchristos # include "internal/nelem.h"
264261787cSchristos # include "ecdsatest.h"
27c7da899bSchristos 
288fbed61eSchristos static fake_random_generate_cb fbytes;
29c7da899bSchristos 
304261787cSchristos static const char *numbers[2];
314261787cSchristos static size_t crv_len = 0;
324261787cSchristos static EC_builtin_curve *curves = NULL;
338fbed61eSchristos static OSSL_PROVIDER *fake_rand = NULL;
34c7da899bSchristos 
fbytes(unsigned char * buf,size_t num,ossl_unused const char * name,EVP_RAND_CTX * ctx)358fbed61eSchristos static int fbytes(unsigned char *buf, size_t num, ossl_unused const char *name,
368fbed61eSchristos                   EVP_RAND_CTX *ctx)
37c7da899bSchristos {
38e0ea3921Schristos     int ret = 0;
394261787cSchristos     static int fbytes_counter = 0;
40c7da899bSchristos     BIGNUM *tmp = NULL;
41c7da899bSchristos 
428fbed61eSchristos     fake_rand_set_callback(ctx, NULL);
43c7da899bSchristos 
444261787cSchristos     if (!TEST_ptr(tmp = BN_new())
454261787cSchristos         || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
464261787cSchristos         || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter]))
474261787cSchristos         /* tmp might need leading zeros so pad it out */
484261787cSchristos         || !TEST_int_le(BN_num_bytes(tmp), num)
498fbed61eSchristos         || !TEST_int_gt(BN_bn2binpad(tmp, buf, num), 0))
504261787cSchristos         goto err;
514261787cSchristos 
524261787cSchristos     fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers);
53c7da899bSchristos     ret = 1;
544261787cSchristos  err:
55c7da899bSchristos     BN_free(tmp);
56c7da899bSchristos     return ret;
57c7da899bSchristos }
58c7da899bSchristos 
594261787cSchristos /*-
604261787cSchristos  * This function hijacks the RNG to feed it the chosen ECDSA key and nonce.
614261787cSchristos  * The ECDSA KATs are from:
624261787cSchristos  * - the X9.62 draft (4)
634261787cSchristos  * - NIST CAVP (720)
644261787cSchristos  *
654261787cSchristos  * It uses the low-level ECDSA_sign_setup instead of EVP to control the RNG.
664261787cSchristos  * NB: This is not how applications should use ECDSA; this is only for testing.
674261787cSchristos  *
684261787cSchristos  * Tests the library can successfully:
694261787cSchristos  * - generate public keys that matches those KATs
704261787cSchristos  * - create ECDSA signatures that match those KATs
714261787cSchristos  * - accept those signatures as valid
724261787cSchristos  */
x9_62_tests(int n)734261787cSchristos static int x9_62_tests(int n)
74c7da899bSchristos {
754261787cSchristos     int nid, md_nid, ret = 0;
764261787cSchristos     const char *r_in = NULL, *s_in = NULL, *tbs = NULL;
774261787cSchristos     unsigned char *pbuf = NULL, *qbuf = NULL, *message = NULL;
784261787cSchristos     unsigned char digest[EVP_MAX_MD_SIZE];
79c7da899bSchristos     unsigned int dgst_len = 0;
804261787cSchristos     long q_len, msg_len = 0;
814261787cSchristos     size_t p_len;
824261787cSchristos     EVP_MD_CTX *mctx = NULL;
83c7da899bSchristos     EC_KEY *key = NULL;
84c7da899bSchristos     ECDSA_SIG *signature = NULL;
85c7da899bSchristos     BIGNUM *r = NULL, *s = NULL;
86c7da899bSchristos     BIGNUM *kinv = NULL, *rp = NULL;
874261787cSchristos     const BIGNUM *sig_r = NULL, *sig_s = NULL;
88c7da899bSchristos 
894261787cSchristos     nid = ecdsa_cavs_kats[n].nid;
904261787cSchristos     md_nid = ecdsa_cavs_kats[n].md_nid;
914261787cSchristos     r_in = ecdsa_cavs_kats[n].r;
924261787cSchristos     s_in = ecdsa_cavs_kats[n].s;
934261787cSchristos     tbs = ecdsa_cavs_kats[n].msg;
944261787cSchristos     numbers[0] = ecdsa_cavs_kats[n].d;
954261787cSchristos     numbers[1] = ecdsa_cavs_kats[n].k;
96c7da899bSchristos 
974261787cSchristos     TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid));
984261787cSchristos 
998fbed61eSchristos #ifdef FIPS_MODULE
1008fbed61eSchristos     if (EC_curve_nid2nist(nid) == NULL)
1018fbed61eSchristos         return TEST_skip("skip non approved curves");
1028fbed61eSchristos #endif /* FIPS_MODULE */
1038fbed61eSchristos 
1044261787cSchristos     if (!TEST_ptr(mctx = EVP_MD_CTX_new())
105c7da899bSchristos         /* get the message digest */
1064261787cSchristos         || !TEST_ptr(message = OPENSSL_hexstr2buf(tbs, &msg_len))
1074261787cSchristos         || !TEST_true(EVP_DigestInit_ex(mctx, EVP_get_digestbynid(md_nid), NULL))
1084261787cSchristos         || !TEST_true(EVP_DigestUpdate(mctx, message, msg_len))
1094261787cSchristos         || !TEST_true(EVP_DigestFinal_ex(mctx, digest, &dgst_len))
110c7da899bSchristos         /* create the key */
1114261787cSchristos         || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid))
1124261787cSchristos         /* load KAT variables */
1134261787cSchristos         || !TEST_ptr(r = BN_new())
1144261787cSchristos         || !TEST_ptr(s = BN_new())
1154261787cSchristos         || !TEST_true(BN_hex2bn(&r, r_in))
1168fbed61eSchristos         || !TEST_true(BN_hex2bn(&s, s_in)))
1174261787cSchristos         goto err;
118e0ea3921Schristos 
1194261787cSchristos     /* public key must match KAT */
1208fbed61eSchristos     fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
1214261787cSchristos     if (!TEST_true(EC_KEY_generate_key(key))
1224261787cSchristos         || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED,
1234261787cSchristos                                              &pbuf, NULL))
1244261787cSchristos         || !TEST_ptr(qbuf = OPENSSL_hexstr2buf(ecdsa_cavs_kats[n].Q, &q_len))
1254261787cSchristos         || !TEST_int_eq(q_len, p_len)
1264261787cSchristos         || !TEST_mem_eq(qbuf, q_len, pbuf, p_len))
1274261787cSchristos         goto err;
1284261787cSchristos 
1294261787cSchristos     /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */
1308fbed61eSchristos     fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
1314261787cSchristos     if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
1324261787cSchristos         || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
1334261787cSchristos                                                   kinv, rp, key))
1344261787cSchristos         /* verify the signature */
1354261787cSchristos         || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1))
1364261787cSchristos         goto err;
137e0ea3921Schristos 
138c7da899bSchristos     /* compare the created signature with the expected signature */
139c7da899bSchristos     ECDSA_SIG_get0(signature, &sig_r, &sig_s);
140e0ea3921Schristos     if (!TEST_BN_eq(sig_r, r)
141e0ea3921Schristos         || !TEST_BN_eq(sig_s, s))
1424261787cSchristos         goto err;
143e0ea3921Schristos 
144c7da899bSchristos     ret = 1;
145e0ea3921Schristos 
1464261787cSchristos  err:
1474261787cSchristos     OPENSSL_free(message);
1484261787cSchristos     OPENSSL_free(pbuf);
1494261787cSchristos     OPENSSL_free(qbuf);
150c7da899bSchristos     EC_KEY_free(key);
151c7da899bSchristos     ECDSA_SIG_free(signature);
152c7da899bSchristos     BN_free(r);
153c7da899bSchristos     BN_free(s);
1544261787cSchristos     EVP_MD_CTX_free(mctx);
155c7da899bSchristos     BN_clear_free(kinv);
156c7da899bSchristos     BN_clear_free(rp);
157c7da899bSchristos     return ret;
158c7da899bSchristos }
159c7da899bSchristos 
1604261787cSchristos /*-
1614261787cSchristos  * Positive and negative ECDSA testing through EVP interface:
1624261787cSchristos  * - EVP_DigestSign (this is the one-shot version)
1634261787cSchristos  * - EVP_DigestVerify
1644261787cSchristos  *
1654261787cSchristos  * Tests the library can successfully:
1664261787cSchristos  * - create a key
1674261787cSchristos  * - create a signature
1684261787cSchristos  * - accept that signature
1694261787cSchristos  * - reject that signature with a different public key
1704261787cSchristos  * - reject that signature if its length is not correct
1714261787cSchristos  * - reject that signature after modifying the message
1724261787cSchristos  * - accept that signature after un-modifying the message
1734261787cSchristos  * - reject that signature after modifying the signature
1744261787cSchristos  * - accept that signature after un-modifying the signature
1754261787cSchristos  */
set_sm2_id(EVP_MD_CTX * mctx,EVP_PKEY * pkey)1768fbed61eSchristos static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey)
1778fbed61eSchristos {
1788fbed61eSchristos     /* With the SM2 key type, the SM2 ID is mandatory */
1798fbed61eSchristos     static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
1808fbed61eSchristos     EVP_PKEY_CTX *pctx;
1818fbed61eSchristos 
1828fbed61eSchristos     if (!TEST_ptr(pctx = EVP_MD_CTX_get_pkey_ctx(mctx))
1838fbed61eSchristos         || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, sm2_id, sizeof(sm2_id)), 0))
1848fbed61eSchristos         return 0;
1858fbed61eSchristos     return 1;
1868fbed61eSchristos }
1878fbed61eSchristos 
test_builtin(int n,int as)1888fbed61eSchristos static int test_builtin(int n, int as)
189c7da899bSchristos {
1904261787cSchristos     EC_KEY *eckey_neg = NULL, *eckey = NULL;
1914261787cSchristos     unsigned char dirt, offset, tbs[128];
1924261787cSchristos     unsigned char *sig = NULL;
1938fbed61eSchristos     EVP_PKEY *pkey_neg = NULL, *pkey = NULL, *dup_pk = NULL;
1944261787cSchristos     EVP_MD_CTX *mctx = NULL;
1954261787cSchristos     size_t sig_len;
196c7da899bSchristos     int nid, ret = 0;
1978fbed61eSchristos     int temp;
198c7da899bSchristos 
199c7da899bSchristos     nid = curves[n].nid;
200bf8eace1Schristos 
2014261787cSchristos     /* skip built-in curves where ord(G) is not prime */
2024261787cSchristos     if (nid == NID_ipsec4 || nid == NID_ipsec3) {
2034261787cSchristos         TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid));
2044261787cSchristos         return 1;
2054261787cSchristos     }
206e0ea3921Schristos 
2078fbed61eSchristos     /*
2088fbed61eSchristos      * skip SM2 curve if 'as' is equal to EVP_PKEY_EC or, skip all curves
2098fbed61eSchristos      * except SM2 curve if 'as' is equal to EVP_PKEY_SM2
2108fbed61eSchristos      */
2118fbed61eSchristos     if (nid == NID_sm2 && as == EVP_PKEY_EC) {
2128fbed61eSchristos         TEST_info("skipped: EC key type unsupported for curve %s",
2138fbed61eSchristos                   OBJ_nid2sn(nid));
2148fbed61eSchristos         return 1;
2158fbed61eSchristos     } else if (nid != NID_sm2 && as == EVP_PKEY_SM2) {
2168fbed61eSchristos         TEST_info("skipped: SM2 key type unsupported for curve %s",
2178fbed61eSchristos                   OBJ_nid2sn(nid));
2188fbed61eSchristos         return 1;
2198fbed61eSchristos     }
2208fbed61eSchristos 
2218fbed61eSchristos     TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid),
2228fbed61eSchristos               as == EVP_PKEY_EC ? "EC" : "SM2");
223c7da899bSchristos 
2244261787cSchristos     if (!TEST_ptr(mctx = EVP_MD_CTX_new())
2254261787cSchristos         /* get some random message data */
2268fbed61eSchristos         || !TEST_int_gt(RAND_bytes(tbs, sizeof(tbs)), 0)
2274261787cSchristos         /* real key */
2284261787cSchristos         || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid))
2294261787cSchristos         || !TEST_true(EC_KEY_generate_key(eckey))
2304261787cSchristos         || !TEST_ptr(pkey = EVP_PKEY_new())
2314261787cSchristos         || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))
2324261787cSchristos         /* fake key for negative testing */
2334261787cSchristos         || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid))
2344261787cSchristos         || !TEST_true(EC_KEY_generate_key(eckey_neg))
2354261787cSchristos         || !TEST_ptr(pkey_neg = EVP_PKEY_new())
2368fbed61eSchristos         || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg, NULL))
2374261787cSchristos         || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg)))
2384261787cSchristos         goto err;
239e0ea3921Schristos 
2408fbed61eSchristos     if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))
2418fbed61eSchristos         || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1))
2428fbed61eSchristos         goto err;
243e0ea3921Schristos 
2448fbed61eSchristos     temp = ECDSA_size(eckey);
2458fbed61eSchristos 
2468fbed61eSchristos     if (!TEST_int_ge(temp, 0)
2478fbed61eSchristos         || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp))
2484261787cSchristos         /* create a signature */
2494261787cSchristos         || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey))
2508fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
2514261787cSchristos         || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs)))
2524261787cSchristos         || !TEST_int_le(sig_len, ECDSA_size(eckey))
2538fbed61eSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx))
2544261787cSchristos         /* negative test, verify with wrong key, 0 return */
2554261787cSchristos         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg))
2568fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg))
2574261787cSchristos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
2588fbed61eSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx))
2594261787cSchristos         /* negative test, verify with wrong signature length, -1 return */
2604261787cSchristos         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
2618fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
2624261787cSchristos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1)
2634261787cSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx))
2648fbed61eSchristos         /* positive test, verify with correct key, 1 return */
2654261787cSchristos         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
2668fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
2678fbed61eSchristos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
2688fbed61eSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
2694261787cSchristos         goto err;
270e0ea3921Schristos 
2714261787cSchristos     /* muck with the message, test it fails with 0 return */
2724261787cSchristos     tbs[0] ^= 1;
2738fbed61eSchristos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
2748fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
2758fbed61eSchristos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
2768fbed61eSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
2774261787cSchristos         goto err;
2784261787cSchristos     /* un-muck and test it verifies */
2794261787cSchristos     tbs[0] ^= 1;
2808fbed61eSchristos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
2818fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
2828fbed61eSchristos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
2838fbed61eSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
2844261787cSchristos         goto err;
285e0ea3921Schristos 
2864261787cSchristos     /*-
2874261787cSchristos      * Muck with the ECDSA signature. The DER encoding is one of:
2884261787cSchristos      * - 30 LL 02 ..
2894261787cSchristos      * - 30 81 LL 02 ..
2904261787cSchristos      *
2914261787cSchristos      * - Sometimes this mucks with the high level DER sequence wrapper:
2924261787cSchristos      *   in that case, DER-parsing of the whole signature should fail.
2934261787cSchristos      *
2944261787cSchristos      * - Sometimes this mucks with the DER-encoding of ECDSA.r:
2954261787cSchristos      *   in that case, DER-parsing of ECDSA.r should fail.
2964261787cSchristos      *
2974261787cSchristos      * - Sometimes this mucks with the DER-encoding of ECDSA.s:
2984261787cSchristos      *   in that case, DER-parsing of ECDSA.s should fail.
2994261787cSchristos      *
3004261787cSchristos      * - Sometimes this mucks with ECDSA.r:
3014261787cSchristos      *   in that case, the signature verification should fail.
3024261787cSchristos      *
3034261787cSchristos      * - Sometimes this mucks with ECDSA.s:
3044261787cSchristos      *   in that case, the signature verification should fail.
3054261787cSchristos      *
3064261787cSchristos      * The usual case is changing the integer value of ECDSA.r or ECDSA.s.
3074261787cSchristos      * Because the ratio of DER overhead to signature bytes is small.
3084261787cSchristos      * So most of the time it will be one of the last two cases.
3094261787cSchristos      *
3104261787cSchristos      * In any case, EVP_PKEY_verify should not return 1 for valid.
311c7da899bSchristos      */
3124261787cSchristos     offset = tbs[0] % sig_len;
3134261787cSchristos     dirt = tbs[1] ? tbs[1] : 1;
3144261787cSchristos     sig[offset] ^= dirt;
3158fbed61eSchristos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
3168fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
3178fbed61eSchristos         || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
3188fbed61eSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
3194261787cSchristos         goto err;
3204261787cSchristos     /* un-muck and test it verifies */
3214261787cSchristos     sig[offset] ^= dirt;
3228fbed61eSchristos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
3238fbed61eSchristos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
3248fbed61eSchristos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
3258fbed61eSchristos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
3264261787cSchristos         goto err;
327c7da899bSchristos 
328c7da899bSchristos     ret = 1;
3294261787cSchristos  err:
3304261787cSchristos     EVP_PKEY_free(pkey);
3314261787cSchristos     EVP_PKEY_free(pkey_neg);
3328fbed61eSchristos     EVP_PKEY_free(dup_pk);
3334261787cSchristos     EVP_MD_CTX_free(mctx);
3344261787cSchristos     OPENSSL_free(sig);
335c7da899bSchristos     return ret;
336c7da899bSchristos }
3378fbed61eSchristos 
test_builtin_as_ec(int n)3388fbed61eSchristos static int test_builtin_as_ec(int n)
3398fbed61eSchristos {
3408fbed61eSchristos     return test_builtin(n, EVP_PKEY_EC);
3418fbed61eSchristos }
3428fbed61eSchristos 
3438fbed61eSchristos # ifndef OPENSSL_NO_SM2
test_builtin_as_sm2(int n)3448fbed61eSchristos static int test_builtin_as_sm2(int n)
3458fbed61eSchristos {
3468fbed61eSchristos     return test_builtin(n, EVP_PKEY_SM2);
3478fbed61eSchristos }
348e0ea3921Schristos # endif
349d6e24a89Schristos 
test_ecdsa_sig_NULL(void)350d6e24a89Schristos static int test_ecdsa_sig_NULL(void)
351d6e24a89Schristos {
352d6e24a89Schristos     int ret;
353*b46c97feSchristos     unsigned int siglen0;
354d6e24a89Schristos     unsigned int siglen;
355d6e24a89Schristos     unsigned char dgst[128] = { 0 };
356d6e24a89Schristos     EC_KEY *eckey = NULL;
357*b46c97feSchristos     unsigned char *sig = NULL;
358*b46c97feSchristos     BIGNUM *kinv = NULL, *rp = NULL;
359d6e24a89Schristos 
360d6e24a89Schristos     ret = TEST_ptr(eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1))
361d6e24a89Schristos           && TEST_int_eq(EC_KEY_generate_key(eckey), 1)
362*b46c97feSchristos           && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen0,
363*b46c97feSchristos                                     eckey), 1)
364*b46c97feSchristos           && TEST_int_gt(siglen0, 0)
365*b46c97feSchristos           && TEST_ptr(sig = OPENSSL_malloc(siglen0))
366*b46c97feSchristos           && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), sig, &siglen,
367*b46c97feSchristos                                     eckey), 1)
368*b46c97feSchristos           && TEST_int_gt(siglen, 0)
369*b46c97feSchristos           && TEST_int_le(siglen, siglen0)
370*b46c97feSchristos           && TEST_int_eq(ECDSA_verify(0, dgst, sizeof(dgst), sig, siglen,
371*b46c97feSchristos                                       eckey), 1)
372*b46c97feSchristos           && TEST_int_eq(ECDSA_sign_setup(eckey, NULL, &kinv, &rp), 1)
373*b46c97feSchristos           && TEST_int_eq(ECDSA_sign_ex(0, dgst, sizeof(dgst), NULL, &siglen,
374*b46c97feSchristos                                        kinv, rp, eckey), 1)
375*b46c97feSchristos           && TEST_int_gt(siglen, 0)
376*b46c97feSchristos           && TEST_int_le(siglen, siglen0)
377*b46c97feSchristos           && TEST_int_eq(ECDSA_sign_ex(0, dgst, sizeof(dgst), sig, &siglen0,
378*b46c97feSchristos                                        kinv, rp, eckey), 1)
379*b46c97feSchristos           && TEST_int_eq(siglen, siglen0)
380*b46c97feSchristos           && TEST_int_eq(ECDSA_verify(0, dgst, sizeof(dgst), sig, siglen,
381*b46c97feSchristos                                       eckey), 1);
382d6e24a89Schristos     EC_KEY_free(eckey);
383*b46c97feSchristos     OPENSSL_free(sig);
384*b46c97feSchristos     BN_free(kinv);
385*b46c97feSchristos     BN_free(rp);
386d6e24a89Schristos     return ret;
387d6e24a89Schristos }
388d6e24a89Schristos 
3898fbed61eSchristos #endif /* OPENSSL_NO_EC */
390c7da899bSchristos 
setup_tests(void)391e0ea3921Schristos int setup_tests(void)
392c7da899bSchristos {
393e0ea3921Schristos #ifdef OPENSSL_NO_EC
394e0ea3921Schristos     TEST_note("Elliptic curves are disabled.");
395e0ea3921Schristos #else
3968fbed61eSchristos     fake_rand = fake_rand_start(NULL);
3978fbed61eSchristos     if (fake_rand == NULL)
3988fbed61eSchristos         return 0;
3998fbed61eSchristos 
4004261787cSchristos     /* get a list of all internal curves */
4014261787cSchristos     crv_len = EC_get_builtin_curves(NULL, 0);
4024261787cSchristos     if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
4038fbed61eSchristos         || !TEST_true(EC_get_builtin_curves(curves, crv_len))) {
4048fbed61eSchristos         fake_rand_finish(fake_rand);
4054261787cSchristos         return 0;
4068fbed61eSchristos     }
4078fbed61eSchristos     ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
408d6e24a89Schristos     ADD_TEST(test_ecdsa_sig_NULL);
4098fbed61eSchristos # ifndef OPENSSL_NO_SM2
4108fbed61eSchristos     ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
4118fbed61eSchristos # endif
4124261787cSchristos     ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats));
413c7da899bSchristos #endif
414e0ea3921Schristos     return 1;
415c7da899bSchristos }
4164261787cSchristos 
cleanup_tests(void)4174261787cSchristos void cleanup_tests(void)
4184261787cSchristos {
4194261787cSchristos #ifndef OPENSSL_NO_EC
4208fbed61eSchristos     fake_rand_finish(fake_rand);
4214261787cSchristos     OPENSSL_free(curves);
4224261787cSchristos #endif
4234261787cSchristos }
424