1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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 * [email protected].
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 * ([email protected]). This product includes software written by Tim
107 * Hudson ([email protected]). */
108
109 #include <openssl/bn.h>
110
111 #include <assert.h>
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115
116 #include <openssl/err.h>
117 #include <openssl/mem.h>
118 #include <openssl/thread.h>
119
120 #include "internal.h"
121 #include "../../internal.h"
122
123
bn_mont_ctx_init(BN_MONT_CTX * mont)124 void bn_mont_ctx_init(BN_MONT_CTX *mont) {
125 OPENSSL_memset(mont, 0, sizeof(BN_MONT_CTX));
126 BN_init(&mont->RR);
127 BN_init(&mont->N);
128 }
129
bn_mont_ctx_cleanup(BN_MONT_CTX * mont)130 void bn_mont_ctx_cleanup(BN_MONT_CTX *mont) {
131 BN_free(&mont->RR);
132 BN_free(&mont->N);
133 }
134
BN_MONT_CTX_new(void)135 BN_MONT_CTX *BN_MONT_CTX_new(void) {
136 BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
137 if (ret == NULL) {
138 return NULL;
139 }
140
141 bn_mont_ctx_init(ret);
142 return ret;
143 }
144
BN_MONT_CTX_free(BN_MONT_CTX * mont)145 void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
146 if (mont == NULL) {
147 return;
148 }
149
150 bn_mont_ctx_cleanup(mont);
151 OPENSSL_free(mont);
152 }
153
BN_MONT_CTX_copy(BN_MONT_CTX * to,const BN_MONT_CTX * from)154 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) {
155 if (to == from) {
156 return to;
157 }
158
159 if (!BN_copy(&to->RR, &from->RR) ||
160 !BN_copy(&to->N, &from->N)) {
161 return NULL;
162 }
163 to->n0[0] = from->n0[0];
164 to->n0[1] = from->n0[1];
165 return to;
166 }
167
bn_mont_ctx_set_N_and_n0(BN_MONT_CTX * mont,const BIGNUM * mod)168 static int bn_mont_ctx_set_N_and_n0(BN_MONT_CTX *mont, const BIGNUM *mod) {
169 if (BN_is_zero(mod)) {
170 OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
171 return 0;
172 }
173 if (!BN_is_odd(mod)) {
174 OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
175 return 0;
176 }
177 if (BN_is_negative(mod)) {
178 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
179 return 0;
180 }
181 if (!bn_fits_in_words(mod, BN_MONTGOMERY_MAX_WORDS)) {
182 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
183 return 0;
184 }
185
186 // Save the modulus.
187 if (!BN_copy(&mont->N, mod)) {
188 OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
189 return 0;
190 }
191 // |mont->N| is always stored minimally. Computing RR efficiently leaks the
192 // size of the modulus. While the modulus may be private in RSA (one of the
193 // primes), their sizes are public, so this is fine.
194 bn_set_minimal_width(&mont->N);
195
196 // Find n0 such that n0 * N == -1 (mod r).
197 //
198 // Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the
199 // others, we could use a shorter R value and use faster |BN_ULONG|-based
200 // math instead of |uint64_t|-based math, which would be double-precision.
201 // However, currently only the assembler files know which is which.
202 static_assert(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
203 "BN_MONT_CTX_N0_LIMBS value is invalid");
204 static_assert(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t),
205 "uint64_t is insufficient precision for n0");
206 uint64_t n0 = bn_mont_n0(&mont->N);
207 mont->n0[0] = (BN_ULONG)n0;
208 #if BN_MONT_CTX_N0_LIMBS == 2
209 mont->n0[1] = (BN_ULONG)(n0 >> BN_BITS2);
210 #else
211 mont->n0[1] = 0;
212 #endif
213 return 1;
214 }
215
BN_MONT_CTX_set(BN_MONT_CTX * mont,const BIGNUM * mod,BN_CTX * ctx)216 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
217 if (!bn_mont_ctx_set_N_and_n0(mont, mod)) {
218 return 0;
219 }
220
221 BN_CTX *new_ctx = NULL;
222 if (ctx == NULL) {
223 new_ctx = BN_CTX_new();
224 if (new_ctx == NULL) {
225 return 0;
226 }
227 ctx = new_ctx;
228 }
229
230 // Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS2 such that R
231 // > mod. Even though the assembly on some 32-bit platforms works with 64-bit
232 // values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS *
233 // BN_BITS2|, is correct because R**2 will still be a multiple of the latter
234 // as |BN_MONT_CTX_N0_LIMBS| is either one or two.
235 unsigned lgBigR = mont->N.width * BN_BITS2;
236 BN_zero(&mont->RR);
237 int ok = BN_set_bit(&mont->RR, lgBigR * 2) &&
238 BN_mod(&mont->RR, &mont->RR, &mont->N, ctx) &&
239 bn_resize_words(&mont->RR, mont->N.width);
240 BN_CTX_free(new_ctx);
241 return ok;
242 }
243
BN_MONT_CTX_new_for_modulus(const BIGNUM * mod,BN_CTX * ctx)244 BN_MONT_CTX *BN_MONT_CTX_new_for_modulus(const BIGNUM *mod, BN_CTX *ctx) {
245 BN_MONT_CTX *mont = BN_MONT_CTX_new();
246 if (mont == NULL ||
247 !BN_MONT_CTX_set(mont, mod, ctx)) {
248 BN_MONT_CTX_free(mont);
249 return NULL;
250 }
251 return mont;
252 }
253
BN_MONT_CTX_new_consttime(const BIGNUM * mod,BN_CTX * ctx)254 BN_MONT_CTX *BN_MONT_CTX_new_consttime(const BIGNUM *mod, BN_CTX *ctx) {
255 BN_MONT_CTX *mont = BN_MONT_CTX_new();
256 if (mont == NULL ||
257 !bn_mont_ctx_set_N_and_n0(mont, mod) ||
258 !bn_mont_ctx_set_RR_consttime(mont, ctx)) {
259 BN_MONT_CTX_free(mont);
260 return NULL;
261 }
262 return mont;
263 }
264
BN_MONT_CTX_set_locked(BN_MONT_CTX ** pmont,CRYPTO_MUTEX * lock,const BIGNUM * mod,BN_CTX * bn_ctx)265 int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
266 const BIGNUM *mod, BN_CTX *bn_ctx) {
267 CRYPTO_MUTEX_lock_read(lock);
268 BN_MONT_CTX *ctx = *pmont;
269 CRYPTO_MUTEX_unlock_read(lock);
270
271 if (ctx) {
272 return 1;
273 }
274
275 CRYPTO_MUTEX_lock_write(lock);
276 if (*pmont == NULL) {
277 *pmont = BN_MONT_CTX_new_for_modulus(mod, bn_ctx);
278 }
279 const int ok = *pmont != NULL;
280 CRYPTO_MUTEX_unlock_write(lock);
281 return ok;
282 }
283
BN_to_montgomery(BIGNUM * ret,const BIGNUM * a,const BN_MONT_CTX * mont,BN_CTX * ctx)284 int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
285 BN_CTX *ctx) {
286 return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
287 }
288
bn_from_montgomery_in_place(BN_ULONG * r,size_t num_r,BN_ULONG * a,size_t num_a,const BN_MONT_CTX * mont)289 static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a,
290 size_t num_a, const BN_MONT_CTX *mont) {
291 const BN_ULONG *n = mont->N.d;
292 size_t num_n = mont->N.width;
293 if (num_r != num_n || num_a != 2 * num_n) {
294 OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
295 return 0;
296 }
297
298 // Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
299 // input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
300 // includes |carry| which is stored separately.
301 BN_ULONG n0 = mont->n0[0];
302 BN_ULONG carry = 0;
303 for (size_t i = 0; i < num_n; i++) {
304 BN_ULONG v = bn_mul_add_words(a + i, n, num_n, a[i] * n0);
305 v += carry + a[i + num_n];
306 carry |= (v != a[i + num_n]);
307 carry &= (v <= a[i + num_n]);
308 a[i + num_n] = v;
309 }
310
311 // Shift |num_n| words to divide by R. We have |a| < 2 * |n|. Note that |a|
312 // includes |carry| which is stored separately.
313 a += num_n;
314
315 // |a| thus requires at most one additional subtraction |n| to be reduced.
316 bn_reduce_once(r, a, carry, n, num_n);
317 return 1;
318 }
319
BN_from_montgomery_word(BIGNUM * ret,BIGNUM * r,const BN_MONT_CTX * mont)320 static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
321 const BN_MONT_CTX *mont) {
322 if (r->neg) {
323 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
324 return 0;
325 }
326
327 const BIGNUM *n = &mont->N;
328 if (n->width == 0) {
329 ret->width = 0;
330 return 1;
331 }
332
333 int max = 2 * n->width; // carry is stored separately
334 if (!bn_resize_words(r, max) ||
335 !bn_wexpand(ret, n->width)) {
336 return 0;
337 }
338
339 ret->width = n->width;
340 ret->neg = 0;
341 return bn_from_montgomery_in_place(ret->d, ret->width, r->d, r->width, mont);
342 }
343
BN_from_montgomery(BIGNUM * r,const BIGNUM * a,const BN_MONT_CTX * mont,BN_CTX * ctx)344 int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont,
345 BN_CTX *ctx) {
346 int ret = 0;
347 BIGNUM *t;
348
349 BN_CTX_start(ctx);
350 t = BN_CTX_get(ctx);
351 if (t == NULL ||
352 !BN_copy(t, a)) {
353 goto err;
354 }
355
356 ret = BN_from_montgomery_word(r, t, mont);
357
358 err:
359 BN_CTX_end(ctx);
360
361 return ret;
362 }
363
bn_one_to_montgomery(BIGNUM * r,const BN_MONT_CTX * mont,BN_CTX * ctx)364 int bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx) {
365 // If the high bit of |n| is set, R = 2^(width*BN_BITS2) < 2 * |n|, so we
366 // compute R - |n| rather than perform Montgomery reduction.
367 const BIGNUM *n = &mont->N;
368 if (n->width > 0 && (n->d[n->width - 1] >> (BN_BITS2 - 1)) != 0) {
369 if (!bn_wexpand(r, n->width)) {
370 return 0;
371 }
372 r->d[0] = 0 - n->d[0];
373 for (int i = 1; i < n->width; i++) {
374 r->d[i] = ~n->d[i];
375 }
376 r->width = n->width;
377 r->neg = 0;
378 return 1;
379 }
380
381 return BN_from_montgomery(r, &mont->RR, mont, ctx);
382 }
383
bn_mod_mul_montgomery_fallback(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BN_MONT_CTX * mont,BN_CTX * ctx)384 static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a,
385 const BIGNUM *b,
386 const BN_MONT_CTX *mont,
387 BN_CTX *ctx) {
388 int ret = 0;
389
390 BN_CTX_start(ctx);
391 BIGNUM *tmp = BN_CTX_get(ctx);
392 if (tmp == NULL) {
393 goto err;
394 }
395
396 if (a == b) {
397 if (!bn_sqr_consttime(tmp, a, ctx)) {
398 goto err;
399 }
400 } else {
401 if (!bn_mul_consttime(tmp, a, b, ctx)) {
402 goto err;
403 }
404 }
405
406 // reduce from aRR to aR
407 if (!BN_from_montgomery_word(r, tmp, mont)) {
408 goto err;
409 }
410
411 ret = 1;
412
413 err:
414 BN_CTX_end(ctx);
415 return ret;
416 }
417
BN_mod_mul_montgomery(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BN_MONT_CTX * mont,BN_CTX * ctx)418 int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
419 const BN_MONT_CTX *mont, BN_CTX *ctx) {
420 if (a->neg || b->neg) {
421 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
422 return 0;
423 }
424
425 #if defined(OPENSSL_BN_ASM_MONT)
426 // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
427 int num = mont->N.width;
428 if (num >= (128 / BN_BITS2) &&
429 a->width == num &&
430 b->width == num) {
431 if (!bn_wexpand(r, num)) {
432 return 0;
433 }
434 // This bound is implied by |bn_mont_ctx_set_N_and_n0|. |bn_mul_mont|
435 // allocates |num| words on the stack, so |num| cannot be too large.
436 assert((size_t)num <= BN_MONTGOMERY_MAX_WORDS);
437 if (!bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
438 // The check above ensures this won't happen.
439 assert(0);
440 OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
441 return 0;
442 }
443 r->neg = 0;
444 r->width = num;
445 return 1;
446 }
447 #endif
448
449 return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx);
450 }
451
bn_less_than_montgomery_R(const BIGNUM * bn,const BN_MONT_CTX * mont)452 int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont) {
453 return !BN_is_negative(bn) &&
454 bn_fits_in_words(bn, mont->N.width);
455 }
456
bn_to_montgomery_small(BN_ULONG * r,const BN_ULONG * a,size_t num,const BN_MONT_CTX * mont)457 void bn_to_montgomery_small(BN_ULONG *r, const BN_ULONG *a, size_t num,
458 const BN_MONT_CTX *mont) {
459 bn_mod_mul_montgomery_small(r, a, mont->RR.d, num, mont);
460 }
461
bn_from_montgomery_small(BN_ULONG * r,size_t num_r,const BN_ULONG * a,size_t num_a,const BN_MONT_CTX * mont)462 void bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
463 size_t num_a, const BN_MONT_CTX *mont) {
464 if (num_r != (size_t)mont->N.width || num_r > BN_SMALL_MAX_WORDS ||
465 num_a > 2 * num_r) {
466 abort();
467 }
468 BN_ULONG tmp[BN_SMALL_MAX_WORDS * 2] = {0};
469 OPENSSL_memcpy(tmp, a, num_a * sizeof(BN_ULONG));
470 if (!bn_from_montgomery_in_place(r, num_r, tmp, 2 * num_r, mont)) {
471 abort();
472 }
473 OPENSSL_cleanse(tmp, 2 * num_r * sizeof(BN_ULONG));
474 }
475
bn_mod_mul_montgomery_small(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,size_t num,const BN_MONT_CTX * mont)476 void bn_mod_mul_montgomery_small(BN_ULONG *r, const BN_ULONG *a,
477 const BN_ULONG *b, size_t num,
478 const BN_MONT_CTX *mont) {
479 if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) {
480 abort();
481 }
482
483 #if defined(OPENSSL_BN_ASM_MONT)
484 // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
485 if (num >= (128 / BN_BITS2)) {
486 if (!bn_mul_mont(r, a, b, mont->N.d, mont->n0, num)) {
487 abort(); // The check above ensures this won't happen.
488 }
489 return;
490 }
491 #endif
492
493 // Compute the product.
494 BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS];
495 if (a == b) {
496 bn_sqr_small(tmp, 2 * num, a, num);
497 } else {
498 bn_mul_small(tmp, 2 * num, a, num, b, num);
499 }
500
501 // Reduce.
502 if (!bn_from_montgomery_in_place(r, num, tmp, 2 * num, mont)) {
503 abort();
504 }
505 OPENSSL_cleanse(tmp, 2 * num * sizeof(BN_ULONG));
506 }
507
508 #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_X86_64)
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0,size_t num)509 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
510 const BN_ULONG *np, const BN_ULONG *n0, size_t num) {
511 if (ap == bp && bn_sqr8x_mont_capable(num)) {
512 return bn_sqr8x_mont(rp, ap, bn_mulx_adx_capable(), np, n0, num);
513 }
514 if (bn_mulx4x_mont_capable(num)) {
515 return bn_mulx4x_mont(rp, ap, bp, np, n0, num);
516 }
517 if (bn_mul4x_mont_capable(num)) {
518 return bn_mul4x_mont(rp, ap, bp, np, n0, num);
519 }
520 return bn_mul_mont_nohw(rp, ap, bp, np, n0, num);
521 }
522 #endif
523
524 #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_ARM)
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0,size_t num)525 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
526 const BN_ULONG *np, const BN_ULONG *n0, size_t num) {
527 if (bn_mul8x_mont_neon_capable(num)) {
528 return bn_mul8x_mont_neon(rp, ap, bp, np, n0, num);
529 }
530 return bn_mul_mont_nohw(rp, ap, bp, np, n0, num);
531 }
532 #endif
533