/src/openssl/crypto/bn/bn_exp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "internal/constant_time_locl.h" |
12 | | #include "bn_lcl.h" |
13 | | |
14 | | #include <stdlib.h> |
15 | | #ifdef _WIN32 |
16 | | # include <malloc.h> |
17 | | # ifndef alloca |
18 | | # define alloca _alloca |
19 | | # endif |
20 | | #elif defined(__GNUC__) |
21 | | # ifndef alloca |
22 | | # define alloca(s) __builtin_alloca((s)) |
23 | | # endif |
24 | | #elif defined(__sun) |
25 | | # include <alloca.h> |
26 | | #endif |
27 | | |
28 | | #include "rsaz_exp.h" |
29 | | |
30 | | #undef SPARC_T4_MONT |
31 | | #if defined(OPENSSL_BN_ASM_MONT) && (defined(__sparc__) || defined(__sparc)) |
32 | | # include "sparc_arch.h" |
33 | | extern unsigned int OPENSSL_sparcv9cap_P[]; |
34 | | # define SPARC_T4_MONT |
35 | | #endif |
36 | | |
37 | | /* maximum precomputation table size for *variable* sliding windows */ |
38 | | #define TABLE_SIZE 32 |
39 | | |
40 | | /* this one works - simple but works */ |
41 | | int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) |
42 | 0 | { |
43 | 0 | int i, bits, ret = 0; |
44 | 0 | BIGNUM *v, *rr; |
45 | 0 |
|
46 | 0 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 |
47 | 0 | || BN_get_flags(a, BN_FLG_CONSTTIME) != 0) { |
48 | 0 | /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */ |
49 | 0 | BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
50 | 0 | return 0; |
51 | 0 | } |
52 | 0 |
|
53 | 0 | BN_CTX_start(ctx); |
54 | 0 | rr = ((r == a) || (r == p)) ? BN_CTX_get(ctx) : r; |
55 | 0 | v = BN_CTX_get(ctx); |
56 | 0 | if (rr == NULL || v == NULL) |
57 | 0 | goto err; |
58 | 0 | |
59 | 0 | if (BN_copy(v, a) == NULL) |
60 | 0 | goto err; |
61 | 0 | bits = BN_num_bits(p); |
62 | 0 |
|
63 | 0 | if (BN_is_odd(p)) { |
64 | 0 | if (BN_copy(rr, a) == NULL) |
65 | 0 | goto err; |
66 | 0 | } else { |
67 | 0 | if (!BN_one(rr)) |
68 | 0 | goto err; |
69 | 0 | } |
70 | 0 | |
71 | 0 | for (i = 1; i < bits; i++) { |
72 | 0 | if (!BN_sqr(v, v, ctx)) |
73 | 0 | goto err; |
74 | 0 | if (BN_is_bit_set(p, i)) { |
75 | 0 | if (!BN_mul(rr, rr, v, ctx)) |
76 | 0 | goto err; |
77 | 0 | } |
78 | 0 | } |
79 | 0 | if (r != rr && BN_copy(r, rr) == NULL) |
80 | 0 | goto err; |
81 | 0 | |
82 | 0 | ret = 1; |
83 | 0 | err: |
84 | 0 | BN_CTX_end(ctx); |
85 | 0 | bn_check_top(r); |
86 | 0 | return ret; |
87 | 0 | } |
88 | | |
89 | | int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, |
90 | | BN_CTX *ctx) |
91 | 0 | { |
92 | 0 | int ret; |
93 | 0 |
|
94 | 0 | bn_check_top(a); |
95 | 0 | bn_check_top(p); |
96 | 0 | bn_check_top(m); |
97 | 0 |
|
98 | 0 | /*- |
99 | 0 | * For even modulus m = 2^k*m_odd, it might make sense to compute |
100 | 0 | * a^p mod m_odd and a^p mod 2^k separately (with Montgomery |
101 | 0 | * exponentiation for the odd part), using appropriate exponent |
102 | 0 | * reductions, and combine the results using the CRT. |
103 | 0 | * |
104 | 0 | * For now, we use Montgomery only if the modulus is odd; otherwise, |
105 | 0 | * exponentiation using the reciprocal-based quick remaindering |
106 | 0 | * algorithm is used. |
107 | 0 | * |
108 | 0 | * (Timing obtained with expspeed.c [computations a^p mod m |
109 | 0 | * where a, p, m are of the same length: 256, 512, 1024, 2048, |
110 | 0 | * 4096, 8192 bits], compared to the running time of the |
111 | 0 | * standard algorithm: |
112 | 0 | * |
113 | 0 | * BN_mod_exp_mont 33 .. 40 % [AMD K6-2, Linux, debug configuration] |
114 | 0 | * 55 .. 77 % [UltraSparc processor, but |
115 | 0 | * debug-solaris-sparcv8-gcc conf.] |
116 | 0 | * |
117 | 0 | * BN_mod_exp_recp 50 .. 70 % [AMD K6-2, Linux, debug configuration] |
118 | 0 | * 62 .. 118 % [UltraSparc, debug-solaris-sparcv8-gcc] |
119 | 0 | * |
120 | 0 | * On the Sparc, BN_mod_exp_recp was faster than BN_mod_exp_mont |
121 | 0 | * at 2048 and more bits, but at 512 and 1024 bits, it was |
122 | 0 | * slower even than the standard algorithm! |
123 | 0 | * |
124 | 0 | * "Real" timings [linux-elf, solaris-sparcv9-gcc configurations] |
125 | 0 | * should be obtained when the new Montgomery reduction code |
126 | 0 | * has been integrated into OpenSSL.) |
127 | 0 | */ |
128 | 0 |
|
129 | 0 | #define MONT_MUL_MOD |
130 | 0 | #define MONT_EXP_WORD |
131 | 0 | #define RECP_MUL_MOD |
132 | 0 |
|
133 | 0 | #ifdef MONT_MUL_MOD |
134 | 0 | if (BN_is_odd(m)) { |
135 | 0 | # ifdef MONT_EXP_WORD |
136 | 0 | if (a->top == 1 && !a->neg |
137 | 0 | && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0) |
138 | 0 | && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0) |
139 | 0 | && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) { |
140 | 0 | BN_ULONG A = a->d[0]; |
141 | 0 | ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL); |
142 | 0 | } else |
143 | 0 | # endif |
144 | 0 | ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL); |
145 | 0 | } else |
146 | 0 | #endif |
147 | 0 | #ifdef RECP_MUL_MOD |
148 | 0 | { |
149 | 0 | ret = BN_mod_exp_recp(r, a, p, m, ctx); |
150 | 0 | } |
151 | | #else |
152 | | { |
153 | | ret = BN_mod_exp_simple(r, a, p, m, ctx); |
154 | | } |
155 | | #endif |
156 | |
|
157 | 0 | bn_check_top(r); |
158 | 0 | return ret; |
159 | 0 | } |
160 | | |
161 | | int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
162 | | const BIGNUM *m, BN_CTX *ctx) |
163 | 0 | { |
164 | 0 | int i, j, bits, ret = 0, wstart, wend, window, wvalue; |
165 | 0 | int start = 1; |
166 | 0 | BIGNUM *aa; |
167 | 0 | /* Table of variables obtained from 'ctx' */ |
168 | 0 | BIGNUM *val[TABLE_SIZE]; |
169 | 0 | BN_RECP_CTX recp; |
170 | 0 |
|
171 | 0 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 |
172 | 0 | || BN_get_flags(a, BN_FLG_CONSTTIME) != 0 |
173 | 0 | || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) { |
174 | 0 | /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */ |
175 | 0 | BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
176 | 0 | return 0; |
177 | 0 | } |
178 | 0 |
|
179 | 0 | bits = BN_num_bits(p); |
180 | 0 | if (bits == 0) { |
181 | 0 | /* x**0 mod 1, or x**0 mod -1 is still zero. */ |
182 | 0 | if (BN_abs_is_word(m, 1)) { |
183 | 0 | ret = 1; |
184 | 0 | BN_zero(r); |
185 | 0 | } else { |
186 | 0 | ret = BN_one(r); |
187 | 0 | } |
188 | 0 | return ret; |
189 | 0 | } |
190 | 0 |
|
191 | 0 | BN_CTX_start(ctx); |
192 | 0 | aa = BN_CTX_get(ctx); |
193 | 0 | val[0] = BN_CTX_get(ctx); |
194 | 0 | if (val[0] == NULL) |
195 | 0 | goto err; |
196 | 0 | |
197 | 0 | BN_RECP_CTX_init(&recp); |
198 | 0 | if (m->neg) { |
199 | 0 | /* ignore sign of 'm' */ |
200 | 0 | if (!BN_copy(aa, m)) |
201 | 0 | goto err; |
202 | 0 | aa->neg = 0; |
203 | 0 | if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0) |
204 | 0 | goto err; |
205 | 0 | } else { |
206 | 0 | if (BN_RECP_CTX_set(&recp, m, ctx) <= 0) |
207 | 0 | goto err; |
208 | 0 | } |
209 | 0 | |
210 | 0 | if (!BN_nnmod(val[0], a, m, ctx)) |
211 | 0 | goto err; /* 1 */ |
212 | 0 | if (BN_is_zero(val[0])) { |
213 | 0 | BN_zero(r); |
214 | 0 | ret = 1; |
215 | 0 | goto err; |
216 | 0 | } |
217 | 0 |
|
218 | 0 | window = BN_window_bits_for_exponent_size(bits); |
219 | 0 | if (window > 1) { |
220 | 0 | if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx)) |
221 | 0 | goto err; /* 2 */ |
222 | 0 | j = 1 << (window - 1); |
223 | 0 | for (i = 1; i < j; i++) { |
224 | 0 | if (((val[i] = BN_CTX_get(ctx)) == NULL) || |
225 | 0 | !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx)) |
226 | 0 | goto err; |
227 | 0 | } |
228 | 0 | } |
229 | 0 |
|
230 | 0 | start = 1; /* This is used to avoid multiplication etc |
231 | 0 | * when there is only the value '1' in the |
232 | 0 | * buffer. */ |
233 | 0 | wvalue = 0; /* The 'value' of the window */ |
234 | 0 | wstart = bits - 1; /* The top bit of the window */ |
235 | 0 | wend = 0; /* The bottom bit of the window */ |
236 | 0 |
|
237 | 0 | if (!BN_one(r)) |
238 | 0 | goto err; |
239 | 0 | |
240 | 0 | for (;;) { |
241 | 0 | if (BN_is_bit_set(p, wstart) == 0) { |
242 | 0 | if (!start) |
243 | 0 | if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) |
244 | 0 | goto err; |
245 | 0 | if (wstart == 0) |
246 | 0 | break; |
247 | 0 | wstart--; |
248 | 0 | continue; |
249 | 0 | } |
250 | 0 | /* |
251 | 0 | * We now have wstart on a 'set' bit, we now need to work out how bit |
252 | 0 | * a window to do. To do this we need to scan forward until the last |
253 | 0 | * set bit before the end of the window |
254 | 0 | */ |
255 | 0 | j = wstart; |
256 | 0 | wvalue = 1; |
257 | 0 | wend = 0; |
258 | 0 | for (i = 1; i < window; i++) { |
259 | 0 | if (wstart - i < 0) |
260 | 0 | break; |
261 | 0 | if (BN_is_bit_set(p, wstart - i)) { |
262 | 0 | wvalue <<= (i - wend); |
263 | 0 | wvalue |= 1; |
264 | 0 | wend = i; |
265 | 0 | } |
266 | 0 | } |
267 | 0 |
|
268 | 0 | /* wend is the size of the current window */ |
269 | 0 | j = wend + 1; |
270 | 0 | /* add the 'bytes above' */ |
271 | 0 | if (!start) |
272 | 0 | for (i = 0; i < j; i++) { |
273 | 0 | if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) |
274 | 0 | goto err; |
275 | 0 | } |
276 | 0 |
|
277 | 0 | /* wvalue will be an odd number < 2^window */ |
278 | 0 | if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx)) |
279 | 0 | goto err; |
280 | 0 | |
281 | 0 | /* move the 'window' down further */ |
282 | 0 | wstart -= wend + 1; |
283 | 0 | wvalue = 0; |
284 | 0 | start = 0; |
285 | 0 | if (wstart < 0) |
286 | 0 | break; |
287 | 0 | } |
288 | 0 | ret = 1; |
289 | 0 | err: |
290 | 0 | BN_CTX_end(ctx); |
291 | 0 | BN_RECP_CTX_free(&recp); |
292 | 0 | bn_check_top(r); |
293 | 0 | return ret; |
294 | 0 | } |
295 | | |
296 | | int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, |
297 | | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) |
298 | 0 | { |
299 | 0 | int i, j, bits, ret = 0, wstart, wend, window, wvalue; |
300 | 0 | int start = 1; |
301 | 0 | BIGNUM *d, *r; |
302 | 0 | const BIGNUM *aa; |
303 | 0 | /* Table of variables obtained from 'ctx' */ |
304 | 0 | BIGNUM *val[TABLE_SIZE]; |
305 | 0 | BN_MONT_CTX *mont = NULL; |
306 | 0 |
|
307 | 0 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 |
308 | 0 | || BN_get_flags(a, BN_FLG_CONSTTIME) != 0 |
309 | 0 | || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) { |
310 | 0 | return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont); |
311 | 0 | } |
312 | 0 | |
313 | 0 | bn_check_top(a); |
314 | 0 | bn_check_top(p); |
315 | 0 | bn_check_top(m); |
316 | 0 |
|
317 | 0 | if (!BN_is_odd(m)) { |
318 | 0 | BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS); |
319 | 0 | return 0; |
320 | 0 | } |
321 | 0 | bits = BN_num_bits(p); |
322 | 0 | if (bits == 0) { |
323 | 0 | /* x**0 mod 1, or x**0 mod -1 is still zero. */ |
324 | 0 | if (BN_abs_is_word(m, 1)) { |
325 | 0 | ret = 1; |
326 | 0 | BN_zero(rr); |
327 | 0 | } else { |
328 | 0 | ret = BN_one(rr); |
329 | 0 | } |
330 | 0 | return ret; |
331 | 0 | } |
332 | 0 |
|
333 | 0 | BN_CTX_start(ctx); |
334 | 0 | d = BN_CTX_get(ctx); |
335 | 0 | r = BN_CTX_get(ctx); |
336 | 0 | val[0] = BN_CTX_get(ctx); |
337 | 0 | if (val[0] == NULL) |
338 | 0 | goto err; |
339 | 0 | |
340 | 0 | /* |
341 | 0 | * If this is not done, things will break in the montgomery part |
342 | 0 | */ |
343 | 0 | |
344 | 0 | if (in_mont != NULL) |
345 | 0 | mont = in_mont; |
346 | 0 | else { |
347 | 0 | if ((mont = BN_MONT_CTX_new()) == NULL) |
348 | 0 | goto err; |
349 | 0 | if (!BN_MONT_CTX_set(mont, m, ctx)) |
350 | 0 | goto err; |
351 | 0 | } |
352 | 0 | |
353 | 0 | if (a->neg || BN_ucmp(a, m) >= 0) { |
354 | 0 | if (!BN_nnmod(val[0], a, m, ctx)) |
355 | 0 | goto err; |
356 | 0 | aa = val[0]; |
357 | 0 | } else |
358 | 0 | aa = a; |
359 | 0 | if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx)) |
360 | 0 | goto err; /* 1 */ |
361 | 0 | |
362 | 0 | window = BN_window_bits_for_exponent_size(bits); |
363 | 0 | if (window > 1) { |
364 | 0 | if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx)) |
365 | 0 | goto err; /* 2 */ |
366 | 0 | j = 1 << (window - 1); |
367 | 0 | for (i = 1; i < j; i++) { |
368 | 0 | if (((val[i] = BN_CTX_get(ctx)) == NULL) || |
369 | 0 | !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx)) |
370 | 0 | goto err; |
371 | 0 | } |
372 | 0 | } |
373 | 0 |
|
374 | 0 | start = 1; /* This is used to avoid multiplication etc |
375 | 0 | * when there is only the value '1' in the |
376 | 0 | * buffer. */ |
377 | 0 | wvalue = 0; /* The 'value' of the window */ |
378 | 0 | wstart = bits - 1; /* The top bit of the window */ |
379 | 0 | wend = 0; /* The bottom bit of the window */ |
380 | 0 |
|
381 | 0 | #if 1 /* by Shay Gueron's suggestion */ |
382 | 0 | j = m->top; /* borrow j */ |
383 | 0 | if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) { |
384 | 0 | if (bn_wexpand(r, j) == NULL) |
385 | 0 | goto err; |
386 | 0 | /* 2^(top*BN_BITS2) - m */ |
387 | 0 | r->d[0] = (0 - m->d[0]) & BN_MASK2; |
388 | 0 | for (i = 1; i < j; i++) |
389 | 0 | r->d[i] = (~m->d[i]) & BN_MASK2; |
390 | 0 | r->top = j; |
391 | 0 | r->flags |= BN_FLG_FIXED_TOP; |
392 | 0 | } else |
393 | 0 | #endif |
394 | 0 | if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx)) |
395 | 0 | goto err; |
396 | 0 | for (;;) { |
397 | 0 | if (BN_is_bit_set(p, wstart) == 0) { |
398 | 0 | if (!start) { |
399 | 0 | if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx)) |
400 | 0 | goto err; |
401 | 0 | } |
402 | 0 | if (wstart == 0) |
403 | 0 | break; |
404 | 0 | wstart--; |
405 | 0 | continue; |
406 | 0 | } |
407 | 0 | /* |
408 | 0 | * We now have wstart on a 'set' bit, we now need to work out how bit |
409 | 0 | * a window to do. To do this we need to scan forward until the last |
410 | 0 | * set bit before the end of the window |
411 | 0 | */ |
412 | 0 | j = wstart; |
413 | 0 | wvalue = 1; |
414 | 0 | wend = 0; |
415 | 0 | for (i = 1; i < window; i++) { |
416 | 0 | if (wstart - i < 0) |
417 | 0 | break; |
418 | 0 | if (BN_is_bit_set(p, wstart - i)) { |
419 | 0 | wvalue <<= (i - wend); |
420 | 0 | wvalue |= 1; |
421 | 0 | wend = i; |
422 | 0 | } |
423 | 0 | } |
424 | 0 |
|
425 | 0 | /* wend is the size of the current window */ |
426 | 0 | j = wend + 1; |
427 | 0 | /* add the 'bytes above' */ |
428 | 0 | if (!start) |
429 | 0 | for (i = 0; i < j; i++) { |
430 | 0 | if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx)) |
431 | 0 | goto err; |
432 | 0 | } |
433 | 0 |
|
434 | 0 | /* wvalue will be an odd number < 2^window */ |
435 | 0 | if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx)) |
436 | 0 | goto err; |
437 | 0 | |
438 | 0 | /* move the 'window' down further */ |
439 | 0 | wstart -= wend + 1; |
440 | 0 | wvalue = 0; |
441 | 0 | start = 0; |
442 | 0 | if (wstart < 0) |
443 | 0 | break; |
444 | 0 | } |
445 | 0 | /* |
446 | 0 | * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery |
447 | 0 | * removes padding [if any] and makes return value suitable for public |
448 | 0 | * API consumer. |
449 | 0 | */ |
450 | | #if defined(SPARC_T4_MONT) |
451 | | if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) { |
452 | | j = mont->N.top; /* borrow j */ |
453 | | val[0]->d[0] = 1; /* borrow val[0] */ |
454 | | for (i = 1; i < j; i++) |
455 | | val[0]->d[i] = 0; |
456 | | val[0]->top = j; |
457 | | if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx)) |
458 | | goto err; |
459 | | } else |
460 | | #endif |
461 | 0 | if (!BN_from_montgomery(rr, r, mont, ctx)) |
462 | 0 | goto err; |
463 | 0 | ret = 1; |
464 | 0 | err: |
465 | 0 | if (in_mont == NULL) |
466 | 0 | BN_MONT_CTX_free(mont); |
467 | 0 | BN_CTX_end(ctx); |
468 | 0 | bn_check_top(rr); |
469 | 0 | return ret; |
470 | 0 | } |
471 | | |
472 | | static BN_ULONG bn_get_bits(const BIGNUM *a, int bitpos) |
473 | 0 | { |
474 | 0 | BN_ULONG ret = 0; |
475 | 0 | int wordpos; |
476 | 0 |
|
477 | 0 | wordpos = bitpos / BN_BITS2; |
478 | 0 | bitpos %= BN_BITS2; |
479 | 0 | if (wordpos >= 0 && wordpos < a->top) { |
480 | 0 | ret = a->d[wordpos] & BN_MASK2; |
481 | 0 | if (bitpos) { |
482 | 0 | ret >>= bitpos; |
483 | 0 | if (++wordpos < a->top) |
484 | 0 | ret |= a->d[wordpos] << (BN_BITS2 - bitpos); |
485 | 0 | } |
486 | 0 | } |
487 | 0 |
|
488 | 0 | return ret & BN_MASK2; |
489 | 0 | } |
490 | | |
491 | | /* |
492 | | * BN_mod_exp_mont_consttime() stores the precomputed powers in a specific |
493 | | * layout so that accessing any of these table values shows the same access |
494 | | * pattern as far as cache lines are concerned. The following functions are |
495 | | * used to transfer a BIGNUM from/to that table. |
496 | | */ |
497 | | |
498 | | static int MOD_EXP_CTIME_COPY_TO_PREBUF(const BIGNUM *b, int top, |
499 | | unsigned char *buf, int idx, |
500 | | int window) |
501 | 0 | { |
502 | 0 | int i, j; |
503 | 0 | int width = 1 << window; |
504 | 0 | BN_ULONG *table = (BN_ULONG *)buf; |
505 | 0 |
|
506 | 0 | if (top > b->top) |
507 | 0 | top = b->top; /* this works because 'buf' is explicitly |
508 | 0 | * zeroed */ |
509 | 0 | for (i = 0, j = idx; i < top; i++, j += width) { |
510 | 0 | table[j] = b->d[i]; |
511 | 0 | } |
512 | 0 |
|
513 | 0 | return 1; |
514 | 0 | } |
515 | | |
516 | | static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top, |
517 | | unsigned char *buf, int idx, |
518 | | int window) |
519 | 0 | { |
520 | 0 | int i, j; |
521 | 0 | int width = 1 << window; |
522 | 0 | /* |
523 | 0 | * We declare table 'volatile' in order to discourage compiler |
524 | 0 | * from reordering loads from the table. Concern is that if |
525 | 0 | * reordered in specific manner loads might give away the |
526 | 0 | * information we are trying to conceal. Some would argue that |
527 | 0 | * compiler can reorder them anyway, but it can as well be |
528 | 0 | * argued that doing so would be violation of standard... |
529 | 0 | */ |
530 | 0 | volatile BN_ULONG *table = (volatile BN_ULONG *)buf; |
531 | 0 |
|
532 | 0 | if (bn_wexpand(b, top) == NULL) |
533 | 0 | return 0; |
534 | 0 | |
535 | 0 | if (window <= 3) { |
536 | 0 | for (i = 0; i < top; i++, table += width) { |
537 | 0 | BN_ULONG acc = 0; |
538 | 0 |
|
539 | 0 | for (j = 0; j < width; j++) { |
540 | 0 | acc |= table[j] & |
541 | 0 | ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1)); |
542 | 0 | } |
543 | 0 |
|
544 | 0 | b->d[i] = acc; |
545 | 0 | } |
546 | 0 | } else { |
547 | 0 | int xstride = 1 << (window - 2); |
548 | 0 | BN_ULONG y0, y1, y2, y3; |
549 | 0 |
|
550 | 0 | i = idx >> (window - 2); /* equivalent of idx / xstride */ |
551 | 0 | idx &= xstride - 1; /* equivalent of idx % xstride */ |
552 | 0 |
|
553 | 0 | y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1); |
554 | 0 | y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1); |
555 | 0 | y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1); |
556 | 0 | y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1); |
557 | 0 |
|
558 | 0 | for (i = 0; i < top; i++, table += width) { |
559 | 0 | BN_ULONG acc = 0; |
560 | 0 |
|
561 | 0 | for (j = 0; j < xstride; j++) { |
562 | 0 | acc |= ( (table[j + 0 * xstride] & y0) | |
563 | 0 | (table[j + 1 * xstride] & y1) | |
564 | 0 | (table[j + 2 * xstride] & y2) | |
565 | 0 | (table[j + 3 * xstride] & y3) ) |
566 | 0 | & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1)); |
567 | 0 | } |
568 | 0 |
|
569 | 0 | b->d[i] = acc; |
570 | 0 | } |
571 | 0 | } |
572 | 0 |
|
573 | 0 | b->top = top; |
574 | 0 | b->flags |= BN_FLG_FIXED_TOP; |
575 | 0 | return 1; |
576 | 0 | } |
577 | | |
578 | | /* |
579 | | * Given a pointer value, compute the next address that is a cache line |
580 | | * multiple. |
581 | | */ |
582 | | #define MOD_EXP_CTIME_ALIGN(x_) \ |
583 | 0 | ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK)))) |
584 | | |
585 | | /* |
586 | | * This variant of BN_mod_exp_mont() uses fixed windows and the special |
587 | | * precomputation memory layout to limit data-dependency to a minimum to |
588 | | * protect secret exponents (cf. the hyper-threading timing attacks pointed |
589 | | * out by Colin Percival, |
590 | | * http://www.daemonology.net/hyperthreading-considered-harmful/) |
591 | | */ |
592 | | int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, |
593 | | const BIGNUM *m, BN_CTX *ctx, |
594 | | BN_MONT_CTX *in_mont) |
595 | 0 | { |
596 | 0 | int i, bits, ret = 0, window, wvalue, wmask, window0; |
597 | 0 | int top; |
598 | 0 | BN_MONT_CTX *mont = NULL; |
599 | 0 |
|
600 | 0 | int numPowers; |
601 | 0 | unsigned char *powerbufFree = NULL; |
602 | 0 | int powerbufLen = 0; |
603 | 0 | unsigned char *powerbuf = NULL; |
604 | 0 | BIGNUM tmp, am; |
605 | | #if defined(SPARC_T4_MONT) |
606 | | unsigned int t4 = 0; |
607 | | #endif |
608 | |
|
609 | 0 | bn_check_top(a); |
610 | 0 | bn_check_top(p); |
611 | 0 | bn_check_top(m); |
612 | 0 |
|
613 | 0 | if (!BN_is_odd(m)) { |
614 | 0 | BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS); |
615 | 0 | return 0; |
616 | 0 | } |
617 | 0 |
|
618 | 0 | top = m->top; |
619 | 0 |
|
620 | 0 | /* |
621 | 0 | * Use all bits stored in |p|, rather than |BN_num_bits|, so we do not leak |
622 | 0 | * whether the top bits are zero. |
623 | 0 | */ |
624 | 0 | bits = p->top * BN_BITS2; |
625 | 0 | if (bits == 0) { |
626 | 0 | /* x**0 mod 1, or x**0 mod -1 is still zero. */ |
627 | 0 | if (BN_abs_is_word(m, 1)) { |
628 | 0 | ret = 1; |
629 | 0 | BN_zero(rr); |
630 | 0 | } else { |
631 | 0 | ret = BN_one(rr); |
632 | 0 | } |
633 | 0 | return ret; |
634 | 0 | } |
635 | 0 |
|
636 | 0 | BN_CTX_start(ctx); |
637 | 0 |
|
638 | 0 | /* |
639 | 0 | * Allocate a montgomery context if it was not supplied by the caller. If |
640 | 0 | * this is not done, things will break in the montgomery part. |
641 | 0 | */ |
642 | 0 | if (in_mont != NULL) |
643 | 0 | mont = in_mont; |
644 | 0 | else { |
645 | 0 | if ((mont = BN_MONT_CTX_new()) == NULL) |
646 | 0 | goto err; |
647 | 0 | if (!BN_MONT_CTX_set(mont, m, ctx)) |
648 | 0 | goto err; |
649 | 0 | } |
650 | 0 | |
651 | 0 | #ifdef RSAZ_ENABLED |
652 | 0 | if (!a->neg) { |
653 | 0 | /* |
654 | 0 | * If the size of the operands allow it, perform the optimized |
655 | 0 | * RSAZ exponentiation. For further information see |
656 | 0 | * crypto/bn/rsaz_exp.c and accompanying assembly modules. |
657 | 0 | */ |
658 | 0 | if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024) |
659 | 0 | && rsaz_avx2_eligible()) { |
660 | 0 | if (NULL == bn_wexpand(rr, 16)) |
661 | 0 | goto err; |
662 | 0 | RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d, |
663 | 0 | mont->n0[0]); |
664 | 0 | rr->top = 16; |
665 | 0 | rr->neg = 0; |
666 | 0 | bn_correct_top(rr); |
667 | 0 | ret = 1; |
668 | 0 | goto err; |
669 | 0 | } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) { |
670 | 0 | if (NULL == bn_wexpand(rr, 8)) |
671 | 0 | goto err; |
672 | 0 | RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d); |
673 | 0 | rr->top = 8; |
674 | 0 | rr->neg = 0; |
675 | 0 | bn_correct_top(rr); |
676 | 0 | ret = 1; |
677 | 0 | goto err; |
678 | 0 | } |
679 | 0 | } |
680 | 0 | #endif |
681 | 0 |
|
682 | 0 | /* Get the window size to use with size of p. */ |
683 | 0 | window = BN_window_bits_for_ctime_exponent_size(bits); |
684 | | #if defined(SPARC_T4_MONT) |
685 | | if (window >= 5 && (top & 15) == 0 && top <= 64 && |
686 | | (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) == |
687 | | (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0])) |
688 | | window = 5; |
689 | | else |
690 | | #endif |
691 | | #if defined(OPENSSL_BN_ASM_MONT5) |
692 | 0 | if (window >= 5) { |
693 | 0 | window = 5; /* ~5% improvement for RSA2048 sign, and even |
694 | 0 | * for RSA4096 */ |
695 | 0 | /* reserve space for mont->N.d[] copy */ |
696 | 0 | powerbufLen += top * sizeof(mont->N.d[0]); |
697 | 0 | } |
698 | 0 | #endif |
699 | 0 | (void)0; |
700 | 0 |
|
701 | 0 | /* |
702 | 0 | * Allocate a buffer large enough to hold all of the pre-computed powers |
703 | 0 | * of am, am itself and tmp. |
704 | 0 | */ |
705 | 0 | numPowers = 1 << window; |
706 | 0 | powerbufLen += sizeof(m->d[0]) * (top * numPowers + |
707 | 0 | ((2 * top) > |
708 | 0 | numPowers ? (2 * top) : numPowers)); |
709 | 0 | #ifdef alloca |
710 | 0 | if (powerbufLen < 3072) |
711 | 0 | powerbufFree = |
712 | 0 | alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH); |
713 | 0 | else |
714 | 0 | #endif |
715 | 0 | if ((powerbufFree = |
716 | 0 | OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) |
717 | 0 | == NULL) |
718 | 0 | goto err; |
719 | 0 | |
720 | 0 | powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree); |
721 | 0 | memset(powerbuf, 0, powerbufLen); |
722 | 0 |
|
723 | 0 | #ifdef alloca |
724 | 0 | if (powerbufLen < 3072) |
725 | 0 | powerbufFree = NULL; |
726 | 0 | #endif |
727 | 0 |
|
728 | 0 | /* lay down tmp and am right after powers table */ |
729 | 0 | tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers); |
730 | 0 | am.d = tmp.d + top; |
731 | 0 | tmp.top = am.top = 0; |
732 | 0 | tmp.dmax = am.dmax = top; |
733 | 0 | tmp.neg = am.neg = 0; |
734 | 0 | tmp.flags = am.flags = BN_FLG_STATIC_DATA; |
735 | 0 |
|
736 | 0 | /* prepare a^0 in Montgomery domain */ |
737 | 0 | #if 1 /* by Shay Gueron's suggestion */ |
738 | 0 | if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) { |
739 | 0 | /* 2^(top*BN_BITS2) - m */ |
740 | 0 | tmp.d[0] = (0 - m->d[0]) & BN_MASK2; |
741 | 0 | for (i = 1; i < top; i++) |
742 | 0 | tmp.d[i] = (~m->d[i]) & BN_MASK2; |
743 | 0 | tmp.top = top; |
744 | 0 | } else |
745 | 0 | #endif |
746 | 0 | if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx)) |
747 | 0 | goto err; |
748 | 0 | |
749 | 0 | /* prepare a^1 in Montgomery domain */ |
750 | 0 | if (a->neg || BN_ucmp(a, m) >= 0) { |
751 | 0 | if (!BN_nnmod(&am, a, m, ctx)) |
752 | 0 | goto err; |
753 | 0 | if (!bn_to_mont_fixed_top(&am, &am, mont, ctx)) |
754 | 0 | goto err; |
755 | 0 | } else if (!bn_to_mont_fixed_top(&am, a, mont, ctx)) |
756 | 0 | goto err; |
757 | 0 | |
758 | | #if defined(SPARC_T4_MONT) |
759 | | if (t4) { |
760 | | typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np, |
761 | | const BN_ULONG *n0, const void *table, |
762 | | int power, int bits); |
763 | | int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np, |
764 | | const BN_ULONG *n0, const void *table, |
765 | | int power, int bits); |
766 | | int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np, |
767 | | const BN_ULONG *n0, const void *table, |
768 | | int power, int bits); |
769 | | int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np, |
770 | | const BN_ULONG *n0, const void *table, |
771 | | int power, int bits); |
772 | | int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np, |
773 | | const BN_ULONG *n0, const void *table, |
774 | | int power, int bits); |
775 | | static const bn_pwr5_mont_f pwr5_funcs[4] = { |
776 | | bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16, |
777 | | bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32 |
778 | | }; |
779 | | bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1]; |
780 | | |
781 | | typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap, |
782 | | const void *bp, const BN_ULONG *np, |
783 | | const BN_ULONG *n0); |
784 | | int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp, |
785 | | const BN_ULONG *np, const BN_ULONG *n0); |
786 | | int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap, |
787 | | const void *bp, const BN_ULONG *np, |
788 | | const BN_ULONG *n0); |
789 | | int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap, |
790 | | const void *bp, const BN_ULONG *np, |
791 | | const BN_ULONG *n0); |
792 | | int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap, |
793 | | const void *bp, const BN_ULONG *np, |
794 | | const BN_ULONG *n0); |
795 | | static const bn_mul_mont_f mul_funcs[4] = { |
796 | | bn_mul_mont_t4_8, bn_mul_mont_t4_16, |
797 | | bn_mul_mont_t4_24, bn_mul_mont_t4_32 |
798 | | }; |
799 | | bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1]; |
800 | | |
801 | | void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, |
802 | | const void *bp, const BN_ULONG *np, |
803 | | const BN_ULONG *n0, int num); |
804 | | void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap, |
805 | | const void *bp, const BN_ULONG *np, |
806 | | const BN_ULONG *n0, int num); |
807 | | void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap, |
808 | | const void *table, const BN_ULONG *np, |
809 | | const BN_ULONG *n0, int num, int power); |
810 | | void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num, |
811 | | void *table, size_t power); |
812 | | void bn_gather5_t4(BN_ULONG *out, size_t num, |
813 | | void *table, size_t power); |
814 | | void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num); |
815 | | |
816 | | BN_ULONG *np = mont->N.d, *n0 = mont->n0; |
817 | | int stride = 5 * (6 - (top / 16 - 1)); /* multiple of 5, but less |
818 | | * than 32 */ |
819 | | |
820 | | /* |
821 | | * BN_to_montgomery can contaminate words above .top [in |
822 | | * BN_DEBUG[_DEBUG] build]... |
823 | | */ |
824 | | for (i = am.top; i < top; i++) |
825 | | am.d[i] = 0; |
826 | | for (i = tmp.top; i < top; i++) |
827 | | tmp.d[i] = 0; |
828 | | |
829 | | bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0); |
830 | | bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1); |
831 | | if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) && |
832 | | !(*mul_worker) (tmp.d, am.d, am.d, np, n0)) |
833 | | bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top); |
834 | | bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2); |
835 | | |
836 | | for (i = 3; i < 32; i++) { |
837 | | /* Calculate a^i = a^(i-1) * a */ |
838 | | if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) && |
839 | | !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0)) |
840 | | bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top); |
841 | | bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i); |
842 | | } |
843 | | |
844 | | /* switch to 64-bit domain */ |
845 | | np = alloca(top * sizeof(BN_ULONG)); |
846 | | top /= 2; |
847 | | bn_flip_t4(np, mont->N.d, top); |
848 | | |
849 | | /* |
850 | | * The exponent may not have a whole number of fixed-size windows. |
851 | | * To simplify the main loop, the initial window has between 1 and |
852 | | * full-window-size bits such that what remains is always a whole |
853 | | * number of windows |
854 | | */ |
855 | | window0 = (bits - 1) % 5 + 1; |
856 | | wmask = (1 << window0) - 1; |
857 | | bits -= window0; |
858 | | wvalue = bn_get_bits(p, bits) & wmask; |
859 | | bn_gather5_t4(tmp.d, top, powerbuf, wvalue); |
860 | | |
861 | | /* |
862 | | * Scan the exponent one window at a time starting from the most |
863 | | * significant bits. |
864 | | */ |
865 | | while (bits > 0) { |
866 | | if (bits < stride) |
867 | | stride = bits; |
868 | | bits -= stride; |
869 | | wvalue = bn_get_bits(p, bits); |
870 | | |
871 | | if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride)) |
872 | | continue; |
873 | | /* retry once and fall back */ |
874 | | if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride)) |
875 | | continue; |
876 | | |
877 | | bits += stride - 5; |
878 | | wvalue >>= stride - 5; |
879 | | wvalue &= 31; |
880 | | bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); |
881 | | bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); |
882 | | bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); |
883 | | bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); |
884 | | bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); |
885 | | bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top, |
886 | | wvalue); |
887 | | } |
888 | | |
889 | | bn_flip_t4(tmp.d, tmp.d, top); |
890 | | top *= 2; |
891 | | /* back to 32-bit domain */ |
892 | | tmp.top = top; |
893 | | bn_correct_top(&tmp); |
894 | | OPENSSL_cleanse(np, top * sizeof(BN_ULONG)); |
895 | | } else |
896 | | #endif |
897 | | #if defined(OPENSSL_BN_ASM_MONT5) |
898 | 0 | if (window == 5 && top > 1) { |
899 | 0 | /* |
900 | 0 | * This optimization uses ideas from http://eprint.iacr.org/2011/239, |
901 | 0 | * specifically optimization of cache-timing attack countermeasures |
902 | 0 | * and pre-computation optimization. |
903 | 0 | */ |
904 | 0 |
|
905 | 0 | /* |
906 | 0 | * Dedicated window==4 case improves 512-bit RSA sign by ~15%, but as |
907 | 0 | * 512-bit RSA is hardly relevant, we omit it to spare size... |
908 | 0 | */ |
909 | 0 | void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap, |
910 | 0 | const void *table, const BN_ULONG *np, |
911 | 0 | const BN_ULONG *n0, int num, int power); |
912 | 0 | void bn_scatter5(const BN_ULONG *inp, size_t num, |
913 | 0 | void *table, size_t power); |
914 | 0 | void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power); |
915 | 0 | void bn_power5(BN_ULONG *rp, const BN_ULONG *ap, |
916 | 0 | const void *table, const BN_ULONG *np, |
917 | 0 | const BN_ULONG *n0, int num, int power); |
918 | 0 | int bn_get_bits5(const BN_ULONG *ap, int off); |
919 | 0 | int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap, |
920 | 0 | const BN_ULONG *not_used, const BN_ULONG *np, |
921 | 0 | const BN_ULONG *n0, int num); |
922 | 0 |
|
923 | 0 | BN_ULONG *n0 = mont->n0, *np; |
924 | 0 |
|
925 | 0 | /* |
926 | 0 | * BN_to_montgomery can contaminate words above .top [in |
927 | 0 | * BN_DEBUG[_DEBUG] build]... |
928 | 0 | */ |
929 | 0 | for (i = am.top; i < top; i++) |
930 | 0 | am.d[i] = 0; |
931 | 0 | for (i = tmp.top; i < top; i++) |
932 | 0 | tmp.d[i] = 0; |
933 | 0 |
|
934 | 0 | /* |
935 | 0 | * copy mont->N.d[] to improve cache locality |
936 | 0 | */ |
937 | 0 | for (np = am.d + top, i = 0; i < top; i++) |
938 | 0 | np[i] = mont->N.d[i]; |
939 | 0 |
|
940 | 0 | bn_scatter5(tmp.d, top, powerbuf, 0); |
941 | 0 | bn_scatter5(am.d, am.top, powerbuf, 1); |
942 | 0 | bn_mul_mont(tmp.d, am.d, am.d, np, n0, top); |
943 | 0 | bn_scatter5(tmp.d, top, powerbuf, 2); |
944 | 0 |
|
945 | | # if 0 |
946 | | for (i = 3; i < 32; i++) { |
947 | | /* Calculate a^i = a^(i-1) * a */ |
948 | | bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); |
949 | | bn_scatter5(tmp.d, top, powerbuf, i); |
950 | | } |
951 | | # else |
952 | | /* same as above, but uses squaring for 1/2 of operations */ |
953 | 0 | for (i = 4; i < 32; i *= 2) { |
954 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
955 | 0 | bn_scatter5(tmp.d, top, powerbuf, i); |
956 | 0 | } |
957 | 0 | for (i = 3; i < 8; i += 2) { |
958 | 0 | int j; |
959 | 0 | bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); |
960 | 0 | bn_scatter5(tmp.d, top, powerbuf, i); |
961 | 0 | for (j = 2 * i; j < 32; j *= 2) { |
962 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
963 | 0 | bn_scatter5(tmp.d, top, powerbuf, j); |
964 | 0 | } |
965 | 0 | } |
966 | 0 | for (; i < 16; i += 2) { |
967 | 0 | bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); |
968 | 0 | bn_scatter5(tmp.d, top, powerbuf, i); |
969 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
970 | 0 | bn_scatter5(tmp.d, top, powerbuf, 2 * i); |
971 | 0 | } |
972 | 0 | for (; i < 32; i += 2) { |
973 | 0 | bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); |
974 | 0 | bn_scatter5(tmp.d, top, powerbuf, i); |
975 | 0 | } |
976 | 0 | # endif |
977 | 0 | /* |
978 | 0 | * The exponent may not have a whole number of fixed-size windows. |
979 | 0 | * To simplify the main loop, the initial window has between 1 and |
980 | 0 | * full-window-size bits such that what remains is always a whole |
981 | 0 | * number of windows |
982 | 0 | */ |
983 | 0 | window0 = (bits - 1) % 5 + 1; |
984 | 0 | wmask = (1 << window0) - 1; |
985 | 0 | bits -= window0; |
986 | 0 | wvalue = bn_get_bits(p, bits) & wmask; |
987 | 0 | bn_gather5(tmp.d, top, powerbuf, wvalue); |
988 | 0 |
|
989 | 0 | /* |
990 | 0 | * Scan the exponent one window at a time starting from the most |
991 | 0 | * significant bits. |
992 | 0 | */ |
993 | 0 | if (top & 7) { |
994 | 0 | while (bits > 0) { |
995 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
996 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
997 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
998 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
999 | 0 | bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); |
1000 | 0 | bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top, |
1001 | 0 | bn_get_bits5(p->d, bits -= 5)); |
1002 | 0 | } |
1003 | 0 | } else { |
1004 | 0 | while (bits > 0) { |
1005 | 0 | bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, |
1006 | 0 | bn_get_bits5(p->d, bits -= 5)); |
1007 | 0 | } |
1008 | 0 | } |
1009 | 0 |
|
1010 | 0 | ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top); |
1011 | 0 | tmp.top = top; |
1012 | 0 | bn_correct_top(&tmp); |
1013 | 0 | if (ret) { |
1014 | 0 | if (!BN_copy(rr, &tmp)) |
1015 | 0 | ret = 0; |
1016 | 0 | goto err; /* non-zero ret means it's not error */ |
1017 | 0 | } |
1018 | 0 | } else |
1019 | 0 | #endif |
1020 | 0 | { |
1021 | 0 | if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window)) |
1022 | 0 | goto err; |
1023 | 0 | if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window)) |
1024 | 0 | goto err; |
1025 | 0 | |
1026 | 0 | /* |
1027 | 0 | * If the window size is greater than 1, then calculate |
1028 | 0 | * val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) (even |
1029 | 0 | * powers could instead be computed as (a^(i/2))^2 to use the slight |
1030 | 0 | * performance advantage of sqr over mul). |
1031 | 0 | */ |
1032 | 0 | if (window > 1) { |
1033 | 0 | if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx)) |
1034 | 0 | goto err; |
1035 | 0 | if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2, |
1036 | 0 | window)) |
1037 | 0 | goto err; |
1038 | 0 | for (i = 3; i < numPowers; i++) { |
1039 | 0 | /* Calculate a^i = a^(i-1) * a */ |
1040 | 0 | if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx)) |
1041 | 0 | goto err; |
1042 | 0 | if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i, |
1043 | 0 | window)) |
1044 | 0 | goto err; |
1045 | 0 | } |
1046 | 0 | } |
1047 | 0 |
|
1048 | 0 | /* |
1049 | 0 | * The exponent may not have a whole number of fixed-size windows. |
1050 | 0 | * To simplify the main loop, the initial window has between 1 and |
1051 | 0 | * full-window-size bits such that what remains is always a whole |
1052 | 0 | * number of windows |
1053 | 0 | */ |
1054 | 0 | window0 = (bits - 1) % window + 1; |
1055 | 0 | wmask = (1 << window0) - 1; |
1056 | 0 | bits -= window0; |
1057 | 0 | wvalue = bn_get_bits(p, bits) & wmask; |
1058 | 0 | if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue, |
1059 | 0 | window)) |
1060 | 0 | goto err; |
1061 | 0 | |
1062 | 0 | wmask = (1 << window) - 1; |
1063 | 0 | /* |
1064 | 0 | * Scan the exponent one window at a time starting from the most |
1065 | 0 | * significant bits. |
1066 | 0 | */ |
1067 | 0 | while (bits > 0) { |
1068 | 0 |
|
1069 | 0 | /* Square the result window-size times */ |
1070 | 0 | for (i = 0; i < window; i++) |
1071 | 0 | if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx)) |
1072 | 0 | goto err; |
1073 | 0 |
|
1074 | 0 | /* |
1075 | 0 | * Get a window's worth of bits from the exponent |
1076 | 0 | * This avoids calling BN_is_bit_set for each bit, which |
1077 | 0 | * is not only slower but also makes each bit vulnerable to |
1078 | 0 | * EM (and likely other) side-channel attacks like One&Done |
1079 | 0 | * (for details see "One&Done: A Single-Decryption EM-Based |
1080 | 0 | * Attack on OpenSSL’s Constant-Time Blinded RSA" by M. Alam, |
1081 | 0 | * H. Khan, M. Dey, N. Sinha, R. Callan, A. Zajic, and |
1082 | 0 | * M. Prvulovic, in USENIX Security'18) |
1083 | 0 | */ |
1084 | 0 | bits -= window; |
1085 | 0 | wvalue = bn_get_bits(p, bits) & wmask; |
1086 | 0 | /* |
1087 | 0 | * Fetch the appropriate pre-computed value from the pre-buf |
1088 | 0 | */ |
1089 | 0 | if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue, |
1090 | 0 | window)) |
1091 | 0 | goto err; |
1092 | 0 | |
1093 | 0 | /* Multiply the result into the intermediate result */ |
1094 | 0 | if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx)) |
1095 | 0 | goto err; |
1096 | 0 | } |
1097 | 0 | } |
1098 | 0 |
|
1099 | 0 | /* |
1100 | 0 | * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery |
1101 | 0 | * removes padding [if any] and makes return value suitable for public |
1102 | 0 | * API consumer. |
1103 | 0 | */ |
1104 | | #if defined(SPARC_T4_MONT) |
1105 | | if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) { |
1106 | | am.d[0] = 1; /* borrow am */ |
1107 | | for (i = 1; i < top; i++) |
1108 | | am.d[i] = 0; |
1109 | | if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx)) |
1110 | | goto err; |
1111 | | } else |
1112 | | #endif |
1113 | 0 | if (!BN_from_montgomery(rr, &tmp, mont, ctx)) |
1114 | 0 | goto err; |
1115 | 0 | ret = 1; |
1116 | 0 | err: |
1117 | 0 | if (in_mont == NULL) |
1118 | 0 | BN_MONT_CTX_free(mont); |
1119 | 0 | if (powerbuf != NULL) { |
1120 | 0 | OPENSSL_cleanse(powerbuf, powerbufLen); |
1121 | 0 | OPENSSL_free(powerbufFree); |
1122 | 0 | } |
1123 | 0 | BN_CTX_end(ctx); |
1124 | 0 | return ret; |
1125 | 0 | } |
1126 | | |
1127 | | int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p, |
1128 | | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) |
1129 | 0 | { |
1130 | 0 | BN_MONT_CTX *mont = NULL; |
1131 | 0 | int b, bits, ret = 0; |
1132 | 0 | int r_is_one; |
1133 | 0 | BN_ULONG w, next_w; |
1134 | 0 | BIGNUM *r, *t; |
1135 | 0 | BIGNUM *swap_tmp; |
1136 | 0 | #define BN_MOD_MUL_WORD(r, w, m) \ |
1137 | 0 | (BN_mul_word(r, (w)) && \ |
1138 | 0 | (/* BN_ucmp(r, (m)) < 0 ? 1 :*/ \ |
1139 | 0 | (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1)))) |
1140 | 0 | /* |
1141 | 0 | * BN_MOD_MUL_WORD is only used with 'w' large, so the BN_ucmp test is |
1142 | 0 | * probably more overhead than always using BN_mod (which uses BN_copy if |
1143 | 0 | * a similar test returns true). |
1144 | 0 | */ |
1145 | 0 | /* |
1146 | 0 | * We can use BN_mod and do not need BN_nnmod because our accumulator is |
1147 | 0 | * never negative (the result of BN_mod does not depend on the sign of |
1148 | 0 | * the modulus). |
1149 | 0 | */ |
1150 | 0 | #define BN_TO_MONTGOMERY_WORD(r, w, mont) \ |
1151 | 0 | (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx)) |
1152 | 0 |
|
1153 | 0 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 |
1154 | 0 | || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) { |
1155 | 0 | /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */ |
1156 | 0 | BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
1157 | 0 | return 0; |
1158 | 0 | } |
1159 | 0 |
|
1160 | 0 | bn_check_top(p); |
1161 | 0 | bn_check_top(m); |
1162 | 0 |
|
1163 | 0 | if (!BN_is_odd(m)) { |
1164 | 0 | BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS); |
1165 | 0 | return 0; |
1166 | 0 | } |
1167 | 0 | if (m->top == 1) |
1168 | 0 | a %= m->d[0]; /* make sure that 'a' is reduced */ |
1169 | 0 |
|
1170 | 0 | bits = BN_num_bits(p); |
1171 | 0 | if (bits == 0) { |
1172 | 0 | /* x**0 mod 1, or x**0 mod -1 is still zero. */ |
1173 | 0 | if (BN_abs_is_word(m, 1)) { |
1174 | 0 | ret = 1; |
1175 | 0 | BN_zero(rr); |
1176 | 0 | } else { |
1177 | 0 | ret = BN_one(rr); |
1178 | 0 | } |
1179 | 0 | return ret; |
1180 | 0 | } |
1181 | 0 | if (a == 0) { |
1182 | 0 | BN_zero(rr); |
1183 | 0 | ret = 1; |
1184 | 0 | return ret; |
1185 | 0 | } |
1186 | 0 |
|
1187 | 0 | BN_CTX_start(ctx); |
1188 | 0 | r = BN_CTX_get(ctx); |
1189 | 0 | t = BN_CTX_get(ctx); |
1190 | 0 | if (t == NULL) |
1191 | 0 | goto err; |
1192 | 0 | |
1193 | 0 | if (in_mont != NULL) |
1194 | 0 | mont = in_mont; |
1195 | 0 | else { |
1196 | 0 | if ((mont = BN_MONT_CTX_new()) == NULL) |
1197 | 0 | goto err; |
1198 | 0 | if (!BN_MONT_CTX_set(mont, m, ctx)) |
1199 | 0 | goto err; |
1200 | 0 | } |
1201 | 0 | |
1202 | 0 | r_is_one = 1; /* except for Montgomery factor */ |
1203 | 0 |
|
1204 | 0 | /* bits-1 >= 0 */ |
1205 | 0 |
|
1206 | 0 | /* The result is accumulated in the product r*w. */ |
1207 | 0 | w = a; /* bit 'bits-1' of 'p' is always set */ |
1208 | 0 | for (b = bits - 2; b >= 0; b--) { |
1209 | 0 | /* First, square r*w. */ |
1210 | 0 | next_w = w * w; |
1211 | 0 | if ((next_w / w) != w) { /* overflow */ |
1212 | 0 | if (r_is_one) { |
1213 | 0 | if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) |
1214 | 0 | goto err; |
1215 | 0 | r_is_one = 0; |
1216 | 0 | } else { |
1217 | 0 | if (!BN_MOD_MUL_WORD(r, w, m)) |
1218 | 0 | goto err; |
1219 | 0 | } |
1220 | 0 | next_w = 1; |
1221 | 0 | } |
1222 | 0 | w = next_w; |
1223 | 0 | if (!r_is_one) { |
1224 | 0 | if (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) |
1225 | 0 | goto err; |
1226 | 0 | } |
1227 | 0 | |
1228 | 0 | /* Second, multiply r*w by 'a' if exponent bit is set. */ |
1229 | 0 | if (BN_is_bit_set(p, b)) { |
1230 | 0 | next_w = w * a; |
1231 | 0 | if ((next_w / a) != w) { /* overflow */ |
1232 | 0 | if (r_is_one) { |
1233 | 0 | if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) |
1234 | 0 | goto err; |
1235 | 0 | r_is_one = 0; |
1236 | 0 | } else { |
1237 | 0 | if (!BN_MOD_MUL_WORD(r, w, m)) |
1238 | 0 | goto err; |
1239 | 0 | } |
1240 | 0 | next_w = a; |
1241 | 0 | } |
1242 | 0 | w = next_w; |
1243 | 0 | } |
1244 | 0 | } |
1245 | 0 |
|
1246 | 0 | /* Finally, set r:=r*w. */ |
1247 | 0 | if (w != 1) { |
1248 | 0 | if (r_is_one) { |
1249 | 0 | if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) |
1250 | 0 | goto err; |
1251 | 0 | r_is_one = 0; |
1252 | 0 | } else { |
1253 | 0 | if (!BN_MOD_MUL_WORD(r, w, m)) |
1254 | 0 | goto err; |
1255 | 0 | } |
1256 | 0 | } |
1257 | 0 | |
1258 | 0 | if (r_is_one) { /* can happen only if a == 1 */ |
1259 | 0 | if (!BN_one(rr)) |
1260 | 0 | goto err; |
1261 | 0 | } else { |
1262 | 0 | if (!BN_from_montgomery(rr, r, mont, ctx)) |
1263 | 0 | goto err; |
1264 | 0 | } |
1265 | 0 | ret = 1; |
1266 | 0 | err: |
1267 | 0 | if (in_mont == NULL) |
1268 | 0 | BN_MONT_CTX_free(mont); |
1269 | 0 | BN_CTX_end(ctx); |
1270 | 0 | bn_check_top(rr); |
1271 | 0 | return ret; |
1272 | 0 | } |
1273 | | |
1274 | | /* The old fallback, simple version :-) */ |
1275 | | int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
1276 | | const BIGNUM *m, BN_CTX *ctx) |
1277 | 0 | { |
1278 | 0 | int i, j, bits, ret = 0, wstart, wend, window, wvalue; |
1279 | 0 | int start = 1; |
1280 | 0 | BIGNUM *d; |
1281 | 0 | /* Table of variables obtained from 'ctx' */ |
1282 | 0 | BIGNUM *val[TABLE_SIZE]; |
1283 | 0 |
|
1284 | 0 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 |
1285 | 0 | || BN_get_flags(a, BN_FLG_CONSTTIME) != 0 |
1286 | 0 | || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) { |
1287 | 0 | /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */ |
1288 | 0 | BNerr(BN_F_BN_MOD_EXP_SIMPLE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
1289 | 0 | return 0; |
1290 | 0 | } |
1291 | 0 |
|
1292 | 0 | bits = BN_num_bits(p); |
1293 | 0 | if (bits == 0) { |
1294 | 0 | /* x**0 mod 1, or x**0 mod -1 is still zero. */ |
1295 | 0 | if (BN_abs_is_word(m, 1)) { |
1296 | 0 | ret = 1; |
1297 | 0 | BN_zero(r); |
1298 | 0 | } else { |
1299 | 0 | ret = BN_one(r); |
1300 | 0 | } |
1301 | 0 | return ret; |
1302 | 0 | } |
1303 | 0 |
|
1304 | 0 | BN_CTX_start(ctx); |
1305 | 0 | d = BN_CTX_get(ctx); |
1306 | 0 | val[0] = BN_CTX_get(ctx); |
1307 | 0 | if (val[0] == NULL) |
1308 | 0 | goto err; |
1309 | 0 | |
1310 | 0 | if (!BN_nnmod(val[0], a, m, ctx)) |
1311 | 0 | goto err; /* 1 */ |
1312 | 0 | if (BN_is_zero(val[0])) { |
1313 | 0 | BN_zero(r); |
1314 | 0 | ret = 1; |
1315 | 0 | goto err; |
1316 | 0 | } |
1317 | 0 |
|
1318 | 0 | window = BN_window_bits_for_exponent_size(bits); |
1319 | 0 | if (window > 1) { |
1320 | 0 | if (!BN_mod_mul(d, val[0], val[0], m, ctx)) |
1321 | 0 | goto err; /* 2 */ |
1322 | 0 | j = 1 << (window - 1); |
1323 | 0 | for (i = 1; i < j; i++) { |
1324 | 0 | if (((val[i] = BN_CTX_get(ctx)) == NULL) || |
1325 | 0 | !BN_mod_mul(val[i], val[i - 1], d, m, ctx)) |
1326 | 0 | goto err; |
1327 | 0 | } |
1328 | 0 | } |
1329 | 0 |
|
1330 | 0 | start = 1; /* This is used to avoid multiplication etc |
1331 | 0 | * when there is only the value '1' in the |
1332 | 0 | * buffer. */ |
1333 | 0 | wvalue = 0; /* The 'value' of the window */ |
1334 | 0 | wstart = bits - 1; /* The top bit of the window */ |
1335 | 0 | wend = 0; /* The bottom bit of the window */ |
1336 | 0 |
|
1337 | 0 | if (!BN_one(r)) |
1338 | 0 | goto err; |
1339 | 0 | |
1340 | 0 | for (;;) { |
1341 | 0 | if (BN_is_bit_set(p, wstart) == 0) { |
1342 | 0 | if (!start) |
1343 | 0 | if (!BN_mod_mul(r, r, r, m, ctx)) |
1344 | 0 | goto err; |
1345 | 0 | if (wstart == 0) |
1346 | 0 | break; |
1347 | 0 | wstart--; |
1348 | 0 | continue; |
1349 | 0 | } |
1350 | 0 | /* |
1351 | 0 | * We now have wstart on a 'set' bit, we now need to work out how bit |
1352 | 0 | * a window to do. To do this we need to scan forward until the last |
1353 | 0 | * set bit before the end of the window |
1354 | 0 | */ |
1355 | 0 | j = wstart; |
1356 | 0 | wvalue = 1; |
1357 | 0 | wend = 0; |
1358 | 0 | for (i = 1; i < window; i++) { |
1359 | 0 | if (wstart - i < 0) |
1360 | 0 | break; |
1361 | 0 | if (BN_is_bit_set(p, wstart - i)) { |
1362 | 0 | wvalue <<= (i - wend); |
1363 | 0 | wvalue |= 1; |
1364 | 0 | wend = i; |
1365 | 0 | } |
1366 | 0 | } |
1367 | 0 |
|
1368 | 0 | /* wend is the size of the current window */ |
1369 | 0 | j = wend + 1; |
1370 | 0 | /* add the 'bytes above' */ |
1371 | 0 | if (!start) |
1372 | 0 | for (i = 0; i < j; i++) { |
1373 | 0 | if (!BN_mod_mul(r, r, r, m, ctx)) |
1374 | 0 | goto err; |
1375 | 0 | } |
1376 | 0 |
|
1377 | 0 | /* wvalue will be an odd number < 2^window */ |
1378 | 0 | if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx)) |
1379 | 0 | goto err; |
1380 | 0 | |
1381 | 0 | /* move the 'window' down further */ |
1382 | 0 | wstart -= wend + 1; |
1383 | 0 | wvalue = 0; |
1384 | 0 | start = 0; |
1385 | 0 | if (wstart < 0) |
1386 | 0 | break; |
1387 | 0 | } |
1388 | 0 | ret = 1; |
1389 | 0 | err: |
1390 | 0 | BN_CTX_end(ctx); |
1391 | 0 | bn_check_top(r); |
1392 | 0 | return ret; |
1393 | 0 | } |