



#include <openssl/evp.h>
#include <openssl/core_names.h>
#include <openssl/pem.h>
#include "dsa.inc"


static const char dsapem[] =
    "-----BEGIN DSA PARAMETERS-----\n"
    "MIICLAKCAQEA1pobSR1FJ3+Tvi0J6Tk1PSV2owZey1Nuo847hGw/59VCS6RPQEqr\n"
    "vp5fhbvBjupBeVGA/AMH6rI4i4h6jlhurrqH1CqUHVcDhJzxV668bMLiP3mIxg5o\n"
    "9Yq8x6BnSOtH5Je0tpeE0/fEvvLjCwBUbwnwWxzjANcvDUEt9XYeRrtB2v52fr56\n"
    "hVYz3wMMNog4CEDOLTvx7/84eVPuUeWDRQFH1EaHMdulP34KBcatEEpEZapkepng\n"
    "nohm9sFSPQhq2utpkH7pNXdG0EILBtRDCvUpF5720a48LYofdggh2VEZfgElAGFk\n"
    "dW/CkvyBDmGIzil5aTz4MMsdudaVYgzt6wIhAPsSGC42Qa+X0AFGvonb5nmfUVm/\n"
    "8aC+tHk7Nb2AYLHXAoIBADx5C0H1+QHsmGKvuOaY+WKUt7aWUrEivD1zBMJAQ6bL\n"
    "Wv9lbCq1CFHvVzojeOVpn872NqDEpkx4HTpvqhxWL5CkbN/HaGItsQzkD59AQg3v\n"
    "4YsLlkesq9Jq6x/aWetJXWO36fszFv1gpD3NY3wliBvMYHx62jfc5suh9D3ZZvu7\n"
    "PLGH4X4kcfzK/R2b0oVbEBjVTe5GMRYZRqnvfSW2f2fA7BzI1OL83UxDDe58cL2M\n"
    "GcAoUYXOBAfZ37qLMm2juf+o5gCrT4CXfRPu6kbapt7V/YIc1nsNgeAOKKoFBHBQ\n"
    "gc5u5G6G/j79FVoSDq9DYwTJcHPsU+eHj1uWHso1AjQ=\n"
    "-----END DSA PARAMETERS-----\n";

static const char hexseed[] =
    "cba30ccd905aa7675a0b81769704bf3c"
    "ccf2ca1892b2eaf6b9e2b38d9bf6affc"
    "42ada55986d8a1772b442770954d0b65";
const int gindex = 42;
const int pcounter = 363;
static const char digest[] = "SHA384";


EVP_PKEY_CTX *create_merged_key(EVP_PKEY *dsaparams, const OSSL_PARAM *newparams,
                                OSSL_LIB_CTX *libctx, const char *propq)
{
    EVP_PKEY_CTX *out = NULL;
    EVP_PKEY_CTX *ctx = NULL;
    EVP_PKEY *pkey = NULL;
    OSSL_PARAM *mergedparams = NULL;
    OSSL_PARAM *loadedparams = NULL;

    
    if (EVP_PKEY_todata(dsaparams, EVP_PKEY_KEY_PARAMETERS, &loadedparams) <= 0) {
        fprintf(stderr, "EVP_PKEY_todata() failed\n");
        goto cleanup;
    }
    mergedparams = OSSL_PARAM_merge(loadedparams, newparams);
    if (mergedparams == NULL) {
        fprintf(stderr, "OSSL_PARAM_merge() failed\n");
        goto cleanup;
    }

    ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
    if (ctx == NULL) {
        fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
        goto cleanup;
    }
    if (EVP_PKEY_fromdata_init(ctx) <= 0
            || EVP_PKEY_fromdata(ctx, &pkey,
                                 EVP_PKEY_KEY_PARAMETERS, mergedparams) <= 0) {
        fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
        goto cleanup;
    }
    out = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
    if (out == NULL) {
        fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
        goto cleanup;
    }

cleanup:
    EVP_PKEY_free(pkey);
    OSSL_PARAM_free(loadedparams);
    OSSL_PARAM_free(mergedparams);
    EVP_PKEY_CTX_free(ctx);
    return out;
}

int main(int argc, char **argv)
{
    int ret = EXIT_FAILURE;
    OSSL_LIB_CTX *libctx = NULL;
    const char *propq = NULL;
    EVP_PKEY *dsaparamskey = NULL;
    EVP_PKEY_CTX *ctx = NULL;
    EVP_PKEY_CTX *ctx1 = NULL;
    EVP_PKEY_CTX *ctx2 = NULL;
    BIO *in = NULL;
    OSSL_PARAM params[6];
    unsigned char seed[64];
    size_t seedlen;

    if (!OPENSSL_hexstr2buf_ex(seed, sizeof(seed), &seedlen, hexseed, '\0'))
        goto cleanup;
    
    in = BIO_new_mem_buf(dsapem, strlen(dsapem));
    if (in == NULL) {
        fprintf(stderr, "BIO_new_mem_buf() failed\n");
        goto cleanup;
    }

    
    dsaparamskey = PEM_read_bio_Parameters_ex(in, NULL, libctx, propq);
    if (dsaparamskey == NULL) {
        fprintf(stderr, "Failed to load dsa params\n");
        goto cleanup;
    }

    ctx = EVP_PKEY_CTX_new_from_pkey(libctx, dsaparamskey, propq);
    if (ctx == NULL) {
        fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
        goto cleanup;
    }
    
    if (EVP_PKEY_param_check(ctx) <= 0) {
        fprintf(stderr, "Simple EVP_PKEY_param_check() failed \n");
        goto cleanup;
    }

    
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
                                                "fips186_4", 0);
    
    params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
                                                  (void *)seed, seedlen);
    params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, (int *)&gindex);
    params[3] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, (int *)&pcounter);
    params[4] = OSSL_PARAM_construct_end();

    
    ctx1 = create_merged_key(dsaparamskey, params, libctx, propq);
    if (ctx1 == NULL)
        goto cleanup;
    
    if (EVP_PKEY_param_check(ctx1) > 0) {
        fprintf(stderr, "EVP_PKEY_param_check() should fail\n");
        goto cleanup;
    }

    
    params[4] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
                                                 (char *)digest, 0);
    params[5] = OSSL_PARAM_construct_end();
    ctx2 = create_merged_key(dsaparamskey, params, libctx, propq);
    if (ctx2 == NULL)
        goto cleanup;
    if (EVP_PKEY_param_check(ctx2) <= 0) {
        fprintf(stderr, "EVP_PKEY_param_check() failed\n");
        goto cleanup;
    }

    if (!dsa_print_key(EVP_PKEY_CTX_get0_pkey(ctx2), 0, libctx, propq))
        goto cleanup;

    ret = EXIT_SUCCESS;
cleanup:
    EVP_PKEY_free(dsaparamskey);
    EVP_PKEY_CTX_free(ctx2);
    EVP_PKEY_CTX_free(ctx1);
    EVP_PKEY_CTX_free(ctx);
    BIO_free(in);
    return ret;
}
