Include <com_err.h>
[heimdal.git] / lib / hcrypto / test_bn.c
blobb180f8e34f48296d7a794fe0e35766e501e4be87
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <config.h>
36 #include <sys/types.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
42 #include <bn.h>
43 #include <rand.h>
45 static int
46 set_get(unsigned long num)
48 BIGNUM *bn;
50 bn = BN_new();
51 if (!BN_set_word(bn, num))
52 return 1;
54 if (BN_get_word(bn) != num)
55 return 1;
57 BN_free(bn);
58 return 0;
61 #define CHECK(x) do { ret += x; } while(0)
63 static int
64 test_BN_set_get(void)
66 int ret = 0;
67 CHECK(set_get(0));
68 CHECK(set_get(1));
69 CHECK(set_get(0xff));
70 CHECK(set_get(0x1ff));
71 CHECK(set_get(0xffff));
72 CHECK(set_get(0xf000));
73 CHECK(set_get(ULONG_MAX / 2));
74 CHECK(set_get(ULONG_MAX - 1));
76 return ret;
79 static int
80 test_BN_bit(void)
82 BIGNUM *bn;
83 int ret = 0;
85 bn = BN_new();
87 /* test setting and getting of "word" */
88 if (!BN_set_word(bn, 1))
89 return 1;
90 if (!BN_is_bit_set(bn, 0))
91 ret += 1;
92 if (!BN_is_bit_set(bn, 0))
93 ret += 1;
95 if (!BN_set_word(bn, 2))
96 return 1;
97 if (!BN_is_bit_set(bn, 1))
98 ret += 1;
100 if (!BN_set_word(bn, 3))
101 return 1;
102 if (!BN_is_bit_set(bn, 0))
103 ret += 1;
104 if (!BN_is_bit_set(bn, 1))
105 ret += 1;
107 if (!BN_set_word(bn, 0x100))
108 return 1;
109 if (!BN_is_bit_set(bn, 8))
110 ret += 1;
112 if (!BN_set_word(bn, 0x1000))
113 return 1;
114 if (!BN_is_bit_set(bn, 12))
115 ret += 1;
117 /* test bitsetting */
118 if (!BN_set_word(bn, 1))
119 return 1;
120 if (!BN_set_bit(bn, 1))
121 return 1;
122 if (BN_get_word(bn) != 3)
123 return 1;
124 if (!BN_clear_bit(bn, 0))
125 return 1;
126 if (BN_get_word(bn) != 2)
127 return 1;
129 /* test bitsetting past end of current end */
130 BN_clear(bn);
131 if (!BN_set_bit(bn, 12))
132 return 1;
133 if (BN_get_word(bn) != 0x1000)
134 return 1;
136 /* test bit and byte counting functions */
137 if (BN_num_bits(bn) != 13)
138 return 1;
139 if (BN_num_bytes(bn) != 2)
140 return 1;
142 BN_free(bn);
143 return ret;
146 struct ietest {
147 char *data;
148 size_t len;
149 unsigned long num;
150 } ietests[] = {
151 { "", 0, 0 },
152 { "\x01", 1, 1 },
153 { "\x02", 1, 2 },
154 { "\xf2", 1, 0xf2 },
155 { "\x01\x00", 2, 256 }
158 static int
159 test_BN_import_export(void)
161 BIGNUM *bn;
162 int ret = 0;
163 int i;
165 bn = BN_new();
167 for (i = 0; i < sizeof(ietests)/sizeof(ietests[0]); i++) {
168 size_t len;
169 unsigned char *p;
170 if (!BN_bin2bn((unsigned char*)ietests[i].data, ietests[i].len, bn))
171 return 1;
172 if (BN_get_word(bn) != ietests[i].num)
173 return 1;
174 len = BN_num_bytes(bn);
175 if (len != ietests[i].len)
176 return 1;
177 p = malloc(len + 1);
178 p[len] = 0xf4;
179 BN_bn2bin(bn, p);
180 if (p[len] != 0xf4)
181 return 1;
182 if (memcmp(p, ietests[i].data, ietests[i].len) != 0)
183 return 1;
184 free(p);
187 BN_free(bn);
188 return ret;
191 static int
192 test_BN_uadd(void)
194 BIGNUM *a, *b, *c;
195 char *p;
197 a = BN_new();
198 b = BN_new();
199 c = BN_new();
201 BN_set_word(a, 1);
202 BN_set_word(b, 2);
204 BN_uadd(c, a, b);
206 if (BN_get_word(c) != 3)
207 return 1;
209 BN_uadd(c, b, a);
211 if (BN_get_word(c) != 3)
212 return 1;
214 BN_set_word(b, 0xff);
216 BN_uadd(c, a, b);
217 if (BN_get_word(c) != 0x100)
218 return 1;
220 BN_uadd(c, b, a);
221 if (BN_get_word(c) != 0x100)
222 return 1;
224 BN_set_word(a, 0xff);
226 BN_uadd(c, a, b);
227 if (BN_get_word(c) != 0x1fe)
228 return 1;
230 BN_uadd(c, b, a);
231 if (BN_get_word(c) != 0x1fe)
232 return 1;
235 BN_free(a);
236 BN_free(b);
238 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
239 BN_hex2bn(&b, "84B6C7E8D28ACA1614954DA");
241 BN_uadd(c, b, a);
242 p = BN_bn2hex(c);
243 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
244 free(p);
245 return 1;
247 free(p);
249 BN_uadd(c, a, b);
250 p = BN_bn2hex(c);
251 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
252 free(p);
253 return 1;
255 free(p);
257 BN_free(a);
258 BN_free(b);
259 BN_free(c);
261 return 0;
264 static int
265 test_BN_cmp(void)
267 BIGNUM *a, *b;
269 a = BN_new();
270 b = BN_new();
272 if (!BN_set_word(a, 1))
273 return 1;
274 if (!BN_set_word(b, 1))
275 return 1;
277 if (BN_cmp(a, b) != 0)
278 return 1;
279 if (BN_cmp(b, a) != 0)
280 return 1;
282 if (!BN_set_word(b, 2))
283 return 1;
285 if (BN_cmp(a, b) >= 0)
286 return 1;
287 if (BN_cmp(b, a) <= 0)
288 return 1;
290 BN_set_negative(b, 1);
292 if (BN_cmp(a, b) <= 0)
293 return 1;
294 if (BN_cmp(b, a) >= 0)
295 return 1;
297 BN_free(a);
298 BN_free(b);
300 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD1");
301 BN_hex2bn(&b, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
303 if (BN_cmp(a, b) >= 0)
304 return 1;
305 if (BN_cmp(b, a) <= 0)
306 return 1;
308 BN_set_negative(b, 1);
310 if (BN_cmp(a, b) <= 0)
311 return 1;
312 if (BN_cmp(b, a) >= 0)
313 return 1;
315 BN_free(a);
316 BN_free(b);
317 return 0;
320 static int
321 test_BN_rand(void)
323 BIGNUM *bn;
325 if (RAND_status() != 1)
326 return 0;
328 bn = BN_new();
329 if (bn == NULL)
330 return 1;
332 if (!BN_rand(bn, 1024, 0, 0))
333 return 1;
335 BN_free(bn);
336 return 0;
339 #define testnum 100
340 #define testnum2 10
342 static int
343 test_BN_CTX(void)
345 unsigned int i, j;
346 BIGNUM *bn;
347 BN_CTX *c;
349 if ((c = BN_CTX_new()) == NULL)
350 return 1;
352 for (i = 0; i < testnum; i++) {
353 BN_CTX_start(c);
354 BN_CTX_end(c);
357 for (i = 0; i < testnum; i++)
358 BN_CTX_start(c);
359 for (i = 0; i < testnum; i++)
360 BN_CTX_end(c);
362 for (i = 0; i < testnum; i++) {
363 BN_CTX_start(c);
364 if ((bn = BN_CTX_get(c)) == NULL)
365 return 1;
366 BN_CTX_end(c);
369 for (i = 0; i < testnum; i++) {
370 BN_CTX_start(c);
371 for (j = 0; j < testnum2; j++)
372 if ((bn = BN_CTX_get(c)) == NULL)
373 return 1;
375 for (i = 0; i < testnum; i++)
376 BN_CTX_end(c);
378 BN_CTX_free(c);
379 return 0;
384 main(int argc, char **argv)
386 int ret = 0;
388 ret += test_BN_set_get();
389 ret += test_BN_bit();
390 ret += test_BN_import_export();
391 ret += test_BN_uadd();
392 ret += test_BN_cmp();
393 ret += test_BN_rand();
394 ret += test_BN_CTX();
396 return ret;