Coverage Report

Created: 2024-11-21 07:03

/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
235
{
15
235
    memset(recp, 0, sizeof(*recp));
16
235
    bn_init(&(recp->N));
17
235
    bn_init(&(recp->Nr));
18
235
}
19
20
BN_RECP_CTX *BN_RECP_CTX_new(void)
21
29
{
22
29
    BN_RECP_CTX *ret;
23
24
29
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
25
0
        return NULL;
26
27
29
    bn_init(&(ret->N));
28
29
    bn_init(&(ret->Nr));
29
29
    ret->flags = BN_FLG_MALLOCED;
30
29
    return ret;
31
29
}
32
33
void BN_RECP_CTX_free(BN_RECP_CTX *recp)
34
469
{
35
469
    if (recp == NULL)
36
205
        return;
37
264
    BN_free(&recp->N);
38
264
    BN_free(&recp->Nr);
39
264
    if (recp->flags & BN_FLG_MALLOCED)
40
29
        OPENSSL_free(recp);
41
264
}
42
43
int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
44
264
{
45
264
    if (BN_is_zero(d) || !BN_copy(&(recp->N), d))
46
4
        return 0;
47
260
    BN_zero(&(recp->Nr));
48
260
    recp->num_bits = BN_num_bits(d);
49
260
    recp->shift = 0;
50
260
    return 1;
51
264
}
52
53
int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
54
                          BN_RECP_CTX *recp, BN_CTX *ctx)
55
301k
{
56
301k
    int ret = 0;
57
301k
    BIGNUM *a;
58
301k
    const BIGNUM *ca;
59
60
301k
    BN_CTX_start(ctx);
61
301k
    if ((a = BN_CTX_get(ctx)) == NULL)
62
0
        goto err;
63
301k
    if (y != NULL) {
64
301k
        if (x == y) {
65
258k
            if (!BN_sqr(a, x, ctx))
66
0
                goto err;
67
258k
        } else {
68
42.3k
            if (!BN_mul(a, x, y, ctx))
69
0
                goto err;
70
42.3k
        }
71
301k
        ca = a;
72
301k
    } else
73
0
        ca = x;                 /* Just do the mod */
74
75
301k
    ret = BN_div_recp(NULL, r, ca, recp, ctx);
76
301k
 err:
77
301k
    BN_CTX_end(ctx);
78
301k
    bn_check_top(r);
79
301k
    return ret;
80
301k
}
81
82
int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
83
                BN_RECP_CTX *recp, BN_CTX *ctx)
84
301k
{
85
301k
    int i, j, ret = 0;
86
301k
    BIGNUM *a, *b, *d, *r;
87
88
301k
    BN_CTX_start(ctx);
89
301k
    d = (dv != NULL) ? dv : BN_CTX_get(ctx);
90
301k
    r = (rem != NULL) ? rem : BN_CTX_get(ctx);
91
301k
    a = BN_CTX_get(ctx);
92
301k
    b = BN_CTX_get(ctx);
93
301k
    if (b == NULL)
94
0
        goto err;
95
96
301k
    if (BN_ucmp(m, &(recp->N)) < 0) {
97
8.37k
        BN_zero(d);
98
8.37k
        if (!BN_copy(r, m)) {
99
0
            BN_CTX_end(ctx);
100
0
            return 0;
101
0
        }
102
8.37k
        BN_CTX_end(ctx);
103
8.37k
        return 1;
104
8.37k
    }
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
292k
    i = BN_num_bits(m);
113
292k
    j = recp->num_bits << 1;
114
292k
    if (j > i)
115
276k
        i = j;
116
117
    /* Nr := round(2^i / N) */
118
292k
    if (i != recp->shift)
119
245
        recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
120
    /* BN_reciprocal could have returned -1 for an error */
121
292k
    if (recp->shift == -1)
122
0
        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
292k
    if (!BN_rshift(a, m, recp->num_bits))
131
0
        goto err;
132
292k
    if (!BN_mul(b, a, &(recp->Nr), ctx))
133
0
        goto err;
134
292k
    if (!BN_rshift(d, b, i - recp->num_bits))
135
0
        goto err;
136
292k
    d->neg = 0;
137
138
292k
    if (!BN_mul(b, &(recp->N), d, ctx))
139
0
        goto err;
140
292k
    if (!BN_usub(r, m, b))
141
0
        goto err;
142
292k
    r->neg = 0;
143
144
292k
    j = 0;
145
535k
    while (BN_ucmp(r, &(recp->N)) >= 0) {
146
242k
        if (j++ > 2) {
147
0
            ERR_raise(ERR_LIB_BN, BN_R_BAD_RECIPROCAL);
148
0
            goto err;
149
0
        }
150
242k
        if (!BN_usub(r, r, &(recp->N)))
151
0
            goto err;
152
242k
        if (!BN_add_word(d, 1))
153
0
            goto err;
154
242k
    }
155
156
292k
    r->neg = BN_is_zero(r) ? 0 : m->neg;
157
292k
    d->neg = m->neg ^ recp->N.neg;
158
292k
    ret = 1;
159
292k
 err:
160
292k
    BN_CTX_end(ctx);
161
292k
    bn_check_top(dv);
162
292k
    bn_check_top(rem);
163
292k
    return ret;
164
292k
}
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
245
{
174
245
    int ret = -1;
175
245
    BIGNUM *t;
176
177
245
    BN_CTX_start(ctx);
178
245
    if ((t = BN_CTX_get(ctx)) == NULL)
179
0
        goto err;
180
181
245
    if (!BN_set_bit(t, len))
182
0
        goto err;
183
184
245
    if (!BN_div(r, NULL, t, m, ctx))
185
0
        goto err;
186
187
245
    ret = len;
188
245
 err:
189
245
    bn_check_top(r);
190
245
    BN_CTX_end(ctx);
191
245
    return ret;
192
245
}