diff options
author | jsing <> | 2023-02-03 05:10:57 +0000 |
---|---|---|
committer | jsing <> | 2023-02-03 05:10:57 +0000 |
commit | a881da8482cf1ded82dda8177f3be4c5a6f48ebc (patch) | |
tree | 719d976bcf4b97c2bc24f5bcde07232a6d14823e /src | |
parent | e89b3fff04261fbe70d2fcb24f92e4689c8ce8f4 (diff) | |
download | openbsd-a881da8482cf1ded82dda8177f3be4c5a6f48ebc.tar.gz openbsd-a881da8482cf1ded82dda8177f3be4c5a6f48ebc.tar.bz2 openbsd-a881da8482cf1ded82dda8177f3be4c5a6f48ebc.zip |
Clean up BN_mod_mul() and simplify BN_mod_sqr().
Use the same naming/code pattern in BN_mod_mul() as is used in BN_mul().
Note that the 'rr' allocation is unnecessary, since both BN_mul() and
BN_sqr() handle the case where r == a || r == b. However, it avoids a
potential copy on the exit from BN_mul()/BN_sqr(), so leave it in place
for now.
Turn BN_mod_sqr() into a wrapper that calls BN_mod_mul(), since it already
calls BN_sqr() in the a == b. The supposed gain of calling BN_mod_ct()
instead of BN_nnmod() does not really exist.
ok tb@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/bn/bn_mod.c | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/src/lib/libcrypto/bn/bn_mod.c b/src/lib/libcrypto/bn/bn_mod.c index 4a62715974..762ffb5580 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.17 2023/02/03 05:06:20 jsing Exp $ */ | 1 | /* $OpenBSD: bn_mod.c,v 1.18 2023/02/03 05:10:57 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 | /* ==================================================================== |
@@ -189,41 +189,43 @@ BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m) | |||
189 | return BN_usub(r, m, r); | 189 | return BN_usub(r, m, r); |
190 | } | 190 | } |
191 | 191 | ||
192 | /* slow but works */ | ||
193 | int | 192 | int |
194 | BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, | 193 | BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, |
195 | BN_CTX *ctx) | 194 | BN_CTX *ctx) |
196 | { | 195 | { |
197 | BIGNUM *t; | 196 | BIGNUM *rr; |
198 | int ret = 0; | 197 | int ret = 0; |
199 | 198 | ||
200 | |||
201 | BN_CTX_start(ctx); | 199 | BN_CTX_start(ctx); |
202 | if ((t = BN_CTX_get(ctx)) == NULL) | 200 | |
201 | rr = r; | ||
202 | if (rr == a || rr == b) | ||
203 | rr = BN_CTX_get(ctx); | ||
204 | if (rr == NULL) | ||
203 | goto err; | 205 | goto err; |
206 | |||
204 | if (a == b) { | 207 | if (a == b) { |
205 | if (!BN_sqr(t, a, ctx)) | 208 | if (!BN_sqr(rr, a, ctx)) |
206 | goto err; | 209 | goto err; |
207 | } else { | 210 | } else { |
208 | if (!BN_mul(t, a,b, ctx)) | 211 | if (!BN_mul(rr, a, b, ctx)) |
209 | goto err; | 212 | goto err; |
210 | } | 213 | } |
211 | if (!BN_nnmod(r, t,m, ctx)) | 214 | if (!BN_nnmod(r, rr, m, ctx)) |
212 | goto err; | 215 | goto err; |
216 | |||
213 | ret = 1; | 217 | ret = 1; |
214 | 218 | ||
215 | err: | 219 | err: |
216 | BN_CTX_end(ctx); | 220 | BN_CTX_end(ctx); |
217 | return (ret); | 221 | |
222 | return ret; | ||
218 | } | 223 | } |
219 | 224 | ||
220 | int | 225 | int |
221 | BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) | 226 | BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) |
222 | { | 227 | { |
223 | if (!BN_sqr(r, a, ctx)) | 228 | return BN_mod_mul(r, a, a, m, ctx); |
224 | return 0; | ||
225 | /* r->neg == 0, thus we don't need BN_nnmod */ | ||
226 | return BN_mod_ct(r, r, m, ctx); | ||
227 | } | 229 | } |
228 | 230 | ||
229 | int | 231 | int |