Coverage Report

Created: 2024-11-21 06:47

/src/openssl/crypto/bn/bn_recp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/cryptlib.h"
11
#include "bn_local.h"
12
13
void BN_RECP_CTX_init(BN_RECP_CTX *recp)
14
1.22k
{
15
1.22k
    memset(recp, 0, sizeof(*recp));
16
1.22k
    bn_init(&(recp->N));
17
1.22k
    bn_init(&(recp->Nr));
18
1.22k
}
19
20
BN_RECP_CTX *BN_RECP_CTX_new(void)
21
3.11k
{
22
3.11k
    BN_RECP_CTX *ret;
23
24
3.11k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
25
0
        return NULL;
26
27
3.11k
    bn_init(&(ret->N));
28
3.11k
    bn_init(&(ret->Nr));
29
3.11k
    ret->flags = BN_FLG_MALLOCED;
30
3.11k
    return ret;
31
3.11k
}
32
33
void BN_RECP_CTX_free(BN_RECP_CTX *recp)
34
4.33k
{
35
4.33k
    if (recp == NULL)
36
0
        return;
37
4.33k
    BN_free(&recp->N);
38
4.33k
    BN_free(&recp->Nr);
39
4.33k
    if (recp->flags & BN_FLG_MALLOCED)
40
3.11k
        OPENSSL_free(recp);
41
4.33k
}
42
43
int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
44
4.33k
{
45
4.33k
    if (BN_is_zero(d) || !BN_copy(&(recp->N), d))
46
34
        return 0;
47
4.30k
    BN_zero(&(recp->Nr));
48
4.30k
    recp->num_bits = BN_num_bits(d);
49
4.30k
    recp->shift = 0;
50
4.30k
    return 1;
51
4.33k
}
52
53
int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
54
                          BN_RECP_CTX *recp, BN_CTX *ctx)
55
4.43M
{
56
4.43M
    int ret = 0;
57
4.43M
    BIGNUM *a;
58
4.43M
    const BIGNUM *ca;
59
60
4.43M
    BN_CTX_start(ctx);
61
4.43M
    if ((a = BN_CTX_get(ctx)) == NULL)
62
0
        goto err;
63
4.43M
    if (y != NULL) {
64
4.43M
        if (x == y) {
65
3.87M
            if (!BN_sqr(a, x, ctx))
66
0
                goto err;
67
3.87M
        } else {
68
555k
            if (!BN_mul(a, x, y, ctx))
69
0
                goto err;
70
555k
        }
71
4.43M
        ca = a;
72
4.43M
    } else
73
0
        ca = x;                 /* Just do the mod */
74
75
4.43M
    ret = BN_div_recp(NULL, r, ca, recp, ctx);
76
4.43M
 err:
77
4.43M
    BN_CTX_end(ctx);
78
4.43M
    bn_check_top(r);
79
4.43M
    return ret;
80
4.43M
}
81
82
int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
83
                BN_RECP_CTX *recp, BN_CTX *ctx)
84
4.43M
{
85
4.43M
    int i, j, ret = 0;
86
4.43M
    BIGNUM *a, *b, *d, *r;
87
88
4.43M
    BN_CTX_start(ctx);
89
4.43M
    d = (dv != NULL) ? dv : BN_CTX_get(ctx);
90
4.43M
    r = (rem != NULL) ? rem : BN_CTX_get(ctx);
91
4.43M
    a = BN_CTX_get(ctx);
92
4.43M
    b = BN_CTX_get(ctx);
93
4.43M
    if (b == NULL)
94
0
        goto err;
95
96
4.43M
    if (BN_ucmp(m, &(recp->N)) < 0) {
97
221k
        BN_zero(d);
98
221k
        if (!BN_copy(r, m)) {
99
0
            BN_CTX_end(ctx);
100
0
            return 0;
101
0
        }
102
221k
        BN_CTX_end(ctx);
103
221k
        return 1;
104
221k
    }
105
106
    /*
107
     * We want the remainder Given input of ABCDEF / ab we need multiply
108
     * ABCDEF by 3 digests of the reciprocal of ab
109
     */
110
111
    /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */
112
4.21M
    i = BN_num_bits(m);
113
4.21M
    j = recp->num_bits << 1;
114
4.21M
    if (j > i)
115
3.89M
        i = j;
116
117
    /* Nr := round(2^i / N) */
118
4.21M
    if (i != recp->shift)
119
3.24k
        recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
120
    /* BN_reciprocal could have returned -1 for an error */
121
4.21M
    if (recp->shift == -1)
122
15
        goto err;
123
124
    /*-
125
     * d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))|
126
     *    = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))|
127
     *   <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|
128
     *    = |m/N|
129
     */
130
4.21M
    if (!BN_rshift(a, m, recp->num_bits))
131
0
        goto err;
132
4.21M
    if (!BN_mul(b, a, &(recp->Nr), ctx))
133
0
        goto err;
134
4.21M
    if (!BN_rshift(d, b, i - recp->num_bits))
135
0
        goto err;
136
4.21M
    d->neg = 0;
137
138
4.21M
    if (!BN_mul(b, &(recp->N), d, ctx))
139
0
        goto err;
140
4.21M
    if (!BN_usub(r, m, b))
141
0
        goto err;
142
4.21M
    r->neg = 0;
143
144
4.21M
    j = 0;
145
7.47M
    while (BN_ucmp(r, &(recp->N)) >= 0) {
146
3.25M
        if (j++ > 2) {
147
6
            ERR_raise(ERR_LIB_BN, BN_R_BAD_RECIPROCAL);
148
6
            goto err;
149
6
        }
150
3.25M
        if (!BN_usub(r, r, &(recp->N)))
151
0
            goto err;
152
3.25M
        if (!BN_add_word(d, 1))
153
0
            goto err;
154
3.25M
    }
155
156
4.21M
    r->neg = BN_is_zero(r) ? 0 : m->neg;
157
4.21M
    d->neg = m->neg ^ recp->N.neg;
158
4.21M
    ret = 1;
159
4.21M
 err:
160
4.21M
    BN_CTX_end(ctx);
161
4.21M
    bn_check_top(dv);
162
4.21M
    bn_check_top(rem);
163
4.21M
    return ret;
164
4.21M
}
165
166
/*
167
 * len is the expected size of the result We actually calculate with an extra
168
 * word of precision, so we can do faster division if the remainder is not
169
 * required.
170
 */
171
/* r := 2^len / m */
172
int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
173
3.24k
{
174
3.24k
    int ret = -1;
175
3.24k
    BIGNUM *t;
176
177
3.24k
    BN_CTX_start(ctx);
178
3.24k
    if ((t = BN_CTX_get(ctx)) == NULL)
179
0
        goto err;
180
181
3.24k
    if (!BN_set_bit(t, len))
182
0
        goto err;
183
184
3.24k
    if (!BN_div(r, NULL, t, m, ctx))
185
15
        goto err;
186
187
3.22k
    ret = len;
188
3.24k
 err:
189
3.24k
    bn_check_top(r);
190
3.24k
    BN_CTX_end(ctx);
191
3.24k
    return ret;
192
3.22k
}