summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/bn_mod.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/lib/libcrypto/bn/bn_mod.c b/src/lib/libcrypto/bn/bn_mod.c
index 938b53d725..8a660ff0dc 100644
--- a/src/lib/libcrypto/bn/bn_mod.c
+++ b/src/lib/libcrypto/bn/bn_mod.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mod.c,v 1.15 2023/02/03 04:47:59 jsing Exp $ */ 1/* $OpenBSD: bn_mod.c,v 1.16 2023/02/03 04:55:13 jsing Exp $ */
2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. */ 3 * for the OpenSSL project. */
4/* ==================================================================== 4/* ====================================================================
@@ -127,21 +127,20 @@ BN_mod_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
127 return BN_div_nonct(NULL, r, a, m, ctx); 127 return BN_div_nonct(NULL, r, a, m, ctx);
128} 128}
129 129
130/*
131 * BN_nnmod() is like BN_mod(), but always returns a non-negative remainder
132 * (that is 0 <= r < |m| always holds). If both a and m have the same sign then
133 * the result is already non-negative. Otherwise, -|m| < r < 0, which needs to
134 * be adjusted as r := r + |m|. This equates to r := |m| - |r|.
135 */
130int 136int
131BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) 137BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
132{ 138{
133 /* like BN_mod, but returns non-negative remainder 139 if (!BN_mod_ct(r, a, m, ctx))
134 * (i.e., 0 <= r < |d| always holds) */
135
136 if (!(BN_mod_ct(r, m,d, ctx)))
137 return 0; 140 return 0;
138 if (!r->neg) 141 if (BN_is_negative(r))
139 return 1; 142 return BN_usub(r, m, r);
140 /* now -|d| < r < 0, so we have to set r := r + |d| */ 143 return 1;
141 if (d->neg)
142 return BN_sub(r, r, d);
143 else
144 return BN_add(r, r, d);
145} 144}
146 145
147int 146int