No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hcrypto / test_bn.c
blob66f65311c3ca99bdf0af30a41d3a58bfa80b99d3
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("$Heimdal: test_bn.c 21047 2007-06-10 19:11:54Z lha $"
40 "$NetBSD$");
41 #endif
43 #include <sys/types.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
49 #include <bn.h>
50 #include <rand.h>
52 static int
53 set_get(unsigned long num)
55 BIGNUM *bn;
57 bn = BN_new();
58 if (!BN_set_word(bn, num))
59 return 1;
61 if (BN_get_word(bn) != num)
62 return 1;
64 BN_free(bn);
65 return 0;
68 #define CHECK(x) do { ret += x; } while(0)
70 static int
71 test_BN_set_get(void)
73 int ret = 0;
74 CHECK(set_get(0));
75 CHECK(set_get(1));
76 CHECK(set_get(0xff));
77 CHECK(set_get(0x1ff));
78 CHECK(set_get(0xffff));
79 CHECK(set_get(0xf000));
80 CHECK(set_get(ULONG_MAX / 2));
81 CHECK(set_get(ULONG_MAX - 1));
83 return ret;
86 static int
87 test_BN_bit(void)
89 BIGNUM *bn;
90 int ret = 0;
92 bn = BN_new();
94 /* test setting and getting of "word" */
95 if (!BN_set_word(bn, 1))
96 return 1;
97 if (!BN_is_bit_set(bn, 0))
98 ret += 1;
99 if (!BN_is_bit_set(bn, 0))
100 ret += 1;
102 if (!BN_set_word(bn, 2))
103 return 1;
104 if (!BN_is_bit_set(bn, 1))
105 ret += 1;
107 if (!BN_set_word(bn, 3))
108 return 1;
109 if (!BN_is_bit_set(bn, 0))
110 ret += 1;
111 if (!BN_is_bit_set(bn, 1))
112 ret += 1;
114 if (!BN_set_word(bn, 0x100))
115 return 1;
116 if (!BN_is_bit_set(bn, 8))
117 ret += 1;
119 if (!BN_set_word(bn, 0x1000))
120 return 1;
121 if (!BN_is_bit_set(bn, 12))
122 ret += 1;
124 /* test bitsetting */
125 if (!BN_set_word(bn, 1))
126 return 1;
127 if (!BN_set_bit(bn, 1))
128 return 1;
129 if (BN_get_word(bn) != 3)
130 return 1;
131 if (!BN_clear_bit(bn, 0))
132 return 1;
133 if (BN_get_word(bn) != 2)
134 return 1;
136 /* test bitsetting past end of current end */
137 BN_clear(bn);
138 if (!BN_set_bit(bn, 12))
139 return 1;
140 if (BN_get_word(bn) != 0x1000)
141 return 1;
143 /* test bit and byte counting functions */
144 if (BN_num_bits(bn) != 13)
145 return 1;
146 if (BN_num_bytes(bn) != 2)
147 return 1;
149 BN_free(bn);
150 return ret;
153 struct ietest {
154 char *data;
155 size_t len;
156 unsigned long num;
157 } ietests[] = {
158 { "", 0, 0 },
159 { "\x01", 1, 1 },
160 { "\x02", 1, 2 },
161 { "\xf2", 1, 0xf2 },
162 { "\x01\x00", 2, 256 }
165 static int
166 test_BN_import_export(void)
168 BIGNUM *bn;
169 int ret = 0;
170 int i;
172 bn = BN_new();
174 for (i = 0; i < sizeof(ietests)/sizeof(ietests[0]); i++) {
175 size_t len;
176 unsigned char *p;
177 if (!BN_bin2bn((unsigned char*)ietests[i].data, ietests[i].len, bn))
178 return 1;
179 if (BN_get_word(bn) != ietests[i].num)
180 return 1;
181 len = BN_num_bytes(bn);
182 if (len != ietests[i].len)
183 return 1;
184 p = malloc(len + 1);
185 p[len] = 0xf4;
186 BN_bn2bin(bn, p);
187 if (p[len] != 0xf4)
188 return 1;
189 if (memcmp(p, ietests[i].data, ietests[i].len) != 0)
190 return 1;
191 free(p);
194 BN_free(bn);
195 return ret;
198 static int
199 test_BN_uadd(void)
201 BIGNUM *a, *b, *c;
202 char *p;
204 a = BN_new();
205 b = BN_new();
206 c = BN_new();
208 BN_set_word(a, 1);
209 BN_set_word(b, 2);
211 BN_uadd(c, a, b);
213 if (BN_get_word(c) != 3)
214 return 1;
216 BN_uadd(c, b, a);
218 if (BN_get_word(c) != 3)
219 return 1;
221 BN_set_word(b, 0xff);
223 BN_uadd(c, a, b);
224 if (BN_get_word(c) != 0x100)
225 return 1;
227 BN_uadd(c, b, a);
228 if (BN_get_word(c) != 0x100)
229 return 1;
231 BN_set_word(a, 0xff);
233 BN_uadd(c, a, b);
234 if (BN_get_word(c) != 0x1fe)
235 return 1;
237 BN_uadd(c, b, a);
238 if (BN_get_word(c) != 0x1fe)
239 return 1;
242 BN_free(a);
243 BN_free(b);
245 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
246 BN_hex2bn(&b, "84B6C7E8D28ACA1614954DA");
248 BN_uadd(c, b, a);
249 p = BN_bn2hex(c);
250 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
251 free(p);
252 return 1;
254 free(p);
256 BN_uadd(c, a, b);
257 p = BN_bn2hex(c);
258 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
259 free(p);
260 return 1;
262 free(p);
264 BN_free(a);
265 BN_free(b);
266 BN_free(c);
268 return 0;
271 static int
272 test_BN_cmp(void)
274 BIGNUM *a, *b;
276 a = BN_new();
277 b = BN_new();
279 if (!BN_set_word(a, 1))
280 return 1;
281 if (!BN_set_word(b, 1))
282 return 1;
284 if (BN_cmp(a, b) != 0)
285 return 1;
286 if (BN_cmp(b, a) != 0)
287 return 1;
289 if (!BN_set_word(b, 2))
290 return 1;
292 if (BN_cmp(a, b) >= 0)
293 return 1;
294 if (BN_cmp(b, a) <= 0)
295 return 1;
297 BN_set_negative(b, 1);
299 if (BN_cmp(a, b) <= 0)
300 return 1;
301 if (BN_cmp(b, a) >= 0)
302 return 1;
304 BN_free(a);
305 BN_free(b);
307 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD1");
308 BN_hex2bn(&b, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
310 if (BN_cmp(a, b) >= 0)
311 return 1;
312 if (BN_cmp(b, a) <= 0)
313 return 1;
315 BN_set_negative(b, 1);
317 if (BN_cmp(a, b) <= 0)
318 return 1;
319 if (BN_cmp(b, a) >= 0)
320 return 1;
322 BN_free(a);
323 BN_free(b);
324 return 0;
327 static int
328 test_BN_rand(void)
330 BIGNUM *bn;
332 if (RAND_status() != 1)
333 return 0;
335 bn = BN_new();
336 if (bn == NULL)
337 return 1;
339 if (!BN_rand(bn, 1024, 0, 0))
340 return 1;
342 BN_free(bn);
343 return 0;
347 main(int argc, char **argv)
349 int ret = 0;
351 ret += test_BN_set_get();
352 ret += test_BN_bit();
353 ret += test_BN_import_export();
354 ret += test_BN_uadd();
355 ret += test_BN_cmp();
356 ret += test_BN_rand();
358 return ret;