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;
    }
    nw = n / BN_BITS2;
    if (bn_wexpand(r, a->top + nw + 1) == NULL)
        return 0;
    r->neg = a->neg;
    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;
}
['ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,\n                               const BIGNUM *in_kinv, const BIGNUM *in_r,\n                               EC_KEY *eckey)\n{\n    int ok = 0, i;\n    BIGNUM *kinv = NULL, *s, *m = NULL;\n    const BIGNUM *order, *ckinv;\n    BN_CTX *ctx = NULL;\n    const EC_GROUP *group;\n    ECDSA_SIG *ret;\n    const BIGNUM *priv_key;\n    group = EC_KEY_get0_group(eckey);\n    priv_key = EC_KEY_get0_private_key(eckey);\n    if (group == NULL || priv_key == NULL) {\n        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);\n        return NULL;\n    }\n    if (!EC_KEY_can_sign(eckey)) {\n        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n        return NULL;\n    }\n    ret = ECDSA_SIG_new();\n    if (ret == NULL) {\n        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    ret->r = BN_new();\n    ret->s = BN_new();\n    if (ret->r == NULL || ret->s == NULL) {\n        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    s = ret->s;\n    if ((ctx = BN_CTX_new()) == NULL\n        || (m = BN_new()) == NULL) {\n        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    order = EC_GROUP_get0_order(group);\n    i = BN_num_bits(order);\n    if (8 * dgst_len > i)\n        dgst_len = (i + 7) / 8;\n    if (!BN_bin2bn(dgst, dgst_len, m)) {\n        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n        goto err;\n    }\n    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n        ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n        goto err;\n    }\n    do {\n        if (in_kinv == NULL || in_r == NULL) {\n            if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {\n                ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);\n                goto err;\n            }\n            ckinv = kinv;\n        } else {\n            ckinv = in_kinv;\n            if (BN_copy(ret->r, in_r) == NULL) {\n                ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n                goto err;\n            }\n        }\n        if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)\n            || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {\n            ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n            goto err;\n        }\n        if (!bn_mod_add_fixed_top(s, s, m, order)) {\n            ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n            goto err;\n        }\n        if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)\n            || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {\n            ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n            goto err;\n        }\n        if (BN_is_zero(s)) {\n            if (in_kinv != NULL && in_r != NULL) {\n                ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);\n                goto err;\n            }\n        } else {\n            break;\n        }\n    } while (1);\n    ok = 1;\n err:\n    if (!ok) {\n        ECDSA_SIG_free(ret);\n        ret = NULL;\n    }\n    BN_CTX_free(ctx);\n    BN_clear_free(m);\n    BN_clear_free(kinv);\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}', 'static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,\n                            BIGNUM **kinvp, BIGNUM **rp,\n                            const unsigned char *dgst, int dlen)\n{\n    BN_CTX *ctx = NULL;\n    BIGNUM *k = NULL, *r = NULL, *X = NULL;\n    const BIGNUM *order;\n    EC_POINT *tmp_point = NULL;\n    const EC_GROUP *group;\n    int ret = 0;\n    int order_bits;\n    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {\n        ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);\n        return 0;\n    }\n    if (!EC_KEY_can_sign(eckey)) {\n        ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n        return 0;\n    }\n    if ((ctx = ctx_in) == NULL) {\n        if ((ctx = BN_CTX_new()) == NULL) {\n            ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n            return 0;\n        }\n    }\n    k = BN_new();\n    r = BN_new();\n    X = BN_new();\n    if (k == NULL || r == NULL || X == NULL) {\n        ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if ((tmp_point = EC_POINT_new(group)) == NULL) {\n        ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n        goto err;\n    }\n    order = EC_GROUP_get0_order(group);\n    order_bits = BN_num_bits(order);\n    if (!BN_set_bit(k, order_bits)\n        || !BN_set_bit(r, order_bits)\n        || !BN_set_bit(X, order_bits))\n        goto err;\n    do {\n        do {\n            if (dgst != NULL) {\n                if (!BN_generate_dsa_nonce(k, order,\n                                           EC_KEY_get0_private_key(eckey),\n                                           dgst, dlen, ctx)) {\n                    ECerr(EC_F_ECDSA_SIGN_SETUP,\n                          EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n                    goto err;\n                }\n            } else {\n                if (!BN_priv_rand_range(k, order)) {\n                    ECerr(EC_F_ECDSA_SIGN_SETUP,\n                          EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n                    goto err;\n                }\n            }\n        } while (BN_is_zero(k));\n        if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {\n            ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n            goto err;\n        }\n        if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n            NID_X9_62_prime_field) {\n            if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, X,\n                                                     NULL, ctx)) {\n                ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n                goto err;\n            }\n        }\n#ifndef OPENSSL_NO_EC2M\n        else {\n            if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp_point, X,\n                                                      NULL, ctx)) {\n                ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n                goto err;\n            }\n        }\n#endif\n        if (!BN_nnmod(r, X, order, ctx)) {\n            ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n            goto err;\n        }\n    } while (BN_is_zero(r));\n    if (!ec_group_do_inverse_ord(group, k, k, ctx)) {\n        ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n        goto err;\n    }\n    BN_clear_free(*rp);\n    BN_clear_free(*kinvp);\n    *rp = r;\n    *kinvp = k;\n    ret = 1;\n err:\n    if (!ret) {\n        BN_clear_free(k);\n        BN_clear_free(r);\n    }\n    if (ctx != ctx_in)\n        BN_CTX_free(ctx);\n    EC_POINT_free(tmp_point);\n    BN_clear_free(X);\n    return ret;\n}', 'int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,\n                          const BIGNUM *priv, const unsigned char *message,\n                          size_t message_len, BN_CTX *ctx)\n{\n    SHA512_CTX sha;\n    unsigned char random_bytes[64];\n    unsigned char digest[SHA512_DIGEST_LENGTH];\n    unsigned done, todo;\n    const unsigned num_k_bytes = BN_num_bytes(range) + 8;\n    unsigned char private_bytes[96];\n    unsigned char *k_bytes;\n    int ret = 0;\n    k_bytes = OPENSSL_malloc(num_k_bytes);\n    if (k_bytes == NULL)\n        goto err;\n    todo = sizeof(priv->d[0]) * priv->top;\n    if (todo > sizeof(private_bytes)) {\n        BNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);\n        goto err;\n    }\n    memcpy(private_bytes, priv->d, todo);\n    memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);\n    for (done = 0; done < num_k_bytes;) {\n        if (RAND_priv_bytes(random_bytes, sizeof(random_bytes)) != 1)\n            goto err;\n        SHA512_Init(&sha);\n        SHA512_Update(&sha, &done, sizeof(done));\n        SHA512_Update(&sha, private_bytes, sizeof(private_bytes));\n        SHA512_Update(&sha, message, message_len);\n        SHA512_Update(&sha, random_bytes, sizeof(random_bytes));\n        SHA512_Final(digest, &sha);\n        todo = num_k_bytes - done;\n        if (todo > SHA512_DIGEST_LENGTH)\n            todo = SHA512_DIGEST_LENGTH;\n        memcpy(k_bytes + done, digest, todo);\n        done += todo;\n    }\n    if (!BN_bin2bn(k_bytes, num_k_bytes, out))\n        goto err;\n    if (BN_mod(out, out, range, ctx) != 1)\n        goto err;\n    ret = 1;\n err:\n    OPENSSL_free(k_bytes);\n    OPENSSL_cleanse(private_bytes, sizeof(private_bytes));\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    res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n    tmp = BN_CTX_get(ctx);\n    snum = BN_CTX_get(ctx);\n    sdiv = BN_CTX_get(ctx);\n    if (sdiv == 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.flags = BN_FLG_STATIC_DATA;\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    if (!bn_wexpand(res, (loop + 1)))\n        goto err;\n    res->neg = (num->neg ^ divisor->neg);\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    nw = n / BN_BITS2;\n    if (bn_wexpand(r, a->top + nw + 1) == NULL)\n        return 0;\n    r->neg = a->neg;\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}']