


#include "internal/deprecated.h"

#include <openssl/opensslconf.h> 
#include "testutil.h"

#ifndef OPENSSL_NO_EC

# include <openssl/evp.h>
# include <openssl/bn.h>
# include <openssl/ec.h>
# include <openssl/rand.h>
# include "internal/nelem.h"
# include "ecdsatest.h"

static fake_random_generate_cb fbytes;

static const char *numbers[2];
static size_t crv_len = 0;
static EC_builtin_curve *curves = NULL;
static OSSL_PROVIDER *fake_rand = NULL;

static int fbytes(unsigned char *buf, size_t num, ossl_unused const char *name,
                  EVP_RAND_CTX *ctx)
{
    int ret = 0;
    static int fbytes_counter = 0;
    BIGNUM *tmp = NULL;

    fake_rand_set_callback(ctx, NULL);

    if (!TEST_ptr(tmp = BN_new())
        || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
        || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter]))
        
        || !TEST_int_le(BN_num_bytes(tmp), num)
        || !TEST_int_gt(BN_bn2binpad(tmp, buf, num), 0))
        goto err;

    fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers);
    ret = 1;
 err:
    BN_free(tmp);
    return ret;
}


static int x9_62_tests(int n)
{
    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];
    unsigned int dgst_len = 0;
    long q_len, msg_len = 0;
    size_t p_len;
    EVP_MD_CTX *mctx = NULL;
    EC_KEY *key = NULL;
    ECDSA_SIG *signature = NULL;
    BIGNUM *r = NULL, *s = NULL;
    BIGNUM *kinv = NULL, *rp = NULL;
    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));

#ifdef FIPS_MODULE
    if (EC_curve_nid2nist(nid) == NULL)
        return TEST_skip("skip non approved curves");
#endif 

    if (!TEST_ptr(mctx = EVP_MD_CTX_new())
        
        || !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))
        
        || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid))
        
        || !TEST_ptr(r = BN_new())
        || !TEST_ptr(s = BN_new())
        || !TEST_true(BN_hex2bn(&r, r_in))
        || !TEST_true(BN_hex2bn(&s, s_in)))
        goto err;

    
    fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
    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;

    
    fake_rand_set_callback(RAND_get0_private(NULL), &fbytes);
    if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
        || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
                                                  kinv, rp, key))
        
        || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1))
        goto err;

    
    ECDSA_SIG_get0(signature, &sig_r, &sig_s);
    if (!TEST_BN_eq(sig_r, r)
        || !TEST_BN_eq(sig_s, s))
        goto err;

    ret = 1;

 err:
    OPENSSL_free(message);
    OPENSSL_free(pbuf);
    OPENSSL_free(qbuf);
    EC_KEY_free(key);
    ECDSA_SIG_free(signature);
    BN_free(r);
    BN_free(s);
    EVP_MD_CTX_free(mctx);
    BN_clear_free(kinv);
    BN_clear_free(rp);
    return ret;
}


static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey)
{
    
    static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
    EVP_PKEY_CTX *pctx;

    if (!TEST_ptr(pctx = EVP_MD_CTX_get_pkey_ctx(mctx))
        || !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)
{
    EC_KEY *eckey_neg = NULL, *eckey = NULL;
    unsigned char dirt, offset, tbs[128];
    unsigned char *sig = NULL;
    EVP_PKEY *pkey_neg = NULL, *pkey = NULL, *dup_pk = NULL;
    EVP_MD_CTX *mctx = NULL;
    size_t sig_len;
    int nid, ret = 0;
    int temp;

    nid = curves[n].nid;

    
    if (nid == NID_ipsec4 || nid == NID_ipsec3) {
        TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid));
        return 1;
    }

    
    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;
    }

    TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid),
              as == EVP_PKEY_EC ? "EC" : "SM2");

    if (!TEST_ptr(mctx = EVP_MD_CTX_new())
        
        || !TEST_int_gt(RAND_bytes(tbs, sizeof(tbs)), 0)
        
        || !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))
        
        || !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())
        || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg, NULL))
        || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg)))
        goto err;

    if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))
        || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1))
        goto err;

    temp = ECDSA_size(eckey);

    if (!TEST_int_ge(temp, 0)
        || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp))
        
        || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
        || !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))
        
        || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg))
        || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
        || !TEST_true(EVP_MD_CTX_reset(mctx))
        
        || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
        || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1)
        || !TEST_true(EVP_MD_CTX_reset(mctx))
        
        || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
        || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
        || !TEST_true(EVP_MD_CTX_reset(mctx)))
        goto err;

    
    tbs[0] ^= 1;
    if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
        || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
        || !TEST_true(EVP_MD_CTX_reset(mctx)))
        goto err;
    
    tbs[0] ^= 1;
    if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
        || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
        || !TEST_true(EVP_MD_CTX_reset(mctx)))
        goto err;

    
    offset = tbs[0] % sig_len;
    dirt = tbs[1] ? tbs[1] : 1;
    sig[offset] ^= dirt;
    if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
        || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
        || !TEST_true(EVP_MD_CTX_reset(mctx)))
        goto err;
    
    sig[offset] ^= dirt;
    if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
        || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
        || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
        || !TEST_true(EVP_MD_CTX_reset(mctx)))
        goto err;

    ret = 1;
 err:
    EVP_PKEY_free(pkey);
    EVP_PKEY_free(pkey_neg);
    EVP_PKEY_free(dup_pk);
    EVP_MD_CTX_free(mctx);
    OPENSSL_free(sig);
    return ret;
}

static int test_builtin_as_ec(int n)
{
    return test_builtin(n, EVP_PKEY_EC);
}

# ifndef OPENSSL_NO_SM2
static int test_builtin_as_sm2(int n)
{
    return test_builtin(n, EVP_PKEY_SM2);
}
# endif

static int test_ecdsa_sig_NULL(void)
{
    int ret;
    unsigned int siglen;
    unsigned char dgst[128] = { 0 };
    EC_KEY *eckey = NULL;

    ret = TEST_ptr(eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1))
          && TEST_int_eq(EC_KEY_generate_key(eckey), 1)
          && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen, eckey), 1)
          && TEST_int_gt(siglen, 0);
    EC_KEY_free(eckey);
    return ret;
}

#endif 

int setup_tests(void)
{
#ifdef OPENSSL_NO_EC
    TEST_note("Elliptic curves are disabled.");
#else
    fake_rand = fake_rand_start(NULL);
    if (fake_rand == NULL)
        return 0;

    
    crv_len = EC_get_builtin_curves(NULL, 0);
    if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
        || !TEST_true(EC_get_builtin_curves(curves, crv_len))) {
        fake_rand_finish(fake_rand);
        return 0;
    }
    ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
    ADD_TEST(test_ecdsa_sig_NULL);
# ifndef OPENSSL_NO_SM2
    ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
# endif
    ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats));
#endif
    return 1;
}

void cleanup_tests(void)
{
#ifndef OPENSSL_NO_EC
    fake_rand_finish(fake_rand);
    OPENSSL_free(curves);
#endif
}
