summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_mont.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c621
1 files changed, 0 insertions, 621 deletions
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
deleted file mode 100644
index edd7bcd0c8..0000000000
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ /dev/null
@@ -1,621 +0,0 @@
1/* $OpenBSD: bn_mont.c,v 1.66 2025/03/09 15:22:40 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112/*
113 * Details about Montgomery multiplication algorithms can be found at
114 * http://security.ece.orst.edu/publications.html, e.g.
115 * http://security.ece.orst.edu/koc/papers/j37acmon.pdf and
116 * sections 3.8 and 4.2 in http://security.ece.orst.edu/koc/papers/r01rsasw.pdf
117 */
118
119#include <stdio.h>
120#include <stdint.h>
121#include <string.h>
122
123#include "bn_internal.h"
124#include "bn_local.h"
125
126BN_MONT_CTX *
127BN_MONT_CTX_new(void)
128{
129 BN_MONT_CTX *mctx;
130
131 if ((mctx = calloc(1, sizeof(BN_MONT_CTX))) == NULL)
132 return NULL;
133 mctx->flags = BN_FLG_MALLOCED;
134
135 BN_init(&mctx->RR);
136 BN_init(&mctx->N);
137
138 return mctx;
139}
140LCRYPTO_ALIAS(BN_MONT_CTX_new);
141
142void
143BN_MONT_CTX_free(BN_MONT_CTX *mctx)
144{
145 if (mctx == NULL)
146 return;
147
148 BN_free(&mctx->RR);
149 BN_free(&mctx->N);
150
151 if (mctx->flags & BN_FLG_MALLOCED)
152 free(mctx);
153}
154LCRYPTO_ALIAS(BN_MONT_CTX_free);
155
156BN_MONT_CTX *
157BN_MONT_CTX_create(const BIGNUM *bn, BN_CTX *bn_ctx)
158{
159 BN_MONT_CTX *mctx;
160
161 if ((mctx = BN_MONT_CTX_new()) == NULL)
162 goto err;
163 if (!BN_MONT_CTX_set(mctx, bn, bn_ctx))
164 goto err;
165
166 return mctx;
167
168 err:
169 BN_MONT_CTX_free(mctx);
170
171 return NULL;
172}
173
174BN_MONT_CTX *
175BN_MONT_CTX_copy(BN_MONT_CTX *dst, const BN_MONT_CTX *src)
176{
177 if (dst == src)
178 return dst;
179
180 if (!bn_copy(&dst->RR, &src->RR))
181 return NULL;
182 if (!bn_copy(&dst->N, &src->N))
183 return NULL;
184
185 dst->ri = src->ri;
186 dst->n0[0] = src->n0[0];
187 dst->n0[1] = src->n0[1];
188
189 return dst;
190}
191LCRYPTO_ALIAS(BN_MONT_CTX_copy);
192
193int
194BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
195{
196 BIGNUM *N, *Ninv, *Rinv, *R;
197 int ret = 0;
198
199 BN_CTX_start(ctx);
200
201 if ((N = BN_CTX_get(ctx)) == NULL)
202 goto err;
203 if ((Ninv = BN_CTX_get(ctx)) == NULL)
204 goto err;
205 if ((R = BN_CTX_get(ctx)) == NULL)
206 goto err;
207 if ((Rinv = BN_CTX_get(ctx)) == NULL)
208 goto err;
209
210 /* Save modulus and determine length of R. */
211 if (BN_is_zero(mod))
212 goto err;
213 if (!bn_copy(&mont->N, mod))
214 goto err;
215 mont->N.neg = 0;
216 mont->ri = ((BN_num_bits(mod) + BN_BITS2 - 1) / BN_BITS2) * BN_BITS2;
217 if (mont->ri * 2 < mont->ri)
218 goto err;
219
220 /*
221 * Compute Ninv = (R * Rinv - 1)/N mod R, for R = 2^64. This provides
222 * a single or double word result (dependent on BN word size), that is
223 * later used to implement Montgomery reduction.
224 */
225 BN_zero(R);
226 if (!BN_set_bit(R, 64))
227 goto err;
228
229 /* N = N mod R. */
230 if (!bn_wexpand(N, 2))
231 goto err;
232 if (!BN_set_word(N, mod->d[0]))
233 goto err;
234#if BN_BITS2 == 32
235 if (mod->top > 1) {
236 N->d[1] = mod->d[1];
237 N->top += bn_ct_ne_zero(N->d[1]);
238 }
239#endif
240
241 /* Rinv = R^-1 mod N */
242 if ((BN_mod_inverse_ct(Rinv, R, N, ctx)) == NULL)
243 goto err;
244
245 /* Ninv = (R * Rinv - 1) / N */
246 if (!BN_lshift(Ninv, Rinv, 64))
247 goto err;
248 if (BN_is_zero(Ninv)) {
249 /* R * Rinv == 0, set to R so that R * Rinv - 1 is mod R. */
250 if (!BN_set_bit(Ninv, 64))
251 goto err;
252 }
253 if (!BN_sub_word(Ninv, 1))
254 goto err;
255 if (!BN_div_ct(Ninv, NULL, Ninv, N, ctx))
256 goto err;
257
258 /* Store least significant word(s) of Ninv. */
259 mont->n0[0] = mont->n0[1] = 0;
260 if (Ninv->top > 0)
261 mont->n0[0] = Ninv->d[0];
262#if BN_BITS2 == 32
263 /* Some BN_BITS2 == 32 platforms (namely parisc) use two words of Ninv. */
264 if (Ninv->top > 1)
265 mont->n0[1] = Ninv->d[1];
266#endif
267
268 /* Compute RR = R * R mod N, for use when converting to Montgomery form. */
269 BN_zero(&mont->RR);
270 if (!BN_set_bit(&mont->RR, mont->ri * 2))
271 goto err;
272 if (!BN_mod_ct(&mont->RR, &mont->RR, &mont->N, ctx))
273 goto err;
274
275 ret = 1;
276 err:
277 BN_CTX_end(ctx);
278
279 return ret;
280}
281LCRYPTO_ALIAS(BN_MONT_CTX_set);
282
283BN_MONT_CTX *
284BN_MONT_CTX_set_locked(BN_MONT_CTX **pmctx, int lock, const BIGNUM *mod,
285 BN_CTX *ctx)
286{
287 BN_MONT_CTX *mctx = NULL;
288
289 CRYPTO_r_lock(lock);
290 mctx = *pmctx;
291 CRYPTO_r_unlock(lock);
292
293 if (mctx != NULL)
294 goto done;
295
296 if ((mctx = BN_MONT_CTX_create(mod, ctx)) == NULL)
297 goto err;
298
299 CRYPTO_w_lock(lock);
300 if (*pmctx != NULL) {
301 /* Someone else raced us... */
302 BN_MONT_CTX_free(mctx);
303 mctx = *pmctx;
304 } else {
305 *pmctx = mctx;
306 }
307 CRYPTO_w_unlock(lock);
308
309 goto done;
310 err:
311 BN_MONT_CTX_free(mctx);
312 mctx = NULL;
313 done:
314 return mctx;
315}
316LCRYPTO_ALIAS(BN_MONT_CTX_set_locked);
317
318/*
319 * bn_montgomery_reduce() performs Montgomery reduction, reducing the input
320 * from its Montgomery form aR to a, returning the result in r. Note that the
321 * input is mutated in the process of performing the reduction, destroying its
322 * original value.
323 */
324static int
325bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx)
326{
327 BIGNUM *n;
328 BN_ULONG *ap, *rp, n0, v, carry, mask;
329 int i, max, n_len;
330
331 n = &mctx->N;
332 n_len = mctx->N.top;
333
334 if (n_len == 0) {
335 BN_zero(r);
336 return 1;
337 }
338
339 if (!bn_wexpand(r, n_len))
340 return 0;
341
342 /*
343 * Expand a to twice the length of the modulus, zero if necessary.
344 * XXX - make this a requirement of the caller.
345 */
346 if ((max = 2 * n_len) < n_len)
347 return 0;
348 if (!bn_wexpand(a, max))
349 return 0;
350 for (i = a->top; i < max; i++)
351 a->d[i] = 0;
352
353 carry = 0;
354 n0 = mctx->n0[0];
355
356 /* Add multiples of the modulus, so that it becomes divisible by R. */
357 for (i = 0; i < n_len; i++) {
358 v = bn_mul_add_words(&a->d[i], n->d, n_len, a->d[i] * n0);
359 bn_addw_addw(v, a->d[i + n_len], carry, &carry,
360 &a->d[i + n_len]);
361 }
362
363 /* Divide by R (this is the equivalent of right shifting by n_len). */
364 ap = &a->d[n_len];
365
366 /*
367 * The output is now in the range of [0, 2N). Attempt to reduce once by
368 * subtracting the modulus. If the reduction was necessary then the
369 * result is already in r, otherwise copy the value prior to reduction
370 * from the top half of a.
371 */
372 mask = carry - bn_sub_words(r->d, ap, n->d, n_len);
373
374 rp = r->d;
375 for (i = 0; i < n_len; i++) {
376 *rp = (*rp & ~mask) | (*ap & mask);
377 rp++;
378 ap++;
379 }
380 r->top = n_len;
381
382 bn_correct_top(r);
383
384 BN_set_negative(r, a->neg ^ n->neg);
385
386 return 1;
387}
388
389static int
390bn_mod_mul_montgomery_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
391 BN_MONT_CTX *mctx, BN_CTX *ctx)
392{
393 BIGNUM *tmp;
394 int ret = 0;
395
396 BN_CTX_start(ctx);
397
398 if ((tmp = BN_CTX_get(ctx)) == NULL)
399 goto err;
400
401 if (a == b) {
402 if (!BN_sqr(tmp, a, ctx))
403 goto err;
404 } else {
405 if (!BN_mul(tmp, a, b, ctx))
406 goto err;
407 }
408
409 /* Reduce from aRR to aR. */
410 if (!bn_montgomery_reduce(r, tmp, mctx))
411 goto err;
412
413 ret = 1;
414 err:
415 BN_CTX_end(ctx);
416
417 return ret;
418}
419
420static void
421bn_montgomery_multiply_word(const BN_ULONG *ap, BN_ULONG b, const BN_ULONG *np,
422 BN_ULONG *tp, BN_ULONG w, BN_ULONG *carry_a, BN_ULONG *carry_n, int n_len)
423{
424 BN_ULONG x3, x2, x1, x0;
425
426 *carry_a = *carry_n = 0;
427
428 while (n_len & ~3) {
429 bn_qwmulw_addqw_addw(ap[3], ap[2], ap[1], ap[0], b,
430 tp[3], tp[2], tp[1], tp[0], *carry_a, carry_a,
431 &x3, &x2, &x1, &x0);
432 bn_qwmulw_addqw_addw(np[3], np[2], np[1], np[0], w,
433 x3, x2, x1, x0, *carry_n, carry_n,
434 &tp[3], &tp[2], &tp[1], &tp[0]);
435 ap += 4;
436 np += 4;
437 tp += 4;
438 n_len -= 4;
439 }
440 while (n_len > 0) {
441 bn_mulw_addw_addw(ap[0], b, tp[0], *carry_a, carry_a, &x0);
442 bn_mulw_addw_addw(np[0], w, x0, *carry_n, carry_n, &tp[0]);
443 ap++;
444 np++;
445 tp++;
446 n_len--;
447 }
448}
449
450/*
451 * bn_montgomery_multiply_words() computes r = aR * bR * R^-1 = abR for the
452 * given word arrays. The caller must ensure that rp, ap, bp and np are all
453 * n_len words in length, while tp must be n_len * 2 + 2 words in length.
454 */
455static void
456bn_montgomery_multiply_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
457 const BN_ULONG *np, BN_ULONG *tp, BN_ULONG n0, int n_len)
458{
459 BN_ULONG a0, b, carry_a, carry_n, carry, mask, w;
460 int i;
461
462 carry = 0;
463
464 for (i = 0; i < n_len; i++)
465 tp[i] = 0;
466
467 a0 = ap[0];
468
469 for (i = 0; i < n_len; i++) {
470 b = bp[i];
471
472 /* Compute new t[0] * n0, as we need it for this iteration. */
473 w = (a0 * b + tp[0]) * n0;
474
475 bn_montgomery_multiply_word(ap, b, np, tp, w, &carry_a,
476 &carry_n, n_len);
477 bn_addw_addw(carry_a, carry_n, carry, &carry, &tp[n_len]);
478
479 tp++;
480 }
481 tp[n_len] = carry;
482
483 /*
484 * The output is now in the range of [0, 2N). Attempt to reduce once by
485 * subtracting the modulus. If the reduction was necessary then the
486 * result is already in r, otherwise copy the value prior to reduction
487 * from tp.
488 */
489 mask = bn_ct_ne_zero(tp[n_len]) - bn_sub_words(rp, tp, np, n_len);
490
491 for (i = 0; i < n_len; i++) {
492 *rp = (*rp & ~mask) | (*tp & mask);
493 rp++;
494 tp++;
495 }
496}
497
498/*
499 * bn_montgomery_multiply() computes r = aR * bR * R^-1 = abR for the given
500 * BIGNUMs. The caller must ensure that the modulus is two or more words in
501 * length and that a and b have the same number of words as the modulus.
502 */
503static int
504bn_montgomery_multiply(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
505 BN_MONT_CTX *mctx, BN_CTX *ctx)
506{
507 BIGNUM *t;
508 int ret = 0;
509
510 BN_CTX_start(ctx);
511
512 if (mctx->N.top <= 1 || a->top != mctx->N.top || b->top != mctx->N.top)
513 goto err;
514 if (!bn_wexpand(r, mctx->N.top))
515 goto err;
516
517 if ((t = BN_CTX_get(ctx)) == NULL)
518 goto err;
519 if (!bn_wexpand(t, mctx->N.top * 2 + 2))
520 goto err;
521
522 bn_montgomery_multiply_words(r->d, a->d, b->d, mctx->N.d, t->d,
523 mctx->n0[0], mctx->N.top);
524
525 r->top = mctx->N.top;
526 bn_correct_top(r);
527
528 BN_set_negative(r, a->neg ^ b->neg);
529
530 ret = 1;
531 err:
532 BN_CTX_end(ctx);
533
534 return ret;
535}
536
537#ifndef OPENSSL_BN_ASM_MONT
538static int
539bn_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
540 BN_MONT_CTX *mctx, BN_CTX *ctx)
541{
542 if (mctx->N.top <= 1 || a->top != mctx->N.top || b->top != mctx->N.top)
543 return bn_mod_mul_montgomery_simple(r, a, b, mctx, ctx);
544
545 return bn_montgomery_multiply(r, a, b, mctx, ctx);
546}
547#else
548
549static int
550bn_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
551 BN_MONT_CTX *mctx, BN_CTX *ctx)
552{
553 if (mctx->N.top <= 1 || a->top != mctx->N.top || b->top != mctx->N.top)
554 return bn_mod_mul_montgomery_simple(r, a, b, mctx, ctx);
555
556 /*
557 * Legacy bn_mul_mont() performs stack based allocation, without
558 * size limitation. Allowing a large size results in the stack
559 * being blown.
560 */
561 if (mctx->N.top > (8 * 1024 / sizeof(BN_ULONG)))
562 return bn_montgomery_multiply(r, a, b, mctx, ctx);
563
564 if (!bn_wexpand(r, mctx->N.top))
565 return 0;
566
567 /*
568 * Legacy bn_mul_mont() can indicate that we should "fallback" to
569 * another implementation.
570 */
571 if (!bn_mul_mont(r->d, a->d, b->d, mctx->N.d, mctx->n0, mctx->N.top))
572 return bn_montgomery_multiply(r, a, b, mctx, ctx);
573
574 r->top = mctx->N.top;
575 bn_correct_top(r);
576
577 BN_set_negative(r, a->neg ^ b->neg);
578
579 return (1);
580}
581#endif
582
583int
584BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
585 BN_MONT_CTX *mctx, BN_CTX *ctx)
586{
587 /* Compute r = aR * bR * R^-1 mod N = abR mod N */
588 return bn_mod_mul_montgomery(r, a, b, mctx, ctx);
589}
590LCRYPTO_ALIAS(BN_mod_mul_montgomery);
591
592int
593BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx)
594{
595 /* Compute r = a * R * R * R^-1 mod N = aR mod N */
596 return bn_mod_mul_montgomery(r, a, &mctx->RR, mctx, ctx);
597}
598LCRYPTO_ALIAS(BN_to_montgomery);
599
600int
601BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx)
602{
603 BIGNUM *tmp;
604 int ret = 0;
605
606 BN_CTX_start(ctx);
607
608 if ((tmp = BN_CTX_get(ctx)) == NULL)
609 goto err;
610 if (!bn_copy(tmp, a))
611 goto err;
612 if (!bn_montgomery_reduce(r, tmp, mctx))
613 goto err;
614
615 ret = 1;
616 err:
617 BN_CTX_end(ctx);
618
619 return ret;
620}
621LCRYPTO_ALIAS(BN_from_montgomery);