BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
    bn_check_top(b);
    if (a == b)
        return a;
    if (bn_wexpand(a, b->top) == NULL)
        return NULL;
    if (b->top > 0)
        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
    a->neg = b->neg;
    a->top = b->top;
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
    bn_check_top(a);
    return a;
}
['int dsaparam_main(int argc, char **argv)\n{\n    ENGINE *e = NULL;\n    DSA *dsa = NULL;\n    BIO *in = NULL, *out = NULL;\n    BN_GENCB *cb = NULL;\n    int numbits = -1, num = 0, genkey = 0;\n    int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;\n    int ret = 1, i, text = 0, private = 0;\n    char *infile = NULL, *outfile = NULL, *prog;\n    OPTION_CHOICE o;\n    prog = opt_init(argc, argv, dsaparam_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\n opthelp:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(dsaparam_options);\n            ret = 0;\n            goto end;\n        case OPT_INFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))\n                goto opthelp;\n            break;\n        case OPT_IN:\n            infile = opt_arg();\n            break;\n        case OPT_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))\n                goto opthelp;\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_ENGINE:\n            e = setup_engine(opt_arg(), 0);\n            break;\n        case OPT_TEXT:\n            text = 1;\n            break;\n        case OPT_C:\n            C = 1;\n            break;\n        case OPT_GENKEY:\n            genkey = 1;\n            break;\n        case OPT_R_CASES:\n            if (!opt_rand(o))\n                goto end;\n            break;\n        case OPT_NOOUT:\n            noout = 1;\n            break;\n        case OPT_VERBOSE:\n            verbose = 1;\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (argc == 1) {\n        if (!opt_int(argv[0], &num) || num < 0)\n            goto end;\n        numbits = num;\n    }\n    private = genkey ? 1 : 0;\n    in = bio_open_default(infile, \'r\', informat);\n    if (in == NULL)\n        goto end;\n    out = bio_open_owner(outfile, outformat, private);\n    if (out == NULL)\n        goto end;\n    if (numbits > 0) {\n        if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)\n            BIO_printf(bio_err,\n                       "Warning: It is not recommended to use more than %d bit for DSA keys.\\n"\n                       "         Your key size is %d! Larger key size may behave not as expected.\\n",\n                       OPENSSL_DSA_MAX_MODULUS_BITS, numbits);\n        cb = BN_GENCB_new();\n        if (cb == NULL) {\n            BIO_printf(bio_err, "Error allocating BN_GENCB object\\n");\n            goto end;\n        }\n        BN_GENCB_set(cb, dsa_cb, bio_err);\n        dsa = DSA_new();\n        if (dsa == NULL) {\n            BIO_printf(bio_err, "Error allocating DSA object\\n");\n            goto end;\n        }\n        if (verbose) {\n            BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\\n",\n                       num);\n            BIO_printf(bio_err, "This could take some time\\n");\n        }\n        if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {\n            ERR_print_errors(bio_err);\n            BIO_printf(bio_err, "Error, DSA key generation failed\\n");\n            goto end;\n        }\n    } else if (informat == FORMAT_ASN1) {\n        dsa = d2i_DSAparams_bio(in, NULL);\n    } else {\n        dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);\n    }\n    if (dsa == NULL) {\n        BIO_printf(bio_err, "unable to load DSA parameters\\n");\n        ERR_print_errors(bio_err);\n        goto end;\n    }\n    if (text) {\n        DSAparams_print(out, dsa);\n    }\n    if (C) {\n        const BIGNUM *p = NULL, *q = NULL, *g = NULL;\n        unsigned char *data;\n        int len, bits_p;\n        DSA_get0_pqg(dsa, &p, &q, &g);\n        len = BN_num_bytes(p);\n        bits_p = BN_num_bits(p);\n        data = app_malloc(len + 20, "BN space");\n        BIO_printf(bio_out, "static DSA *get_dsa%d(void)\\n{\\n", bits_p);\n        print_bignum_var(bio_out, p, "dsap", bits_p, data);\n        print_bignum_var(bio_out, q, "dsaq", bits_p, data);\n        print_bignum_var(bio_out, g, "dsag", bits_p, data);\n        BIO_printf(bio_out, "    DSA *dsa = DSA_new();\\n"\n                            "    BIGNUM *p, *q, *g;\\n"\n                            "\\n");\n        BIO_printf(bio_out, "    if (dsa == NULL)\\n"\n                            "        return NULL;\\n");\n        BIO_printf(bio_out, "    if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\\n",\n                   bits_p, bits_p);\n        BIO_printf(bio_out, "                           q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\\n",\n                   bits_p, bits_p);\n        BIO_printf(bio_out, "                           g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\\n",\n                   bits_p, bits_p);\n        BIO_printf(bio_out, "        DSA_free(dsa);\\n"\n                            "        BN_free(p);\\n"\n                            "        BN_free(q);\\n"\n                            "        BN_free(g);\\n"\n                            "        return NULL;\\n"\n                            "    }\\n"\n                            "    return dsa;\\n}\\n");\n        OPENSSL_free(data);\n    }\n    if (outformat == FORMAT_ASN1 && genkey)\n        noout = 1;\n    if (!noout) {\n        if (outformat == FORMAT_ASN1)\n            i = i2d_DSAparams_bio(out, dsa);\n        else\n            i = PEM_write_bio_DSAparams(out, dsa);\n        if (!i) {\n            BIO_printf(bio_err, "unable to write DSA parameters\\n");\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (genkey) {\n        DSA *dsakey;\n        if ((dsakey = DSAparams_dup(dsa)) == NULL)\n            goto end;\n        if (!DSA_generate_key(dsakey)) {\n            ERR_print_errors(bio_err);\n            DSA_free(dsakey);\n            goto end;\n        }\n        assert(private);\n        if (outformat == FORMAT_ASN1)\n            i = i2d_DSAPrivateKey_bio(out, dsakey);\n        else\n            i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,\n                                            NULL);\n        DSA_free(dsakey);\n    }\n    ret = 0;\n end:\n    BN_GENCB_free(cb);\n    BIO_free(in);\n    BIO_free_all(out);\n    DSA_free(dsa);\n    release_engine(e);\n    return ret;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n    int i = a->top - 1;\n    bn_check_top(a);\n    if (BN_is_zero(a))\n        return 0;\n    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n    return a->top == 0;\n}', 'void print_bignum_var(BIO *out, const BIGNUM *in, const char *var,\n                      int len, unsigned char *buffer)\n{\n    BIO_printf(out, "    static unsigned char %s_%d[] = {", var, len);\n    if (BN_is_zero(in)) {\n        BIO_printf(out, "\\n        0x00");\n    } else {\n        int i, l;\n        l = BN_bn2bin(in, buffer);\n        for (i = 0; i < l; i++) {\n            BIO_printf(out, (i % 10) == 0 ? "\\n        " : " ");\n            if (i < l - 1)\n                BIO_printf(out, "0x%02X,", buffer[i]);\n            else\n                BIO_printf(out, "0x%02X", buffer[i]);\n        }\n    }\n    BIO_printf(out, "\\n    };\\n");\n}', 'int DSA_generate_key(DSA *dsa)\n{\n    if (dsa->meth->dsa_keygen)\n        return dsa->meth->dsa_keygen(dsa);\n    return dsa_builtin_keygen(dsa);\n}', 'static int dsa_builtin_keygen(DSA *dsa)\n{\n    int ok = 0;\n    BN_CTX *ctx = NULL;\n    BIGNUM *pub_key = NULL, *priv_key = NULL;\n    if ((ctx = BN_CTX_new()) == NULL)\n        goto err;\n    if (dsa->priv_key == NULL) {\n        if ((priv_key = BN_secure_new()) == NULL)\n            goto err;\n    } else\n        priv_key = dsa->priv_key;\n    do\n        if (!BN_priv_rand_range(priv_key, dsa->q))\n            goto err;\n    while (BN_is_zero(priv_key)) ;\n    if (dsa->pub_key == NULL) {\n        if ((pub_key = BN_new()) == NULL)\n            goto err;\n    } else\n        pub_key = dsa->pub_key;\n    {\n        BIGNUM *prk = BN_new();\n        if (prk == NULL)\n            goto err;\n        BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);\n        if (!BN_mod_exp(pub_key, dsa->g, prk, dsa->p, ctx)) {\n            BN_free(prk);\n            goto err;\n        }\n        BN_free(prk);\n    }\n    dsa->priv_key = priv_key;\n    dsa->pub_key = pub_key;\n    ok = 1;\n err:\n    if (pub_key != dsa->pub_key)\n        BN_free(pub_key);\n    if (priv_key != dsa->priv_key)\n        BN_free(priv_key);\n    BN_CTX_free(ctx);\n    return ok;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n               BN_CTX *ctx)\n{\n    int ret;\n    bn_check_top(a);\n    bn_check_top(p);\n    bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n    if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n        if (a->top == 1 && !a->neg\n            && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n            && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n            && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n            BN_ULONG A = a->d[0];\n            ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n        } else\n# endif\n            ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n    } else\n#endif\n#ifdef RECP_MUL_MOD\n    {\n        ret = BN_mod_exp_recp(r, a, p, m, ctx);\n    }\n#else\n    {\n        ret = BN_mod_exp_simple(r, a, p, m, ctx);\n    }\n#endif\n    bn_check_top(r);\n    return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n    return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n                    const BIGNUM *m, BN_CTX *ctx)\n{\n    int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n    int start = 1;\n    BIGNUM *aa;\n    BIGNUM *val[TABLE_SIZE];\n    BN_RECP_CTX recp;\n    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n        BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n        return 0;\n    }\n    bits = BN_num_bits(p);\n    if (bits == 0) {\n        if (BN_abs_is_word(m, 1)) {\n            ret = 1;\n            BN_zero(r);\n        } else {\n            ret = BN_one(r);\n        }\n        return ret;\n    }\n    BN_CTX_start(ctx);\n    aa = BN_CTX_get(ctx);\n    val[0] = BN_CTX_get(ctx);\n    if (val[0] == NULL)\n        goto err;\n    BN_RECP_CTX_init(&recp);\n    if (m->neg) {\n        if (!BN_copy(aa, m))\n            goto err;\n        aa->neg = 0;\n        if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n            goto err;\n    } else {\n        if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n            goto err;\n    }\n    if (!BN_nnmod(val[0], a, m, ctx))\n        goto err;\n    if (BN_is_zero(val[0])) {\n        BN_zero(r);\n        ret = 1;\n        goto err;\n    }\n    window = BN_window_bits_for_exponent_size(bits);\n    if (window > 1) {\n        if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n            goto err;\n        j = 1 << (window - 1);\n        for (i = 1; i < j; i++) {\n            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n                !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n                goto err;\n        }\n    }\n    start = 1;\n    wvalue = 0;\n    wstart = bits - 1;\n    wend = 0;\n    if (!BN_one(r))\n        goto err;\n    for (;;) {\n        if (BN_is_bit_set(p, wstart) == 0) {\n            if (!start)\n                if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n                    goto err;\n            if (wstart == 0)\n                break;\n            wstart--;\n            continue;\n        }\n        j = wstart;\n        wvalue = 1;\n        wend = 0;\n        for (i = 1; i < window; i++) {\n            if (wstart - i < 0)\n                break;\n            if (BN_is_bit_set(p, wstart - i)) {\n                wvalue <<= (i - wend);\n                wvalue |= 1;\n                wend = i;\n            }\n        }\n        j = wend + 1;\n        if (!start)\n            for (i = 0; i < j; i++) {\n                if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n                    goto err;\n            }\n        if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n            goto err;\n        wstart -= wend + 1;\n        wvalue = 0;\n        start = 0;\n        if (wstart < 0)\n            break;\n    }\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    BN_RECP_CTX_free(&recp);\n    bn_check_top(r);\n    return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    bn_check_top(b);\n    if (a == b)\n        return a;\n    if (bn_wexpand(a, b->top) == NULL)\n        return NULL;\n    if (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->neg = b->neg;\n    a->top = b->top;\n    a->flags |= b->flags & BN_FLG_FIXED_TOP;\n    bn_check_top(a);\n    return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']