diff options
-rw-r--r-- | src/regress/lib/libcrypto/bn/general/Makefile | 8 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/bn/general/bn_primes.c | 90 |
2 files changed, 97 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/bn/general/Makefile b/src/regress/lib/libcrypto/bn/general/Makefile index 4f1fcc852a..4c36648ec7 100644 --- a/src/regress/lib/libcrypto/bn/general/Makefile +++ b/src/regress/lib/libcrypto/bn/general/Makefile | |||
@@ -1,16 +1,18 @@ | |||
1 | # $OpenBSD: Makefile,v 1.10 2022/03/16 12:37:44 bluhm Exp $ | 1 | # $OpenBSD: Makefile,v 1.11 2022/06/18 19:53:19 tb Exp $ |
2 | 2 | ||
3 | .include "../../Makefile.inc" | 3 | .include "../../Makefile.inc" |
4 | 4 | ||
5 | PROGS += bntest | 5 | PROGS += bntest |
6 | PROGS += bn_mod_exp2_mont | 6 | PROGS += bn_mod_exp2_mont |
7 | PROGS += bn_mod_sqrt | 7 | PROGS += bn_mod_sqrt |
8 | PROGS += bn_primes | ||
8 | PROGS += bn_to_string | 9 | PROGS += bn_to_string |
9 | 10 | ||
10 | LDADD = ${CRYPTO_INT} | 11 | LDADD = ${CRYPTO_INT} |
11 | DPADD = ${LIBCRYPTO} | 12 | DPADD = ${LIBCRYPTO} |
12 | WARNINGS = Yes | 13 | WARNINGS = Yes |
13 | CFLAGS += -Werror | 14 | CFLAGS += -Werror |
15 | CFLAGS += -I${.CURDIR}/../../../../../lib/libcrypto/bn/ | ||
14 | CLEANFILES = bntest.out bc.out | 16 | CLEANFILES = bntest.out bc.out |
15 | 17 | ||
16 | REGRESS_TARGETS += run-bntest | 18 | REGRESS_TARGETS += run-bntest |
@@ -30,6 +32,10 @@ REGRESS_TARGETS += run-bn_mod_sqrt | |||
30 | run-bn_mod_sqrt: bn_mod_sqrt | 32 | run-bn_mod_sqrt: bn_mod_sqrt |
31 | ./bn_mod_sqrt | 33 | ./bn_mod_sqrt |
32 | 34 | ||
35 | REGRESS_TARGETS += run-bn_primes | ||
36 | run-bn_primes: bn_primes | ||
37 | ./bn_primes | ||
38 | |||
33 | REGRESS_TARGETS += run-bn_to_string | 39 | REGRESS_TARGETS += run-bn_to_string |
34 | run-bn_to_string: bn_to_string | 40 | run-bn_to_string: bn_to_string |
35 | ./bn_to_string | 41 | ./bn_to_string |
diff --git a/src/regress/lib/libcrypto/bn/general/bn_primes.c b/src/regress/lib/libcrypto/bn/general/bn_primes.c new file mode 100644 index 0000000000..f9d358f709 --- /dev/null +++ b/src/regress/lib/libcrypto/bn/general/bn_primes.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* $OpenBSD: bn_primes.c,v 1.1 2022/06/18 19:53:19 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <openssl/bn.h> | ||
19 | |||
20 | #include "bn_prime.h" | ||
21 | |||
22 | static int | ||
23 | test_bn_is_prime_fasttest(int do_trial_division) | ||
24 | { | ||
25 | BIGNUM *n = NULL; | ||
26 | char *descr = NULL; | ||
27 | prime_t i, j, max; | ||
28 | int is_prime, ret; | ||
29 | int failed = 1; | ||
30 | |||
31 | if (asprintf(&descr, "with%s trial divisions", | ||
32 | do_trial_division ? "" : "out") == -1) { | ||
33 | descr = NULL; | ||
34 | fprintf(stderr, "asprintf failed\n"); | ||
35 | goto err; | ||
36 | } | ||
37 | |||
38 | if ((n = BN_new()) == NULL) { | ||
39 | fprintf(stderr, "BN_new failed\n"); | ||
40 | goto err; | ||
41 | } | ||
42 | |||
43 | max = primes[NUMPRIMES - 1] + 1; | ||
44 | |||
45 | failed = 0; | ||
46 | for (i = 1, j = 0; i < max && j < NUMPRIMES; i++) { | ||
47 | if (!BN_set_word(n, i)) { | ||
48 | fprintf(stderr, "BN_set_word(%d) failed", i); | ||
49 | failed = 1; | ||
50 | goto err; | ||
51 | } | ||
52 | |||
53 | is_prime = i == primes[j]; | ||
54 | if (is_prime) | ||
55 | j++; | ||
56 | |||
57 | ret = BN_is_prime_fasttest_ex(n, BN_prime_checks, NULL, | ||
58 | do_trial_division, NULL); | ||
59 | if (ret != is_prime) { | ||
60 | fprintf(stderr, | ||
61 | "BN_is_prime_fasttest_ex(%d) %s: want %d, got %d\n", | ||
62 | i, descr, is_prime, ret); | ||
63 | failed = 1; | ||
64 | } | ||
65 | } | ||
66 | |||
67 | if (i < max || j < NUMPRIMES) { | ||
68 | fprintf(stderr, "%s: %d < %d or %d < %d\n", descr, i, max, j, | ||
69 | NUMPRIMES); | ||
70 | failed = 1; | ||
71 | } | ||
72 | |||
73 | err: | ||
74 | BN_free(n); | ||
75 | free(descr); | ||
76 | return failed; | ||
77 | } | ||
78 | |||
79 | int | ||
80 | main(void) | ||
81 | { | ||
82 | int failed = 0; | ||
83 | |||
84 | failed |= test_bn_is_prime_fasttest(0); | ||
85 | failed |= test_bn_is_prime_fasttest(1); | ||
86 | |||
87 | printf("%s\n", failed ? "FAILED" : "SUCCESS"); | ||
88 | |||
89 | return failed; | ||
90 | } | ||