2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include "internal/constant_time.h"
18 # define alloca _alloca
20 #elif defined(__GNUC__)
22 # define alloca(s) __builtin_alloca((s))
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
37 /* maximum precomputation table size for *variable* sliding windows */
40 /* this one works - simple but works */
41 int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
46 if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
47 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0) {
48 /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
49 ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
54 rr = ((r == a) || (r == p)) ? BN_CTX_get(ctx) : r;
56 if (rr == NULL || v == NULL)
59 if (BN_copy(v, a) == NULL)
61 bits = BN_num_bits(p);
64 if (BN_copy(rr, a) == NULL)
71 for (i = 1; i < bits; i++) {
72 if (!BN_sqr(v, v, ctx))
74 if (BN_is_bit_set(p, i)) {
75 if (!BN_mul(rr, rr, v, ctx))
79 if (r != rr && BN_copy(r, rr) == NULL)
89 int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
99 * For even modulus m = 2^k*m_odd, it might make sense to compute
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.
104 * For now, we use Montgomery only if the modulus is odd; otherwise,
105 * exponentiation using the reciprocal-based quick remaindering
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:
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.]
117 * BN_mod_exp_recp 50 .. 70 % [AMD K6-2, Linux, debug configuration]
118 * 62 .. 118 % [UltraSparc, debug-solaris-sparcv8-gcc]
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!
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.)
130 #define MONT_EXP_WORD
135 # ifdef MONT_EXP_WORD
136 if (a->top == 1 && !a->neg
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)) {
140 BN_ULONG A = a->d[0];
141 ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);
144 ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);
149 ret = BN_mod_exp_recp(r, a, p, m, ctx);
153 ret = BN_mod_exp_simple(r, a, p, m, ctx);
161 int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
162 const BIGNUM *m, BN_CTX *ctx)
164 int i, j, bits, ret = 0, wstart, wend, window, wvalue;
167 /* Table of variables obtained from 'ctx' */
168 BIGNUM *val[TABLE_SIZE];
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) {
174 /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
175 ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
179 bits = BN_num_bits(p);
181 /* x**0 mod 1, or x**0 mod -1 is still zero. */
182 if (BN_abs_is_word(m, 1)) {
192 aa = BN_CTX_get(ctx);
193 val[0] = BN_CTX_get(ctx);
197 BN_RECP_CTX_init(&recp);
199 /* ignore sign of 'm' */
203 if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)
206 if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)
210 if (!BN_nnmod(val[0], a, m, ctx))
212 if (BN_is_zero(val[0])) {
218 window = BN_window_bits_for_exponent_size(bits);
220 if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))
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))
230 start = 1; /* This is used to avoid multiplication etc
231 * when there is only the value '1' in the
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 */
241 if (BN_is_bit_set(p, wstart) == 0) {
243 if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
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
257 for (i = 1; i < window; i++) {
260 if (BN_is_bit_set(p, wstart - i)) {
261 wvalue <<= (i - wend);
267 /* wend is the size of the current window */
269 /* add the 'bytes above' */
271 for (i = 0; i < j; i++) {
272 if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
276 /* wvalue will be an odd number < 2^window */
277 if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))
280 /* move the 'window' down further */
290 BN_RECP_CTX_free(&recp);
295 int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
296 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
298 int i, j, bits, ret = 0, wstart, wend, window, wvalue;
302 /* Table of variables obtained from 'ctx' */
303 BIGNUM *val[TABLE_SIZE];
304 BN_MONT_CTX *mont = NULL;
306 if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
307 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0
308 || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {
309 return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
317 ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
320 bits = BN_num_bits(p);
322 /* x**0 mod 1, or x**0 mod -1 is still zero. */
323 if (BN_abs_is_word(m, 1)) {
335 val[0] = BN_CTX_get(ctx);
340 * If this is not done, things will break in the montgomery part
346 if ((mont = BN_MONT_CTX_new()) == NULL)
348 if (!BN_MONT_CTX_set(mont, m, ctx))
352 if (a->neg || BN_ucmp(a, m) >= 0) {
353 if (!BN_nnmod(val[0], a, m, ctx))
358 if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))
361 window = BN_window_bits_for_exponent_size(bits);
363 if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))
365 j = 1 << (window - 1);
366 for (i = 1; i < j; i++) {
367 if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
368 !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))
373 start = 1; /* This is used to avoid multiplication etc
374 * when there is only the value '1' in the
376 wvalue = 0; /* The 'value' of the window */
377 wstart = bits - 1; /* The top bit of the window */
378 wend = 0; /* The bottom bit of the window */
380 #if 1 /* by Shay Gueron's suggestion */
381 j = m->top; /* borrow j */
382 if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
383 if (bn_wexpand(r, j) == NULL)
385 /* 2^(top*BN_BITS2) - m */
386 r->d[0] = (0 - m->d[0]) & BN_MASK2;
387 for (i = 1; i < j; i++)
388 r->d[i] = (~m->d[i]) & BN_MASK2;
390 r->flags |= BN_FLG_FIXED_TOP;
393 if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))
396 if (BN_is_bit_set(p, wstart) == 0) {
398 if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))
407 * We now have wstart on a 'set' bit, we now need to work out how bit
408 * a window to do. To do this we need to scan forward until the last
409 * set bit before the end of the window
413 for (i = 1; i < window; i++) {
416 if (BN_is_bit_set(p, wstart - i)) {
417 wvalue <<= (i - wend);
423 /* wend is the size of the current window */
425 /* add the 'bytes above' */
427 for (i = 0; i < j; i++) {
428 if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))
432 /* wvalue will be an odd number < 2^window */
433 if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))
436 /* move the 'window' down further */
444 * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery
445 * removes padding [if any] and makes return value suitable for public
448 #if defined(SPARC_T4_MONT)
449 if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {
450 j = mont->N.top; /* borrow j */
451 val[0]->d[0] = 1; /* borrow val[0] */
452 for (i = 1; i < j; i++)
455 if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))
459 if (!BN_from_montgomery(rr, r, mont, ctx))
464 BN_MONT_CTX_free(mont);
470 static BN_ULONG bn_get_bits(const BIGNUM *a, int bitpos)
475 wordpos = bitpos / BN_BITS2;
477 if (wordpos >= 0 && wordpos < a->top) {
478 ret = a->d[wordpos] & BN_MASK2;
481 if (++wordpos < a->top)
482 ret |= a->d[wordpos] << (BN_BITS2 - bitpos);
486 return ret & BN_MASK2;
490 * BN_mod_exp_mont_consttime() stores the precomputed powers in a specific
491 * layout so that accessing any of these table values shows the same access
492 * pattern as far as cache lines are concerned. The following functions are
493 * used to transfer a BIGNUM from/to that table.
496 static int MOD_EXP_CTIME_COPY_TO_PREBUF(const BIGNUM *b, int top,
497 unsigned char *buf, int idx,
501 int width = 1 << window;
502 BN_ULONG *table = (BN_ULONG *)buf;
505 top = b->top; /* this works because 'buf' is explicitly
507 for (i = 0, j = idx; i < top; i++, j += width) {
514 static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,
515 unsigned char *buf, int idx,
519 int width = 1 << window;
521 * We declare table 'volatile' in order to discourage compiler
522 * from reordering loads from the table. Concern is that if
523 * reordered in specific manner loads might give away the
524 * information we are trying to conceal. Some would argue that
525 * compiler can reorder them anyway, but it can as well be
526 * argued that doing so would be violation of standard...
528 volatile BN_ULONG *table = (volatile BN_ULONG *)buf;
530 if (bn_wexpand(b, top) == NULL)
534 for (i = 0; i < top; i++, table += width) {
537 for (j = 0; j < width; j++) {
539 ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));
545 int xstride = 1 << (window - 2);
546 BN_ULONG y0, y1, y2, y3;
548 i = idx >> (window - 2); /* equivalent of idx / xstride */
549 idx &= xstride - 1; /* equivalent of idx % xstride */
551 y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);
552 y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);
553 y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);
554 y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);
556 for (i = 0; i < top; i++, table += width) {
559 for (j = 0; j < xstride; j++) {
560 acc |= ( (table[j + 0 * xstride] & y0) |
561 (table[j + 1 * xstride] & y1) |
562 (table[j + 2 * xstride] & y2) |
563 (table[j + 3 * xstride] & y3) )
564 & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));
572 b->flags |= BN_FLG_FIXED_TOP;
577 * Given a pointer value, compute the next address that is a cache line
580 #define MOD_EXP_CTIME_ALIGN(x_) \
581 ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
584 * This variant of BN_mod_exp_mont() uses fixed windows and the special
585 * precomputation memory layout to limit data-dependency to a minimum to
586 * protect secret exponents (cf. the hyper-threading timing attacks pointed
587 * out by Colin Percival,
588 * http://www.daemonology.net/hyperthreading-considered-harmful/)
590 int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
591 const BIGNUM *m, BN_CTX *ctx,
592 BN_MONT_CTX *in_mont)
594 int i, bits, ret = 0, window, wvalue, wmask, window0;
596 BN_MONT_CTX *mont = NULL;
599 unsigned char *powerbufFree = NULL;
601 unsigned char *powerbuf = NULL;
603 #if defined(SPARC_T4_MONT)
612 ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
619 * Use all bits stored in |p|, rather than |BN_num_bits|, so we do not leak
620 * whether the top bits are zero.
622 bits = p->top * BN_BITS2;
624 /* x**0 mod 1, or x**0 mod -1 is still zero. */
625 if (BN_abs_is_word(m, 1)) {
637 * Allocate a montgomery context if it was not supplied by the caller. If
638 * this is not done, things will break in the montgomery part.
643 if ((mont = BN_MONT_CTX_new()) == NULL)
645 if (!BN_MONT_CTX_set(mont, m, ctx))
649 if (a->neg || BN_ucmp(a, m) >= 0) {
650 BIGNUM *reduced = BN_CTX_get(ctx);
652 || !BN_nnmod(reduced, a, m, ctx)) {
660 * If the size of the operands allow it, perform the optimized
661 * RSAZ exponentiation. For further information see
662 * crypto/bn/rsaz_exp.c and accompanying assembly modules.
664 if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)
665 && rsaz_avx2_eligible()) {
666 if (NULL == bn_wexpand(rr, 16))
668 RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,
675 } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {
676 if (NULL == bn_wexpand(rr, 8))
678 RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);
687 /* Get the window size to use with size of p. */
688 window = BN_window_bits_for_ctime_exponent_size(bits);
689 #if defined(SPARC_T4_MONT)
690 if (window >= 5 && (top & 15) == 0 && top <= 64 &&
691 (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
692 (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))
696 #if defined(OPENSSL_BN_ASM_MONT5)
698 window = 5; /* ~5% improvement for RSA2048 sign, and even
700 /* reserve space for mont->N.d[] copy */
701 powerbufLen += top * sizeof(mont->N.d[0]);
707 * Allocate a buffer large enough to hold all of the pre-computed powers
708 * of am, am itself and tmp.
710 numPowers = 1 << window;
711 powerbufLen += sizeof(m->d[0]) * (top * numPowers +
713 numPowers ? (2 * top) : numPowers));
715 if (powerbufLen < 3072)
717 alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
721 OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))
725 powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
726 memset(powerbuf, 0, powerbufLen);
729 if (powerbufLen < 3072)
733 /* lay down tmp and am right after powers table */
734 tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);
736 tmp.top = am.top = 0;
737 tmp.dmax = am.dmax = top;
738 tmp.neg = am.neg = 0;
739 tmp.flags = am.flags = BN_FLG_STATIC_DATA;
741 /* prepare a^0 in Montgomery domain */
742 #if 1 /* by Shay Gueron's suggestion */
743 if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
744 /* 2^(top*BN_BITS2) - m */
745 tmp.d[0] = (0 - m->d[0]) & BN_MASK2;
746 for (i = 1; i < top; i++)
747 tmp.d[i] = (~m->d[i]) & BN_MASK2;
751 if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))
754 /* prepare a^1 in Montgomery domain */
755 if (!bn_to_mont_fixed_top(&am, a, mont, ctx))
758 #if defined(SPARC_T4_MONT)
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
779 bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];
781 typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
782 const void *bp, const BN_ULONG *np,
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,
789 int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
790 const void *bp, const BN_ULONG *np,
792 int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
793 const void *bp, const BN_ULONG *np,
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
799 bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];
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);
816 BN_ULONG *np = mont->N.d, *n0 = mont->n0;
817 int stride = 5 * (6 - (top / 16 - 1)); /* multiple of 5, but less
821 * BN_to_montgomery can contaminate words above .top [in
824 for (i = am.top; i < top; i++)
826 for (i = tmp.top; i < top; i++)
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);
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);
844 /* switch to 64-bit domain */
845 np = alloca(top * sizeof(BN_ULONG));
847 bn_flip_t4(np, mont->N.d, top);
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
855 window0 = (bits - 1) % 5 + 1;
856 wmask = (1 << window0) - 1;
858 wvalue = bn_get_bits(p, bits) & wmask;
859 bn_gather5_t4(tmp.d, top, powerbuf, wvalue);
862 * Scan the exponent one window at a time starting from the most
869 wvalue = bn_get_bits(p, bits);
871 if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))
873 /* retry once and fall back */
874 if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))
878 wvalue >>= stride - 5;
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,
889 bn_flip_t4(tmp.d, tmp.d, top);
891 /* back to 32-bit domain */
893 bn_correct_top(&tmp);
894 OPENSSL_cleanse(np, top * sizeof(BN_ULONG));
897 #if defined(OPENSSL_BN_ASM_MONT5)
898 if (window == 5 && top > 1) {
900 * This optimization uses ideas from http://eprint.iacr.org/2011/239,
901 * specifically optimization of cache-timing attack countermeasures
902 * and pre-computation optimization.
906 * Dedicated window==4 case improves 512-bit RSA sign by ~15%, but as
907 * 512-bit RSA is hardly relevant, we omit it to spare size...
909 void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,
910 const void *table, const BN_ULONG *np,
911 const BN_ULONG *n0, int num, int power);
912 void bn_scatter5(const BN_ULONG *inp, size_t num,
913 void *table, size_t power);
914 void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);
915 void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,
916 const void *table, const BN_ULONG *np,
917 const BN_ULONG *n0, int num, int power);
918 int bn_get_bits5(const BN_ULONG *ap, int off);
919 int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,
920 const BN_ULONG *not_used, const BN_ULONG *np,
921 const BN_ULONG *n0, int num);
923 BN_ULONG *n0 = mont->n0, *np;
926 * BN_to_montgomery can contaminate words above .top [in
929 for (i = am.top; i < top; i++)
931 for (i = tmp.top; i < top; i++)
935 * copy mont->N.d[] to improve cache locality
937 for (np = am.d + top, i = 0; i < top; i++)
938 np[i] = mont->N.d[i];
940 bn_scatter5(tmp.d, top, powerbuf, 0);
941 bn_scatter5(am.d, am.top, powerbuf, 1);
942 bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);
943 bn_scatter5(tmp.d, top, powerbuf, 2);
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);
952 /* same as above, but uses squaring for 1/2 of operations */
953 for (i = 4; i < 32; i *= 2) {
954 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
955 bn_scatter5(tmp.d, top, powerbuf, i);
957 for (i = 3; i < 8; i += 2) {
959 bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
960 bn_scatter5(tmp.d, top, powerbuf, i);
961 for (j = 2 * i; j < 32; j *= 2) {
962 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
963 bn_scatter5(tmp.d, top, powerbuf, j);
966 for (; i < 16; i += 2) {
967 bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
968 bn_scatter5(tmp.d, top, powerbuf, i);
969 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
970 bn_scatter5(tmp.d, top, powerbuf, 2 * i);
972 for (; i < 32; i += 2) {
973 bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
974 bn_scatter5(tmp.d, top, powerbuf, i);
978 * The exponent may not have a whole number of fixed-size windows.
979 * To simplify the main loop, the initial window has between 1 and
980 * full-window-size bits such that what remains is always a whole
983 window0 = (bits - 1) % 5 + 1;
984 wmask = (1 << window0) - 1;
986 wvalue = bn_get_bits(p, bits) & wmask;
987 bn_gather5(tmp.d, top, powerbuf, wvalue);
990 * Scan the exponent one window at a time starting from the most
995 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
996 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
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_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,
1001 bn_get_bits5(p->d, bits -= 5));
1005 bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,
1006 bn_get_bits5(p->d, bits -= 5));
1010 ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);
1012 bn_correct_top(&tmp);
1014 if (!BN_copy(rr, &tmp))
1016 goto err; /* non-zero ret means it's not error */
1021 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))
1023 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))
1027 * If the window size is greater than 1, then calculate
1028 * val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) (even
1029 * powers could instead be computed as (a^(i/2))^2 to use the slight
1030 * performance advantage of sqr over mul).
1033 if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))
1035 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,
1038 for (i = 3; i < numPowers; i++) {
1039 /* Calculate a^i = a^(i-1) * a */
1040 if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))
1042 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,
1049 * The exponent may not have a whole number of fixed-size windows.
1050 * To simplify the main loop, the initial window has between 1 and
1051 * full-window-size bits such that what remains is always a whole
1054 window0 = (bits - 1) % window + 1;
1055 wmask = (1 << window0) - 1;
1057 wvalue = bn_get_bits(p, bits) & wmask;
1058 if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,
1062 wmask = (1 << window) - 1;
1064 * Scan the exponent one window at a time starting from the most
1069 /* Square the result window-size times */
1070 for (i = 0; i < window; i++)
1071 if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))
1075 * Get a window's worth of bits from the exponent
1076 * This avoids calling BN_is_bit_set for each bit, which
1077 * is not only slower but also makes each bit vulnerable to
1078 * EM (and likely other) side-channel attacks like One&Done
1079 * (for details see "One&Done: A Single-Decryption EM-Based
1080 * Attack on OpenSSL's Constant-Time Blinded RSA" by M. Alam,
1081 * H. Khan, M. Dey, N. Sinha, R. Callan, A. Zajic, and
1082 * M. Prvulovic, in USENIX Security'18)
1085 wvalue = bn_get_bits(p, bits) & wmask;
1087 * Fetch the appropriate pre-computed value from the pre-buf
1089 if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,
1093 /* Multiply the result into the intermediate result */
1094 if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))
1100 * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery
1101 * removes padding [if any] and makes return value suitable for public
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++)
1109 if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))
1113 if (!BN_from_montgomery(rr, &tmp, mont, ctx))
1117 if (in_mont == NULL)
1118 BN_MONT_CTX_free(mont);
1119 if (powerbuf != NULL) {
1120 OPENSSL_cleanse(powerbuf, powerbufLen);
1121 OPENSSL_free(powerbufFree);
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)
1130 BN_MONT_CTX *mont = NULL;
1131 int b, bits, ret = 0;
1136 #define BN_MOD_MUL_WORD(r, w, m) \
1137 (BN_mul_word(r, (w)) && \
1138 (/* BN_ucmp(r, (m)) < 0 ? 1 :*/ \
1139 (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
1141 * BN_MOD_MUL_WORD is only used with 'w' large, so the BN_ucmp test is
1142 * probably more overhead than always using BN_mod (which uses BN_copy if
1143 * a similar test returns true).
1146 * We can use BN_mod and do not need BN_nnmod because our accumulator is
1147 * never negative (the result of BN_mod does not depend on the sign of
1150 #define BN_TO_MONTGOMERY_WORD(r, w, mont) \
1151 (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
1153 if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
1154 || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {
1155 /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
1156 ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1163 if (!BN_is_odd(m)) {
1164 ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS);
1168 a %= m->d[0]; /* make sure that 'a' is reduced */
1170 bits = BN_num_bits(p);
1172 /* x**0 mod 1, or x**0 mod -1 is still zero. */
1173 if (BN_abs_is_word(m, 1)) {
1188 r = BN_CTX_get(ctx);
1189 t = BN_CTX_get(ctx);
1193 if (in_mont != NULL)
1196 if ((mont = BN_MONT_CTX_new()) == NULL)
1198 if (!BN_MONT_CTX_set(mont, m, ctx))
1202 r_is_one = 1; /* except for Montgomery factor */
1206 /* The result is accumulated in the product r*w. */
1207 w = a; /* bit 'bits-1' of 'p' is always set */
1208 for (b = bits - 2; b >= 0; b--) {
1209 /* First, square r*w. */
1211 if ((next_w / w) != w) { /* overflow */
1213 if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
1217 if (!BN_MOD_MUL_WORD(r, w, m))
1224 if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
1228 /* Second, multiply r*w by 'a' if exponent bit is set. */
1229 if (BN_is_bit_set(p, b)) {
1231 if ((next_w / a) != w) { /* overflow */
1233 if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
1237 if (!BN_MOD_MUL_WORD(r, w, m))
1246 /* Finally, set r:=r*w. */
1249 if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
1253 if (!BN_MOD_MUL_WORD(r, w, m))
1258 if (r_is_one) { /* can happen only if a == 1 */
1262 if (!BN_from_montgomery(rr, r, mont, ctx))
1267 if (in_mont == NULL)
1268 BN_MONT_CTX_free(mont);
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)
1278 int i, j, bits, ret = 0, wstart, wend, window, wvalue;
1281 /* Table of variables obtained from 'ctx' */
1282 BIGNUM *val[TABLE_SIZE];
1284 if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0
1285 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0
1286 || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {
1287 /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
1288 ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1292 bits = BN_num_bits(p);
1294 /* x**0 mod 1, or x**0 mod -1 is still zero. */
1295 if (BN_abs_is_word(m, 1)) {
1305 d = BN_CTX_get(ctx);
1306 val[0] = BN_CTX_get(ctx);
1310 if (!BN_nnmod(val[0], a, m, ctx))
1312 if (BN_is_zero(val[0])) {
1318 window = BN_window_bits_for_exponent_size(bits);
1320 if (!BN_mod_mul(d, val[0], val[0], m, ctx))
1322 j = 1 << (window - 1);
1323 for (i = 1; i < j; i++) {
1324 if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
1325 !BN_mod_mul(val[i], val[i - 1], d, m, ctx))
1330 start = 1; /* This is used to avoid multiplication etc
1331 * when there is only the value '1' in the
1333 wvalue = 0; /* The 'value' of the window */
1334 wstart = bits - 1; /* The top bit of the window */
1335 wend = 0; /* The bottom bit of the window */
1341 if (BN_is_bit_set(p, wstart) == 0) {
1343 if (!BN_mod_mul(r, r, r, m, ctx))
1351 * We now have wstart on a 'set' bit, we now need to work out how bit
1352 * a window to do. To do this we need to scan forward until the last
1353 * set bit before the end of the window
1357 for (i = 1; i < window; i++) {
1360 if (BN_is_bit_set(p, wstart - i)) {
1361 wvalue <<= (i - wend);
1367 /* wend is the size of the current window */
1369 /* add the 'bytes above' */
1371 for (i = 0; i < j; i++) {
1372 if (!BN_mod_mul(r, r, r, m, ctx))
1376 /* wvalue will be an odd number < 2^window */
1377 if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))
1380 /* move the 'window' down further */
1395 * This is a variant of modular exponentiation optimization that does
1396 * parallel 2-primes exponentiation using 256-bit (AVX512VL) AVX512_IFMA ISA
1397 * in 52-bit binary redundant representation.
1398 * If such instructions are not available, or input data size is not supported,
1399 * it falls back to two BN_mod_exp_mont_consttime() calls.
1401 int BN_mod_exp_mont_consttime_x2(BIGNUM *rr1, const BIGNUM *a1, const BIGNUM *p1,
1402 const BIGNUM *m1, BN_MONT_CTX *in_mont1,
1403 BIGNUM *rr2, const BIGNUM *a2, const BIGNUM *p2,
1404 const BIGNUM *m2, BN_MONT_CTX *in_mont2,
1410 BN_MONT_CTX *mont1 = NULL;
1411 BN_MONT_CTX *mont2 = NULL;
1413 if (ossl_rsaz_avx512ifma_eligible() &&
1414 ((a1->top == 16) && (p1->top == 16) && (BN_num_bits(m1) == 1024) &&
1415 (a2->top == 16) && (p2->top == 16) && (BN_num_bits(m2) == 1024))) {
1417 if (bn_wexpand(rr1, 16) == NULL)
1419 if (bn_wexpand(rr2, 16) == NULL)
1422 /* Ensure that montgomery contexts are initialized */
1423 if (in_mont1 != NULL) {
1426 if ((mont1 = BN_MONT_CTX_new()) == NULL)
1428 if (!BN_MONT_CTX_set(mont1, m1, ctx))
1431 if (in_mont2 != NULL) {
1434 if ((mont2 = BN_MONT_CTX_new()) == NULL)
1436 if (!BN_MONT_CTX_set(mont2, m2, ctx))
1440 ret = ossl_rsaz_mod_exp_avx512_x2(rr1->d, a1->d, p1->d, m1->d,
1441 mont1->RR.d, mont1->n0[0],
1442 rr2->d, a2->d, p2->d, m2->d,
1443 mont2->RR.d, mont2->n0[0],
1444 1024 /* factor bit size */);
1448 bn_correct_top(rr1);
1453 bn_correct_top(rr2);
1460 /* rr1 = a1^p1 mod m1 */
1461 ret = BN_mod_exp_mont_consttime(rr1, a1, p1, m1, ctx, in_mont1);
1462 /* rr2 = a2^p2 mod m2 */
1463 ret &= BN_mod_exp_mont_consttime(rr2, a2, p2, m2, ctx, in_mont2);
1467 if (in_mont2 == NULL)
1468 BN_MONT_CTX_free(mont2);
1469 if (in_mont1 == NULL)
1470 BN_MONT_CTX_free(mont1);