Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bn/bn_exp2.c
Line
Count
Source
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
2.86k
{
20
2.86k
    int i, j, bits, b, bits1, bits2, ret = 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;
21
2.86k
    int r_is_one = 1;
22
2.86k
    BIGNUM *d, *r;
23
2.86k
    const BIGNUM *a_mod_m;
24
    /* Tables of variables obtained from 'ctx' */
25
2.86k
    BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];
26
2.86k
    BN_MONT_CTX *mont = NULL;
27
28
2.86k
    bn_check_top(a1);
29
2.86k
    bn_check_top(p1);
30
2.86k
    bn_check_top(a2);
31
2.86k
    bn_check_top(p2);
32
2.86k
    bn_check_top(m);
33
34
2.86k
    if (!BN_is_odd(m)) {
35
0
        ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
36
0
        return 0;
37
0
    }
38
2.86k
    bits1 = BN_num_bits(p1);
39
2.86k
    bits2 = BN_num_bits(p2);
40
2.86k
    if ((bits1 == 0) && (bits2 == 0)) {
41
0
        ret = BN_one(rr);
42
0
        return ret;
43
0
    }
44
45
2.86k
    bits = (bits1 > bits2) ? bits1 : bits2;
46
47
2.86k
    BN_CTX_start(ctx);
48
2.86k
    d = BN_CTX_get(ctx);
49
2.86k
    r = BN_CTX_get(ctx);
50
2.86k
    val1[0] = BN_CTX_get(ctx);
51
2.86k
    val2[0] = BN_CTX_get(ctx);
52
2.86k
    if (val2[0] == NULL)
53
0
        goto err;
54
55
2.86k
    if (in_mont != NULL)
56
2.86k
        mont = in_mont;
57
0
    else {
58
0
        if ((mont = BN_MONT_CTX_new()) == NULL)
59
0
            goto err;
60
0
        if (!BN_MONT_CTX_set(mont, m, ctx))
61
0
            goto err;
62
0
    }
63
64
2.86k
    window1 = BN_window_bits_for_exponent_size(bits1);
65
2.86k
    window2 = BN_window_bits_for_exponent_size(bits2);
66
67
    /*
68
     * Build table for a1:   val1[i] := a1^(2*i + 1) mod m  for i = 0 .. 2^(window1-1)
69
     */
70
2.86k
    if (a1->neg || BN_ucmp(a1, m) >= 0) {
71
1.30k
        if (!BN_mod(val1[0], a1, m, ctx))
72
0
            goto err;
73
1.30k
        a_mod_m = val1[0];
74
1.30k
    } else
75
1.56k
        a_mod_m = a1;
76
2.86k
    if (BN_is_zero(a_mod_m)) {
77
11
        BN_zero(rr);
78
11
        ret = 1;
79
11
        goto err;
80
11
    }
81
82
2.85k
    if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))
83
0
        goto err;
84
2.85k
    if (window1 > 1) {
85
2.85k
        if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))
86
0
            goto err;
87
88
2.85k
        j = 1 << (window1 - 1);
89
22.9k
        for (i = 1; i < j; i++) {
90
20.1k
            if (((val1[i] = BN_CTX_get(ctx)) == NULL) || !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))
91
0
                goto err;
92
20.1k
        }
93
2.85k
    }
94
95
    /*
96
     * Build table for a2:   val2[i] := a2^(2*i + 1) mod m  for i = 0 .. 2^(window2-1)
97
     */
98
2.85k
    if (a2->neg || BN_ucmp(a2, m) >= 0) {
99
1.34k
        if (!BN_mod(val2[0], a2, m, ctx))
100
0
            goto err;
101
1.34k
        a_mod_m = val2[0];
102
1.34k
    } else
103
1.51k
        a_mod_m = a2;
104
2.85k
    if (BN_is_zero(a_mod_m)) {
105
43
        BN_zero(rr);
106
43
        ret = 1;
107
43
        goto err;
108
43
    }
109
2.81k
    if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))
110
0
        goto err;
111
2.81k
    if (window2 > 1) {
112
2.79k
        if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))
113
0
            goto err;
114
115
2.79k
        j = 1 << (window2 - 1);
116
22.3k
        for (i = 1; i < j; i++) {
117
19.5k
            if (((val2[i] = BN_CTX_get(ctx)) == NULL) || !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))
118
0
                goto err;
119
19.5k
        }
120
2.79k
    }
121
122
    /* Now compute the power product, using independent windows. */
123
2.81k
    r_is_one = 1;
124
2.81k
    wvalue1 = 0; /* The 'value' of the first window */
125
2.81k
    wvalue2 = 0; /* The 'value' of the second window */
126
2.81k
    wpos1 = 0; /* If wvalue1 > 0, the bottom bit of the
127
                * first window */
128
2.81k
    wpos2 = 0; /* If wvalue2 > 0, the bottom bit of the
129
                * second window */
130
131
2.81k
    if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
132
0
        goto err;
133
559k
    for (b = bits - 1; b >= 0; b--) {
134
557k
        if (!r_is_one) {
135
549k
            if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
136
0
                goto err;
137
549k
        }
138
139
557k
        if (!wvalue1)
140
321k
            if (BN_is_bit_set(p1, b)) {
141
                /*
142
                 * consider bits b-window1+1 .. b for this window
143
                 */
144
111k
                i = b - window1 + 1;
145
211k
                while (!BN_is_bit_set(p1, i)) /* works for i<0 */
146
99.6k
                    i++;
147
111k
                wpos1 = i;
148
111k
                wvalue1 = 1;
149
347k
                for (i = b - 1; i >= wpos1; i--) {
150
235k
                    wvalue1 <<= 1;
151
235k
                    if (BN_is_bit_set(p1, i))
152
166k
                        wvalue1++;
153
235k
                }
154
111k
            }
155
156
557k
        if (!wvalue2)
157
326k
            if (BN_is_bit_set(p2, b)) {
158
                /*
159
                 * consider bits b-window2+1 .. b for this window
160
                 */
161
110k
                i = b - window2 + 1;
162
210k
                while (!BN_is_bit_set(p2, i))
163
100k
                    i++;
164
110k
                wpos2 = i;
165
110k
                wvalue2 = 1;
166
340k
                for (i = b - 1; i >= wpos2; i--) {
167
230k
                    wvalue2 <<= 1;
168
230k
                    if (BN_is_bit_set(p2, i))
169
162k
                        wvalue2++;
170
230k
                }
171
110k
            }
172
173
557k
        if (wvalue1 && b == wpos1) {
174
            /* wvalue1 is odd and < 2^window1 */
175
111k
            if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))
176
0
                goto err;
177
111k
            wvalue1 = 0;
178
111k
            r_is_one = 0;
179
111k
        }
180
181
557k
        if (wvalue2 && b == wpos2) {
182
            /* wvalue2 is odd and < 2^window2 */
183
110k
            if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))
184
0
                goto err;
185
110k
            wvalue2 = 0;
186
110k
            r_is_one = 0;
187
110k
        }
188
557k
    }
189
2.81k
    if (!BN_from_montgomery(rr, r, mont, ctx))
190
0
        goto err;
191
2.81k
    ret = 1;
192
2.86k
err:
193
2.86k
    if (in_mont == NULL)
194
0
        BN_MONT_CTX_free(mont);
195
2.86k
    BN_CTX_end(ctx);
196
2.86k
    bn_check_top(rr);
197
2.86k
    return ret;
198
2.81k
}