int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
    int i, nw, lb, rb;
    BN_ULONG *t, *f;
    BN_ULONG l;
    bn_check_top(r);
    bn_check_top(a);
    if (n < 0) {
        BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
        return 0;
    }
    r->neg = a->neg;
    nw = n / BN_BITS2;
    if (bn_wexpand(r, a->top + nw + 1) == NULL)
        return (0);
    lb = n % BN_BITS2;
    rb = BN_BITS2 - lb;
    f = a->d;
    t = r->d;
    t[a->top + nw] = 0;
    if (lb == 0)
        for (i = a->top - 1; i >= 0; i--)
            t[nw + i] = f[i];
    else
        for (i = a->top - 1; i >= 0; i--) {
            l = f[i];
            t[nw + i + 1] |= (l >> rb) & BN_MASK2;
            t[nw + i] = (l << lb) & BN_MASK2;
        }
    memset(t, 0, sizeof(*t) * nw);
    r->top = a->top + nw + 1;
    bn_correct_top(r);
    bn_check_top(r);
    return (1);
}
['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, *inrand = 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            (void)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_RAND:\n            inrand = opt_arg();\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (argv[0] && (!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    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        if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {\n            BIO_printf(bio_err,\n                       "warning, not much extra random data, consider using the -rand option\\n");\n        }\n        if (inrand != NULL)\n            BIO_printf(bio_err, "%ld semi-random bytes loaded\\n",\n                       app_RAND_load_files(inrand));\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        app_RAND_write_file(NULL);\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            else\n                dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);\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    out = bio_open_default(outfile, \'w\', outformat);\n    if (out == NULL)\n        goto end;\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, "#ifndef HEADER_DH_H\\n"\n                        "# include <openssl/dh.h>\\n"\n                        "#endif\\n"\n                        "\\n");\n        BIO_printf(out, "DH *get_dh%d()\\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 *dhp_bn, *dhg_bn;\\n"\n                        "\\n"\n                        "    if (dh == NULL)\\n"\n                        "        return NULL;\\n");\n        BIO_printf(out, "    dhp_bn = BN_bin2bn(dhp_%d, sizeof (dhp_%d), NULL);\\n",\n                   bits, bits);\n        BIO_printf(out, "    dhg_bn = BN_bin2bn(dhg_%d, sizeof (dhg_%d), NULL);\\n",\n                   bits, bits);\n        BIO_printf(out, "    if (dhp_bn == NULL || dhg_bn == NULL\\n"\n                        "            || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) {\\n"\n                        "        DH_free(dh);\\n"\n                        "        BN_free(dhp_bn);\\n"\n                        "        BN_free(dhg_bn);\\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                        "    }\\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            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        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    return (ret);\n}', 'int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,\n                              BN_GENCB *cb)\n{\n    if (ret->meth->generate_params)\n        return ret->meth->generate_params(ret, prime_len, generator, cb);\n    return dh_builtin_genparams(ret, prime_len, generator, cb);\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    if (t1 == NULL)\n        goto err;\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, BN_prime_checks, 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 != 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 != 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, BN_prime_checks, 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, BN_prime_checks, ctx, NULL);\n        if (r < 0)\n            goto err;\n\tif (!r)\n            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;\n    }\n    ok = 1;\n err:\n    if (ctx != NULL) {\n        BN_CTX_end(ctx);\n        BN_CTX_free(ctx);\n    }\n    return (ok);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n           BN_CTX *ctx)\n{\n    int norm_shift, i, loop;\n    BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n    BN_ULONG *resp, *wnump;\n    BN_ULONG d0, d1;\n    int num_n, div_n;\n    int no_branch = 0;\n    if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n        (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n        return 0;\n    }\n    bn_check_top(num);\n    bn_check_top(divisor);\n    if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n        no_branch = 1;\n    }\n    bn_check_top(dv);\n    bn_check_top(rm);\n    if (BN_is_zero(divisor)) {\n        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n        return (0);\n    }\n    if (!no_branch && BN_ucmp(num, divisor) < 0) {\n        if (rm != NULL) {\n            if (BN_copy(rm, num) == NULL)\n                return (0);\n        }\n        if (dv != NULL)\n            BN_zero(dv);\n        return (1);\n    }\n    BN_CTX_start(ctx);\n    tmp = BN_CTX_get(ctx);\n    snum = BN_CTX_get(ctx);\n    sdiv = BN_CTX_get(ctx);\n    if (dv == NULL)\n        res = BN_CTX_get(ctx);\n    else\n        res = dv;\n    if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n        goto err;\n    norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n    if (!(BN_lshift(sdiv, divisor, norm_shift)))\n        goto err;\n    sdiv->neg = 0;\n    norm_shift += BN_BITS2;\n    if (!(BN_lshift(snum, num, norm_shift)))\n        goto err;\n    snum->neg = 0;\n    if (no_branch) {\n        if (snum->top <= sdiv->top + 1) {\n            if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n                goto err;\n            for (i = snum->top; i < sdiv->top + 2; i++)\n                snum->d[i] = 0;\n            snum->top = sdiv->top + 2;\n        } else {\n            if (bn_wexpand(snum, snum->top + 1) == NULL)\n                goto err;\n            snum->d[snum->top] = 0;\n            snum->top++;\n        }\n    }\n    div_n = sdiv->top;\n    num_n = snum->top;\n    loop = num_n - div_n;\n    wnum.neg = 0;\n    wnum.d = &(snum->d[loop]);\n    wnum.top = div_n;\n    wnum.dmax = snum->dmax - loop;\n    d0 = sdiv->d[div_n - 1];\n    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n    wnump = &(snum->d[num_n - 1]);\n    res->neg = (num->neg ^ divisor->neg);\n    if (!bn_wexpand(res, (loop + 1)))\n        goto err;\n    res->top = loop - no_branch;\n    resp = &(res->d[loop - 1]);\n    if (!bn_wexpand(tmp, (div_n + 1)))\n        goto err;\n    if (!no_branch) {\n        if (BN_ucmp(&wnum, sdiv) >= 0) {\n            bn_clear_top2max(&wnum);\n            bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n            *resp = 1;\n        } else\n            res->top--;\n    }\n    resp++;\n    if (res->top == 0)\n        res->neg = 0;\n    else\n        resp--;\n    for (i = 0; i < loop - 1; i++, wnump--) {\n        BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n        BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n        q = bn_div_3_words(wnump, d1, d0);\n# else\n        BN_ULONG n0, n1, rem = 0;\n        n0 = wnump[0];\n        n1 = wnump[-1];\n        if (n0 == d0)\n            q = BN_MASK2;\n        else {\n#  ifdef BN_LLONG\n            BN_ULLONG t2;\n#   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n            q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n#   else\n            q = bn_div_words(n0, n1, d0);\n#   endif\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n            t2 = (BN_ULLONG) d1 *q;\n            for (;;) {\n                if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                t2 -= d1;\n            }\n#  else\n            BN_ULONG t2l, t2h;\n            q = bn_div_words(n0, n1, d0);\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n#   if defined(BN_UMULT_LOHI)\n            BN_UMULT_LOHI(t2l, t2h, d1, q);\n#   elif defined(BN_UMULT_HIGH)\n            t2l = d1 * q;\n            t2h = BN_UMULT_HIGH(d1, q);\n#   else\n            {\n                BN_ULONG ql, qh;\n                t2l = LBITS(d1);\n                t2h = HBITS(d1);\n                ql = LBITS(q);\n                qh = HBITS(q);\n                mul64(t2l, t2h, ql, qh);\n            }\n#   endif\n            for (;;) {\n                if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                if (t2l < d1)\n                    t2h--;\n                t2l -= d1;\n            }\n#  endif\n        }\n# endif\n        l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n        tmp->d[div_n] = l0;\n        wnum.d--;\n        if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n            q--;\n            if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n                (*wnump)++;\n        }\n        resp--;\n        *resp = q;\n    }\n    bn_correct_top(snum);\n    if (rm != NULL) {\n        int neg = num->neg;\n        BN_rshift(rm, snum, norm_shift);\n        if (!BN_is_zero(rm))\n            rm->neg = neg;\n        bn_check_top(rm);\n    }\n    if (no_branch)\n        bn_correct_top(res);\n    BN_CTX_end(ctx);\n    return (1);\n err:\n    bn_check_top(rm);\n    BN_CTX_end(ctx);\n    return (0);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n    int i, nw, lb, rb;\n    BN_ULONG *t, *f;\n    BN_ULONG l;\n    bn_check_top(r);\n    bn_check_top(a);\n    if (n < 0) {\n        BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n        return 0;\n    }\n    r->neg = a->neg;\n    nw = n / BN_BITS2;\n    if (bn_wexpand(r, a->top + nw + 1) == NULL)\n        return (0);\n    lb = n % BN_BITS2;\n    rb = BN_BITS2 - lb;\n    f = a->d;\n    t = r->d;\n    t[a->top + nw] = 0;\n    if (lb == 0)\n        for (i = a->top - 1; i >= 0; i--)\n            t[nw + i] = f[i];\n    else\n        for (i = a->top - 1; i >= 0; i--) {\n            l = f[i];\n            t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n            t[nw + i] = (l << lb) & BN_MASK2;\n        }\n    memset(t, 0, sizeof(*t) * nw);\n    r->top = a->top + nw + 1;\n    bn_correct_top(r);\n    bn_check_top(r);\n    return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']