Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
      4      1.1  christos  *
      5      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      7      1.1  christos  * in the file LICENSE in the source distribution or at
      8      1.1  christos  * https://www.openssl.org/source/license.html
      9      1.1  christos  */
     10      1.1  christos 
     11      1.1  christos /*
     12      1.1  christos  * Low level APIs are deprecated for public use, but still ok for internal use.
     13      1.1  christos  */
     14      1.1  christos #include "internal/deprecated.h"
     15      1.1  christos 
     16      1.1  christos #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
     17      1.1  christos #include "testutil.h"
     18      1.1  christos 
     19      1.1  christos #ifndef OPENSSL_NO_EC
     20      1.1  christos 
     21  1.1.1.2  christos #include <openssl/evp.h>
     22  1.1.1.2  christos #include <openssl/bn.h>
     23  1.1.1.2  christos #include <openssl/ec.h>
     24  1.1.1.2  christos #include <openssl/rand.h>
     25  1.1.1.2  christos #include "internal/nelem.h"
     26  1.1.1.2  christos #include "ecdsatest.h"
     27      1.1  christos 
     28      1.1  christos static fake_random_generate_cb fbytes;
     29      1.1  christos 
     30      1.1  christos static const char *numbers[2];
     31      1.1  christos static size_t crv_len = 0;
     32      1.1  christos static EC_builtin_curve *curves = NULL;
     33      1.1  christos static OSSL_PROVIDER *fake_rand = NULL;
     34      1.1  christos 
     35      1.1  christos static int fbytes(unsigned char *buf, size_t num, ossl_unused const char *name,
     36  1.1.1.2  christos     EVP_RAND_CTX *ctx)
     37      1.1  christos {
     38      1.1  christos     int ret = 0;
     39      1.1  christos     static int fbytes_counter = 0;
     40      1.1  christos     BIGNUM *tmp = NULL;
     41      1.1  christos 
     42      1.1  christos     fake_rand_set_callback(ctx, NULL);
     43      1.1  christos 
     44      1.1  christos     if (!TEST_ptr(tmp = BN_new())
     45      1.1  christos         || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
     46      1.1  christos         || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter]))
     47      1.1  christos         /* tmp might need leading zeros so pad it out */
     48      1.1  christos         || !TEST_int_le(BN_num_bytes(tmp), num)
     49      1.1  christos         || !TEST_int_gt(BN_bn2binpad(tmp, buf, num), 0))
     50      1.1  christos         goto err;
     51      1.1  christos 
     52      1.1  christos     fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers);
     53      1.1  christos     ret = 1;
     54  1.1.1.2  christos err:
     55      1.1  christos     BN_free(tmp);
     56      1.1  christos     return ret;
     57      1.1  christos }
     58      1.1  christos 
     59      1.1  christos /*-
     60      1.1  christos  * This function hijacks the RNG to feed it the chosen ECDSA key and nonce.
     61      1.1  christos  * The ECDSA KATs are from:
     62      1.1  christos  * - the X9.62 draft (4)
     63      1.1  christos  * - NIST CAVP (720)
     64      1.1  christos  *
     65      1.1  christos  * It uses the low-level ECDSA_sign_setup instead of EVP to control the RNG.
     66      1.1  christos  * NB: This is not how applications should use ECDSA; this is only for testing.
     67      1.1  christos  *
     68      1.1  christos  * Tests the library can successfully:
     69      1.1  christos  * - generate public keys that matches those KATs
     70      1.1  christos  * - create ECDSA signatures that match those KATs
     71      1.1  christos  * - accept those signatures as valid
     72      1.1  christos  */
     73      1.1  christos static int x9_62_tests(int n)
     74      1.1  christos {
     75      1.1  christos     int nid, md_nid, ret = 0;
     76      1.1  christos     const char *r_in = NULL, *s_in = NULL, *tbs = NULL;
     77      1.1  christos     unsigned char *pbuf = NULL, *qbuf = NULL, *message = NULL;
     78      1.1  christos     unsigned char digest[EVP_MAX_MD_SIZE];
     79      1.1  christos     unsigned int dgst_len = 0;
     80      1.1  christos     long q_len, msg_len = 0;
     81      1.1  christos     size_t p_len;
     82      1.1  christos     EVP_MD_CTX *mctx = NULL;
     83      1.1  christos     EC_KEY *key = NULL;
     84      1.1  christos     ECDSA_SIG *signature = NULL;
     85      1.1  christos     BIGNUM *r = NULL, *s = NULL;
     86      1.1  christos     BIGNUM *kinv = NULL, *rp = NULL;
     87      1.1  christos     const BIGNUM *sig_r = NULL, *sig_s = NULL;
     88      1.1  christos 
     89      1.1  christos     nid = ecdsa_cavs_kats[n].nid;
     90      1.1  christos     md_nid = ecdsa_cavs_kats[n].md_nid;
     91      1.1  christos     r_in = ecdsa_cavs_kats[n].r;
     92      1.1  christos     s_in = ecdsa_cavs_kats[n].s;
     93      1.1  christos     tbs = ecdsa_cavs_kats[n].msg;
     94      1.1  christos     numbers[0] = ecdsa_cavs_kats[n].d;
     95      1.1  christos     numbers[1] = ecdsa_cavs_kats[n].k;
     96      1.1  christos 
     97      1.1  christos     TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid));
     98      1.1  christos 
     99      1.1  christos #ifdef FIPS_MODULE
    100      1.1  christos     if (EC_curve_nid2nist(nid) == NULL)
    101      1.1  christos         return TEST_skip("skip non approved curves");
    102      1.1  christos #endif /* FIPS_MODULE */
    103      1.1  christos 
    104      1.1  christos     if (!TEST_ptr(mctx = EVP_MD_CTX_new())
    105      1.1  christos         /* get the message digest */
    106      1.1  christos         || !TEST_ptr(message = OPENSSL_hexstr2buf(tbs, &msg_len))
    107      1.1  christos         || !TEST_true(EVP_DigestInit_ex(mctx, EVP_get_digestbynid(md_nid), NULL))
    108      1.1  christos         || !TEST_true(EVP_DigestUpdate(mctx, message, msg_len))
    109      1.1  christos         || !TEST_true(EVP_DigestFinal_ex(mctx, digest, &dgst_len))
    110      1.1  christos         /* create the key */
    111      1.1  christos         || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid))
    112      1.1  christos         /* load KAT variables */
    113      1.1  christos         || !TEST_ptr(r = BN_new())
    114      1.1  christos         || !TEST_ptr(s = BN_new())
    115      1.1  christos         || !TEST_true(BN_hex2bn(&r, r_in))
    116      1.1  christos         || !TEST_true(BN_hex2bn(&s, s_in)))
    117      1.1  christos         goto err;
    118      1.1  christos 
    119      1.1  christos     /* public key must match KAT */
    120      1.1  christos     fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
    121      1.1  christos     if (!TEST_true(EC_KEY_generate_key(key))
    122      1.1  christos         || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED,
    123  1.1.1.2  christos                           &pbuf, NULL))
    124      1.1  christos         || !TEST_ptr(qbuf = OPENSSL_hexstr2buf(ecdsa_cavs_kats[n].Q, &q_len))
    125      1.1  christos         || !TEST_int_eq(q_len, p_len)
    126      1.1  christos         || !TEST_mem_eq(qbuf, q_len, pbuf, p_len))
    127      1.1  christos         goto err;
    128      1.1  christos 
    129      1.1  christos     /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */
    130      1.1  christos     fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
    131      1.1  christos     if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
    132      1.1  christos         || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
    133  1.1.1.2  christos                          kinv, rp, key))
    134      1.1  christos         /* verify the signature */
    135      1.1  christos         || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1))
    136      1.1  christos         goto err;
    137      1.1  christos 
    138      1.1  christos     /* compare the created signature with the expected signature */
    139      1.1  christos     ECDSA_SIG_get0(signature, &sig_r, &sig_s);
    140      1.1  christos     if (!TEST_BN_eq(sig_r, r)
    141      1.1  christos         || !TEST_BN_eq(sig_s, s))
    142      1.1  christos         goto err;
    143      1.1  christos 
    144      1.1  christos     ret = 1;
    145      1.1  christos 
    146  1.1.1.2  christos err:
    147      1.1  christos     OPENSSL_free(message);
    148      1.1  christos     OPENSSL_free(pbuf);
    149      1.1  christos     OPENSSL_free(qbuf);
    150      1.1  christos     EC_KEY_free(key);
    151      1.1  christos     ECDSA_SIG_free(signature);
    152      1.1  christos     BN_free(r);
    153      1.1  christos     BN_free(s);
    154      1.1  christos     EVP_MD_CTX_free(mctx);
    155      1.1  christos     BN_clear_free(kinv);
    156      1.1  christos     BN_clear_free(rp);
    157      1.1  christos     return ret;
    158      1.1  christos }
    159      1.1  christos 
    160      1.1  christos /*-
    161      1.1  christos  * Positive and negative ECDSA testing through EVP interface:
    162      1.1  christos  * - EVP_DigestSign (this is the one-shot version)
    163      1.1  christos  * - EVP_DigestVerify
    164      1.1  christos  *
    165      1.1  christos  * Tests the library can successfully:
    166      1.1  christos  * - create a key
    167      1.1  christos  * - create a signature
    168      1.1  christos  * - accept that signature
    169      1.1  christos  * - reject that signature with a different public key
    170      1.1  christos  * - reject that signature if its length is not correct
    171      1.1  christos  * - reject that signature after modifying the message
    172      1.1  christos  * - accept that signature after un-modifying the message
    173      1.1  christos  * - reject that signature after modifying the signature
    174      1.1  christos  * - accept that signature after un-modifying the signature
    175      1.1  christos  */
    176      1.1  christos static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey)
    177      1.1  christos {
    178      1.1  christos     /* With the SM2 key type, the SM2 ID is mandatory */
    179      1.1  christos     static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
    180      1.1  christos     EVP_PKEY_CTX *pctx;
    181      1.1  christos 
    182      1.1  christos     if (!TEST_ptr(pctx = EVP_MD_CTX_get_pkey_ctx(mctx))
    183      1.1  christos         || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, sm2_id, sizeof(sm2_id)), 0))
    184      1.1  christos         return 0;
    185      1.1  christos     return 1;
    186      1.1  christos }
    187      1.1  christos 
    188      1.1  christos static int test_builtin(int n, int as)
    189      1.1  christos {
    190      1.1  christos     EC_KEY *eckey_neg = NULL, *eckey = NULL;
    191      1.1  christos     unsigned char dirt, offset, tbs[128];
    192      1.1  christos     unsigned char *sig = NULL;
    193      1.1  christos     EVP_PKEY *pkey_neg = NULL, *pkey = NULL, *dup_pk = NULL;
    194      1.1  christos     EVP_MD_CTX *mctx = NULL;
    195      1.1  christos     size_t sig_len;
    196      1.1  christos     int nid, ret = 0;
    197      1.1  christos     int temp;
    198      1.1  christos 
    199      1.1  christos     nid = curves[n].nid;
    200      1.1  christos 
    201      1.1  christos     /* skip built-in curves where ord(G) is not prime */
    202      1.1  christos     if (nid == NID_ipsec4 || nid == NID_ipsec3) {
    203      1.1  christos         TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid));
    204      1.1  christos         return 1;
    205      1.1  christos     }
    206      1.1  christos 
    207      1.1  christos     /*
    208      1.1  christos      * skip SM2 curve if 'as' is equal to EVP_PKEY_EC or, skip all curves
    209      1.1  christos      * except SM2 curve if 'as' is equal to EVP_PKEY_SM2
    210      1.1  christos      */
    211      1.1  christos     if (nid == NID_sm2 && as == EVP_PKEY_EC) {
    212      1.1  christos         TEST_info("skipped: EC key type unsupported for curve %s",
    213  1.1.1.2  christos             OBJ_nid2sn(nid));
    214      1.1  christos         return 1;
    215      1.1  christos     } else if (nid != NID_sm2 && as == EVP_PKEY_SM2) {
    216      1.1  christos         TEST_info("skipped: SM2 key type unsupported for curve %s",
    217  1.1.1.2  christos             OBJ_nid2sn(nid));
    218      1.1  christos         return 1;
    219      1.1  christos     }
    220      1.1  christos 
    221      1.1  christos     TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid),
    222  1.1.1.2  christos         as == EVP_PKEY_EC ? "EC" : "SM2");
    223      1.1  christos 
    224      1.1  christos     if (!TEST_ptr(mctx = EVP_MD_CTX_new())
    225      1.1  christos         /* get some random message data */
    226      1.1  christos         || !TEST_int_gt(RAND_bytes(tbs, sizeof(tbs)), 0)
    227      1.1  christos         /* real key */
    228      1.1  christos         || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid))
    229      1.1  christos         || !TEST_true(EC_KEY_generate_key(eckey))
    230      1.1  christos         || !TEST_ptr(pkey = EVP_PKEY_new())
    231      1.1  christos         || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))
    232      1.1  christos         /* fake key for negative testing */
    233      1.1  christos         || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid))
    234      1.1  christos         || !TEST_true(EC_KEY_generate_key(eckey_neg))
    235      1.1  christos         || !TEST_ptr(pkey_neg = EVP_PKEY_new())
    236      1.1  christos         || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg, NULL))
    237      1.1  christos         || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg)))
    238      1.1  christos         goto err;
    239      1.1  christos 
    240      1.1  christos     if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))
    241      1.1  christos         || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1))
    242      1.1  christos         goto err;
    243      1.1  christos 
    244      1.1  christos     temp = ECDSA_size(eckey);
    245      1.1  christos 
    246      1.1  christos     if (!TEST_int_ge(temp, 0)
    247      1.1  christos         || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp))
    248      1.1  christos         /* create a signature */
    249      1.1  christos         || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey))
    250      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
    251      1.1  christos         || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs)))
    252      1.1  christos         || !TEST_int_le(sig_len, ECDSA_size(eckey))
    253      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx))
    254      1.1  christos         /* negative test, verify with wrong key, 0 return */
    255      1.1  christos         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg))
    256      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg))
    257      1.1  christos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
    258      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx))
    259      1.1  christos         /* negative test, verify with wrong signature length, -1 return */
    260      1.1  christos         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
    261      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
    262      1.1  christos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1)
    263      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx))
    264      1.1  christos         /* positive test, verify with correct key, 1 return */
    265      1.1  christos         || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
    266      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
    267      1.1  christos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
    268      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
    269      1.1  christos         goto err;
    270      1.1  christos 
    271      1.1  christos     /* muck with the message, test it fails with 0 return */
    272      1.1  christos     tbs[0] ^= 1;
    273      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
    274      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
    275      1.1  christos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
    276      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
    277      1.1  christos         goto err;
    278      1.1  christos     /* un-muck and test it verifies */
    279      1.1  christos     tbs[0] ^= 1;
    280      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
    281      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
    282      1.1  christos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
    283      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
    284      1.1  christos         goto err;
    285      1.1  christos 
    286      1.1  christos     /*-
    287      1.1  christos      * Muck with the ECDSA signature. The DER encoding is one of:
    288      1.1  christos      * - 30 LL 02 ..
    289      1.1  christos      * - 30 81 LL 02 ..
    290      1.1  christos      *
    291      1.1  christos      * - Sometimes this mucks with the high level DER sequence wrapper:
    292      1.1  christos      *   in that case, DER-parsing of the whole signature should fail.
    293      1.1  christos      *
    294      1.1  christos      * - Sometimes this mucks with the DER-encoding of ECDSA.r:
    295      1.1  christos      *   in that case, DER-parsing of ECDSA.r should fail.
    296      1.1  christos      *
    297      1.1  christos      * - Sometimes this mucks with the DER-encoding of ECDSA.s:
    298      1.1  christos      *   in that case, DER-parsing of ECDSA.s should fail.
    299      1.1  christos      *
    300      1.1  christos      * - Sometimes this mucks with ECDSA.r:
    301      1.1  christos      *   in that case, the signature verification should fail.
    302      1.1  christos      *
    303      1.1  christos      * - Sometimes this mucks with ECDSA.s:
    304      1.1  christos      *   in that case, the signature verification should fail.
    305      1.1  christos      *
    306      1.1  christos      * The usual case is changing the integer value of ECDSA.r or ECDSA.s.
    307      1.1  christos      * Because the ratio of DER overhead to signature bytes is small.
    308      1.1  christos      * So most of the time it will be one of the last two cases.
    309      1.1  christos      *
    310      1.1  christos      * In any case, EVP_PKEY_verify should not return 1 for valid.
    311      1.1  christos      */
    312      1.1  christos     offset = tbs[0] % sig_len;
    313      1.1  christos     dirt = tbs[1] ? tbs[1] : 1;
    314      1.1  christos     sig[offset] ^= dirt;
    315      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
    316      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
    317      1.1  christos         || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
    318      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
    319      1.1  christos         goto err;
    320      1.1  christos     /* un-muck and test it verifies */
    321      1.1  christos     sig[offset] ^= dirt;
    322      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
    323      1.1  christos         || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
    324      1.1  christos         || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
    325      1.1  christos         || !TEST_true(EVP_MD_CTX_reset(mctx)))
    326      1.1  christos         goto err;
    327      1.1  christos 
    328      1.1  christos     ret = 1;
    329  1.1.1.2  christos err:
    330      1.1  christos     EVP_PKEY_free(pkey);
    331      1.1  christos     EVP_PKEY_free(pkey_neg);
    332      1.1  christos     EVP_PKEY_free(dup_pk);
    333      1.1  christos     EVP_MD_CTX_free(mctx);
    334      1.1  christos     OPENSSL_free(sig);
    335      1.1  christos     return ret;
    336      1.1  christos }
    337      1.1  christos 
    338      1.1  christos static int test_builtin_as_ec(int n)
    339      1.1  christos {
    340      1.1  christos     return test_builtin(n, EVP_PKEY_EC);
    341      1.1  christos }
    342      1.1  christos 
    343  1.1.1.2  christos #ifndef OPENSSL_NO_SM2
    344      1.1  christos static int test_builtin_as_sm2(int n)
    345      1.1  christos {
    346      1.1  christos     return test_builtin(n, EVP_PKEY_SM2);
    347      1.1  christos }
    348  1.1.1.2  christos #endif
    349      1.1  christos 
    350      1.1  christos static int test_ecdsa_sig_NULL(void)
    351      1.1  christos {
    352      1.1  christos     int ret;
    353      1.1  christos     unsigned int siglen0;
    354      1.1  christos     unsigned int siglen;
    355      1.1  christos     unsigned char dgst[128] = { 0 };
    356      1.1  christos     EC_KEY *eckey = NULL;
    357      1.1  christos     unsigned char *sig = NULL;
    358      1.1  christos     BIGNUM *kinv = NULL, *rp = NULL;
    359      1.1  christos 
    360      1.1  christos     ret = TEST_ptr(eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1))
    361  1.1.1.2  christos         && TEST_int_eq(EC_KEY_generate_key(eckey), 1)
    362  1.1.1.2  christos         && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen0,
    363  1.1.1.2  christos                            eckey),
    364  1.1.1.2  christos             1)
    365  1.1.1.2  christos         && TEST_int_gt(siglen0, 0)
    366  1.1.1.2  christos         && TEST_ptr(sig = OPENSSL_malloc(siglen0))
    367  1.1.1.2  christos         && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), sig, &siglen,
    368  1.1.1.2  christos                            eckey),
    369  1.1.1.2  christos             1)
    370  1.1.1.2  christos         && TEST_int_gt(siglen, 0)
    371  1.1.1.2  christos         && TEST_int_le(siglen, siglen0)
    372  1.1.1.2  christos         && TEST_int_eq(ECDSA_verify(0, dgst, sizeof(dgst), sig, siglen,
    373  1.1.1.2  christos                            eckey),
    374  1.1.1.2  christos             1)
    375  1.1.1.2  christos         && TEST_int_eq(ECDSA_sign_setup(eckey, NULL, &kinv, &rp), 1)
    376  1.1.1.2  christos         && TEST_int_eq(ECDSA_sign_ex(0, dgst, sizeof(dgst), NULL, &siglen,
    377  1.1.1.2  christos                            kinv, rp, eckey),
    378  1.1.1.2  christos             1)
    379  1.1.1.2  christos         && TEST_int_gt(siglen, 0)
    380  1.1.1.2  christos         && TEST_int_le(siglen, siglen0)
    381  1.1.1.2  christos         && TEST_int_eq(ECDSA_sign_ex(0, dgst, sizeof(dgst), sig, &siglen0,
    382  1.1.1.2  christos                            kinv, rp, eckey),
    383  1.1.1.2  christos             1)
    384  1.1.1.2  christos         && TEST_int_eq(siglen, siglen0)
    385  1.1.1.2  christos         && TEST_int_eq(ECDSA_verify(0, dgst, sizeof(dgst), sig, siglen,
    386  1.1.1.2  christos                            eckey),
    387  1.1.1.2  christos             1);
    388      1.1  christos     EC_KEY_free(eckey);
    389      1.1  christos     OPENSSL_free(sig);
    390      1.1  christos     BN_free(kinv);
    391      1.1  christos     BN_free(rp);
    392      1.1  christos     return ret;
    393      1.1  christos }
    394      1.1  christos 
    395      1.1  christos #endif /* OPENSSL_NO_EC */
    396      1.1  christos 
    397      1.1  christos int setup_tests(void)
    398      1.1  christos {
    399      1.1  christos #ifdef OPENSSL_NO_EC
    400      1.1  christos     TEST_note("Elliptic curves are disabled.");
    401      1.1  christos #else
    402      1.1  christos     fake_rand = fake_rand_start(NULL);
    403      1.1  christos     if (fake_rand == NULL)
    404      1.1  christos         return 0;
    405      1.1  christos 
    406      1.1  christos     /* get a list of all internal curves */
    407      1.1  christos     crv_len = EC_get_builtin_curves(NULL, 0);
    408      1.1  christos     if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
    409      1.1  christos         || !TEST_true(EC_get_builtin_curves(curves, crv_len))) {
    410      1.1  christos         fake_rand_finish(fake_rand);
    411      1.1  christos         return 0;
    412      1.1  christos     }
    413      1.1  christos     ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
    414      1.1  christos     ADD_TEST(test_ecdsa_sig_NULL);
    415  1.1.1.2  christos #ifndef OPENSSL_NO_SM2
    416      1.1  christos     ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
    417  1.1.1.2  christos #endif
    418      1.1  christos     ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats));
    419      1.1  christos #endif
    420      1.1  christos     return 1;
    421      1.1  christos }
    422      1.1  christos 
    423      1.1  christos void cleanup_tests(void)
    424      1.1  christos {
    425      1.1  christos #ifndef OPENSSL_NO_EC
    426      1.1  christos     fake_rand_finish(fake_rand);
    427      1.1  christos     OPENSSL_free(curves);
    428      1.1  christos #endif
    429      1.1  christos }
    430