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);
}
['MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)\n{\n    int al;\n    unsigned long alg_k;\n#ifndef OPENSSL_NO_RSA\n    RSA *rsa = NULL;\n#endif\n#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)\n    EVP_PKEY *ckey = NULL;\n#endif\n    PACKET enc_premaster;\n    unsigned char *rsa_decrypt = NULL;\n    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n#ifndef OPENSSL_NO_PSK\n    if (alg_k & SSL_PSK) {\n        unsigned char psk[PSK_MAX_PSK_LEN];\n        size_t psklen;\n        PACKET psk_identity;\n        if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_DATA_LENGTH_TOO_LONG);\n            goto f_err;\n        }\n        if (s->psk_server_callback == NULL) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_PSK_NO_SERVER_CB);\n            goto f_err;\n        }\n        if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            al = SSL_AD_INTERNAL_ERROR;\n            goto f_err;\n        }\n        psklen = s->psk_server_callback(s, s->session->psk_identity,\n                                         psk, sizeof(psk));\n        if (psklen > PSK_MAX_PSK_LEN) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        } else if (psklen == 0) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_PSK_IDENTITY_NOT_FOUND);\n            al = SSL_AD_UNKNOWN_PSK_IDENTITY;\n            goto f_err;\n        }\n        OPENSSL_free(s->s3->tmp.psk);\n        s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);\n        OPENSSL_cleanse(psk, psklen);\n        if (s->s3->tmp.psk == NULL) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto f_err;\n        }\n        s->s3->tmp.psklen = psklen;\n    }\n    if (alg_k & SSL_kPSK) {\n        if (PACKET_remaining(pkt) != 0) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (!ssl_generate_master_secret(s, NULL, 0, 0)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n    } else\n#endif\n#ifndef OPENSSL_NO_RSA\n    if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {\n        unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];\n        int decrypt_len;\n        unsigned char decrypt_good, version_good;\n        size_t j;\n        rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey);\n        if (rsa == NULL) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_MISSING_RSA_CERTIFICATE);\n            goto f_err;\n        }\n        if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {\n            enc_premaster = *pkt;\n        } else {\n            if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)\n                || PACKET_remaining(pkt) != 0) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                       SSL_R_LENGTH_MISMATCH);\n                goto f_err;\n            }\n        }\n        if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   RSA_R_KEY_SIZE_TOO_SMALL);\n            goto f_err;\n        }\n        rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));\n        if (rsa_decrypt == NULL) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto f_err;\n        }\n        if (RAND_bytes(rand_premaster_secret,\n                       sizeof(rand_premaster_secret)) <= 0) {\n            goto err;\n        }\n        decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster),\n                                          PACKET_data(&enc_premaster),\n                                          rsa_decrypt, rsa, RSA_PKCS1_PADDING);\n        ERR_clear_error();\n        decrypt_good =\n            constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);\n        version_good =\n            constant_time_eq_8(rsa_decrypt[0],\n                               (unsigned)(s->client_version >> 8));\n        version_good &=\n            constant_time_eq_8(rsa_decrypt[1],\n                               (unsigned)(s->client_version & 0xff));\n        if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {\n            unsigned char workaround_good;\n            workaround_good =\n                constant_time_eq_8(rsa_decrypt[0], (unsigned)(s->version >> 8));\n            workaround_good &=\n                constant_time_eq_8(rsa_decrypt[1],\n                                   (unsigned)(s->version & 0xff));\n            version_good |= workaround_good;\n        }\n        decrypt_good &= version_good;\n        for (j = 0; j < sizeof(rand_premaster_secret); j++) {\n            rsa_decrypt[j] =\n                constant_time_select_8(decrypt_good, rsa_decrypt[j],\n                                       rand_premaster_secret[j]);\n        }\n        if (!ssl_generate_master_secret(s, rsa_decrypt,\n                                        sizeof(rand_premaster_secret), 0)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        OPENSSL_free(rsa_decrypt);\n        rsa_decrypt = NULL;\n    } else\n#endif\n#ifndef OPENSSL_NO_DH\n    if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {\n        EVP_PKEY *skey = NULL;\n        DH *cdh;\n        unsigned int i;\n        BIGNUM *pub_key;\n        const unsigned char *data;\n        if (!PACKET_get_net_2(pkt, &i)) {\n            if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {\n                al = SSL_AD_HANDSHAKE_FAILURE;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                       SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);\n                goto f_err;\n            }\n            i = 0;\n        }\n        if (PACKET_remaining(pkt) != i) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);\n            goto err;\n        }\n        skey = s->s3->tmp.pkey;\n        if (skey == NULL) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_MISSING_TMP_DH_KEY);\n            goto f_err;\n        }\n        if (PACKET_remaining(pkt) == 0L) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_MISSING_TMP_DH_KEY);\n            goto f_err;\n        }\n        if (!PACKET_get_bytes(pkt, &data, i)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        ckey = EVP_PKEY_new();\n        if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);\n            goto err;\n        }\n        cdh = EVP_PKEY_get0_DH(ckey);\n        pub_key = BN_bin2bn(data, i, NULL);\n        if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            if (pub_key != NULL)\n                BN_free(pub_key);\n            goto err;\n        }\n        if (ssl_derive(s, skey, ckey) == 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        EVP_PKEY_free(ckey);\n        ckey = NULL;\n        EVP_PKEY_free(s->s3->tmp.pkey);\n        s->s3->tmp.pkey = NULL;\n    } else\n#endif\n#ifndef OPENSSL_NO_EC\n    if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {\n        EVP_PKEY *skey = s->s3->tmp.pkey;\n        if (PACKET_remaining(pkt) == 0L) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_MISSING_TMP_ECDH_KEY);\n            goto f_err;\n        } else {\n            unsigned int i;\n            const unsigned char *data;\n            if (!PACKET_get_1(pkt, &i)) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                       SSL_R_LENGTH_MISMATCH);\n                goto f_err;\n            }\n            if (!PACKET_get_bytes(pkt, &data, i)\n                    || PACKET_remaining(pkt) != 0) {\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);\n                goto err;\n            }\n            ckey = EVP_PKEY_new();\n            if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB);\n                goto err;\n            }\n            if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(ckey), data, i,\n                               NULL) == 0) {\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);\n                goto err;\n            }\n        }\n        if (ssl_derive(s, skey, ckey) == 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        EVP_PKEY_free(ckey);\n        ckey = NULL;\n        EVP_PKEY_free(s->s3->tmp.pkey);\n        s->s3->tmp.pkey = NULL;\n        return MSG_PROCESS_CONTINUE_PROCESSING;\n    } else\n#endif\n#ifndef OPENSSL_NO_SRP\n    if (alg_k & SSL_kSRP) {\n        unsigned int i;\n        const unsigned char *data;\n        if (!PACKET_get_net_2(pkt, &i)\n                || !PACKET_get_bytes(pkt, &data, i)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BAD_SRP_A_LENGTH);\n            goto f_err;\n        }\n        if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0\n            || BN_is_zero(s->srp_ctx.A)) {\n            al = SSL_AD_ILLEGAL_PARAMETER;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_BAD_SRP_PARAMETERS);\n            goto f_err;\n        }\n        OPENSSL_free(s->session->srp_username);\n        s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);\n        if (s->session->srp_username == NULL) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (!srp_generate_server_master_secret(s)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n    } else\n#endif\n#ifndef OPENSSL_NO_GOST\n    if (alg_k & SSL_kGOST) {\n        EVP_PKEY_CTX *pkey_ctx;\n        EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;\n        unsigned char premaster_secret[32];\n        const unsigned char *start;\n        size_t outlen = 32, inlen;\n        unsigned long alg_a;\n        int Ttag, Tclass;\n        long Tlen;\n        long sess_key_len;\n        const unsigned char *data;\n        alg_a = s->s3->tmp.new_cipher->algorithm_auth;\n        if (alg_a & SSL_aGOST12) {\n            pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;\n            if (pk == NULL) {\n                pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;\n            }\n            if (pk == NULL) {\n                pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;\n            }\n        } else if (alg_a & SSL_aGOST01) {\n            pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;\n        }\n        pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);\n        if (pkey_ctx == NULL) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto f_err;\n        }\n        if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        client_pub_pkey = X509_get0_pubkey(s->session->peer);\n        if (client_pub_pkey) {\n            if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)\n                ERR_clear_error();\n        }\n        sess_key_len = PACKET_remaining(pkt);\n        if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto gerr;\n        }\n        if (ASN1_get_object ((const unsigned char **)&data, &Tlen, &Ttag,\n                             &Tclass, sess_key_len) != V_ASN1_CONSTRUCTED\n            || Ttag != V_ASN1_SEQUENCE\n            || Tclass != V_ASN1_UNIVERSAL) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_DECRYPTION_FAILED);\n            goto gerr;\n        }\n        start = data;\n        inlen = Tlen;\n        if (EVP_PKEY_decrypt\n            (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,\n                   SSL_R_DECRYPTION_FAILED);\n            goto gerr;\n        }\n        if (!ssl_generate_master_secret(s, premaster_secret,\n                                        sizeof(premaster_secret), 0)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto gerr;\n        }\n        if (EVP_PKEY_CTX_ctrl\n            (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)\n            s->statem.no_cert_verify = 1;\n        EVP_PKEY_CTX_free(pkey_ctx);\n        return MSG_PROCESS_CONTINUE_PROCESSING;\n gerr:\n        EVP_PKEY_CTX_free(pkey_ctx);\n        goto f_err;\n    } else\n#endif\n    {\n        al = SSL_AD_HANDSHAKE_FAILURE;\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);\n        goto f_err;\n    }\n    return MSG_PROCESS_CONTINUE_PROCESSING;\n f_err:\n    ssl3_send_alert(s, SSL3_AL_FATAL, al);\n#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)\n err:\n#endif\n#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)\n    EVP_PKEY_free(ckey);\n#endif\n    OPENSSL_free(rsa_decrypt);\n#ifndef OPENSSL_NO_PSK\n    OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);\n    s->s3->tmp.psk = NULL;\n#endif\n    ossl_statem_set_error(s);\n    return MSG_PROCESS_ERROR;\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n    unsigned int i, m;\n    unsigned int n;\n    BN_ULONG l;\n    BIGNUM *bn = NULL;\n    if (ret == NULL)\n        ret = bn = BN_new();\n    if (ret == NULL)\n        return (NULL);\n    bn_check_top(ret);\n    for ( ; len > 0 && *s == 0; s++, len--)\n        continue;\n    n = len;\n    if (n == 0) {\n        ret->top = 0;\n        return (ret);\n    }\n    i = ((n - 1) / BN_BYTES) + 1;\n    m = ((n - 1) % (BN_BYTES));\n    if (bn_wexpand(ret, (int)i) == NULL) {\n        BN_free(bn);\n        return NULL;\n    }\n    ret->top = i;\n    ret->neg = 0;\n    l = 0;\n    while (n--) {\n        l = (l << 8L) | *(s++);\n        if (m-- == 0) {\n            ret->d[--i] = l;\n            l = 0;\n            m = BN_BYTES - 1;\n        }\n    }\n    bn_correct_top(ret);\n    return (ret);\n}', 'int srp_generate_server_master_secret(SSL *s)\n{\n    BIGNUM *K = NULL, *u = NULL;\n    int ret = -1, tmp_len = 0;\n    unsigned char *tmp = NULL;\n    if (!SRP_Verify_A_mod_N(s->srp_ctx.A, s->srp_ctx.N))\n        goto err;\n    if ((u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N)) == NULL)\n        goto err;\n    if ((K = SRP_Calc_server_key(s->srp_ctx.A, s->srp_ctx.v, u, s->srp_ctx.b,\n                                 s->srp_ctx.N)) == NULL)\n        goto err;\n    tmp_len = BN_num_bytes(K);\n    if ((tmp = OPENSSL_malloc(tmp_len)) == NULL)\n        goto err;\n    BN_bn2bin(K, tmp);\n    ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);\n err:\n    BN_clear_free(K);\n    BN_clear_free(u);\n    return ret;\n}', 'int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)\n{\n    return SRP_Verify_B_mod_N(A, N);\n}', 'int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)\n{\n    BIGNUM *r;\n    BN_CTX *bn_ctx;\n    int ret = 0;\n    if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n        return 0;\n    if ((r = BN_new()) == NULL)\n        goto err;\n    if (!BN_nnmod(r, B, N, bn_ctx))\n        goto err;\n    ret = !BN_is_zero(r);\n err:\n    BN_CTX_free(bn_ctx);\n    BN_free(r);\n    return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n    if (!(BN_mod(r, m, d, ctx)))\n        return 0;\n    if (!r->neg)\n        return 1;\n    return (d->neg ? BN_sub : BN_add) (r, r, d);\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}']