| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/thread.h> |
| 25 |
| 26 | #include "../../internal.h" |
| 27 | #include "internal.h" |
| 28 |
| 29 |
| 30 | void bn_mont_ctx_init(BN_MONT_CTX *mont) { |
| 31 | OPENSSL_memset(mont, 0, sizeof(BN_MONT_CTX)); |
| 32 | BN_init(&mont->RR); |
| 33 | BN_init(&mont->N); |
| 34 | } |
| 35 |
| 36 | void bn_mont_ctx_cleanup(BN_MONT_CTX *mont) { |
| 37 | BN_free(&mont->RR); |
| 38 | BN_free(&mont->N); |
| 39 | } |
| 40 |
| 41 | BN_MONT_CTX *BN_MONT_CTX_new(void) { |
| 42 | BN_MONT_CTX *ret = |
| 43 | reinterpret_cast<BN_MONT_CTX *>(OPENSSL_malloc(sizeof(BN_MONT_CTX))); |
| 44 | if (ret == NULL) { |
| 45 | return NULL; |
| 46 | } |
| 47 |
| 48 | bn_mont_ctx_init(ret); |
| 49 | return ret; |
| 50 | } |
| 51 |
| 52 | void BN_MONT_CTX_free(BN_MONT_CTX *mont) { |
| 53 | if (mont == nullptr) { |
| 54 | return; |
| 55 | } |
| 56 | bn_mont_ctx_cleanup(mont); |
| 57 | OPENSSL_free(mont); |
| 58 | } |
| 59 |
| 60 | BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) { |
| 61 | if (to == from) { |
| 62 | return to; |
| 63 | } |
| 64 |
| 65 | if (!BN_copy(&to->RR, &from->RR) || !BN_copy(&to->N, &from->N)) { |
| 66 | return NULL; |
| 67 | } |
| 68 | to->n0[0] = from->n0[0]; |
| 69 | to->n0[1] = from->n0[1]; |
| 70 | return to; |
| 71 | } |
| 72 |
| 73 | static int bn_mont_ctx_set_N_and_n0(BN_MONT_CTX *mont, const BIGNUM *mod) { |
| 74 | if (BN_is_zero(mod)) { |
| 75 | OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO); |
| 76 | return 0; |
| 77 | } |
| 78 | if (!BN_is_odd(mod)) { |
| 79 | OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS); |
| 80 | return 0; |
| 81 | } |
| 82 | if (BN_is_negative(mod)) { |
| 83 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 84 | return 0; |
| 85 | } |
| 86 | if (!bn_fits_in_words(mod, BN_MONTGOMERY_MAX_WORDS)) { |
| 87 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 88 | return 0; |
| 89 | } |
| 90 |
| 91 | // Save the modulus. |
| 92 | if (!BN_copy(&mont->N, mod)) { |
| 93 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); |
| 94 | return 0; |
| 95 | } |
| 96 | // |mont->N| is always stored minimally. Computing RR efficiently leaks the |
| 97 | // size of the modulus. While the modulus may be private in RSA (one of the |
| 98 | // primes), their sizes are public, so this is fine. |
| 99 | bn_set_minimal_width(&mont->N); |
| 100 |
| 101 | // Find n0 such that n0 * N == -1 (mod r). |
| 102 | // |
| 103 | // Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the |
| 104 | // others, we could use a shorter R value and use faster |BN_ULONG|-based |
| 105 | // math instead of |uint64_t|-based math, which would be double-precision. |
| 106 | // However, currently only the assembler files know which is which. |
| 107 | static_assert(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2, |
| 108 | "BN_MONT_CTX_N0_LIMBS value is invalid"); |
| 109 | static_assert(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t), |
| 110 | "uint64_t is insufficient precision for n0"); |
| 111 | uint64_t n0 = bn_mont_n0(&mont->N); |
| 112 | mont->n0[0] = (BN_ULONG)n0; |
| 113 | #if BN_MONT_CTX_N0_LIMBS == 2 |
| 114 | mont->n0[1] = (BN_ULONG)(n0 >> BN_BITS2); |
| 115 | #else |
| 116 | mont->n0[1] = 0; |
| 117 | #endif |
| 118 | return 1; |
| 119 | } |
| 120 |
| 121 | int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) { |
| 122 | if (!bn_mont_ctx_set_N_and_n0(mont, mod)) { |
| 123 | return 0; |
| 124 | } |
| 125 |
| 126 | BN_CTX *new_ctx = NULL; |
| 127 | if (ctx == NULL) { |
| 128 | new_ctx = BN_CTX_new(); |
| 129 | if (new_ctx == NULL) { |
| 130 | return 0; |
| 131 | } |
| 132 | ctx = new_ctx; |
| 133 | } |
| 134 |
| 135 | // Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS2 such that R |
| 136 | // > mod. Even though the assembly on some 32-bit platforms works with 64-bit |
| 137 | // values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS * |
| 138 | // BN_BITS2|, is correct because R**2 will still be a multiple of the latter |
| 139 | // as |BN_MONT_CTX_N0_LIMBS| is either one or two. |
| 140 | unsigned lgBigR = mont->N.width * BN_BITS2; |
| 141 | BN_zero(&mont->RR); |
| 142 | int ok = BN_set_bit(&mont->RR, lgBigR * 2) && |
| 143 | BN_mod(&mont->RR, &mont->RR, &mont->N, ctx) && |
| 144 | bn_resize_words(&mont->RR, mont->N.width); |
| 145 | BN_CTX_free(new_ctx); |
| 146 | return ok; |
| 147 | } |
| 148 |
| 149 | BN_MONT_CTX *BN_MONT_CTX_new_for_modulus(const BIGNUM *mod, BN_CTX *ctx) { |
| 150 | BN_MONT_CTX *mont = BN_MONT_CTX_new(); |
| 151 | if (mont == NULL || !BN_MONT_CTX_set(mont, mod, ctx)) { |
| 152 | BN_MONT_CTX_free(mont); |
| 153 | return NULL; |
| 154 | } |
| 155 | return mont; |
| 156 | } |
| 157 |
| 158 | BN_MONT_CTX *BN_MONT_CTX_new_consttime(const BIGNUM *mod, BN_CTX *ctx) { |
| 159 | BN_MONT_CTX *mont = BN_MONT_CTX_new(); |
| 160 | if (mont == NULL || !bn_mont_ctx_set_N_and_n0(mont, mod) || |
| 161 | !bn_mont_ctx_set_RR_consttime(mont, ctx)) { |
| 162 | BN_MONT_CTX_free(mont); |
| 163 | return NULL; |
| 164 | } |
| 165 | return mont; |
| 166 | } |
| 167 |
| 168 | int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock, |
| 169 | const BIGNUM *mod, BN_CTX *bn_ctx) { |
| 170 | CRYPTO_MUTEX_lock_read(lock); |
| 171 | BN_MONT_CTX *ctx = *pmont; |
| 172 | CRYPTO_MUTEX_unlock_read(lock); |
| 173 |
| 174 | if (ctx) { |
| 175 | return 1; |
| 176 | } |
| 177 |
| 178 | CRYPTO_MUTEX_lock_write(lock); |
| 179 | if (*pmont == NULL) { |
| 180 | *pmont = BN_MONT_CTX_new_for_modulus(mod, bn_ctx); |
| 181 | } |
| 182 | const int ok = *pmont != NULL; |
| 183 | CRYPTO_MUTEX_unlock_write(lock); |
| 184 | return ok; |
| 185 | } |
| 186 |
| 187 | int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont, |
| 188 | BN_CTX *ctx) { |
| 189 | return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx); |
| 190 | } |
| 191 |
| 192 | static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a, |
| 193 | size_t num_a, const BN_MONT_CTX *mont) { |
| 194 | const BN_ULONG *n = mont->N.d; |
| 195 | size_t num_n = mont->N.width; |
| 196 | if (num_r != num_n || num_a != 2 * num_n) { |
| 197 | OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 198 | return 0; |
| 199 | } |
| 200 |
| 201 | // Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On |
| 202 | // input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r| |
| 203 | // includes |carry| which is stored separately. |
| 204 | BN_ULONG n0 = mont->n0[0]; |
| 205 | BN_ULONG carry = 0; |
| 206 | for (size_t i = 0; i < num_n; i++) { |
| 207 | BN_ULONG v = bn_mul_add_words(a + i, n, num_n, a[i] * n0); |
| 208 | v += carry + a[i + num_n]; |
| 209 | carry |= (v != a[i + num_n]); |
| 210 | carry &= (v <= a[i + num_n]); |
| 211 | a[i + num_n] = v; |
| 212 | } |
| 213 |
| 214 | // Shift |num_n| words to divide by R. We have |a| < 2 * |n|. Note that |a| |
| 215 | // includes |carry| which is stored separately. |
| 216 | a += num_n; |
| 217 |
| 218 | // |a| thus requires at most one additional subtraction |n| to be reduced. |
| 219 | bn_reduce_once(r, a, carry, n, num_n); |
| 220 | return 1; |
| 221 | } |
| 222 |
| 223 | static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, |
| 224 | const BN_MONT_CTX *mont) { |
| 225 | if (r->neg) { |
| 226 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 227 | return 0; |
| 228 | } |
| 229 |
| 230 | const BIGNUM *n = &mont->N; |
| 231 | if (n->width == 0) { |
| 232 | ret->width = 0; |
| 233 | return 1; |
| 234 | } |
| 235 |
| 236 | int max = 2 * n->width; // carry is stored separately |
| 237 | if (!bn_resize_words(r, max) || !bn_wexpand(ret, n->width)) { |
| 238 | return 0; |
| 239 | } |
| 240 |
| 241 | ret->width = n->width; |
| 242 | ret->neg = 0; |
| 243 | return bn_from_montgomery_in_place(ret->d, ret->width, r->d, r->width, mont); |
| 244 | } |
| 245 |
| 246 | int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont, |
| 247 | BN_CTX *ctx) { |
| 248 | int ret = 0; |
| 249 | BIGNUM *t; |
| 250 |
| 251 | BN_CTX_start(ctx); |
| 252 | t = BN_CTX_get(ctx); |
| 253 | if (t == NULL || !BN_copy(t, a)) { |
| 254 | goto err; |
| 255 | } |
| 256 |
| 257 | ret = BN_from_montgomery_word(r, t, mont); |
| 258 |
| 259 | err: |
| 260 | BN_CTX_end(ctx); |
| 261 |
| 262 | return ret; |
| 263 | } |
| 264 |
| 265 | int bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 266 | // If the high bit of |n| is set, R = 2^(width*BN_BITS2) < 2 * |n|, so we |
| 267 | // compute R - |n| rather than perform Montgomery reduction. |
| 268 | const BIGNUM *n = &mont->N; |
| 269 | if (n->width > 0 && (n->d[n->width - 1] >> (BN_BITS2 - 1)) != 0) { |
| 270 | if (!bn_wexpand(r, n->width)) { |
| 271 | return 0; |
| 272 | } |
| 273 | r->d[0] = 0 - n->d[0]; |
| 274 | for (int i = 1; i < n->width; i++) { |
| 275 | r->d[i] = ~n->d[i]; |
| 276 | } |
| 277 | r->width = n->width; |
| 278 | r->neg = 0; |
| 279 | return 1; |
| 280 | } |
| 281 |
| 282 | return BN_from_montgomery(r, &mont->RR, mont, ctx); |
| 283 | } |
| 284 |
| 285 | static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a, |
| 286 | const BIGNUM *b, |
| 287 | const BN_MONT_CTX *mont, |
| 288 | BN_CTX *ctx) { |
| 289 | int ret = 0; |
| 290 |
| 291 | BN_CTX_start(ctx); |
| 292 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 293 | if (tmp == NULL) { |
| 294 | goto err; |
| 295 | } |
| 296 |
| 297 | if (a == b) { |
| 298 | if (!bn_sqr_consttime(tmp, a, ctx)) { |
| 299 | goto err; |
| 300 | } |
| 301 | } else { |
| 302 | if (!bn_mul_consttime(tmp, a, b, ctx)) { |
| 303 | goto err; |
| 304 | } |
| 305 | } |
| 306 |
| 307 | // reduce from aRR to aR |
| 308 | if (!BN_from_montgomery_word(r, tmp, mont)) { |
| 309 | goto err; |
| 310 | } |
| 311 |
| 312 | ret = 1; |
| 313 |
| 314 | err: |
| 315 | BN_CTX_end(ctx); |
| 316 | return ret; |
| 317 | } |
| 318 |
| 319 | int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 320 | const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 321 | if (a->neg || b->neg) { |
| 322 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 323 | return 0; |
| 324 | } |
| 325 |
| 326 | #if defined(OPENSSL_BN_ASM_MONT) |
| 327 | // |bn_mul_mont| requires at least 128 bits of limbs. |
| 328 | int num = mont->N.width; |
| 329 | if (num >= (128 / BN_BITS2) && a->width == num && b->width == num) { |
| 330 | if (!bn_wexpand(r, num)) { |
| 331 | return 0; |
| 332 | } |
| 333 | // This bound is implied by |bn_mont_ctx_set_N_and_n0|. |bn_mul_mont| |
| 334 | // allocates |num| words on the stack, so |num| cannot be too large. |
| 335 | assert((size_t)num <= BN_MONTGOMERY_MAX_WORDS); |
| 336 | bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num); |
| 337 | r->neg = 0; |
| 338 | r->width = num; |
| 339 | return 1; |
| 340 | } |
| 341 | #endif |
| 342 |
| 343 | return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx); |
| 344 | } |
| 345 |
| 346 | int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont) { |
| 347 | return !BN_is_negative(bn) && bn_fits_in_words(bn, mont->N.width); |
| 348 | } |
| 349 |
| 350 | void bn_to_montgomery_small(BN_ULONG *r, const BN_ULONG *a, size_t num, |
| 351 | const BN_MONT_CTX *mont) { |
| 352 | bn_mod_mul_montgomery_small(r, a, mont->RR.d, num, mont); |
| 353 | } |
| 354 |
| 355 | void bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, |
| 356 | size_t num_a, const BN_MONT_CTX *mont) { |
| 357 | if (num_r != (size_t)mont->N.width || num_r > BN_SMALL_MAX_WORDS || |
| 358 | num_a > 2 * num_r) { |
| 359 | abort(); |
| 360 | } |
| 361 | BN_ULONG tmp[BN_SMALL_MAX_WORDS * 2] = {0}; |
| 362 | OPENSSL_memcpy(tmp, a, num_a * sizeof(BN_ULONG)); |
| 363 | if (!bn_from_montgomery_in_place(r, num_r, tmp, 2 * num_r, mont)) { |
| 364 | abort(); |
| 365 | } |
| 366 | OPENSSL_cleanse(tmp, 2 * num_r * sizeof(BN_ULONG)); |
| 367 | } |
| 368 |
| 369 | void bn_mod_mul_montgomery_small(BN_ULONG *r, const BN_ULONG *a, |
| 370 | const BN_ULONG *b, size_t num, |
| 371 | const BN_MONT_CTX *mont) { |
| 372 | if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) { |
| 373 | abort(); |
| 374 | } |
| 375 |
| 376 | #if defined(OPENSSL_BN_ASM_MONT) |
| 377 | // |bn_mul_mont| requires at least 128 bits of limbs. |
| 378 | if (num >= (128 / BN_BITS2)) { |
| 379 | bn_mul_mont(r, a, b, mont->N.d, mont->n0, num); |
| 380 | return; |
| 381 | } |
| 382 | #endif |
| 383 |
| 384 | // Compute the product. |
| 385 | BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; |
| 386 | if (a == b) { |
| 387 | bn_sqr_small(tmp, 2 * num, a, num); |
| 388 | } else { |
| 389 | bn_mul_small(tmp, 2 * num, a, num, b, num); |
| 390 | } |
| 391 |
| 392 | // Reduce. |
| 393 | if (!bn_from_montgomery_in_place(r, num, tmp, 2 * num, mont)) { |
| 394 | abort(); |
| 395 | } |
| 396 | OPENSSL_cleanse(tmp, 2 * num * sizeof(BN_ULONG)); |
| 397 | } |
| 398 |
| 399 | #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_X86_64) |
| 400 | void bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
| 401 | const BN_ULONG *np, const BN_ULONG *n0, size_t num) { |
| 402 | if (ap == bp && bn_sqr8x_mont_capable(num)) { |
| 403 | bn_sqr8x_mont(rp, ap, bn_mulx_adx_capable(), np, n0, num); |
| 404 | } else if (bn_mulx4x_mont_capable(num)) { |
| 405 | bn_mulx4x_mont(rp, ap, bp, np, n0, num); |
| 406 | } else if (bn_mul4x_mont_capable(num)) { |
| 407 | bn_mul4x_mont(rp, ap, bp, np, n0, num); |
| 408 | } else { |
| 409 | bn_mul_mont_nohw(rp, ap, bp, np, n0, num); |
| 410 | } |
| 411 | } |
| 412 | #endif |
| 413 |
| 414 | #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_ARM) |
| 415 | void bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
| 416 | const BN_ULONG *np, const BN_ULONG *n0, size_t num) { |
| 417 | if (bn_mul8x_mont_neon_capable(num)) { |
| 418 | bn_mul8x_mont_neon(rp, ap, bp, np, n0, num); |
| 419 | } else { |
| 420 | bn_mul_mont_nohw(rp, ap, bp, np, n0, num); |
| 421 | } |
| 422 | } |
| 423 | #endif |
| 424 |