heimdal:camellia: include roken.h
[heimdal.git] / lib / hcrypto / test_bn.c
blob4a5fe22b7d943c41c6a55cfe528e71418df0db7b
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 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #ifdef RCSID
39 RCSID("$Id$");
40 #endif
42 #include <sys/types.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include <bn.h>
49 #include <rand.h>
51 static int
52 set_get(unsigned long num)
54 BIGNUM *bn;
56 bn = BN_new();
57 if (!BN_set_word(bn, num))
58 return 1;
60 if (BN_get_word(bn) != num)
61 return 1;
63 BN_free(bn);
64 return 0;
67 #define CHECK(x) do { ret += x; } while(0)
69 static int
70 test_BN_set_get(void)
72 int ret = 0;
73 CHECK(set_get(0));
74 CHECK(set_get(1));
75 CHECK(set_get(0xff));
76 CHECK(set_get(0x1ff));
77 CHECK(set_get(0xffff));
78 CHECK(set_get(0xf000));
79 CHECK(set_get(ULONG_MAX / 2));
80 CHECK(set_get(ULONG_MAX - 1));
82 return ret;
85 static int
86 test_BN_bit(void)
88 BIGNUM *bn;
89 int ret = 0;
91 bn = BN_new();
93 /* test setting and getting of "word" */
94 if (!BN_set_word(bn, 1))
95 return 1;
96 if (!BN_is_bit_set(bn, 0))
97 ret += 1;
98 if (!BN_is_bit_set(bn, 0))
99 ret += 1;
101 if (!BN_set_word(bn, 2))
102 return 1;
103 if (!BN_is_bit_set(bn, 1))
104 ret += 1;
106 if (!BN_set_word(bn, 3))
107 return 1;
108 if (!BN_is_bit_set(bn, 0))
109 ret += 1;
110 if (!BN_is_bit_set(bn, 1))
111 ret += 1;
113 if (!BN_set_word(bn, 0x100))
114 return 1;
115 if (!BN_is_bit_set(bn, 8))
116 ret += 1;
118 if (!BN_set_word(bn, 0x1000))
119 return 1;
120 if (!BN_is_bit_set(bn, 12))
121 ret += 1;
123 /* test bitsetting */
124 if (!BN_set_word(bn, 1))
125 return 1;
126 if (!BN_set_bit(bn, 1))
127 return 1;
128 if (BN_get_word(bn) != 3)
129 return 1;
130 if (!BN_clear_bit(bn, 0))
131 return 1;
132 if (BN_get_word(bn) != 2)
133 return 1;
135 /* test bitsetting past end of current end */
136 BN_clear(bn);
137 if (!BN_set_bit(bn, 12))
138 return 1;
139 if (BN_get_word(bn) != 0x1000)
140 return 1;
142 /* test bit and byte counting functions */
143 if (BN_num_bits(bn) != 13)
144 return 1;
145 if (BN_num_bytes(bn) != 2)
146 return 1;
148 BN_free(bn);
149 return ret;
152 struct ietest {
153 char *data;
154 size_t len;
155 unsigned long num;
156 } ietests[] = {
157 { "", 0, 0 },
158 { "\x01", 1, 1 },
159 { "\x02", 1, 2 },
160 { "\xf2", 1, 0xf2 },
161 { "\x01\x00", 2, 256 }
164 static int
165 test_BN_import_export(void)
167 BIGNUM *bn;
168 int ret = 0;
169 int i;
171 bn = BN_new();
173 for (i = 0; i < sizeof(ietests)/sizeof(ietests[0]); i++) {
174 size_t len;
175 unsigned char *p;
176 if (!BN_bin2bn((unsigned char*)ietests[i].data, ietests[i].len, bn))
177 return 1;
178 if (BN_get_word(bn) != ietests[i].num)
179 return 1;
180 len = BN_num_bytes(bn);
181 if (len != ietests[i].len)
182 return 1;
183 p = malloc(len + 1);
184 p[len] = 0xf4;
185 BN_bn2bin(bn, p);
186 if (p[len] != 0xf4)
187 return 1;
188 if (memcmp(p, ietests[i].data, ietests[i].len) != 0)
189 return 1;
190 free(p);
193 BN_free(bn);
194 return ret;
197 static int
198 test_BN_uadd(void)
200 BIGNUM *a, *b, *c;
201 char *p;
203 a = BN_new();
204 b = BN_new();
205 c = BN_new();
207 BN_set_word(a, 1);
208 BN_set_word(b, 2);
210 BN_uadd(c, a, b);
212 if (BN_get_word(c) != 3)
213 return 1;
215 BN_uadd(c, b, a);
217 if (BN_get_word(c) != 3)
218 return 1;
220 BN_set_word(b, 0xff);
222 BN_uadd(c, a, b);
223 if (BN_get_word(c) != 0x100)
224 return 1;
226 BN_uadd(c, b, a);
227 if (BN_get_word(c) != 0x100)
228 return 1;
230 BN_set_word(a, 0xff);
232 BN_uadd(c, a, b);
233 if (BN_get_word(c) != 0x1fe)
234 return 1;
236 BN_uadd(c, b, a);
237 if (BN_get_word(c) != 0x1fe)
238 return 1;
241 BN_free(a);
242 BN_free(b);
244 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
245 BN_hex2bn(&b, "84B6C7E8D28ACA1614954DA");
247 BN_uadd(c, b, a);
248 p = BN_bn2hex(c);
249 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
250 free(p);
251 return 1;
253 free(p);
255 BN_uadd(c, a, b);
256 p = BN_bn2hex(c);
257 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
258 free(p);
259 return 1;
261 free(p);
263 BN_free(a);
264 BN_free(b);
265 BN_free(c);
267 return 0;
270 static int
271 test_BN_cmp(void)
273 BIGNUM *a, *b;
275 a = BN_new();
276 b = BN_new();
278 if (!BN_set_word(a, 1))
279 return 1;
280 if (!BN_set_word(b, 1))
281 return 1;
283 if (BN_cmp(a, b) != 0)
284 return 1;
285 if (BN_cmp(b, a) != 0)
286 return 1;
288 if (!BN_set_word(b, 2))
289 return 1;
291 if (BN_cmp(a, b) >= 0)
292 return 1;
293 if (BN_cmp(b, a) <= 0)
294 return 1;
296 BN_set_negative(b, 1);
298 if (BN_cmp(a, b) <= 0)
299 return 1;
300 if (BN_cmp(b, a) >= 0)
301 return 1;
303 BN_free(a);
304 BN_free(b);
306 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD1");
307 BN_hex2bn(&b, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
309 if (BN_cmp(a, b) >= 0)
310 return 1;
311 if (BN_cmp(b, a) <= 0)
312 return 1;
314 BN_set_negative(b, 1);
316 if (BN_cmp(a, b) <= 0)
317 return 1;
318 if (BN_cmp(b, a) >= 0)
319 return 1;
321 BN_free(a);
322 BN_free(b);
323 return 0;
326 static int
327 test_BN_rand(void)
329 BIGNUM *bn;
331 if (RAND_status() != 1)
332 return 0;
334 bn = BN_new();
335 if (bn == NULL)
336 return 1;
338 if (!BN_rand(bn, 1024, 0, 0))
339 return 1;
341 BN_free(bn);
342 return 0;
345 #define testnum 100
346 #define testnum2 10
348 static int
349 test_BN_CTX(void)
351 unsigned int i, j;
352 BIGNUM *bn;
353 BN_CTX *c;
355 if ((c = BN_CTX_new()) == NULL)
356 return 1;
358 for (i = 0; i < testnum; i++) {
359 BN_CTX_start(c);
360 BN_CTX_end(c);
363 for (i = 0; i < testnum; i++)
364 BN_CTX_start(c);
365 for (i = 0; i < testnum; i++)
366 BN_CTX_end(c);
368 for (i = 0; i < testnum; i++) {
369 BN_CTX_start(c);
370 if ((bn = BN_CTX_get(c)) == NULL)
371 return 1;
372 BN_CTX_end(c);
375 for (i = 0; i < testnum; i++) {
376 BN_CTX_start(c);
377 for (j = 0; j < testnum2; j++)
378 if ((bn = BN_CTX_get(c)) == NULL)
379 return 1;
381 for (i = 0; i < testnum; i++)
382 BN_CTX_end(c);
384 BN_CTX_free(c);
385 return 0;
390 main(int argc, char **argv)
392 int ret = 0;
394 ret += test_BN_set_get();
395 ret += test_BN_bit();
396 ret += test_BN_import_export();
397 ret += test_BN_uadd();
398 ret += test_BN_cmp();
399 ret += test_BN_rand();
400 ret += test_BN_CTX();
402 return ret;