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