1 /* $OpenBSD: wycheproof-primes.c,v 1.2 2022/12/01 13:49:12 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 <err.h>
19 #include <stdio.h>
20
21 #include <openssl/bn.h>
22
23 #include "primality_testcases.h"
24
25 int
primality_test(struct wycheproof_testcase * test)26 primality_test(struct wycheproof_testcase *test)
27 {
28 BIGNUM *value = NULL;
29 int ret;
30 int failed = 1;
31
32 if (!BN_hex2bn(&value, test->value))
33 errx(1, "%d: failed to set value \"%s\"", test->id, test->value);
34
35 if ((ret = BN_is_prime_ex(value, BN_prime_checks, NULL, NULL)) < 0)
36 errx(1, "%d: BN_is_prime_ex errored", test->id);
37
38 if (ret != test->result && !test->acceptable) {
39 fprintf(stderr, "%d failed, want %d, got %d\n", test->id,
40 test->result, ret);
41 goto err;
42 }
43
44 failed = 0;
45 err:
46 BN_free(value);
47
48 return failed;
49 }
50
51 int
main(void)52 main(void)
53 {
54 size_t i;
55 int failed = 0;
56
57 for (i = 0; i < N_TESTS; i++)
58 failed |= primality_test(&testcases[i]);
59
60 return failed;
61 }
62