diff options
-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 |