Coverage Report

Created: 2025-12-10 06:24

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