| 6 |
| * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 |
| 109 | #include <openssl/bn.h> |
| 110 |
| 111 | #include <assert.h> |
| 112 | #include <string.h> |
| 113 |
| 114 | #include <openssl/err.h> |
| 115 | #include <openssl/mem.h> |
| 116 | #include <openssl/thread.h> |
| 117 | #include <openssl/type_check.h> |
| 118 |
| 119 | #include "internal.h" |
| 120 | #include "../../internal.h" |
| 121 |
| 122 |
| 123 | #if !defined(OPENSSL_NO_ASM) && \ |
| 124 | (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \ |
| 125 | defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) |
| 126 | #define OPENSSL_BN_ASM_MONT |
| 127 | #endif |
| 128 |
| 129 | static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a, |
| 130 | const BIGNUM *b, |
| 131 | const BN_MONT_CTX *mont, BN_CTX *ctx); |
| 132 |
| 133 |
| 134 | BN_MONT_CTX *BN_MONT_CTX_new(void) { |
| 135 | BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX)); |
| 136 |
| 137 | if (ret == NULL) { |
| 138 | return NULL; |
| 139 | } |
| 140 |
| 141 | OPENSSL_memset(ret, 0, sizeof(BN_MONT_CTX)); |
| 142 | BN_init(&ret->RR); |
| 143 | BN_init(&ret->N); |
| 144 |
| 145 | return ret; |
| 146 | } |
| 147 |
| 148 | void BN_MONT_CTX_free(BN_MONT_CTX *mont) { |
| 149 | if (mont == NULL) { |
| 150 | return; |
| 151 | } |
| 152 |
| 153 | BN_free(&mont->RR); |
| 154 | BN_free(&mont->N); |
| 155 | OPENSSL_free(mont); |
| 156 | } |
| 157 |
| 158 | BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) { |
| 159 | if (to == from) { |
| 160 | return to; |
| 161 | } |
| 162 |
| 163 | if (!BN_copy(&to->RR, &from->RR) || |
| 164 | !BN_copy(&to->N, &from->N)) { |
| 165 | return NULL; |
| 166 | } |
| 167 | to->n0[0] = from->n0[0]; |
| 168 | to->n0[1] = from->n0[1]; |
| 169 | return to; |
| 170 | } |
| 171 |
| 172 | OPENSSL_COMPILE_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2, |
| 173 | BN_MONT_CTX_N0_LIMBS_VALUE_INVALID); |
| 174 | OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == |
| 175 | sizeof(uint64_t), BN_MONT_CTX_set_64_bit_mismatch); |
| 176 |
| 177 | int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) { |
| 178 | if (BN_is_zero(mod)) { |
| 179 | OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO); |
| 180 | return 0; |
| 181 | } |
| 182 | if (!BN_is_odd(mod)) { |
| 183 | OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS); |
| 184 | return 0; |
| 185 | } |
| 186 | if (BN_is_negative(mod)) { |
| 187 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 188 | return 0; |
| 189 | } |
| 190 |
| 191 | // Save the modulus. |
| 192 | if (!BN_copy(&mont->N, mod)) { |
| 193 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); |
| 194 | return 0; |
| 195 | } |
| 196 |
| 197 | // Find n0 such that n0 * N == -1 (mod r). |
| 198 | // |
| 199 | // Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the |
| 200 | // others, we could use a shorter R value and use faster |BN_ULONG|-based |
| 201 | // math instead of |uint64_t|-based math, which would be double-precision. |
| 202 | // However, currently only the assembler files know which is which. |
| 203 | uint64_t n0 = bn_mont_n0(mod); |
| 204 | mont->n0[0] = (BN_ULONG)n0; |
| 205 | #if BN_MONT_CTX_N0_LIMBS == 2 |
| 206 | mont->n0[1] = (BN_ULONG)(n0 >> BN_BITS2); |
| 207 | #else |
| 208 | mont->n0[1] = 0; |
| 209 | #endif |
| 210 |
| 211 | // Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS2 such that R |
| 212 | // > mod. Even though the assembly on some 32-bit platforms works with 64-bit |
| 213 | // values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS * |
| 214 | // BN_BITS2|, is correct because R**2 will still be a multiple of the latter |
| 215 | // as |BN_MONT_CTX_N0_LIMBS| is either one or two. |
| 216 | // |
| 217 | // XXX: This is not constant time with respect to |mont->N|, but it should be. |
| 218 | unsigned lgBigR = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2; |
| 219 | if (!bn_mod_exp_base_2_vartime(&mont->RR, lgBigR * 2, &mont->N)) { |
| 220 | return 0; |
| 221 | } |
| 222 |
| 223 | return 1; |
| 224 | } |
| 225 |
| 226 | int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock, |
| 227 | const BIGNUM *mod, BN_CTX *bn_ctx) { |
| 228 | CRYPTO_MUTEX_lock_read(lock); |
| 229 | BN_MONT_CTX *ctx = *pmont; |
| 230 | CRYPTO_MUTEX_unlock_read(lock); |
| 231 |
| 232 | if (ctx) { |
| 233 | return 1; |
| 234 | } |
| 235 |
| 236 | CRYPTO_MUTEX_lock_write(lock); |
| 237 | ctx = *pmont; |
| 238 | if (ctx) { |
| 239 | goto out; |
| 240 | } |
| 241 |
| 242 | ctx = BN_MONT_CTX_new(); |
| 243 | if (ctx == NULL) { |
| 244 | goto out; |
| 245 | } |
| 246 | if (!BN_MONT_CTX_set(ctx, mod, bn_ctx)) { |
| 247 | BN_MONT_CTX_free(ctx); |
| 248 | ctx = NULL; |
| 249 | goto out; |
| 250 | } |
| 251 | *pmont = ctx; |
| 252 |
| 253 | out: |
| 254 | CRYPTO_MUTEX_unlock_write(lock); |
| 255 | return ctx != NULL; |
| 256 | } |
| 257 |
| 258 | int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont, |
| 259 | BN_CTX *ctx) { |
| 260 | return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx); |
| 261 | } |
| 262 |
| 263 | static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a, |
| 264 | size_t num_a, const BN_MONT_CTX *mont) { |
| 265 | const BN_ULONG *n = mont->N.d; |
| 266 | size_t num_n = mont->N.top; |
| 267 | if (num_r != num_n || num_a != 2 * num_n) { |
| 268 | OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 269 | return 0; |
| 270 | } |
| 271 |
| 272 | // Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On |
| 273 | // input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r| |
| 274 | // includes |carry| which is stored separately. |
| 275 | BN_ULONG n0 = mont->n0[0]; |
| 276 | BN_ULONG carry = 0; |
| 277 | for (size_t i = 0; i < num_n; i++) { |
| 278 | BN_ULONG v = bn_mul_add_words(a + i, n, num_n, a[i] * n0); |
| 279 | v += carry + a[i + num_n]; |
| 280 | carry |= (v != a[i + num_n]); |
| 281 | carry &= (v <= a[i + num_n]); |
| 282 | a[i + num_n] = v; |
| 283 | } |
| 284 |
| 285 | // Shift |num_n| words to divide by R. We have |a| < 2 * |n|. Note that |a| |
| 286 | // includes |carry| which is stored separately. |
| 287 | a += num_n; |
| 288 |
| 289 | // |a| thus requires at most one additional subtraction |n| to be reduced. |
| 290 | // Subtract |n| and select the answer in constant time. |
| 291 | OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 292 | crypto_word_t_too_small); |
| 293 | BN_ULONG v = bn_sub_words(r, a, n, num_n) - carry; |
| 294 | // |v| is one if |a| - |n| underflowed or zero if it did not. Note |v| cannot |
| 295 | // be -1. That would imply the subtraction did not fit in |num_n| words, and |
| 296 | // we know at most one subtraction is needed. |
| 297 | v = 0u - v; |
| 298 | for (size_t i = 0; i < num_n; i++) { |
| 299 | r[i] = constant_time_select_w(v, a[i], r[i]); |
| 300 | a[i] = 0; |
| 301 | } |
| 302 | return 1; |
| 303 | } |
| 304 |
| 305 | static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, |
| 306 | const BN_MONT_CTX *mont) { |
| 307 | const BIGNUM *n = &mont->N; |
| 308 | if (n->top == 0) { |
| 309 | ret->top = 0; |
| 310 | return 1; |
| 311 | } |
| 312 |
| 313 | int max = (2 * n->top); // carry is stored separately |
| 314 | if (!bn_wexpand(r, max) || |
| 315 | !bn_wexpand(ret, n->top)) { |
| 316 | return 0; |
| 317 | } |
| 318 | // Clear the top words of |r|. |
| 319 | if (max > r->top) { |
| 320 | OPENSSL_memset(r->d + r->top, 0, (max - r->top) * sizeof(BN_ULONG)); |
| 321 | } |
| 322 | r->top = max; |
| 323 | ret->top = n->top; |
| 324 |
| 325 | if (!bn_from_montgomery_in_place(ret->d, ret->top, r->d, r->top, mont)) { |
| 326 | return 0; |
| 327 | } |
| 328 | ret->neg = r->neg; |
| 329 |
| 330 | bn_correct_top(r); |
| 331 | bn_correct_top(ret); |
| 332 | return 1; |
| 333 | } |
| 334 |
| 335 | int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont, |
| 336 | BN_CTX *ctx) { |
| 337 | int ret = 0; |
| 338 | BIGNUM *t; |
| 339 |
| 340 | BN_CTX_start(ctx); |
| 341 | t = BN_CTX_get(ctx); |
| 342 | if (t == NULL || |
| 343 | !BN_copy(t, a)) { |
| 344 | goto err; |
| 345 | } |
| 346 |
| 347 | ret = BN_from_montgomery_word(r, t, mont); |
| 348 |
| 349 | err: |
| 350 | BN_CTX_end(ctx); |
| 351 |
| 352 | return ret; |
| 353 | } |
| 354 |
| 355 | int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 356 | const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 357 | #if !defined(OPENSSL_BN_ASM_MONT) |
| 358 | return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx); |
| 359 | #else |
| 360 | int num = mont->N.top; |
| 361 |
| 362 | // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86. |
| 363 | if (num < (128 / BN_BITS2) || |
| 364 | a->top != num || |
| 365 | b->top != num) { |
| 366 | return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx); |
| 367 | } |
| 368 |
| 369 | if (!bn_wexpand(r, num)) { |
| 370 | return 0; |
| 371 | } |
| 372 | if (!bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) { |
| 373 | // The check above ensures this won't happen. |
| 374 | assert(0); |
| 375 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); |
| 376 | return 0; |
| 377 | } |
| 378 | r->neg = a->neg ^ b->neg; |
| 379 | r->top = num; |
| 380 | bn_correct_top(r); |
| 381 |
| 382 | return 1; |
| 383 | #endif |
| 384 | } |
| 385 |
| 386 | static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a, |
| 387 | const BIGNUM *b, |
| 388 | const BN_MONT_CTX *mont, |
| 389 | BN_CTX *ctx) { |
| 390 | int ret = 0; |
| 391 |
| 392 | BN_CTX_start(ctx); |
| 393 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 394 | if (tmp == NULL) { |
| 395 | goto err; |
| 396 | } |
| 397 |
| 398 | if (a == b) { |
| 399 | if (!BN_sqr(tmp, a, ctx)) { |
| 400 | goto err; |
| 401 | } |
| 402 | } else { |
| 403 | if (!BN_mul(tmp, a, b, ctx)) { |
| 404 | goto err; |
| 405 | } |
| 406 | } |
| 407 |
| 408 | // reduce from aRR to aR |
| 409 | if (!BN_from_montgomery_word(r, tmp, mont)) { |
| 410 | goto err; |
| 411 | } |
| 412 |
| 413 | ret = 1; |
| 414 |
| 415 | err: |
| 416 | BN_CTX_end(ctx); |
| 417 | return ret; |
| 418 | } |
| 419 |
| 420 | int bn_to_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, |
| 421 | size_t num_a, const BN_MONT_CTX *mont) { |
| 422 | return bn_mod_mul_montgomery_small(r, num_r, a, num_a, mont->RR.d, |
| 423 | mont->RR.top, mont); |
| 424 | } |
| 425 |
| 426 | int bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, |
| 427 | size_t num_a, const BN_MONT_CTX *mont) { |
| 428 | size_t num_n = mont->N.top; |
| 429 | if (num_a > 2 * num_n || num_r != num_n || num_n > BN_SMALL_MAX_WORDS) { |
| 430 | OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 431 | return 0; |
| 432 | } |
| 433 | BN_ULONG tmp[BN_SMALL_MAX_WORDS * 2]; |
| 434 | size_t num_tmp = 2 * num_n; |
| 435 | OPENSSL_memcpy(tmp, a, num_a * sizeof(BN_ULONG)); |
| 436 | OPENSSL_memset(tmp + num_a, 0, (num_tmp - num_a) * sizeof(BN_ULONG)); |
| 437 | int ret = bn_from_montgomery_in_place(r, num_r, tmp, num_tmp, mont); |
| 438 | OPENSSL_cleanse(tmp, num_tmp * sizeof(BN_ULONG)); |
| 439 | return ret; |
| 440 | } |
| 441 |
| 442 | int bn_mod_mul_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, |
| 443 | size_t num_a, const BN_ULONG *b, size_t num_b, |
| 444 | const BN_MONT_CTX *mont) { |
| 445 | size_t num_n = mont->N.top; |
| 446 | if (num_r != num_n || num_a + num_b > 2 * num_n || |
| 447 | num_n > BN_SMALL_MAX_WORDS) { |
| 448 | OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 449 | return 0; |
| 450 | } |
| 451 |
| 452 | #if defined(OPENSSL_BN_ASM_MONT) |
| 453 | // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86. |
| 454 | if (num_n >= (128 / BN_BITS2) && |
| 455 | num_a == num_n && |
| 456 | num_b == num_n) { |
| 457 | if (!bn_mul_mont(r, a, b, mont->N.d, mont->n0, num_n)) { |
| 458 | assert(0); // The check above ensures this won't happen. |
| 459 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); |
| 460 | return 0; |
| 461 | } |
| 462 | return 1; |
| 463 | } |
| 464 | #endif |
| 465 |
| 466 | // Compute the product. |
| 467 | BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; |
| 468 | size_t num_tmp = 2 * num_n; |
| 469 | size_t num_ab = num_a + num_b; |
| 470 | if (a == b && num_a == num_b) { |
| 471 | if (!bn_sqr_small(tmp, num_ab, a, num_a)) { |
| 472 | return 0; |
| 473 | } |
| 474 | } else if (!bn_mul_small(tmp, num_ab, a, num_a, b, num_b)) { |
| 475 | return 0; |
| 476 | } |
| 477 |
| 478 | // Zero-extend to full width and reduce. |
| 479 | OPENSSL_memset(tmp + num_ab, 0, (num_tmp - num_ab) * sizeof(BN_ULONG)); |
| 480 | int ret = bn_from_montgomery_in_place(r, num_r, tmp, num_tmp, mont); |
| 481 | OPENSSL_cleanse(tmp, num_tmp * sizeof(BN_ULONG)); |
| 482 | return ret; |
| 483 | } |
| 484 |