diff options
-rw-r--r-- | src/regress/lib/libcrypto/bn/Makefile | 4 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_print.c | 280 |
2 files changed, 283 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/bn/Makefile b/src/regress/lib/libcrypto/bn/Makefile index 1b4d68b984..1072f587e7 100644 --- a/src/regress/lib/libcrypto/bn/Makefile +++ b/src/regress/lib/libcrypto/bn/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.33 2023/06/03 21:20:29 tb Exp $ | 1 | # $OpenBSD: Makefile,v 1.34 2023/07/06 15:08:54 tb Exp $ |
2 | 2 | ||
3 | PROGS += bn_add_sub | 3 | PROGS += bn_add_sub |
4 | PROGS += bn_cmp | 4 | PROGS += bn_cmp |
@@ -12,6 +12,7 @@ PROGS += bn_mod_sqrt | |||
12 | PROGS += bn_mont | 12 | PROGS += bn_mont |
13 | PROGS += bn_mul_div | 13 | PROGS += bn_mul_div |
14 | PROGS += bn_primes | 14 | PROGS += bn_primes |
15 | PROGS += bn_print | ||
15 | PROGS += bn_rand_interval | 16 | PROGS += bn_rand_interval |
16 | PROGS += bn_shift | 17 | PROGS += bn_shift |
17 | PROGS += bn_test | 18 | PROGS += bn_test |
@@ -22,6 +23,7 @@ PROGS += bn_word | |||
22 | STATIC_LINK += bn_gcd | 23 | STATIC_LINK += bn_gcd |
23 | STATIC_LINK += bn_isqrt | 24 | STATIC_LINK += bn_isqrt |
24 | STATIC_LINK += bn_mod_exp | 25 | STATIC_LINK += bn_mod_exp |
26 | STATIC_LINK += bn_print | ||
25 | STATIC_LINK += bn_rand_interval | 27 | STATIC_LINK += bn_rand_interval |
26 | STATIC_LINK += bn_test | 28 | STATIC_LINK += bn_test |
27 | 29 | ||
diff --git a/src/regress/lib/libcrypto/bn/bn_print.c b/src/regress/lib/libcrypto/bn/bn_print.c new file mode 100644 index 0000000000..a19fa84cbc --- /dev/null +++ b/src/regress/lib/libcrypto/bn/bn_print.c | |||
@@ -0,0 +1,280 @@ | |||
1 | /* $OpenBSD: bn_print.c,v 1.1 2023/07/06 15:08:54 tb Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <err.h> | ||
20 | #include <stdio.h> | ||
21 | #include <string.h> | ||
22 | |||
23 | #include <openssl/asn1.h> | ||
24 | #include <openssl/bio.h> | ||
25 | #include <openssl/bn.h> | ||
26 | |||
27 | #include "bn_local.h" | ||
28 | |||
29 | #define BATIHDIDIDI "mana mana" | ||
30 | #define BUF_MEM_LEN 1024 | ||
31 | |||
32 | static const char *pk = "040d305e1b159d03d0a17935b73a3c927aca151ccd62f39c" | ||
33 | "265c073de554faa3d6cc12eaf4145fe88e19ab2f2e48e6ac" | ||
34 | "184378acd037c3bdb2cd2ce647e21ae663b83d2e2f78c44f" | ||
35 | "dbf40fa4684c55726b951d4e18429578cc373c91e29b652b" | ||
36 | "29"; | ||
37 | |||
38 | const struct print_test { | ||
39 | const char *desc; | ||
40 | const char *want; | ||
41 | } bn_print_tests[] = { | ||
42 | { | ||
43 | .desc = "zero", | ||
44 | .want = " mana mana 0\n", | ||
45 | }, | ||
46 | { | ||
47 | .desc = "minus one", | ||
48 | .want = " mana mana 1 (0x1)\n", | ||
49 | }, | ||
50 | { | ||
51 | .desc = "minus one", | ||
52 | .want = " mana mana -1 (-0x1)\n", | ||
53 | }, | ||
54 | #ifdef _LP64 | ||
55 | { | ||
56 | .desc = "largest word", | ||
57 | .want = " mana mana 18446744073709551615 " | ||
58 | "(0xffffffffffffffff)\n", | ||
59 | }, | ||
60 | { | ||
61 | .desc = "smallest word", | ||
62 | .want = " mana mana -18446744073709551615 " | ||
63 | "(-0xffffffffffffffff)\n", | ||
64 | }, | ||
65 | { | ||
66 | .desc = "largest negative non-word", | ||
67 | .want = " mana mana (Negative)\n" | ||
68 | " 01:00:00:00:00:00:00:00:00\n", | ||
69 | }, | ||
70 | { | ||
71 | .desc = "smallest positive non-word", | ||
72 | .want = " mana mana\n" | ||
73 | " 01:00:00:00:00:00:00:00:00\n", | ||
74 | }, | ||
75 | #else | ||
76 | { | ||
77 | .desc = "largest word", | ||
78 | .want = " mana mana 4294967295 (0xffffffff)\n", | ||
79 | }, | ||
80 | { | ||
81 | .desc = "smallest word", | ||
82 | .want = " mana mana -4294967295 (-0xffffffff)\n", | ||
83 | }, | ||
84 | { | ||
85 | .desc = "largest negative non-word", | ||
86 | .want = " mana mana (Negative)\n" | ||
87 | " 01:00:00:00:00\n", | ||
88 | }, | ||
89 | { | ||
90 | .desc = "smallest positive non-word", | ||
91 | .want = " mana mana\n" | ||
92 | " 01:00:00:00:00\n", | ||
93 | }, | ||
94 | #endif | ||
95 | { | ||
96 | .desc = "some pubkey", | ||
97 | .want = " mana mana\n" | ||
98 | " 04:0d:30:5e:1b:15:9d:03:d0:a1:79:35:b7:3a:3c:\n" | ||
99 | " 92:7a:ca:15:1c:cd:62:f3:9c:26:5c:07:3d:e5:54:\n" | ||
100 | " fa:a3:d6:cc:12:ea:f4:14:5f:e8:8e:19:ab:2f:2e:\n" | ||
101 | " 48:e6:ac:18:43:78:ac:d0:37:c3:bd:b2:cd:2c:e6:\n" | ||
102 | " 47:e2:1a:e6:63:b8:3d:2e:2f:78:c4:4f:db:f4:0f:\n" | ||
103 | " a4:68:4c:55:72:6b:95:1d:4e:18:42:95:78:cc:37:\n" | ||
104 | " 3c:91:e2:9b:65:2b:29\n", | ||
105 | }, | ||
106 | { | ||
107 | .desc = "negated pubkey", | ||
108 | .want = " mana mana (Negative)\n" | ||
109 | " 04:0d:30:5e:1b:15:9d:03:d0:a1:79:35:b7:3a:3c:\n" | ||
110 | " 92:7a:ca:15:1c:cd:62:f3:9c:26:5c:07:3d:e5:54:\n" | ||
111 | " fa:a3:d6:cc:12:ea:f4:14:5f:e8:8e:19:ab:2f:2e:\n" | ||
112 | " 48:e6:ac:18:43:78:ac:d0:37:c3:bd:b2:cd:2c:e6:\n" | ||
113 | " 47:e2:1a:e6:63:b8:3d:2e:2f:78:c4:4f:db:f4:0f:\n" | ||
114 | " a4:68:4c:55:72:6b:95:1d:4e:18:42:95:78:cc:37:\n" | ||
115 | " 3c:91:e2:9b:65:2b:29\n", | ||
116 | }, | ||
117 | { | ||
118 | .desc = "shifted negated pubkey", | ||
119 | .want = " mana mana (Negative)\n" | ||
120 | " 04:0d:30:5e:1b:15:9d:03:d0:a1:79:35:b7:3a:3c:\n" | ||
121 | " 92:7a:ca:15:1c:cd:62:f3:9c:26:5c:07:3d:e5:54:\n" | ||
122 | " fa:a3:d6:cc:12:ea:f4:14:5f:e8:8e:19:ab:2f:2e:\n" | ||
123 | " 48:e6:ac:18:43:78:ac:d0:37:c3:bd:b2:cd:2c:e6:\n" | ||
124 | " 47:e2:1a:e6:63:b8:3d:2e:2f:78:c4:4f:db:f4:0f:\n" | ||
125 | " a4:68:4c:55:72:6b:95:1d:4e:18:42:95:78:cc:37\n", | ||
126 | }, | ||
127 | { | ||
128 | .desc = "shifted pubkey", | ||
129 | .want = " mana mana\n" | ||
130 | " 04:0d:30:5e:1b:15:9d:03:d0:a1:79:35:b7:3a:3c:\n" | ||
131 | " 92:7a:ca:15:1c:cd:62:f3:9c:26:5c:07:3d:e5:54:\n" | ||
132 | " fa:a3:d6:cc:12:ea:f4:14:5f:e8:8e:19:ab:2f:2e:\n" | ||
133 | " 48:e6:ac:18:43:78:ac:d0:37:c3:bd:b2:cd:2c:e6:\n" | ||
134 | " 47:e2:1a:e6:63:b8:3d:2e:2f:78:c4:4f:db:f4:0f:\n" | ||
135 | " a4:68:4c:55:72:6b:95:1d:4e:18:42:95:78:cc:37\n", | ||
136 | }, | ||
137 | }; | ||
138 | |||
139 | #define N_TESTCASES (sizeof(bn_print_tests) / sizeof(bn_print_tests[0])) | ||
140 | |||
141 | static int | ||
142 | bn_print_testcase(const BIGNUM *bn, const struct print_test *test) | ||
143 | { | ||
144 | BIO *bio; | ||
145 | char *got; | ||
146 | size_t want_len; | ||
147 | long got_len; | ||
148 | int failed = 1; | ||
149 | |||
150 | if ((bio = BIO_new(BIO_s_mem())) == NULL) | ||
151 | errx(1, "BIO_new"); | ||
152 | |||
153 | if (!bn_printf(bio, bn, 4, "%s", BATIHDIDIDI)) | ||
154 | errx(1, "bn_printf"); | ||
155 | |||
156 | if ((got_len = BIO_get_mem_data(bio, &got)) < 0) | ||
157 | errx(1, "BIO_get_mem_data"); | ||
158 | |||
159 | if ((want_len = strlen(test->want)) != (size_t)got_len) { | ||
160 | fprintf(stderr, "%s: want: %zu, got %ld\n", | ||
161 | test->desc, want_len, got_len); | ||
162 | goto err; | ||
163 | } | ||
164 | |||
165 | if (strncmp(got, test->want, want_len) != 0) { | ||
166 | fprintf(stderr, "%s: strings differ\n", test->desc); | ||
167 | fprintf(stderr, "want: \"%s\"\ngot : \"%*s\"\n", | ||
168 | test->want, (int)got_len, got); | ||
169 | goto err; | ||
170 | } | ||
171 | |||
172 | failed = 0; | ||
173 | err: | ||
174 | BIO_free(bio); | ||
175 | |||
176 | return failed; | ||
177 | } | ||
178 | |||
179 | int | ||
180 | main(void) | ||
181 | { | ||
182 | const struct print_test *test; | ||
183 | size_t testcase = 0; | ||
184 | BIO *bio; | ||
185 | BIGNUM *bn; | ||
186 | int failed = 0; | ||
187 | |||
188 | if ((bio = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL) | ||
189 | errx(1, "BIO_new_fp"); | ||
190 | |||
191 | /* zero */ | ||
192 | if ((bn = BN_new()) == NULL) | ||
193 | errx(1, "BN_new"); | ||
194 | if (testcase >= N_TESTCASES) | ||
195 | errx(1, "Too many tests"); | ||
196 | test = &bn_print_tests[testcase++]; | ||
197 | failed |= bn_print_testcase(bn, test); | ||
198 | |||
199 | /* one */ | ||
200 | if (!BN_set_word(bn, 1)) | ||
201 | errx(1, "BIO_set_word"); | ||
202 | if (testcase >= N_TESTCASES) | ||
203 | errx(1, "Too many tests"); | ||
204 | test = &bn_print_tests[testcase++]; | ||
205 | failed |= bn_print_testcase(bn, test); | ||
206 | |||
207 | /* minus one */ | ||
208 | BN_set_negative(bn, 1); | ||
209 | if (testcase >= N_TESTCASES) | ||
210 | errx(1, "Too many tests"); | ||
211 | test = &bn_print_tests[testcase++]; | ||
212 | failed |= bn_print_testcase(bn, test); | ||
213 | |||
214 | /* largest word */ | ||
215 | if (!BN_set_word(bn, ~0)) | ||
216 | errx(1, "BN_set_word"); | ||
217 | if (testcase >= N_TESTCASES) | ||
218 | errx(1, "Too many tests"); | ||
219 | test = &bn_print_tests[testcase++]; | ||
220 | failed |= bn_print_testcase(bn, test); | ||
221 | |||
222 | /* smallest word */ | ||
223 | BN_set_negative(bn, 1); | ||
224 | if (testcase >= N_TESTCASES) | ||
225 | errx(1, "Too many tests"); | ||
226 | test = &bn_print_tests[testcase++]; | ||
227 | failed |= bn_print_testcase(bn, test); | ||
228 | |||
229 | /* largest negative non-word */ | ||
230 | if (!BN_sub_word(bn, 1)) | ||
231 | errx(1, "ASN1_bn_print"); | ||
232 | if (testcase >= N_TESTCASES) | ||
233 | errx(1, "Too many tests"); | ||
234 | test = &bn_print_tests[testcase++]; | ||
235 | failed |= bn_print_testcase(bn, test); | ||
236 | |||
237 | /* smallest positive non-word */ | ||
238 | BN_set_negative(bn, 0); | ||
239 | if (testcase >= N_TESTCASES) | ||
240 | errx(1, "Too many tests"); | ||
241 | test = &bn_print_tests[testcase++]; | ||
242 | failed |= bn_print_testcase(bn, test); | ||
243 | |||
244 | /* some pubkey */ | ||
245 | if (BN_hex2bn(&bn, pk) == 0) | ||
246 | errx(1, "BN_hex2bn"); | ||
247 | if (testcase >= N_TESTCASES) | ||
248 | errx(1, "Too many tests"); | ||
249 | test = &bn_print_tests[testcase++]; | ||
250 | failed |= bn_print_testcase(bn, test); | ||
251 | |||
252 | /* negated pubkey */ | ||
253 | BN_set_negative(bn, 1); | ||
254 | if (testcase >= N_TESTCASES) | ||
255 | errx(1, "Too many tests"); | ||
256 | test = &bn_print_tests[testcase++]; | ||
257 | failed |= bn_print_testcase(bn, test); | ||
258 | |||
259 | /* shifted negated pubkey */ | ||
260 | if (!BN_rshift(bn, bn, 7 * 8)) | ||
261 | errx(1, "BN_rshift"); | ||
262 | if (testcase >= N_TESTCASES) | ||
263 | errx(1, "Too many tests"); | ||
264 | test = &bn_print_tests[testcase++]; | ||
265 | failed |= bn_print_testcase(bn, test); | ||
266 | |||
267 | /* shifted pubkey */ | ||
268 | BN_set_negative(bn, 0); | ||
269 | if (testcase >= N_TESTCASES) | ||
270 | errx(1, "Too many tests"); | ||
271 | test = &bn_print_tests[testcase++]; | ||
272 | failed |= bn_print_testcase(bn, test); | ||
273 | |||
274 | if (testcase != N_TESTCASES) { | ||
275 | warnx("Not all tests run"); | ||
276 | failed |= 1; | ||
277 | } | ||
278 | |||
279 | return failed; | ||
280 | } | ||