diff options
author | tb <> | 2023-03-29 07:29:11 +0000 |
---|---|---|
committer | tb <> | 2023-03-29 07:29:11 +0000 |
commit | ddc4d362bb10d543a1c1ce747913120955252e6a (patch) | |
tree | 7aad059f9c1a97ae25998c156ddb61baa85f06ef /src | |
parent | ef44c81e7f79e3cd9191b53194e3ec55e38bc3d8 (diff) | |
download | openbsd-ddc4d362bb10d543a1c1ce747913120955252e6a.tar.gz openbsd-ddc4d362bb10d543a1c1ce747913120955252e6a.tar.bz2 openbsd-ddc4d362bb10d543a1c1ce747913120955252e6a.zip |
Dedup generate_test_triple() and generate_test_quintuple()
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_mod_exp.c | 70 |
1 files changed, 20 insertions, 50 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_mod_exp.c b/src/regress/lib/libcrypto/bn/bn_mod_exp.c index f5b8f7dab5..0cd42a16d0 100644 --- a/src/regress/lib/libcrypto/bn/bn_mod_exp.c +++ b/src/regress/lib/libcrypto/bn/bn_mod_exp.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_mod_exp.c,v 1.30 2023/03/29 06:53:49 tb Exp $ */ | 1 | /* $OpenBSD: bn_mod_exp.c,v 1.31 2023/03/29 07:29:11 tb Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2022,2023 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2022,2023 Theo Buehler <tb@openbsd.org> |
@@ -199,6 +199,9 @@ generate_bn(BIGNUM *bn, int avg_bits, int deviate, int force_odd) | |||
199 | { | 199 | { |
200 | int bits; | 200 | int bits; |
201 | 201 | ||
202 | if (bn == NULL) | ||
203 | return 1; | ||
204 | |||
202 | if (avg_bits <= 0 || deviate <= 0 || deviate >= avg_bits) | 205 | if (avg_bits <= 0 || deviate <= 0 || deviate >= avg_bits) |
203 | return 0; | 206 | return 0; |
204 | 207 | ||
@@ -208,55 +211,8 @@ generate_bn(BIGNUM *bn, int avg_bits, int deviate, int force_odd) | |||
208 | } | 211 | } |
209 | 212 | ||
210 | static int | 213 | static int |
211 | generate_test_triple(int reduce, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx) | 214 | generate_test_quintuple(int reduce, BIGNUM *a, BIGNUM *p, BIGNUM *b, BIGNUM *q, |
212 | { | 215 | BIGNUM *m, BN_CTX *ctx) |
213 | BIGNUM *mmodified; | ||
214 | BN_ULONG multiple; | ||
215 | int avg = 2 * BN_BITS, deviate = BN_BITS / 2; | ||
216 | int ret = 0; | ||
217 | |||
218 | if (!generate_bn(a, avg, deviate, 0)) | ||
219 | return 0; | ||
220 | |||
221 | if (!generate_bn(p, avg, deviate, 0)) | ||
222 | return 0; | ||
223 | |||
224 | if (!generate_bn(m, avg, deviate, 1)) | ||
225 | return 0; | ||
226 | |||
227 | if (reduce) | ||
228 | return BN_mod(a, a, m, ctx); | ||
229 | |||
230 | /* | ||
231 | * Add a random multiple of m to a to test unreduced exponentiation. | ||
232 | */ | ||
233 | |||
234 | BN_CTX_start(ctx); | ||
235 | |||
236 | if ((mmodified = BN_CTX_get(ctx)) == NULL) | ||
237 | goto err; | ||
238 | |||
239 | if (!bn_copy(mmodified, m)) | ||
240 | goto err; | ||
241 | |||
242 | multiple = arc4random_uniform(1023) + 2; | ||
243 | |||
244 | if (!BN_mul_word(mmodified, multiple)) | ||
245 | goto err; | ||
246 | |||
247 | if (!BN_add(a, a, mmodified)) | ||
248 | goto err; | ||
249 | |||
250 | ret = 1; | ||
251 | err: | ||
252 | BN_CTX_end(ctx); | ||
253 | |||
254 | return ret; | ||
255 | } | ||
256 | |||
257 | static int | ||
258 | generate_test_quintuple(int reduce, BIGNUM *a, BIGNUM *p, | ||
259 | BIGNUM *b, BIGNUM *q, BIGNUM *m, BN_CTX *ctx) | ||
260 | { | 216 | { |
261 | BIGNUM *mmodified; | 217 | BIGNUM *mmodified; |
262 | BN_ULONG multiple; | 218 | BN_ULONG multiple; |
@@ -282,6 +238,9 @@ generate_test_quintuple(int reduce, BIGNUM *a, BIGNUM *p, | |||
282 | if (!BN_mod(a, a, m, ctx)) | 238 | if (!BN_mod(a, a, m, ctx)) |
283 | return 0; | 239 | return 0; |
284 | 240 | ||
241 | if (b == NULL) | ||
242 | return 1; | ||
243 | |||
285 | return BN_mod(b, b, m, ctx); | 244 | return BN_mod(b, b, m, ctx); |
286 | } | 245 | } |
287 | 246 | ||
@@ -305,16 +264,27 @@ generate_test_quintuple(int reduce, BIGNUM *a, BIGNUM *p, | |||
305 | if (!BN_add(a, a, mmodified)) | 264 | if (!BN_add(a, a, mmodified)) |
306 | goto err; | 265 | goto err; |
307 | 266 | ||
267 | if (b == NULL) | ||
268 | goto done; | ||
269 | |||
308 | if (!BN_add(b, b, mmodified)) | 270 | if (!BN_add(b, b, mmodified)) |
309 | goto err; | 271 | goto err; |
310 | 272 | ||
273 | done: | ||
311 | ret = 1; | 274 | ret = 1; |
275 | |||
312 | err: | 276 | err: |
313 | BN_CTX_end(ctx); | 277 | BN_CTX_end(ctx); |
314 | 278 | ||
315 | return ret; | 279 | return ret; |
316 | } | 280 | } |
317 | 281 | ||
282 | static int | ||
283 | generate_test_triple(int reduce, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx) | ||
284 | { | ||
285 | return generate_test_quintuple(reduce, a, p, NULL, NULL, m, ctx); | ||
286 | } | ||
287 | |||
318 | static void | 288 | static void |
319 | dump_exp_results(const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | 289 | dump_exp_results(const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, |
320 | const BIGNUM *want, const BIGNUM *got, const char *name) | 290 | const BIGNUM *want, const BIGNUM *got, const char *name) |