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