summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2023-04-04 15:32:02 +0000
committertb <>2023-04-04 15:32:02 +0000
commitd953e42079697b91918dd0ba43dc0d5ccbed414a (patch)
treeeb74d332eef1c014a660ebcb634881d42db0304e
parent4a09fb09af6652f38afe21ea7ba4d260f701b5de (diff)
downloadopenbsd-d953e42079697b91918dd0ba43dc0d5ccbed414a.tar.gz
openbsd-d953e42079697b91918dd0ba43dc0d5ccbed414a.tar.bz2
openbsd-d953e42079697b91918dd0ba43dc0d5ccbed414a.zip
Clean bn_mod_sqrt up a little
This makes it look a bit more like other tests and also prepares the addition of further test cases and different tests.
-rw-r--r--src/regress/lib/libcrypto/bn/bn_mod_sqrt.c81
1 files changed, 53 insertions, 28 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c b/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c
index 7757c2a1ca..74702f950c 100644
--- a/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c
+++ b/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mod_sqrt.c,v 1.2 2022/12/06 18:23:29 tb Exp $ */ 1/* $OpenBSD: bn_mod_sqrt.c,v 1.3 2023/04/04 15:32:02 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
4 * 4 *
@@ -15,6 +15,7 @@
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */ 16 */
17 17
18#include <err.h>
18#include <stdio.h> 19#include <stdio.h>
19 20
20#include <openssl/bn.h> 21#include <openssl/bn.h>
@@ -51,19 +52,26 @@ struct mod_sqrt_test {
51 52
52const size_t N_TESTS = sizeof(mod_sqrt_test_data) / sizeof(*mod_sqrt_test_data); 53const size_t N_TESTS = sizeof(mod_sqrt_test_data) / sizeof(*mod_sqrt_test_data);
53 54
54int mod_sqrt_test(struct mod_sqrt_test *test); 55static int
55 56mod_sqrt_test(struct mod_sqrt_test *test, BN_CTX *ctx)
56int
57mod_sqrt_test(struct mod_sqrt_test *test)
58{ 57{
59 BN_CTX *ctx = NULL; 58 BIGNUM *a, *p, *want, *got, *diff, *sum;
60 BIGNUM *a = NULL, *p = NULL, *want = NULL, *got = NULL, *diff = NULL;
61 int failed = 1; 59 int failed = 1;
62 60
63 if ((ctx = BN_CTX_new()) == NULL) { 61 BN_CTX_start(ctx);
64 fprintf(stderr, "BN_CTX_new failed\n"); 62
65 goto out; 63 if ((a = BN_CTX_get(ctx)) == NULL)
66 } 64 errx(1, "a = BN_CTX_get()");
65 if ((p = BN_CTX_get(ctx)) == NULL)
66 errx(1, "p = BN_CTX_get()");
67 if ((want = BN_CTX_get(ctx)) == NULL)
68 errx(1, "want = BN_CTX_get()");
69 if ((got = BN_CTX_get(ctx)) == NULL)
70 errx(1, "got = BN_CTX_get()");
71 if ((diff = BN_CTX_get(ctx)) == NULL)
72 errx(1, "diff = BN_CTX_get()");
73 if ((sum = BN_CTX_get(ctx)) == NULL)
74 errx(1, "sum = BN_CTX_get()");
67 75
68 if (!BN_hex2bn(&a, test->a)) { 76 if (!BN_hex2bn(&a, test->a)) {
69 fprintf(stderr, "BN_hex2bn(a) failed\n"); 77 fprintf(stderr, "BN_hex2bn(a) failed\n");
@@ -78,8 +86,7 @@ mod_sqrt_test(struct mod_sqrt_test *test)
78 goto out; 86 goto out;
79 } 87 }
80 88
81 if (((got = BN_mod_sqrt(NULL, a, p, ctx)) == NULL) != 89 if ((BN_mod_sqrt(got, a, p, ctx) == NULL) != test->bn_mod_sqrt_fails) {
82 test->bn_mod_sqrt_fails) {
83 fprintf(stderr, "BN_mod_sqrt %s unexpectedly\n", 90 fprintf(stderr, "BN_mod_sqrt %s unexpectedly\n",
84 test->bn_mod_sqrt_fails ? "succeeded" : "failed"); 91 test->bn_mod_sqrt_fails ? "succeeded" : "failed");
85 goto out; 92 goto out;
@@ -90,42 +97,60 @@ mod_sqrt_test(struct mod_sqrt_test *test)
90 goto out; 97 goto out;
91 } 98 }
92 99
93 if ((diff = BN_new()) == NULL) { 100 if (!BN_mod_sub(diff, want, got, p, ctx)) {
94 fprintf(stderr, "diff = BN_new() failed\n"); 101 fprintf(stderr, "BN_mod_sub() failed\n");
95 goto out; 102 goto out;
96 } 103 }
97 104 if (!BN_mod_add(sum, want, got, p, ctx)) {
98 if (!BN_mod_sub(diff, want, got, p, ctx)) { 105 fprintf(stderr, "BN_mod_add() failed\n");
99 fprintf(stderr, "BN_mod_sub failed\n");
100 goto out; 106 goto out;
101 } 107 }
102 108
103 if (!BN_is_zero(diff)) { 109 /* XXX - Remove sum once we return the canonical square root. */
110 if (!BN_is_zero(diff) && !BN_is_zero(sum)) {
104 fprintf(stderr, "want != got\n"); 111 fprintf(stderr, "want != got\n");
112
113 fprintf(stderr, "a: %s\n", test->a);
114 fprintf(stderr, "p: %s\n", test->p);
115 fprintf(stderr, "want: %s:", test->sqrt);
116 fprintf(stderr, "got: ");
117 BN_print_fp(stderr, got);
118 fprintf(stderr, "\n\n");
119
105 goto out; 120 goto out;
106 } 121 }
107 122
108 failed = 0; 123 failed = 0;
109 124
110 out: 125 out:
111 BN_CTX_free(ctx); 126 BN_CTX_end(ctx);
112 BN_free(a);
113 BN_free(p);
114 BN_free(want);
115 BN_free(got);
116 BN_free(diff);
117 127
118 return failed; 128 return failed;
119} 129}
120 130
121int 131static int
122main(void) 132bn_mod_sqrt_test(void)
123{ 133{
134 BN_CTX *ctx;
124 size_t i; 135 size_t i;
125 int failed = 0; 136 int failed = 0;
126 137
138 if ((ctx = BN_CTX_new()) == NULL)
139 errx(1, "BN_CTX_new()");
140
127 for (i = 0; i < N_TESTS; i++) 141 for (i = 0; i < N_TESTS; i++)
128 failed |= mod_sqrt_test(&mod_sqrt_test_data[i]); 142 failed |= mod_sqrt_test(&mod_sqrt_test_data[i], ctx);
143
144 BN_CTX_free(ctx);
145
146 return failed;
147}
148int
149main(void)
150{
151 int failed = 0;
152
153 failed |= bn_mod_sqrt_test();
129 154
130 return failed; 155 return failed;
131} 156}