/src/boringssl/crypto/fipsmodule/bn/montgomery.c.inc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
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 <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 | | |
124 | 914 | void bn_mont_ctx_init(BN_MONT_CTX *mont) { |
125 | 914 | OPENSSL_memset(mont, 0, sizeof(BN_MONT_CTX)); |
126 | 914 | BN_init(&mont->RR); |
127 | 914 | BN_init(&mont->N); |
128 | 914 | } |
129 | | |
130 | 914 | void bn_mont_ctx_cleanup(BN_MONT_CTX *mont) { |
131 | 914 | BN_free(&mont->RR); |
132 | 914 | BN_free(&mont->N); |
133 | 914 | } |
134 | | |
135 | 38.9k | BN_MONT_CTX *BN_MONT_CTX_new(void) { |
136 | 38.9k | BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX)); |
137 | 38.9k | if (ret == NULL) { |
138 | 0 | return NULL; |
139 | 0 | } |
140 | | |
141 | 38.9k | bn_mont_ctx_init(ret); |
142 | 38.9k | return ret; |
143 | 38.9k | } |
144 | | |
145 | 5.43k | void BN_MONT_CTX_free(BN_MONT_CTX *mont) { |
146 | 5.43k | if (mont == NULL) { |
147 | 4.52k | return; |
148 | 4.52k | } |
149 | | |
150 | 914 | bn_mont_ctx_cleanup(mont); |
151 | 914 | OPENSSL_free(mont); |
152 | 914 | } |
153 | | |
154 | 0 | BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) { |
155 | 0 | if (to == from) { |
156 | 0 | return to; |
157 | 0 | } |
158 | | |
159 | 0 | if (!BN_copy(&to->RR, &from->RR) || |
160 | 0 | !BN_copy(&to->N, &from->N)) { |
161 | 0 | return NULL; |
162 | 0 | } |
163 | 0 | to->n0[0] = from->n0[0]; |
164 | 0 | to->n0[1] = from->n0[1]; |
165 | 0 | return to; |
166 | 0 | } |
167 | | |
168 | 914 | static int bn_mont_ctx_set_N_and_n0(BN_MONT_CTX *mont, const BIGNUM *mod) { |
169 | 914 | if (BN_is_zero(mod)) { |
170 | 17 | OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO); |
171 | 17 | return 0; |
172 | 17 | } |
173 | 897 | if (!BN_is_odd(mod)) { |
174 | 26 | OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS); |
175 | 26 | return 0; |
176 | 26 | } |
177 | 871 | if (BN_is_negative(mod)) { |
178 | 0 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
179 | 0 | return 0; |
180 | 0 | } |
181 | 871 | if (!bn_fits_in_words(mod, BN_MONTGOMERY_MAX_WORDS)) { |
182 | 0 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
183 | 0 | return 0; |
184 | 0 | } |
185 | | |
186 | | // Save the modulus. |
187 | 871 | if (!BN_copy(&mont->N, mod)) { |
188 | 0 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); |
189 | 0 | return 0; |
190 | 0 | } |
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 | 871 | 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 | 871 | static_assert(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2, |
203 | 871 | "BN_MONT_CTX_N0_LIMBS value is invalid"); |
204 | 871 | static_assert(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t), |
205 | 871 | "uint64_t is insufficient precision for n0"); |
206 | 871 | uint64_t n0 = bn_mont_n0(&mont->N); |
207 | 871 | 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 | 871 | mont->n0[1] = 0; |
212 | 871 | #endif |
213 | 871 | return 1; |
214 | 871 | } |
215 | | |
216 | 116 | int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) { |
217 | 116 | if (!bn_mont_ctx_set_N_and_n0(mont, mod)) { |
218 | 43 | return 0; |
219 | 43 | } |
220 | | |
221 | 73 | BN_CTX *new_ctx = NULL; |
222 | 73 | if (ctx == NULL) { |
223 | 0 | new_ctx = BN_CTX_new(); |
224 | 0 | if (new_ctx == NULL) { |
225 | 0 | return 0; |
226 | 0 | } |
227 | 0 | ctx = new_ctx; |
228 | 0 | } |
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 | 73 | unsigned lgBigR = mont->N.width * BN_BITS2; |
236 | 73 | BN_zero(&mont->RR); |
237 | 73 | int ok = BN_set_bit(&mont->RR, lgBigR * 2) && |
238 | 73 | BN_mod(&mont->RR, &mont->RR, &mont->N, ctx) && |
239 | 73 | bn_resize_words(&mont->RR, mont->N.width); |
240 | 73 | BN_CTX_free(new_ctx); |
241 | 73 | return ok; |
242 | 73 | } |
243 | | |
244 | 76 | BN_MONT_CTX *BN_MONT_CTX_new_for_modulus(const BIGNUM *mod, BN_CTX *ctx) { |
245 | 76 | BN_MONT_CTX *mont = BN_MONT_CTX_new(); |
246 | 76 | if (mont == NULL || |
247 | 76 | !BN_MONT_CTX_set(mont, mod, ctx)) { |
248 | 11 | BN_MONT_CTX_free(mont); |
249 | 11 | return NULL; |
250 | 11 | } |
251 | 65 | return mont; |
252 | 76 | } |
253 | | |
254 | 798 | BN_MONT_CTX *BN_MONT_CTX_new_consttime(const BIGNUM *mod, BN_CTX *ctx) { |
255 | 798 | BN_MONT_CTX *mont = BN_MONT_CTX_new(); |
256 | 798 | if (mont == NULL || |
257 | 798 | !bn_mont_ctx_set_N_and_n0(mont, mod) || |
258 | 798 | !bn_mont_ctx_set_RR_consttime(mont, ctx)) { |
259 | 0 | BN_MONT_CTX_free(mont); |
260 | 0 | return NULL; |
261 | 0 | } |
262 | 798 | return mont; |
263 | 798 | } |
264 | | |
265 | | int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock, |
266 | 59 | const BIGNUM *mod, BN_CTX *bn_ctx) { |
267 | 59 | CRYPTO_MUTEX_lock_read(lock); |
268 | 59 | BN_MONT_CTX *ctx = *pmont; |
269 | 59 | CRYPTO_MUTEX_unlock_read(lock); |
270 | | |
271 | 59 | if (ctx) { |
272 | 0 | return 1; |
273 | 0 | } |
274 | | |
275 | 59 | CRYPTO_MUTEX_lock_write(lock); |
276 | 59 | if (*pmont == NULL) { |
277 | 59 | *pmont = BN_MONT_CTX_new_for_modulus(mod, bn_ctx); |
278 | 59 | } |
279 | 59 | const int ok = *pmont != NULL; |
280 | 59 | CRYPTO_MUTEX_unlock_write(lock); |
281 | 59 | return ok; |
282 | 59 | } |
283 | | |
284 | | int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont, |
285 | 22.7k | BN_CTX *ctx) { |
286 | 22.7k | return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx); |
287 | 22.7k | } |
288 | | |
289 | | static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a, |
290 | 2.47M | size_t num_a, const BN_MONT_CTX *mont) { |
291 | 2.47M | const BN_ULONG *n = mont->N.d; |
292 | 2.47M | size_t num_n = mont->N.width; |
293 | 2.47M | if (num_r != num_n || num_a != 2 * num_n) { |
294 | 0 | OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
295 | 0 | return 0; |
296 | 0 | } |
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 | 2.47M | BN_ULONG n0 = mont->n0[0]; |
302 | 2.47M | BN_ULONG carry = 0; |
303 | 37.2M | for (size_t i = 0; i < num_n; i++) { |
304 | 34.8M | BN_ULONG v = bn_mul_add_words(a + i, n, num_n, a[i] * n0); |
305 | 34.8M | v += carry + a[i + num_n]; |
306 | 34.8M | carry |= (v != a[i + num_n]); |
307 | 34.8M | carry &= (v <= a[i + num_n]); |
308 | 34.8M | a[i + num_n] = v; |
309 | 34.8M | } |
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 | 2.47M | a += num_n; |
314 | | |
315 | | // |a| thus requires at most one additional subtraction |n| to be reduced. |
316 | 2.47M | bn_reduce_once(r, a, carry, n, num_n); |
317 | 2.47M | return 1; |
318 | 2.47M | } |
319 | | |
320 | | static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, |
321 | 2.26M | const BN_MONT_CTX *mont) { |
322 | 2.26M | if (r->neg) { |
323 | 0 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
324 | 0 | return 0; |
325 | 0 | } |
326 | | |
327 | 2.26M | const BIGNUM *n = &mont->N; |
328 | 2.26M | if (n->width == 0) { |
329 | 0 | ret->width = 0; |
330 | 0 | return 1; |
331 | 0 | } |
332 | | |
333 | 2.26M | int max = 2 * n->width; // carry is stored separately |
334 | 2.26M | if (!bn_resize_words(r, max) || |
335 | 2.26M | !bn_wexpand(ret, n->width)) { |
336 | 0 | return 0; |
337 | 0 | } |
338 | | |
339 | 2.26M | ret->width = n->width; |
340 | 2.26M | ret->neg = 0; |
341 | 2.26M | return bn_from_montgomery_in_place(ret->d, ret->width, r->d, r->width, mont); |
342 | 2.26M | } |
343 | | |
344 | | int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont, |
345 | 7.75k | BN_CTX *ctx) { |
346 | 7.75k | int ret = 0; |
347 | 7.75k | BIGNUM *t; |
348 | | |
349 | 7.75k | BN_CTX_start(ctx); |
350 | 7.75k | t = BN_CTX_get(ctx); |
351 | 7.75k | if (t == NULL || |
352 | 7.75k | !BN_copy(t, a)) { |
353 | 0 | goto err; |
354 | 0 | } |
355 | | |
356 | 7.75k | ret = BN_from_montgomery_word(r, t, mont); |
357 | | |
358 | 7.75k | err: |
359 | 7.75k | BN_CTX_end(ctx); |
360 | | |
361 | 7.75k | return ret; |
362 | 7.75k | } |
363 | | |
364 | 4.66k | 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 | 4.66k | const BIGNUM *n = &mont->N; |
368 | 4.66k | if (n->width > 0 && (n->d[n->width - 1] >> (BN_BITS2 - 1)) != 0) { |
369 | 1.16k | if (!bn_wexpand(r, n->width)) { |
370 | 0 | return 0; |
371 | 0 | } |
372 | 1.16k | r->d[0] = 0 - n->d[0]; |
373 | 15.6k | for (int i = 1; i < n->width; i++) { |
374 | 14.5k | r->d[i] = ~n->d[i]; |
375 | 14.5k | } |
376 | 1.16k | r->width = n->width; |
377 | 1.16k | r->neg = 0; |
378 | 1.16k | return 1; |
379 | 1.16k | } |
380 | | |
381 | 3.49k | return BN_from_montgomery(r, &mont->RR, mont, ctx); |
382 | 4.66k | } |
383 | | |
384 | | static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a, |
385 | | const BIGNUM *b, |
386 | | const BN_MONT_CTX *mont, |
387 | 2.25M | BN_CTX *ctx) { |
388 | 2.25M | int ret = 0; |
389 | | |
390 | 2.25M | BN_CTX_start(ctx); |
391 | 2.25M | BIGNUM *tmp = BN_CTX_get(ctx); |
392 | 2.25M | if (tmp == NULL) { |
393 | 0 | goto err; |
394 | 0 | } |
395 | | |
396 | 2.25M | if (a == b) { |
397 | 1.92M | if (!bn_sqr_consttime(tmp, a, ctx)) { |
398 | 0 | goto err; |
399 | 0 | } |
400 | 1.92M | } else { |
401 | 325k | if (!bn_mul_consttime(tmp, a, b, ctx)) { |
402 | 0 | goto err; |
403 | 0 | } |
404 | 325k | } |
405 | | |
406 | | // reduce from aRR to aR |
407 | 2.25M | if (!BN_from_montgomery_word(r, tmp, mont)) { |
408 | 0 | goto err; |
409 | 0 | } |
410 | | |
411 | 2.25M | ret = 1; |
412 | | |
413 | 2.25M | err: |
414 | 2.25M | BN_CTX_end(ctx); |
415 | 2.25M | return ret; |
416 | 2.25M | } |
417 | | |
418 | | int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
419 | 2.62M | const BN_MONT_CTX *mont, BN_CTX *ctx) { |
420 | 2.62M | if (a->neg || b->neg) { |
421 | 0 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
422 | 0 | return 0; |
423 | 0 | } |
424 | | |
425 | | #if defined(OPENSSL_BN_ASM_MONT) |
426 | | // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86. |
427 | 372k | int num = mont->N.width; |
428 | 372k | if (num >= (128 / BN_BITS2) && |
429 | 372k | a->width == num && |
430 | 372k | b->width == num) { |
431 | 371k | if (!bn_wexpand(r, num)) { |
432 | 0 | return 0; |
433 | 0 | } |
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 | 371k | assert((size_t)num <= BN_MONTGOMERY_MAX_WORDS); |
437 | 371k | 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 | 0 | assert(0); |
440 | 0 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); |
441 | 0 | return 0; |
442 | 0 | } |
443 | 371k | r->neg = 0; |
444 | 371k | r->width = num; |
445 | 371k | return 1; |
446 | 371k | } |
447 | 426 | #endif |
448 | | |
449 | 2.25M | return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx); |
450 | 372k | } Line | Count | Source | 419 | 2.25M | const BN_MONT_CTX *mont, BN_CTX *ctx) { | 420 | 2.25M | if (a->neg || b->neg) { | 421 | 0 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); | 422 | 0 | return 0; | 423 | 0 | } | 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 | 2.25M | return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx); | 450 | 2.25M | } |
Line | Count | Source | 419 | 372k | const BN_MONT_CTX *mont, BN_CTX *ctx) { | 420 | 372k | if (a->neg || b->neg) { | 421 | 0 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); | 422 | 0 | return 0; | 423 | 0 | } | 424 | | | 425 | 372k | #if defined(OPENSSL_BN_ASM_MONT) | 426 | | // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86. | 427 | 372k | int num = mont->N.width; | 428 | 372k | if (num >= (128 / BN_BITS2) && | 429 | 372k | a->width == num && | 430 | 372k | b->width == num) { | 431 | 371k | if (!bn_wexpand(r, num)) { | 432 | 0 | return 0; | 433 | 0 | } | 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 | 371k | assert((size_t)num <= BN_MONTGOMERY_MAX_WORDS); | 437 | 371k | 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 | 0 | assert(0); | 440 | 0 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); | 441 | 0 | return 0; | 442 | 0 | } | 443 | 371k | r->neg = 0; | 444 | 371k | r->width = num; | 445 | 371k | return 1; | 446 | 371k | } | 447 | 426 | #endif | 448 | | | 449 | 426 | return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx); | 450 | 372k | } |
|
451 | | |
452 | 0 | int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont) { |
453 | 0 | return !BN_is_negative(bn) && |
454 | 0 | bn_fits_in_words(bn, mont->N.width); |
455 | 0 | } |
456 | | |
457 | | void bn_to_montgomery_small(BN_ULONG *r, const BN_ULONG *a, size_t num, |
458 | 565 | const BN_MONT_CTX *mont) { |
459 | 565 | bn_mod_mul_montgomery_small(r, a, mont->RR.d, num, mont); |
460 | 565 | } |
461 | | |
462 | | void bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, |
463 | 161 | size_t num_a, const BN_MONT_CTX *mont) { |
464 | 161 | if (num_r != (size_t)mont->N.width || num_r > BN_SMALL_MAX_WORDS || |
465 | 161 | num_a > 2 * num_r) { |
466 | 0 | abort(); |
467 | 0 | } |
468 | 161 | BN_ULONG tmp[BN_SMALL_MAX_WORDS * 2] = {0}; |
469 | 161 | OPENSSL_memcpy(tmp, a, num_a * sizeof(BN_ULONG)); |
470 | 161 | if (!bn_from_montgomery_in_place(r, num_r, tmp, 2 * num_r, mont)) { |
471 | 0 | abort(); |
472 | 0 | } |
473 | 161 | OPENSSL_cleanse(tmp, 2 * num_r * sizeof(BN_ULONG)); |
474 | 161 | } |
475 | | |
476 | | void bn_mod_mul_montgomery_small(BN_ULONG *r, const BN_ULONG *a, |
477 | | const BN_ULONG *b, size_t num, |
478 | 269k | const BN_MONT_CTX *mont) { |
479 | 269k | if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) { |
480 | 0 | abort(); |
481 | 0 | } |
482 | | |
483 | | #if defined(OPENSSL_BN_ASM_MONT) |
484 | | // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86. |
485 | 59.6k | if (num >= (128 / BN_BITS2)) { |
486 | 59.6k | if (!bn_mul_mont(r, a, b, mont->N.d, mont->n0, num)) { |
487 | 0 | abort(); // The check above ensures this won't happen. |
488 | 0 | } |
489 | 59.6k | return; |
490 | 59.6k | } |
491 | 0 | #endif |
492 | | |
493 | | // Compute the product. |
494 | 209k | BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; |
495 | 209k | if (a == b) { |
496 | 115k | bn_sqr_small(tmp, 2 * num, a, num); |
497 | 115k | } else { |
498 | 94.0k | bn_mul_small(tmp, 2 * num, a, num, b, num); |
499 | 94.0k | } |
500 | | |
501 | | // Reduce. |
502 | 209k | if (!bn_from_montgomery_in_place(r, num, tmp, 2 * num, mont)) { |
503 | 0 | abort(); |
504 | 0 | } |
505 | 209k | OPENSSL_cleanse(tmp, 2 * num * sizeof(BN_ULONG)); |
506 | 209k | } bn_mod_mul_montgomery_small Line | Count | Source | 478 | 209k | const BN_MONT_CTX *mont) { | 479 | 209k | if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) { | 480 | 0 | abort(); | 481 | 0 | } | 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 | 209k | BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; | 495 | 209k | if (a == b) { | 496 | 115k | bn_sqr_small(tmp, 2 * num, a, num); | 497 | 115k | } else { | 498 | 94.0k | bn_mul_small(tmp, 2 * num, a, num, b, num); | 499 | 94.0k | } | 500 | | | 501 | | // Reduce. | 502 | 209k | if (!bn_from_montgomery_in_place(r, num, tmp, 2 * num, mont)) { | 503 | 0 | abort(); | 504 | 0 | } | 505 | 209k | OPENSSL_cleanse(tmp, 2 * num * sizeof(BN_ULONG)); | 506 | 209k | } |
bn_mod_mul_montgomery_small Line | Count | Source | 478 | 59.6k | const BN_MONT_CTX *mont) { | 479 | 59.6k | if (num != (size_t)mont->N.width || num > BN_SMALL_MAX_WORDS) { | 480 | 0 | abort(); | 481 | 0 | } | 482 | | | 483 | 59.6k | #if defined(OPENSSL_BN_ASM_MONT) | 484 | | // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86. | 485 | 59.6k | if (num >= (128 / BN_BITS2)) { | 486 | 59.6k | if (!bn_mul_mont(r, a, b, mont->N.d, mont->n0, num)) { | 487 | 0 | abort(); // The check above ensures this won't happen. | 488 | 0 | } | 489 | 59.6k | return; | 490 | 59.6k | } | 491 | 0 | #endif | 492 | | | 493 | | // Compute the product. | 494 | 0 | BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; | 495 | 0 | if (a == b) { | 496 | 0 | bn_sqr_small(tmp, 2 * num, a, num); | 497 | 0 | } else { | 498 | 0 | bn_mul_small(tmp, 2 * num, a, num, b, num); | 499 | 0 | } | 500 | | | 501 | | // Reduce. | 502 | 0 | if (!bn_from_montgomery_in_place(r, num, tmp, 2 * num, mont)) { | 503 | 0 | abort(); | 504 | 0 | } | 505 | 0 | OPENSSL_cleanse(tmp, 2 * num * sizeof(BN_ULONG)); | 506 | 0 | } |
|
507 | | |
508 | | #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_X86_64) |
509 | | int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
510 | 599k | const BN_ULONG *np, const BN_ULONG *n0, size_t num) { |
511 | 599k | if (ap == bp && bn_sqr8x_mont_capable(num)) { |
512 | 8.98k | return bn_sqr8x_mont(rp, ap, bn_mulx_adx_capable(), np, n0, num); |
513 | 8.98k | } |
514 | 590k | if (bn_mulx4x_mont_capable(num)) { |
515 | 2.04k | return bn_mulx4x_mont(rp, ap, bp, np, n0, num); |
516 | 2.04k | } |
517 | 588k | if (bn_mul4x_mont_capable(num)) { |
518 | 0 | return bn_mul4x_mont(rp, ap, bp, np, n0, num); |
519 | 0 | } |
520 | 588k | return bn_mul_mont_nohw(rp, ap, bp, np, n0, num); |
521 | 588k | } |
522 | | #endif |
523 | | |
524 | | #if defined(OPENSSL_BN_ASM_MONT) && defined(OPENSSL_ARM) |
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 |