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