Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/bn/montgomery.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/bn.h>
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include <openssl/err.h>
13 #include <openssl/mem.h>
14 #include <openssl/thread.h>
15 #include <openssl/type_check.h>
16
17 #include "internal.h"
18 #include "../cpucap/internal.h"
19 #include "../../internal.h"
20
21 #if !defined(OPENSSL_NO_ASM) && \
22 (defined(OPENSSL_LINUX) || defined(OPENSSL_APPLE) || \
23 defined(OPENSSL_OPENBSD) || defined(OPENSSL_FREEBSD) || \
24 defined(OPENSSL_NETBSD) ) && \
25 defined(OPENSSL_AARCH64) && defined(OPENSSL_BN_ASM_MONT)
26
27 #include "../../../third_party/s2n-bignum/s2n-bignum_aws-lc.h"
28
29 #define BN_MONTGOMERY_S2N_BIGNUM_CAPABLE 1
30
31 OPENSSL_INLINE int montgomery_use_s2n_bignum(unsigned int num) {
32 // Use s2n-bignum's functions only if
33 // (1) The ARM architecture has slow multipliers, and
34 // (2) num (which is the number of words) is multiplie of 8, because
35 // s2n-bignum's bignum_emontredc_8n requires it, and
36 // (3) The word size is 64 bits.
37 // (4) CPU has NEON.
38 assert(S2NBIGNUM_KSQR_16_32_TEMP_NWORDS <= S2NBIGNUM_KMUL_32_64_TEMP_NWORDS &&
39 S2NBIGNUM_KSQR_32_64_TEMP_NWORDS <= S2NBIGNUM_KMUL_32_64_TEMP_NWORDS &&
40 S2NBIGNUM_KMUL_16_32_TEMP_NWORDS <= S2NBIGNUM_KMUL_32_64_TEMP_NWORDS);
41 assert(BN_BITS2 == 64);
42 return !CRYPTO_is_ARMv8_wide_multiplier_capable() &&
43 (num % 8 == 0) &&
44 CRYPTO_is_NEON_capable();
45 }
46
47 #else
48
49 OPENSSL_INLINE int montgomery_use_s2n_bignum(unsigned int num) {
50 return 0;
51 }
52
53 #endif
54
55 void bn_mont_ctx_init(BN_MONT_CTX *mont) {
56 OPENSSL_memset(mont, 0, sizeof(BN_MONT_CTX));
57 BN_init(&mont->RR);
58 BN_init(&mont->N);
59 }
60
61 void bn_mont_ctx_cleanup(BN_MONT_CTX *mont) {
62 BN_free(&mont->RR);
63 BN_free(&mont->N);
64 }
65
66 BN_MONT_CTX *BN_MONT_CTX_new(void) {
67 BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
68 if (ret == NULL) {
69 return NULL;
70 }
71
72 bn_mont_ctx_init(ret);
73 return ret;
74 }
75
76 void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
77 if (mont == NULL) {
78 return;
79 }
80
81 bn_mont_ctx_cleanup(mont);
82 OPENSSL_free(mont);
83 }
84
85 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) {
86 if (to == from) {
87 return to;
88 }
89
90 if (!BN_copy(&to->RR, &from->RR) ||
91 !BN_copy(&to->N, &from->N)) {
92 return NULL;
93 }
94 to->n0[0] = from->n0[0];
95 to->n0[1] = from->n0[1];
96 return to;
97 }
98
99 static int bn_mont_ctx_set_N_and_n0(BN_MONT_CTX *mont, const BIGNUM *mod) {
100 if (BN_is_zero(mod)) {
101 OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
102 return 0;
103 }
104 if (!BN_is_odd(mod)) {
105 OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
106 return 0;
107 }
108 if (BN_is_negative(mod)) {
109 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
110 return 0;
111 }
112 if (!bn_fits_in_words(mod, BN_MONTGOMERY_MAX_WORDS)) {
113 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
114 return 0;
115 }
116
117 // Save the modulus.
118 if (!BN_copy(&mont->N, mod)) {
119 OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
120 return 0;
121 }
122 // |mont->N| is always stored minimally. Computing RR efficiently leaks the
123 // size of the modulus. While the modulus may be private in RSA (one of the
124 // primes), their sizes are public, so this is fine.
125 bn_set_minimal_width(&mont->N);
126
127 // Find n0 such that n0 * N == -1 (mod r).
128 //
129 // Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the
130 // others, we could use a shorter R value and use faster |BN_ULONG|-based
131 // math instead of |uint64_t|-based math, which would be double-precision.
132 // However, currently only the assembler files know which is which.
133 OPENSSL_STATIC_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
134 BN_MONT_CTX_N0_LIMBS_value_is_invalid)
135 OPENSSL_STATIC_ASSERT(
136 sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t),
137 uint64_t_is_insufficient_precision_for_n0);
138 uint64_t n0 = bn_mont_n0(&mont->N);
139 mont->n0[0] = (BN_ULONG)n0;
140 #if BN_MONT_CTX_N0_LIMBS == 2
141 mont->n0[1] = (BN_ULONG)(n0 >> BN_BITS2);
142 #else
143 mont->n0[1] = 0;
144 #endif
145 return 1;
146 }
147
148 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
149 if (!bn_mont_ctx_set_N_and_n0(mont, mod)) {
150 return 0;
151 }
152
153 BN_CTX *new_ctx = NULL;
154 if (ctx == NULL) {
155 new_ctx = BN_CTX_new();
156 if (new_ctx == NULL) {
157 return 0;
158 }
159 ctx = new_ctx;
160 }
161
162 // Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS2 such that R
163 // > mod. Even though the assembly on some 32-bit platforms works with 64-bit
164 // values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS *
165 // BN_BITS2|, is correct because R**2 will still be a multiple of the latter
166 // as |BN_MONT_CTX_N0_LIMBS| is either one or two.
167 unsigned lgBigR = mont->N.width * BN_BITS2;
168 BN_zero(&mont->RR);
169 int ok = BN_set_bit(&mont->RR, lgBigR * 2) &&
170 BN_mod(&mont->RR, &mont->RR, &mont->N, ctx) &&
171 bn_resize_words(&mont->RR, mont->N.width);
172 BN_CTX_free(new_ctx);
173 return ok;
174 }
175
176 BN_MONT_CTX *BN_MONT_CTX_new_for_modulus(const BIGNUM *mod, BN_CTX *ctx) {
177 BN_MONT_CTX *mont = BN_MONT_CTX_new();
178 if (mont == NULL ||
179 !BN_MONT_CTX_set(mont, mod, ctx)) {
180 BN_MONT_CTX_free(mont);
181 return NULL;
182 }
183 return mont;
184 }
185
186 BN_MONT_CTX *BN_MONT_CTX_new_consttime(const BIGNUM *mod, BN_CTX *ctx) {
187 BN_MONT_CTX *mont = BN_MONT_CTX_new();
188 if (mont == NULL ||
189 !bn_mont_ctx_set_N_and_n0(mont, mod) ||
190 !bn_mont_ctx_set_RR_consttime(mont, ctx)) {
191 BN_MONT_CTX_free(mont);
192 return NULL;
193 }
194 return mont;
195 }
196
197 int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
198 const BIGNUM *mod, BN_CTX *bn_ctx) {
199 CRYPTO_MUTEX_lock_read(lock);
200 BN_MONT_CTX *ctx = *pmont;
201 CRYPTO_MUTEX_unlock_read(lock);
202
203 if (ctx) {
204 return 1;
205 }
206
207 CRYPTO_MUTEX_lock_write(lock);
208 if (*pmont == NULL) {
209 *pmont = BN_MONT_CTX_new_for_modulus(mod, bn_ctx);
210 }
211 const int ok = *pmont != NULL;
212 CRYPTO_MUTEX_unlock_write(lock);
213 return ok;
214 }
215
216 int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
217 BN_CTX *ctx) {
218 return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
219 }
220
221 static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a,
222 size_t num_a, const BN_MONT_CTX *mont) {
223 const BN_ULONG *n = mont->N.d;
224 size_t num_n = mont->N.width;
225 if (num_r != num_n || num_a != 2 * num_n) {
226 OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
227 return 0;
228 }
229
230 // Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
231 // input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
232 // includes |carry| which is stored separately.
233 BN_ULONG n0 = mont->n0[0];
234 BN_ULONG carry = 0;
235 for (size_t i = 0; i < num_n; i++) {
236 BN_ULONG v = bn_mul_add_words(a + i, n, num_n, a[i] * n0);
237 v += carry + a[i + num_n];
238 carry |= (v != a[i + num_n]);
239 carry &= (v <= a[i + num_n]);
240 a[i + num_n] = v;
241 }
242
243 // Shift |num_n| words to divide by R. We have |a| < 2 * |n|. Note that |a|
244 // includes |carry| which is stored separately.
245 a += num_n;
246
247 // |a| thus requires at most one additional subtraction |n| to be reduced.
248 bn_reduce_once(r, a, carry, n, num_n);
249 return 1;
250 }
251
252 static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
253 const BN_MONT_CTX *mont) {
254 if (r->neg) {
255 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
256 return 0;
257 }
258
259 const BIGNUM *n = &mont->N;
260 if (n->width == 0) {
261 ret->width = 0;
262 return 1;
263 }
264
265 int max = 2 * n->width; // carry is stored separately
266 if (!bn_resize_words(r, max) ||
267 !bn_wexpand(ret, n->width)) {
268 return 0;
269 }
270
271 ret->width = n->width;
272 ret->neg = 0;
273 return bn_from_montgomery_in_place(ret->d, ret->width, r->d, r->width, mont);
274 }
275
276 int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont,
277 BN_CTX *ctx) {
278 int ret = 0;
279 BIGNUM *t;
280
281 BN_CTX_start(ctx);
282 t = BN_CTX_get(ctx);
283 if (t == NULL ||
284 !BN_copy(t, a)) {
285 goto err;
286 }
287
288 ret = BN_from_montgomery_word(r, t, mont);
289
290 err:
291 BN_CTX_end(ctx);
292
293 return ret;
294 }
295
296 int bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx) {
297 // If the high bit of |n| is set, R = 2^(width*BN_BITS2) < 2 * |n|, so we
298 // compute R - |n| rather than perform Montgomery reduction.
299 const BIGNUM *n = &mont->N;
300 if (n->width > 0 && (n->d[n->width - 1] >> (BN_BITS2 - 1)) != 0) {
301 if (!bn_wexpand(r, n->width)) {
302 return 0;
303 }
304 r->d[0] = 0 - n->d[0];
305 for (int i = 1; i < n->width; i++) {
306 r->d[i] = ~n->d[i];
307 }
308 r->width = n->width;
309 r->neg = 0;
310 return 1;
311 }
312
313 return BN_from_montgomery(r, &mont->RR, mont, ctx);
314 }
315
316 static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a,
317 const BIGNUM *b,
318 const BN_MONT_CTX *mont,
319 BN_CTX *ctx) {
320 int ret = 0;
321
322 BN_CTX_start(ctx);
323 BIGNUM *tmp = BN_CTX_get(ctx);
324 if (tmp == NULL) {
325 goto err;
326 }
327
328 if (a == b) {
329 if (!bn_sqr_consttime(tmp, a, ctx)) {
330 goto err;
331 }
332 } else {
333 if (!bn_mul_consttime(tmp, a, b, ctx)) {
334 goto err;
335 }
336 }
337
338 // reduce from aRR to aR
339 if (!BN_from_montgomery_word(r, tmp, mont)) {
340 goto err;
341 }
342
343 ret = 1;
344
345 err:
346 BN_CTX_end(ctx);
347 return ret;
348 }
349
350
351 #if defined(OPENSSL_BN_ASM_MONT)
352
353 // Perform montgomery multiplication using s2n-bignum functions. The arguments
354 // are equivalent to the arguments of bn_mul_mont.
355 // montgomery_s2n_bignum_mul_mont works only if num is a multiple of 8.
356 // montgomery_use_s2n_bignum(num) must be called in advance to check this
357 // condition, as well as other s2n-bignum requirements.
358 // For num = 32 or num = 16, this uses faster primitives in s2n-bignum.
359 // montgomery_s2n_bignum_mul_mont allocates S2NBIGNUM_KMUL_32_64_TEMP_NWORDS +
360 // 2 * BN_MONTGOMERY_MAX_WORDS uint64_t words at the stack.
361 static void montgomery_s2n_bignum_mul_mont(BN_ULONG *rp, const BN_ULONG *ap,
362 const BN_ULONG *bp,
363 const BN_ULONG *np,
364 const BN_ULONG *n0, size_t num) {
365
366 #if defined(BN_MONTGOMERY_S2N_BIGNUM_CAPABLE)
367
368 // t is a temporary buffer used by Karatsuba multiplication.
369 // bignum_kmul_32_64 requires S2NBIGNUM_KMUL_32_64_TEMP_NWORDS words.
370 uint64_t t[S2NBIGNUM_KMUL_32_64_TEMP_NWORDS];
371 // mulres is the output buffer of big-int multiplication which uses
372 // 2 * num elements of mulres. Note that num <= BN_MONTGOMERY_MAX_WORDS
373 // is guaranteed by the caller (BN_mod_mul_montgomery).
374 uint64_t mulres[2 * BN_MONTGOMERY_MAX_WORDS];
375
376 // Given m the prime number stored at np, m * w = -1 mod 2^64.
377 uint64_t w = n0[0];
378
379 if (num == 32) {
380 if (ap == bp) {
381 bignum_ksqr_32_64(mulres, ap, t);
382 } else {
383 bignum_kmul_32_64(mulres, ap, bp, t);
384 }
385 } else if (num == 16) {
386 if (ap == bp) {
387 bignum_ksqr_16_32(mulres, ap, t);
388 } else {
389 bignum_kmul_16_32(mulres, ap, bp, t);
390 }
391 } else {
392 if (ap == bp) {
393 bignum_sqr(num * 2, mulres, num, ap);
394 } else {
395 bignum_mul(num * 2, mulres, num, ap, num, bp);
396 }
397 }
398
399 // Do montgomery reduction. We follow the definition of montgomery reduction
400 // which is:
401 // 1. Calculate (mulres + ((mulres mod R) * (-m^-1 mod R) mod R) * m) / R
402 // using bignum_emontredc_8n, where R is 2^(64*num).
403 // The calculated result is stored in [mulres+num ... mulres+2*num-1]. If
404 // the result >= 2^(64*num), bignum_emontredc_8n returns 1.
405 // 2. Optionally subtract the result if the (result of step 1) >= m.
406 // The comparison is true if either A or B holds:
407 // A. The result of step 1 >= 2^(64*num), meaning that bignum_emontredc_8n
408 // returned 1. Since m is less than 2^(64*num), (result of step 1) >= m holds.
409 // B. The result of step 1 fits in 2^(64*num), and the result >= m.
410 uint64_t c = bignum_emontredc_8n(num, mulres, np, w); // c: case A
411 c |= bignum_ge(num, mulres + num, num, np); // c: case B
412 // Optionally subtract and store the result at rp
413 bignum_optsub(num, rp, mulres + num, c, np);
414
415 #else
416
417 // Should not call this function unless s2n-bignum is supported.
418 abort();
419
420 #endif
421 }
422
423 #endif
424
425
426 int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
427 const BN_MONT_CTX *mont, BN_CTX *ctx) {
428 if (a->neg || b->neg) {
429 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
430 return 0;
431 }
432
433 #if defined(OPENSSL_BN_ASM_MONT)
434 // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
435 int num = mont->N.width;
436 if (num >= (128 / BN_BITS2) &&
437 a->width == num &&
438 b->width == num) {
439 if (!bn_wexpand(r, num)) {
440 return 0;
441 }
442 // This bound is implied by |bn_mont_ctx_set_N_and_n0|. |bn_mul_mont|
443 // allocates |num| words on the stack, so |num| cannot be too large.
444 assert((size_t)num <= BN_MONTGOMERY_MAX_WORDS);
445
446 if (montgomery_use_s2n_bignum(num)) {
447 // Do montgomery multiplication using s2n-bignum.
448 montgomery_s2n_bignum_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0,
449 num);
450 } else {
451 if (!bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
452 // The check above ensures this won't happen.
453 assert(0);
454 OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
455 return 0;
456 }
457 }
458 r->neg = 0;
459 r->width = num;
460 return 1;
461 }
462 #endif
463
464 return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx);
465 }
466
467 int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont) {
468 return !BN_is_negative(bn) &&
469 bn_fits_in_words(bn, mont->N.width);
470 }
471
472 void bn_to_montgomery_small(BN_ULONG *r, const BN_ULONG *a, size_t num,
473 const BN_MONT_CTX *mont) {
474 bn_mod_mul_montgomery_small(r, a, mont->RR.d, num, mont);
475 }
476
477 void bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
478 size_t num_a, const BN_MONT_CTX *mont) {
479 if (num_r != (size_t)mont->N.width || num_r > BN_SMALL_MAX_WORDS ||
480 num_a > 2 * num_r) {
481 abort();
482 }
483 BN_ULONG tmp[BN_SMALL_MAX_WORDS * 2] = {0};
484 OPENSSL_memcpy(tmp, a, num_a * sizeof(BN_ULONG));
485 if (!bn_from_montgomery_in_place(r, num_r, tmp, 2 * num_r, mont)) {
486 abort();
487 }
488 OPENSSL_cleanse(tmp, 2 * num_r * sizeof(BN_ULONG));
489 }
490
491 void bn_mod_mul_montgomery_small(BN_ULONG *r, const BN_ULONG *a,
492 const BN_ULONG *b, size_t num,
493 const BN_MONT_CTX *mont) {
494 if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) {
495 abort();
496 }
497
498 #if defined(OPENSSL_BN_ASM_MONT)
499 // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
500 if (num >= (128 / BN_BITS2)) {
501 if (!bn_mul_mont(r, a, b, mont->N.d, mont->n0, num)) {
502 abort(); // The check above ensures this won't happen.
503 }
504 return;
505 }
506 #endif
507
508 // Compute the product.
509 BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS];
510 if (a == b) {
511 bn_sqr_small(tmp, 2 * num, a, num);
512 } else {
513 bn_mul_small(tmp, 2 * num, a, num, b, num);
514 }
515
516 // Reduce.
517 if (!bn_from_montgomery_in_place(r, num, tmp, 2 * num, mont)) {
518 abort();
519 }
520 OPENSSL_cleanse(tmp, 2 * num * sizeof(BN_ULONG));
521 }
522
523 #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_X86_64)
524 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
525 const BN_ULONG *np, const BN_ULONG *n0, size_t num)
526 {
527 #if !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
528 if (ap == bp && bn_sqr8x_mont_capable(num)) {
529 return bn_sqr8x_mont(rp, ap, bn_mulx_adx_capable(), np, n0, num);
530 }
531 if (bn_mulx4x_mont_capable(num)) {
532 return bn_mulx4x_mont(rp, ap, bp, np, n0, num);
533 }
534 #endif // !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
535 if (bn_mul4x_mont_capable(num)) {
536 return bn_mul4x_mont(rp, ap, bp, np, n0, num);
537 }
538 return bn_mul_mont_nohw(rp, ap, bp, np, n0, num);
539 }
540 #endif
541
542 #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_ARM)
543 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
544 const BN_ULONG *np, const BN_ULONG *n0, size_t num) {
545 if (bn_mul8x_mont_neon_capable(num)) {
546 return bn_mul8x_mont_neon(rp, ap, bp, np, n0, num);
547 }
548 return bn_mul_mont_nohw(rp, ap, bp, np, n0, num);
549 }
550 #endif