Coverage Report

Created: 2023-09-25 06:41

/src/openssl30/crypto/bn/bn_exp2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2022 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include "bn_local.h"
13
14
#define TABLE_SIZE      32
15
16
int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
17
                     const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
18
                     BN_CTX *ctx, BN_MONT_CTX *in_mont)
19
377
{
20
377
    int i, j, bits, b, bits1, bits2, ret =
21
377
        0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;
22
377
    int r_is_one = 1;
23
377
    BIGNUM *d, *r;
24
377
    const BIGNUM *a_mod_m;
25
    /* Tables of variables obtained from 'ctx' */
26
377
    BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];
27
377
    BN_MONT_CTX *mont = NULL;
28
29
377
    bn_check_top(a1);
30
377
    bn_check_top(p1);
31
377
    bn_check_top(a2);
32
377
    bn_check_top(p2);
33
377
    bn_check_top(m);
34
35
377
    if (!BN_is_odd(m)) {
36
0
        ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
37
0
        return 0;
38
0
    }
39
377
    bits1 = BN_num_bits(p1);
40
377
    bits2 = BN_num_bits(p2);
41
377
    if ((bits1 == 0) && (bits2 == 0)) {
42
0
        ret = BN_one(rr);
43
0
        return ret;
44
0
    }
45
46
377
    bits = (bits1 > bits2) ? bits1 : bits2;
47
48
377
    BN_CTX_start(ctx);
49
377
    d = BN_CTX_get(ctx);
50
377
    r = BN_CTX_get(ctx);
51
377
    val1[0] = BN_CTX_get(ctx);
52
377
    val2[0] = BN_CTX_get(ctx);
53
377
    if (val2[0] == NULL)
54
0
        goto err;
55
56
377
    if (in_mont != NULL)
57
377
        mont = in_mont;
58
0
    else {
59
0
        if ((mont = BN_MONT_CTX_new()) == NULL)
60
0
            goto err;
61
0
        if (!BN_MONT_CTX_set(mont, m, ctx))
62
0
            goto err;
63
0
    }
64
65
377
    window1 = BN_window_bits_for_exponent_size(bits1);
66
377
    window2 = BN_window_bits_for_exponent_size(bits2);
67
68
    /*
69
     * Build table for a1:   val1[i] := a1^(2*i + 1) mod m  for i = 0 .. 2^(window1-1)
70
     */
71
377
    if (a1->neg || BN_ucmp(a1, m) >= 0) {
72
165
        if (!BN_mod(val1[0], a1, m, ctx))
73
0
            goto err;
74
165
        a_mod_m = val1[0];
75
165
    } else
76
212
        a_mod_m = a1;
77
377
    if (BN_is_zero(a_mod_m)) {
78
2
        BN_zero(rr);
79
2
        ret = 1;
80
2
        goto err;
81
2
    }
82
83
375
    if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))
84
0
        goto err;
85
375
    if (window1 > 1) {
86
375
        if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))
87
0
            goto err;
88
89
375
        j = 1 << (window1 - 1);
90
3.00k
        for (i = 1; i < j; i++) {
91
2.62k
            if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||
92
2.62k
                !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))
93
0
                goto err;
94
2.62k
        }
95
375
    }
96
97
    /*
98
     * Build table for a2:   val2[i] := a2^(2*i + 1) mod m  for i = 0 .. 2^(window2-1)
99
     */
100
375
    if (a2->neg || BN_ucmp(a2, m) >= 0) {
101
131
        if (!BN_mod(val2[0], a2, m, ctx))
102
0
            goto err;
103
131
        a_mod_m = val2[0];
104
131
    } else
105
244
        a_mod_m = a2;
106
375
    if (BN_is_zero(a_mod_m)) {
107
5
        BN_zero(rr);
108
5
        ret = 1;
109
5
        goto err;
110
5
    }
111
370
    if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))
112
0
        goto err;
113
370
    if (window2 > 1) {
114
367
        if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))
115
0
            goto err;
116
117
367
        j = 1 << (window2 - 1);
118
2.92k
        for (i = 1; i < j; i++) {
119
2.55k
            if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||
120
2.55k
                !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))
121
0
                goto err;
122
2.55k
        }
123
367
    }
124
125
    /* Now compute the power product, using independent windows. */
126
370
    r_is_one = 1;
127
370
    wvalue1 = 0;                /* The 'value' of the first window */
128
370
    wvalue2 = 0;                /* The 'value' of the second window */
129
370
    wpos1 = 0;                  /* If wvalue1 > 0, the bottom bit of the
130
                                 * first window */
131
370
    wpos2 = 0;                  /* If wvalue2 > 0, the bottom bit of the
132
                                 * second window */
133
134
370
    if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
135
0
        goto err;
136
69.7k
    for (b = bits - 1; b >= 0; b--) {
137
69.4k
        if (!r_is_one) {
138
68.5k
            if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
139
0
                goto err;
140
68.5k
        }
141
142
69.4k
        if (!wvalue1)
143
40.1k
            if (BN_is_bit_set(p1, b)) {
144
                /*
145
                 * consider bits b-window1+1 .. b for this window
146
                 */
147
13.9k
                i = b - window1 + 1;
148
26.5k
                while (!BN_is_bit_set(p1, i)) /* works for i<0 */
149
12.5k
                    i++;
150
13.9k
                wpos1 = i;
151
13.9k
                wvalue1 = 1;
152
43.1k
                for (i = b - 1; i >= wpos1; i--) {
153
29.2k
                    wvalue1 <<= 1;
154
29.2k
                    if (BN_is_bit_set(p1, i))
155
20.6k
                        wvalue1++;
156
29.2k
                }
157
13.9k
            }
158
159
69.4k
        if (!wvalue2)
160
41.0k
            if (BN_is_bit_set(p2, b)) {
161
                /*
162
                 * consider bits b-window2+1 .. b for this window
163
                 */
164
13.6k
                i = b - window2 + 1;
165
26.0k
                while (!BN_is_bit_set(p2, i))
166
12.4k
                    i++;
167
13.6k
                wpos2 = i;
168
13.6k
                wvalue2 = 1;
169
42.0k
                for (i = b - 1; i >= wpos2; i--) {
170
28.3k
                    wvalue2 <<= 1;
171
28.3k
                    if (BN_is_bit_set(p2, i))
172
20.0k
                        wvalue2++;
173
28.3k
                }
174
13.6k
            }
175
176
69.4k
        if (wvalue1 && b == wpos1) {
177
            /* wvalue1 is odd and < 2^window1 */
178
13.9k
            if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))
179
0
                goto err;
180
13.9k
            wvalue1 = 0;
181
13.9k
            r_is_one = 0;
182
13.9k
        }
183
184
69.4k
        if (wvalue2 && b == wpos2) {
185
            /* wvalue2 is odd and < 2^window2 */
186
13.6k
            if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))
187
0
                goto err;
188
13.6k
            wvalue2 = 0;
189
13.6k
            r_is_one = 0;
190
13.6k
        }
191
69.4k
    }
192
370
    if (!BN_from_montgomery(rr, r, mont, ctx))
193
0
        goto err;
194
370
    ret = 1;
195
377
 err:
196
377
    if (in_mont == NULL)
197
0
        BN_MONT_CTX_free(mont);
198
377
    BN_CTX_end(ctx);
199
377
    bn_check_top(rr);
200
377
    return ret;
201
370
}