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 rsa_main(int argc, char **argv)\n{\n    ENGINE *e = NULL;\n    BIO *out = NULL;\n    RSA *rsa = NULL;\n    const EVP_CIPHER *enc = NULL;\n    char *infile = NULL, *outfile = NULL, *prog;\n    char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;\n    int i, private = 0;\n    int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;\n    int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;\n# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)\n    int pvk_encr = 2;\n# endif\n    OPTION_CHOICE o;\n    prog = opt_init(argc, argv, rsa_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(rsa_options);\n            ret = 0;\n            goto end;\n        case OPT_INFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_ANY, &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_ANY, &outformat))\n                goto opthelp;\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_PASSIN:\n            passinarg = opt_arg();\n            break;\n        case OPT_PASSOUT:\n            passoutarg = opt_arg();\n            break;\n        case OPT_ENGINE:\n            e = setup_engine(opt_arg(), 0);\n            break;\n        case OPT_PUBIN:\n            pubin = 1;\n            break;\n        case OPT_PUBOUT:\n            pubout = 1;\n            break;\n        case OPT_RSAPUBKEY_IN:\n            pubin = 2;\n            break;\n        case OPT_RSAPUBKEY_OUT:\n            pubout = 2;\n            break;\n        case OPT_PVK_STRONG:\n        case OPT_PVK_WEAK:\n        case OPT_PVK_NONE:\n# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)\n            pvk_encr = (o - OPT_PVK_NONE);\n# endif\n            break;\n        case OPT_NOOUT:\n            noout = 1;\n            break;\n        case OPT_TEXT:\n            text = 1;\n            break;\n        case OPT_MODULUS:\n            modulus = 1;\n            break;\n        case OPT_CHECK:\n            check = 1;\n            break;\n        case OPT_CIPHER:\n            if (!opt_cipher(opt_unknown(), &enc))\n                goto opthelp;\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    if (argc != 0)\n        goto opthelp;\n    private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;\n    if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {\n        BIO_printf(bio_err, "Error getting passwords\\n");\n        goto end;\n    }\n    if (check && pubin) {\n        BIO_printf(bio_err, "Only private keys can be checked\\n");\n        goto end;\n    }\n    {\n        EVP_PKEY *pkey;\n        if (pubin) {\n            int tmpformat = -1;\n            if (pubin == 2) {\n                if (informat == FORMAT_PEM)\n                    tmpformat = FORMAT_PEMRSA;\n                else if (informat == FORMAT_ASN1)\n                    tmpformat = FORMAT_ASN1RSA;\n            } else\n                tmpformat = informat;\n            pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");\n        } else\n            pkey = load_key(infile, informat, 1, passin, e, "Private Key");\n        if (pkey != NULL)\n            rsa = EVP_PKEY_get1_RSA(pkey);\n        EVP_PKEY_free(pkey);\n    }\n    if (rsa == NULL) {\n        ERR_print_errors(bio_err);\n        goto end;\n    }\n    out = bio_open_owner(outfile, outformat, private);\n    if (out == NULL)\n        goto end;\n    if (text) {\n        assert(pubin || private);\n        if (!RSA_print(out, rsa, 0)) {\n            perror(outfile);\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (modulus) {\n        const BIGNUM *n;\n        RSA_get0_key(rsa, &n, NULL, NULL);\n        BIO_printf(out, "Modulus=");\n        BN_print(out, n);\n        BIO_printf(out, "\\n");\n    }\n    if (check) {\n        int r = RSA_check_key(rsa);\n        if (r == 1)\n            BIO_printf(out, "RSA key ok\\n");\n        else if (r == 0) {\n            unsigned long err;\n            while ((err = ERR_peek_error()) != 0 &&\n                   ERR_GET_LIB(err) == ERR_LIB_RSA &&\n                   ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&\n                   ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {\n                BIO_printf(out, "RSA key error: %s\\n",\n                           ERR_reason_error_string(err));\n                ERR_get_error();\n            }\n        }\n        if (r == -1 || ERR_peek_error() != 0) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (noout) {\n        ret = 0;\n        goto end;\n    }\n    BIO_printf(bio_err, "writing RSA key\\n");\n    if (outformat == FORMAT_ASN1) {\n        if (pubout || pubin) {\n            if (pubout == 2)\n                i = i2d_RSAPublicKey_bio(out, rsa);\n            else\n                i = i2d_RSA_PUBKEY_bio(out, rsa);\n        } else {\n            assert(private);\n            i = i2d_RSAPrivateKey_bio(out, rsa);\n        }\n    }\n    else if (outformat == FORMAT_PEM) {\n        if (pubout || pubin) {\n            if (pubout == 2)\n                i = PEM_write_bio_RSAPublicKey(out, rsa);\n            else\n                i = PEM_write_bio_RSA_PUBKEY(out, rsa);\n        } else {\n            assert(private);\n            i = PEM_write_bio_RSAPrivateKey(out, rsa,\n                                            enc, NULL, 0, NULL, passout);\n        }\n# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)\n    } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {\n        EVP_PKEY *pk;\n        pk = EVP_PKEY_new();\n        EVP_PKEY_set1_RSA(pk, rsa);\n        if (outformat == FORMAT_PVK) {\n            if (pubin) {\n                BIO_printf(bio_err, "PVK form impossible with public key input\\n");\n                EVP_PKEY_free(pk);\n                goto end;\n            }\n            assert(private);\n            i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);\n        } else if (pubin || pubout) {\n            i = i2b_PublicKey_bio(out, pk);\n        } else {\n            assert(private);\n            i = i2b_PrivateKey_bio(out, pk);\n        }\n        EVP_PKEY_free(pk);\n# endif\n    } else {\n        BIO_printf(bio_err, "bad output format specified for outfile\\n");\n        goto end;\n    }\n    if (i <= 0) {\n        BIO_printf(bio_err, "unable to write key\\n");\n        ERR_print_errors(bio_err);\n    } else\n        ret = 0;\n end:\n    BIO_free_all(out);\n    RSA_free(rsa);\n    OPENSSL_free(passin);\n    OPENSSL_free(passout);\n    return (ret);\n}', 'int BN_print(BIO *bp, const BIGNUM *a)\n{\n    int i, j, v, z = 0;\n    int ret = 0;\n    if ((a->neg) && (BIO_write(bp, "-", 1) != 1))\n        goto end;\n    if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))\n        goto end;\n    for (i = a->top - 1; i >= 0; i--) {\n        for (j = BN_BITS2 - 4; j >= 0; j -= 4) {\n            v = ((int)(a->d[i] >> (long)j)) & 0x0f;\n            if (z || (v != 0)) {\n                if (BIO_write(bp, &(Hex[v]), 1) != 1)\n                    goto end;\n                z = 1;\n            }\n        }\n    }\n    ret = 1;\n end:\n    return (ret);\n}', 'int RSA_check_key(const RSA *key)\n{\n    return RSA_check_key_ex(key, NULL);\n}', 'int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n    BIGNUM *i, *j, *k, *l, *m;\n    BN_CTX *ctx;\n    int ret = 1;\n    if (key->p == NULL || key->q == NULL || key->n == NULL\n            || key->e == NULL || key->d == NULL) {\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n        return 0;\n    }\n    i = BN_new();\n    j = BN_new();\n    k = BN_new();\n    l = BN_new();\n    m = BN_new();\n    ctx = BN_CTX_new();\n    if (i == NULL || j == NULL || k == NULL || l == NULL\n            || m == NULL || ctx == NULL) {\n        ret = -1;\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (BN_is_one(key->e)) {\n        ret = 0;\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n    }\n    if (!BN_is_odd(key->e)) {\n        ret = 0;\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n    }\n    if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {\n        ret = 0;\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n    }\n    if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {\n        ret = 0;\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n    }\n    if (!BN_mul(i, key->p, key->q, ctx)) {\n        ret = -1;\n        goto err;\n    }\n    if (BN_cmp(i, key->n) != 0) {\n        ret = 0;\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n    }\n    if (!BN_sub(i, key->p, BN_value_one())) {\n        ret = -1;\n        goto err;\n    }\n    if (!BN_sub(j, key->q, BN_value_one())) {\n        ret = -1;\n        goto err;\n    }\n    if (!BN_mul(l, i, j, ctx)) {\n        ret = -1;\n        goto err;\n    }\n    if (!BN_gcd(m, i, j, ctx)) {\n        ret = -1;\n        goto err;\n    }\n    if (!BN_div(k, NULL, l, m, ctx)) {\n        ret = -1;\n        goto err;\n    }\n    if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {\n        ret = -1;\n        goto err;\n    }\n    if (!BN_is_one(i)) {\n        ret = 0;\n        RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n    }\n    if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n        if (!BN_sub(i, key->p, BN_value_one())) {\n            ret = -1;\n            goto err;\n        }\n        if (!BN_mod(j, key->d, i, ctx)) {\n            ret = -1;\n            goto err;\n        }\n        if (BN_cmp(j, key->dmp1) != 0) {\n            ret = 0;\n            RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n        }\n        if (!BN_sub(i, key->q, BN_value_one())) {\n            ret = -1;\n            goto err;\n        }\n        if (!BN_mod(j, key->d, i, ctx)) {\n            ret = -1;\n            goto err;\n        }\n        if (BN_cmp(j, key->dmq1) != 0) {\n            ret = 0;\n            RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n        }\n        if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n            ret = -1;\n            goto err;\n        }\n        if (BN_cmp(i, key->iqmp) != 0) {\n            ret = 0;\n            RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n        }\n    }\n err:\n    BN_free(i);\n    BN_free(j);\n    BN_free(k);\n    BN_free(l);\n    BN_free(m);\n    BN_CTX_free(ctx);\n    return ret;\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}']