summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2024-03-26 04:14:45 +0000
committerjsing <>2024-03-26 04:14:45 +0000
commit6aa75b1ecf0d0a1d8d5b03f84fbaf697818b9a9e (patch)
treec4056fbe5431948b21d05ea1ae5c18beefbcc1d3 /src
parent8a6f69fc8ad3fff3144805a448d7a79d1cd0bf61 (diff)
downloadopenbsd-6aa75b1ecf0d0a1d8d5b03f84fbaf697818b9a9e.tar.gz
openbsd-6aa75b1ecf0d0a1d8d5b03f84fbaf697818b9a9e.tar.bz2
openbsd-6aa75b1ecf0d0a1d8d5b03f84fbaf697818b9a9e.zip
Move bn_montgomery_reduce() and drop prototype.
No functional change.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c144
1 files changed, 71 insertions, 73 deletions
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
index 12fea44c5a..7fdbfbd54d 100644
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mont.c,v 1.61 2023/07/08 12:21:58 beck Exp $ */ 1/* $OpenBSD: bn_mont.c,v 1.62 2024/03/26 04:14:45 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -299,7 +299,76 @@ BN_MONT_CTX_set_locked(BN_MONT_CTX **pmctx, int lock, const BIGNUM *mod,
299} 299}
300LCRYPTO_ALIAS(BN_MONT_CTX_set_locked); 300LCRYPTO_ALIAS(BN_MONT_CTX_set_locked);
301 301
302static int bn_montgomery_reduce(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mctx); 302/*
303 * bn_montgomery_reduce() performs Montgomery reduction, reducing the input
304 * from its Montgomery form aR to a, returning the result in r. Note that the
305 * input is mutated in the process of performing the reduction, destroying its
306 * original value.
307 */
308static int
309bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx)
310{
311 BIGNUM *n;
312 BN_ULONG *ap, *rp, n0, v, carry, mask;
313 int i, max, n_len;
314
315 n = &mctx->N;
316 n_len = mctx->N.top;
317
318 if (n_len == 0) {
319 BN_zero(r);
320 return 1;
321 }
322
323 if (!bn_wexpand(r, n_len))
324 return 0;
325
326 /*
327 * Expand a to twice the length of the modulus, zero if necessary.
328 * XXX - make this a requirement of the caller.
329 */
330 if ((max = 2 * n_len) < n_len)
331 return 0;
332 if (!bn_wexpand(a, max))
333 return 0;
334 for (i = a->top; i < max; i++)
335 a->d[i] = 0;
336
337 carry = 0;
338 n0 = mctx->n0[0];
339
340 /* Add multiples of the modulus, so that it becomes divisible by R. */
341 for (i = 0; i < n_len; i++) {
342 v = bn_mul_add_words(&a->d[i], n->d, n_len, a->d[i] * n0);
343 bn_addw_addw(v, a->d[i + n_len], carry, &carry,
344 &a->d[i + n_len]);
345 }
346
347 /* Divide by R (this is the equivalent of right shifting by n_len). */
348 ap = &a->d[n_len];
349
350 /*
351 * The output is now in the range of [0, 2N). Attempt to reduce once by
352 * subtracting the modulus. If the reduction was necessary then the
353 * result is already in r, otherwise copy the value prior to reduction
354 * from the top half of a.
355 */
356 mask = carry - bn_sub_words(r->d, ap, n->d, n_len);
357
358 rp = r->d;
359 for (i = 0; i < n_len; i++) {
360 *rp = (*rp & ~mask) | (*ap & mask);
361 rp++;
362 ap++;
363 }
364 r->top = n_len;
365
366 bn_correct_top(r);
367
368 BN_set_negative(r, a->neg ^ n->neg);
369
370 return 1;
371}
303 372
304static int 373static int
305bn_mod_mul_montgomery_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 374bn_mod_mul_montgomery_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
@@ -512,77 +581,6 @@ BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx)
512} 581}
513LCRYPTO_ALIAS(BN_to_montgomery); 582LCRYPTO_ALIAS(BN_to_montgomery);
514 583
515/*
516 * bn_montgomery_reduce() performs Montgomery reduction, reducing the input
517 * from its Montgomery form aR to a, returning the result in r. Note that the
518 * input is mutated in the process of performing the reduction, destroying its
519 * original value.
520 */
521static int
522bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx)
523{
524 BIGNUM *n;
525 BN_ULONG *ap, *rp, n0, v, carry, mask;
526 int i, max, n_len;
527
528 n = &mctx->N;
529 n_len = mctx->N.top;
530
531 if (n_len == 0) {
532 BN_zero(r);
533 return 1;
534 }
535
536 if (!bn_wexpand(r, n_len))
537 return 0;
538
539 /*
540 * Expand a to twice the length of the modulus, zero if necessary.
541 * XXX - make this a requirement of the caller.
542 */
543 if ((max = 2 * n_len) < n_len)
544 return 0;
545 if (!bn_wexpand(a, max))
546 return 0;
547 for (i = a->top; i < max; i++)
548 a->d[i] = 0;
549
550 carry = 0;
551 n0 = mctx->n0[0];
552
553 /* Add multiples of the modulus, so that it becomes divisible by R. */
554 for (i = 0; i < n_len; i++) {
555 v = bn_mul_add_words(&a->d[i], n->d, n_len, a->d[i] * n0);
556 bn_addw_addw(v, a->d[i + n_len], carry, &carry,
557 &a->d[i + n_len]);
558 }
559
560 /* Divide by R (this is the equivalent of right shifting by n_len). */
561 ap = &a->d[n_len];
562
563 /*
564 * The output is now in the range of [0, 2N). Attempt to reduce once by
565 * subtracting the modulus. If the reduction was necessary then the
566 * result is already in r, otherwise copy the value prior to reduction
567 * from the top half of a.
568 */
569 mask = carry - bn_sub_words(r->d, ap, n->d, n_len);
570
571 rp = r->d;
572 for (i = 0; i < n_len; i++) {
573 *rp = (*rp & ~mask) | (*ap & mask);
574 rp++;
575 ap++;
576 }
577 r->top = n_len;
578
579 bn_correct_top(r);
580
581 BN_set_negative(r, a->neg ^ n->neg);
582
583 return 1;
584}
585
586int 584int
587BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx) 585BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx)
588{ 586{