diff options
author | tb <> | 2018-07-10 16:57:50 +0000 |
---|---|---|
committer | tb <> | 2018-07-10 16:57:50 +0000 |
commit | d2d7b6d676d20469f351583020067019315dfa84 (patch) | |
tree | d92812c1829484758ebc0cd8dd8d103220cd951a /src | |
parent | 8f893d2599ab84649f4191a0d3b4d6321ff08314 (diff) | |
download | openbsd-d2d7b6d676d20469f351583020067019315dfa84.tar.gz openbsd-d2d7b6d676d20469f351583020067019315dfa84.tar.bz2 openbsd-d2d7b6d676d20469f351583020067019315dfa84.zip |
Add simple regression tests for BN_{,u}{add,sub}(3). With input from jca
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/addsub/Makefile | 11 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/bn/addsub/bnaddsub.c | 237 |
2 files changed, 248 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/bn/addsub/Makefile b/src/regress/lib/libcrypto/bn/addsub/Makefile new file mode 100644 index 0000000000..c35182db4f --- /dev/null +++ b/src/regress/lib/libcrypto/bn/addsub/Makefile | |||
@@ -0,0 +1,11 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2018/07/10 16:57:50 tb Exp $ | ||
2 | |||
3 | .include "../../Makefile.inc" | ||
4 | |||
5 | PROG= bnaddsub | ||
6 | LDADD= ${CRYPTO_INT} | ||
7 | DPADD= ${LIBCRYPTO} | ||
8 | WARNINGS= Yes | ||
9 | CFLAGS+= -Werror | ||
10 | |||
11 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libcrypto/bn/addsub/bnaddsub.c b/src/regress/lib/libcrypto/bn/addsub/bnaddsub.c new file mode 100644 index 0000000000..6c47708718 --- /dev/null +++ b/src/regress/lib/libcrypto/bn/addsub/bnaddsub.c | |||
@@ -0,0 +1,237 @@ | |||
1 | /* $OpenBSD: bnaddsub.c,v 1.1 2018/07/10 16:57:50 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018 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 | /* Test basic functionality of BN_add(), BN_sub(), BN_uadd() and BN_usub() */ | ||
19 | |||
20 | #include <err.h> | ||
21 | #include <stdio.h> | ||
22 | |||
23 | #include <openssl/bio.h> | ||
24 | #include <openssl/bn.h> | ||
25 | #include <openssl/err.h> | ||
26 | |||
27 | #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) | ||
28 | |||
29 | BIO *bio_err; | ||
30 | |||
31 | struct hexinput_st { | ||
32 | const char *a_hex; | ||
33 | const char *b_hex; | ||
34 | const char *e_hex; /* expected result */ | ||
35 | const char ret; /* check return value */ | ||
36 | int compare; /* use BN_cmp() to verify results */ | ||
37 | }; | ||
38 | |||
39 | int bn_op_test(int (*)(BIGNUM *, const BIGNUM *, const BIGNUM *), | ||
40 | struct hexinput_st[], unsigned int, const char *); | ||
41 | void print_failure_case(BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, int, | ||
42 | const char *); | ||
43 | |||
44 | struct hexinput_st test_bn_add[] = { | ||
45 | { | ||
46 | "F", | ||
47 | "F", | ||
48 | "1E", | ||
49 | 1, | ||
50 | 1, | ||
51 | }, | ||
52 | { | ||
53 | "FFFFFFFFFFFFFFFFFFF", | ||
54 | "1", | ||
55 | "10000000000000000000", | ||
56 | 1, | ||
57 | 1, | ||
58 | }, | ||
59 | { | ||
60 | "7878787878787878", | ||
61 | "1010101010101010", | ||
62 | "8888888888888888", | ||
63 | 1, | ||
64 | 1, | ||
65 | }, | ||
66 | { | ||
67 | "FFFFFFFFFFFFFFFF0000000000000000", | ||
68 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", | ||
69 | "1FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", | ||
70 | 1, | ||
71 | 1, | ||
72 | }, | ||
73 | { | ||
74 | "F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0", | ||
75 | "10101010101010101010101010101010", | ||
76 | "101010101010101010101010101010100", | ||
77 | 1, | ||
78 | 1, | ||
79 | }, | ||
80 | }; | ||
81 | |||
82 | struct hexinput_st test_bn_sub[] = { | ||
83 | { | ||
84 | "10", | ||
85 | "1", | ||
86 | "F", | ||
87 | 1, | ||
88 | 1, | ||
89 | }, | ||
90 | { | ||
91 | "10", | ||
92 | "1", | ||
93 | "E", | ||
94 | 1, | ||
95 | 0, | ||
96 | }, | ||
97 | { | ||
98 | "100000000001000000000", | ||
99 | "11100000001", | ||
100 | "FFFFFFFFFEFEFFFFFFFF", | ||
101 | 1, | ||
102 | 1, | ||
103 | }, | ||
104 | { | ||
105 | "-FFFFFFFFFFFFFFFFFFFF", | ||
106 | "1", | ||
107 | "-100000000000000000000", | ||
108 | 1, | ||
109 | 1, | ||
110 | }, | ||
111 | }; | ||
112 | |||
113 | struct hexinput_st test_bn_usub[] = { | ||
114 | { | ||
115 | "10", | ||
116 | "1", | ||
117 | "F", | ||
118 | 1, | ||
119 | 1, | ||
120 | }, | ||
121 | { | ||
122 | "10", | ||
123 | "1", | ||
124 | "E", | ||
125 | 1, | ||
126 | 0, | ||
127 | }, | ||
128 | { | ||
129 | "100000000001000000000", | ||
130 | "11100000001", | ||
131 | "FFFFFFFFFEFEFFFFFFFF", | ||
132 | 1, | ||
133 | 1, | ||
134 | }, | ||
135 | { | ||
136 | "100000000000000000000", | ||
137 | "1", | ||
138 | "FFFFFFFFFFFFFFFFFFFF", | ||
139 | 1, | ||
140 | 1, | ||
141 | }, | ||
142 | { | ||
143 | "0", | ||
144 | "1", | ||
145 | "0", | ||
146 | 0, | ||
147 | 0, | ||
148 | }, | ||
149 | }; | ||
150 | |||
151 | void | ||
152 | print_failure_case(BIGNUM *a, BIGNUM *b, BIGNUM *e, BIGNUM *r, int i, | ||
153 | const char *testname) | ||
154 | { | ||
155 | BIO_printf(bio_err, "%s #%d failed:", testname, i); | ||
156 | BIO_printf(bio_err, "\na = "); | ||
157 | BN_print(bio_err, a); | ||
158 | BIO_printf(bio_err, "\nb = "); | ||
159 | BN_print(bio_err, b); | ||
160 | BIO_printf(bio_err, "\nexpected: e = "); | ||
161 | BN_print(bio_err, e); | ||
162 | BIO_printf(bio_err, "\nobtained: r = "); | ||
163 | BN_print(bio_err, r); | ||
164 | BIO_printf(bio_err, "\n"); | ||
165 | } | ||
166 | |||
167 | int | ||
168 | bn_op_test(int (*bn_op)(BIGNUM *, const BIGNUM *, const BIGNUM *), | ||
169 | struct hexinput_st tests[], unsigned int ntests, const char *testname) | ||
170 | { | ||
171 | BIGNUM *a = NULL, *b = NULL, *e = NULL, *r = NULL; | ||
172 | unsigned int i; | ||
173 | int failed = 0; | ||
174 | |||
175 | if (((a = BN_new()) == NULL) || | ||
176 | ((b = BN_new()) == NULL) || | ||
177 | ((e = BN_new()) == NULL) || | ||
178 | ((r = BN_new()) == NULL)) { | ||
179 | failed = 1; | ||
180 | ERR_print_errors(bio_err); | ||
181 | goto err; | ||
182 | } | ||
183 | |||
184 | for (i = 0; i < ntests; i++) { | ||
185 | int print = 0; | ||
186 | |||
187 | if (!BN_hex2bn(&a, tests[i].a_hex) || | ||
188 | !BN_hex2bn(&b, tests[i].b_hex) || | ||
189 | !BN_hex2bn(&e, tests[i].e_hex)) { | ||
190 | print = 1; | ||
191 | ERR_print_errors(bio_err); | ||
192 | } | ||
193 | |||
194 | if (tests[i].ret != bn_op(r, a, b)) | ||
195 | print = 1; | ||
196 | if (tests[i].compare == 1 && BN_cmp(e, r) != 0) | ||
197 | print = 1; | ||
198 | if (print) { | ||
199 | failed = 1; | ||
200 | print_failure_case(a, b, e, r, i, testname); | ||
201 | } | ||
202 | } | ||
203 | |||
204 | err: | ||
205 | BN_free(a); | ||
206 | BN_free(b); | ||
207 | BN_free(e); | ||
208 | BN_free(r); | ||
209 | return failed; | ||
210 | } | ||
211 | |||
212 | int | ||
213 | main(int argc, char *argv[]) | ||
214 | { | ||
215 | int failed = 0; | ||
216 | |||
217 | if ((bio_err = BIO_new_fp(stderr, BIO_NOCLOSE)) == NULL) { | ||
218 | fprintf(stderr, "%s: failed to initialize bio_err", | ||
219 | getprogname()); | ||
220 | return 1; | ||
221 | } | ||
222 | |||
223 | if (bn_op_test(BN_add, test_bn_add, nitems(test_bn_add), | ||
224 | "BN_add with test_bn_add[]")) | ||
225 | failed = 1; | ||
226 | if (bn_op_test(BN_uadd, test_bn_add, nitems(test_bn_add), | ||
227 | "BN_uadd with test_bn_add[]")) | ||
228 | failed = 1; | ||
229 | if (bn_op_test(BN_sub, test_bn_sub, nitems(test_bn_sub), | ||
230 | "BN_sub with test_bn_sub[]")) | ||
231 | failed = 1; | ||
232 | if (bn_op_test(BN_usub, test_bn_usub, nitems(test_bn_usub), | ||
233 | "BN_usub with test_bn_usub[]")) | ||
234 | failed = 1; | ||
235 | |||
236 | return failed; | ||
237 | } | ||