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