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 dhparam_main(int argc, char **argv)\n{\n    BIO *in = NULL, *out = NULL;\n    DH *dh = NULL;\n    char *infile = NULL, *outfile = NULL, *prog;\n    ENGINE *e = NULL;\n#ifndef OPENSSL_NO_DSA\n    int dsaparam = 0;\n#endif\n    int i, text = 0, C = 0, ret = 1, num = 0, g = 0;\n    int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;\n    OPTION_CHOICE o;\n    prog = opt_init(argc, argv, dhparam_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(dhparam_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_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))\n                goto opthelp;\n            break;\n        case OPT_IN:\n            infile = opt_arg();\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_CHECK:\n            check = 1;\n            break;\n        case OPT_TEXT:\n            text = 1;\n            break;\n        case OPT_DSAPARAM:\n#ifndef OPENSSL_NO_DSA\n            dsaparam = 1;\n#endif\n            break;\n        case OPT_C:\n            C = 1;\n            break;\n        case OPT_2:\n            g = 2;\n            break;\n        case OPT_5:\n            g = 5;\n            break;\n        case OPT_NOOUT:\n            noout = 1;\n            break;\n        case OPT_R_CASES:\n            if (!opt_rand(o))\n                goto end;\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))\n        goto end;\n    if (g && !num)\n        num = DEFBITS;\n# ifndef OPENSSL_NO_DSA\n    if (dsaparam && g) {\n        BIO_printf(bio_err,\n                   "generator may not be chosen for DSA parameters\\n");\n        goto end;\n    }\n# endif\n    out = bio_open_default(outfile, \'w\', outformat);\n    if (out == NULL)\n        goto end;\n    if (num && !g)\n        g = 2;\n    if (num) {\n        BN_GENCB *cb;\n        cb = BN_GENCB_new();\n        if (cb == NULL) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n        BN_GENCB_set(cb, dh_cb, bio_err);\n# ifndef OPENSSL_NO_DSA\n        if (dsaparam) {\n            DSA *dsa = DSA_new();\n            BIO_printf(bio_err,\n                       "Generating DSA parameters, %d bit long prime\\n", num);\n            if (dsa == NULL\n                || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,\n                                               cb)) {\n                DSA_free(dsa);\n                BN_GENCB_free(cb);\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n            dh = DSA_dup_DH(dsa);\n            DSA_free(dsa);\n            if (dh == NULL) {\n                BN_GENCB_free(cb);\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        } else\n# endif\n        {\n            dh = DH_new();\n            BIO_printf(bio_err,\n                       "Generating DH parameters, %d bit long safe prime, generator %d\\n",\n                       num, g);\n            BIO_printf(bio_err, "This is going to take a long time\\n");\n            if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {\n                BN_GENCB_free(cb);\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        }\n        BN_GENCB_free(cb);\n    } else {\n        in = bio_open_default(infile, \'r\', informat);\n        if (in == NULL)\n            goto end;\n# ifndef OPENSSL_NO_DSA\n        if (dsaparam) {\n            DSA *dsa;\n            if (informat == FORMAT_ASN1)\n                dsa = d2i_DSAparams_bio(in, NULL);\n            else\n                dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);\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            dh = DSA_dup_DH(dsa);\n            DSA_free(dsa);\n            if (dh == NULL) {\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        } else\n# endif\n        {\n            if (informat == FORMAT_ASN1) {\n                dh = d2i_DHparams_bio(in, NULL);\n                if (dh == NULL && BIO_reset(in) == 0)\n                    dh = d2i_DHxparams_bio(in, NULL);\n            } else {\n                dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);\n            }\n            if (dh == NULL) {\n                BIO_printf(bio_err, "unable to load DH parameters\\n");\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        }\n    }\n    if (text) {\n        DHparams_print(out, dh);\n    }\n    if (check) {\n        if (!DH_check(dh, &i)) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n        if (i & DH_CHECK_P_NOT_PRIME)\n            BIO_printf(bio_err, "WARNING: p value is not prime\\n");\n        if (i & DH_CHECK_P_NOT_SAFE_PRIME)\n            BIO_printf(bio_err, "WARNING: p value is not a safe prime\\n");\n        if (i & DH_CHECK_Q_NOT_PRIME)\n            BIO_printf(bio_err, "WARNING: q value is not a prime\\n");\n        if (i & DH_CHECK_INVALID_Q_VALUE)\n            BIO_printf(bio_err, "WARNING: q value is invalid\\n");\n        if (i & DH_CHECK_INVALID_J_VALUE)\n            BIO_printf(bio_err, "WARNING: j value is invalid\\n");\n        if (i & DH_UNABLE_TO_CHECK_GENERATOR)\n            BIO_printf(bio_err,\n                       "WARNING: unable to check the generator value\\n");\n        if (i & DH_NOT_SUITABLE_GENERATOR)\n            BIO_printf(bio_err, "WARNING: the g value is not a generator\\n");\n        if (i == 0)\n            BIO_printf(bio_err, "DH parameters appear to be ok.\\n");\n        if (num != 0 && i != 0) {\n            BIO_printf(bio_err, "ERROR: Invalid parameters generated\\n");\n            goto end;\n        }\n    }\n    if (C) {\n        unsigned char *data;\n        int len, bits;\n        const BIGNUM *pbn, *gbn;\n        len = DH_size(dh);\n        bits = DH_bits(dh);\n        DH_get0_pqg(dh, &pbn, NULL, &gbn);\n        data = app_malloc(len, "print a BN");\n        BIO_printf(out, "static DH *get_dh%d(void)\\n{\\n", bits);\n        print_bignum_var(out, pbn, "dhp", bits, data);\n        print_bignum_var(out, gbn, "dhg", bits, data);\n        BIO_printf(out, "    DH *dh = DH_new();\\n"\n                        "    BIGNUM *p, *g;\\n"\n                        "\\n"\n                        "    if (dh == NULL)\\n"\n                        "        return NULL;\\n");\n        BIO_printf(out, "    p = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\\n",\n                   bits, bits);\n        BIO_printf(out, "    g = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\\n",\n                   bits, bits);\n        BIO_printf(out, "    if (p == NULL || g == NULL\\n"\n                        "            || !DH_set0_pqg(dh, p, NULL, g)) {\\n"\n                        "        DH_free(dh);\\n"\n                        "        BN_free(p);\\n"\n                        "        BN_free(g);\\n"\n                        "        return NULL;\\n"\n                        "    }\\n");\n        if (DH_get_length(dh) > 0)\n            BIO_printf(out,\n                        "    if (!DH_set_length(dh, %ld)) {\\n"\n                        "        DH_free(dh);\\n"\n                        "        return NULL;\\n"\n                        "    }\\n", DH_get_length(dh));\n        BIO_printf(out, "    return dh;\\n}\\n");\n        OPENSSL_free(data);\n    }\n    if (!noout) {\n        const BIGNUM *q;\n        DH_get0_pqg(dh, NULL, &q, NULL);\n        if (outformat == FORMAT_ASN1) {\n            if (q != NULL)\n                i = i2d_DHxparams_bio(out, dh);\n            else\n                i = i2d_DHparams_bio(out, dh);\n        } else if (q != NULL) {\n            i = PEM_write_bio_DHxparams(out, dh);\n        } else {\n            i = PEM_write_bio_DHparams(out, dh);\n        }\n        if (!i) {\n            BIO_printf(bio_err, "unable to write DH parameters\\n");\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    ret = 0;\n end:\n    BIO_free(in);\n    BIO_free_all(out);\n    DH_free(dh);\n    release_engine(e);\n    return ret;\n}', 'int DHparams_print(BIO *bp, const DH *x)\n{\n    return do_dh_print(bp, x, 4, 0);\n}', 'static int do_dh_print(BIO *bp, const DH *x, int indent, int ptype)\n{\n    int reason = ERR_R_BUF_LIB;\n    const char *ktype = NULL;\n    BIGNUM *priv_key, *pub_key;\n    if (ptype == 2)\n        priv_key = x->priv_key;\n    else\n        priv_key = NULL;\n    if (ptype > 0)\n        pub_key = x->pub_key;\n    else\n        pub_key = NULL;\n    if (x->p == NULL || (ptype == 2 && priv_key == NULL)\n            || (ptype > 0 && pub_key == NULL)) {\n        reason = ERR_R_PASSED_NULL_PARAMETER;\n        goto err;\n    }\n    if (ptype == 2)\n        ktype = "DH Private-Key";\n    else if (ptype == 1)\n        ktype = "DH Public-Key";\n    else\n        ktype = "DH Parameters";\n    BIO_indent(bp, indent, 128);\n    if (BIO_printf(bp, "%s: (%d bit)\\n", ktype, BN_num_bits(x->p)) <= 0)\n        goto err;\n    indent += 4;\n    if (!ASN1_bn_print(bp, "private-key:", priv_key, NULL, indent))\n        goto err;\n    if (!ASN1_bn_print(bp, "public-key:", pub_key, NULL, indent))\n        goto err;\n    if (!ASN1_bn_print(bp, "prime:", x->p, NULL, indent))\n        goto err;\n    if (!ASN1_bn_print(bp, "generator:", x->g, NULL, indent))\n        goto err;\n    if (x->q && !ASN1_bn_print(bp, "subgroup order:", x->q, NULL, indent))\n        goto err;\n    if (x->j && !ASN1_bn_print(bp, "subgroup factor:", x->j, NULL, indent))\n        goto err;\n    if (x->seed) {\n        int i;\n        BIO_indent(bp, indent, 128);\n        BIO_puts(bp, "seed:");\n        for (i = 0; i < x->seedlen; i++) {\n            if ((i % 15) == 0) {\n                if (BIO_puts(bp, "\\n") <= 0\n                    || !BIO_indent(bp, indent + 4, 128))\n                    goto err;\n            }\n            if (BIO_printf(bp, "%02x%s", x->seed[i],\n                           ((i + 1) == x->seedlen) ? "" : ":") <= 0)\n                goto err;\n        }\n        if (BIO_write(bp, "\\n", 1) <= 0)\n            return 0;\n    }\n    if (x->counter && !ASN1_bn_print(bp, "counter:", x->counter, NULL, indent))\n        goto err;\n    if (x->length != 0) {\n        BIO_indent(bp, indent, 128);\n        if (BIO_printf(bp, "recommended-private-length: %d bits\\n",\n                       (int)x->length) <= 0)\n            goto err;\n    }\n    return 1;\n err:\n    DHerr(DH_F_DO_DH_PRINT, reason);\n    return 0;\n}', 'int DH_check(const DH *dh, int *ret)\n{\n    int ok = 0, r;\n    BN_CTX *ctx = NULL;\n    BN_ULONG l;\n    BIGNUM *t1 = NULL, *t2 = NULL;\n    *ret = 0;\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    t1 = BN_CTX_get(ctx);\n    t2 = BN_CTX_get(ctx);\n    if (t2 == NULL)\n        goto err;\n    if (dh->q) {\n        if (BN_cmp(dh->g, BN_value_one()) <= 0)\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n        else if (BN_cmp(dh->g, dh->p) >= 0)\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n        else {\n            if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))\n                goto err;\n            if (!BN_is_one(t1))\n                *ret |= DH_NOT_SUITABLE_GENERATOR;\n        }\n        r = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);\n        if (r < 0)\n            goto err;\n        if (!r)\n            *ret |= DH_CHECK_Q_NOT_PRIME;\n        if (!BN_div(t1, t2, dh->p, dh->q, ctx))\n            goto err;\n        if (!BN_is_one(t2))\n            *ret |= DH_CHECK_INVALID_Q_VALUE;\n        if (dh->j && BN_cmp(dh->j, t1))\n            *ret |= DH_CHECK_INVALID_J_VALUE;\n    } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {\n        l = BN_mod_word(dh->p, 24);\n        if (l == (BN_ULONG)-1)\n            goto err;\n        if (l != 11)\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n    } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {\n        l = BN_mod_word(dh->p, 10);\n        if (l == (BN_ULONG)-1)\n            goto err;\n        if ((l != 3) && (l != 7))\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n    } else\n        *ret |= DH_UNABLE_TO_CHECK_GENERATOR;\n    r = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);\n    if (r < 0)\n        goto err;\n    if (!r)\n        *ret |= DH_CHECK_P_NOT_PRIME;\n    else if (!dh->q) {\n        if (!BN_rshift1(t1, dh->p))\n            goto err;\n        r = BN_is_prime_ex(t1, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);\n        if (r < 0)\n            goto err;\n        if (!r)\n            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;\n    }\n    ok = 1;\n err:\n    BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    return ok;\n}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n{\n    int i;\n    int gt, lt;\n    BN_ULONG t1, t2;\n    if ((a == NULL) || (b == NULL)) {\n        if (a != NULL)\n            return -1;\n        else if (b != NULL)\n            return 1;\n        else\n            return 0;\n    }\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->neg != b->neg) {\n        if (a->neg)\n            return -1;\n        else\n            return 1;\n    }\n    if (a->neg == 0) {\n        gt = 1;\n        lt = -1;\n    } else {\n        gt = -1;\n        lt = 1;\n    }\n    if (a->top > b->top)\n        return gt;\n    if (a->top < b->top)\n        return lt;\n    for (i = a->top - 1; i >= 0; i--) {\n        t1 = a->d[i];\n        t2 = b->d[i];\n        if (t1 > t2)\n            return gt;\n        if (t1 < t2)\n            return lt;\n    }\n    return 0;\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}']