diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_recp.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_recp.c | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c index 35390e30d4..c567a5b017 100644 --- a/src/lib/libcrypto/bn/bn_recp.c +++ b/src/lib/libcrypto/bn/bn_recp.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_recp.c,v 1.19 2023/03/27 10:25:02 tb Exp $ */ | 1 | /* $OpenBSD: bn_recp.c,v 1.20 2025/01/06 12:35:27 tb 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 | * |
@@ -107,37 +107,35 @@ BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) | |||
107 | return (1); | 107 | return (1); |
108 | } | 108 | } |
109 | 109 | ||
110 | int | 110 | /* len is the expected size of the result |
111 | BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, | 111 | * We actually calculate with an extra word of precision, so |
112 | BN_RECP_CTX *recp, BN_CTX *ctx) | 112 | * we can do faster division if the remainder is not required. |
113 | */ | ||
114 | /* r := 2^len / m */ | ||
115 | static int | ||
116 | BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) | ||
113 | { | 117 | { |
114 | int ret = 0; | 118 | int ret = -1; |
115 | BIGNUM *a; | 119 | BIGNUM *t; |
116 | const BIGNUM *ca; | ||
117 | 120 | ||
118 | BN_CTX_start(ctx); | 121 | BN_CTX_start(ctx); |
119 | if ((a = BN_CTX_get(ctx)) == NULL) | 122 | if ((t = BN_CTX_get(ctx)) == NULL) |
120 | goto err; | 123 | goto err; |
121 | if (y != NULL) { | ||
122 | if (x == y) { | ||
123 | if (!BN_sqr(a, x, ctx)) | ||
124 | goto err; | ||
125 | } else { | ||
126 | if (!BN_mul(a, x, y, ctx)) | ||
127 | goto err; | ||
128 | } | ||
129 | ca = a; | ||
130 | } else | ||
131 | ca = x; /* Just do the mod */ | ||
132 | 124 | ||
133 | ret = BN_div_recp(NULL, r, ca, recp, ctx); | 125 | if (!BN_set_bit(t, len)) |
126 | goto err; | ||
127 | |||
128 | if (!BN_div_ct(r, NULL, t,m, ctx)) | ||
129 | goto err; | ||
130 | |||
131 | ret = len; | ||
134 | 132 | ||
135 | err: | 133 | err: |
136 | BN_CTX_end(ctx); | 134 | BN_CTX_end(ctx); |
137 | return (ret); | 135 | return (ret); |
138 | } | 136 | } |
139 | 137 | ||
140 | int | 138 | static int |
141 | BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | 139 | BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, |
142 | BN_CTX *ctx) | 140 | BN_CTX *ctx) |
143 | { | 141 | { |
@@ -231,28 +229,31 @@ err: | |||
231 | return (ret); | 229 | return (ret); |
232 | } | 230 | } |
233 | 231 | ||
234 | /* len is the expected size of the result | 232 | |
235 | * We actually calculate with an extra word of precision, so | ||
236 | * we can do faster division if the remainder is not required. | ||
237 | */ | ||
238 | /* r := 2^len / m */ | ||
239 | int | 233 | int |
240 | BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) | 234 | BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, |
235 | BN_RECP_CTX *recp, BN_CTX *ctx) | ||
241 | { | 236 | { |
242 | int ret = -1; | 237 | int ret = 0; |
243 | BIGNUM *t; | 238 | BIGNUM *a; |
239 | const BIGNUM *ca; | ||
244 | 240 | ||
245 | BN_CTX_start(ctx); | 241 | BN_CTX_start(ctx); |
246 | if ((t = BN_CTX_get(ctx)) == NULL) | 242 | if ((a = BN_CTX_get(ctx)) == NULL) |
247 | goto err; | ||
248 | |||
249 | if (!BN_set_bit(t, len)) | ||
250 | goto err; | ||
251 | |||
252 | if (!BN_div_ct(r, NULL, t,m, ctx)) | ||
253 | goto err; | 243 | goto err; |
244 | if (y != NULL) { | ||
245 | if (x == y) { | ||
246 | if (!BN_sqr(a, x, ctx)) | ||
247 | goto err; | ||
248 | } else { | ||
249 | if (!BN_mul(a, x, y, ctx)) | ||
250 | goto err; | ||
251 | } | ||
252 | ca = a; | ||
253 | } else | ||
254 | ca = x; /* Just do the mod */ | ||
254 | 255 | ||
255 | ret = len; | 256 | ret = BN_div_recp(NULL, r, ca, recp, ctx); |
256 | 257 | ||
257 | err: | 258 | err: |
258 | BN_CTX_end(ctx); | 259 | BN_CTX_end(ctx); |