source: heimdal/trunk/lib/hcrypto/test_bn.c

Last change on this file was 1, checked in by Paul Smedley, 10 years ago

Initial commit of Heimdal 1.5.3

File size: 7.4 KB
Line 
1/*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34#include <config.h>
35
36#include <sys/types.h>
37#include <limits.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42#include <bn.h>
43#include <rand.h>
44
45static int
46set_get(unsigned long num)
47{
48 BIGNUM *bn;
49
50 bn = BN_new();
51 if (!BN_set_word(bn, num))
52 return 1;
53
54 if (BN_get_word(bn) != num)
55 return 1;
56
57 BN_free(bn);
58 return 0;
59}
60
61#define CHECK(x) do { ret += x; } while(0)
62
63static int
64test_BN_set_get(void)
65{
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));
75
76 return ret;
77}
78
79static int
80test_BN_bit(void)
81{
82 BIGNUM *bn;
83 int ret = 0;
84
85 bn = BN_new();
86
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;
94
95 if (!BN_set_word(bn, 2))
96 return 1;
97 if (!BN_is_bit_set(bn, 1))
98 ret += 1;
99
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;
106
107 if (!BN_set_word(bn, 0x100))
108 return 1;
109 if (!BN_is_bit_set(bn, 8))
110 ret += 1;
111
112 if (!BN_set_word(bn, 0x1000))
113 return 1;
114 if (!BN_is_bit_set(bn, 12))
115 ret += 1;
116
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;
128
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;
135
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;
141
142 BN_free(bn);
143 return ret;
144}
145
146struct 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 }
156};
157
158static int
159test_BN_import_export(void)
160{
161 BIGNUM *bn;
162 int ret = 0;
163 int i;
164
165 bn = BN_new();
166
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);
185 }
186
187 BN_free(bn);
188 return ret;
189}
190
191static int
192test_BN_uadd(void)
193{
194 BIGNUM *a, *b, *c;
195 char *p;
196
197 a = BN_new();
198 b = BN_new();
199 c = BN_new();
200
201 BN_set_word(a, 1);
202 BN_set_word(b, 2);
203
204 BN_uadd(c, a, b);
205
206 if (BN_get_word(c) != 3)
207 return 1;
208
209 BN_uadd(c, b, a);
210
211 if (BN_get_word(c) != 3)
212 return 1;
213
214 BN_set_word(b, 0xff);
215
216 BN_uadd(c, a, b);
217 if (BN_get_word(c) != 0x100)
218 return 1;
219
220 BN_uadd(c, b, a);
221 if (BN_get_word(c) != 0x100)
222 return 1;
223
224 BN_set_word(a, 0xff);
225
226 BN_uadd(c, a, b);
227 if (BN_get_word(c) != 0x1fe)
228 return 1;
229
230 BN_uadd(c, b, a);
231 if (BN_get_word(c) != 0x1fe)
232 return 1;
233
234
235 BN_free(a);
236 BN_free(b);
237
238 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
239 BN_hex2bn(&b, "84B6C7E8D28ACA1614954DA");
240
241 BN_uadd(c, b, a);
242 p = BN_bn2hex(c);
243 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
244 free(p);
245 return 1;
246 }
247 free(p);
248
249 BN_uadd(c, a, b);
250 p = BN_bn2hex(c);
251 if (strcmp(p, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
252 free(p);
253 return 1;
254 }
255 free(p);
256
257 BN_free(a);
258 BN_free(b);
259 BN_free(c);
260
261 return 0;
262}
263
264static int
265test_BN_cmp(void)
266{
267 BIGNUM *a, *b;
268
269 a = BN_new();
270 b = BN_new();
271
272 if (!BN_set_word(a, 1))
273 return 1;
274 if (!BN_set_word(b, 1))
275 return 1;
276
277 if (BN_cmp(a, b) != 0)
278 return 1;
279 if (BN_cmp(b, a) != 0)
280 return 1;
281
282 if (!BN_set_word(b, 2))
283 return 1;
284
285 if (BN_cmp(a, b) >= 0)
286 return 1;
287 if (BN_cmp(b, a) <= 0)
288 return 1;
289
290 BN_set_negative(b, 1);
291
292 if (BN_cmp(a, b) <= 0)
293 return 1;
294 if (BN_cmp(b, a) >= 0)
295 return 1;
296
297 BN_free(a);
298 BN_free(b);
299
300 BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD1");
301 BN_hex2bn(&b, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
302
303 if (BN_cmp(a, b) >= 0)
304 return 1;
305 if (BN_cmp(b, a) <= 0)
306 return 1;
307
308 BN_set_negative(b, 1);
309
310 if (BN_cmp(a, b) <= 0)
311 return 1;
312 if (BN_cmp(b, a) >= 0)
313 return 1;
314
315 BN_free(a);
316 BN_free(b);
317 return 0;
318}
319
320static int
321test_BN_rand(void)
322{
323 BIGNUM *bn;
324
325 if (RAND_status() != 1)
326 return 0;
327
328 bn = BN_new();
329 if (bn == NULL)
330 return 1;
331
332 if (!BN_rand(bn, 1024, 0, 0))
333 return 1;
334
335 BN_free(bn);
336 return 0;
337}
338
339#define testnum 100
340#define testnum2 10
341
342static int
343test_BN_CTX(void)
344{
345 unsigned int i, j;
346 BIGNUM *bn;
347 BN_CTX *c;
348
349 if ((c = BN_CTX_new()) == NULL)
350 return 1;
351
352 for (i = 0; i < testnum; i++) {
353 BN_CTX_start(c);
354 BN_CTX_end(c);
355 }
356
357 for (i = 0; i < testnum; i++)
358 BN_CTX_start(c);
359 for (i = 0; i < testnum; i++)
360 BN_CTX_end(c);
361
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);
367 }
368
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;
374 }
375 for (i = 0; i < testnum; i++)
376 BN_CTX_end(c);
377
378 BN_CTX_free(c);
379 return 0;
380}
381
382
383int
384main(int argc, char **argv)
385{
386 int ret = 0;
387
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();
395
396 return ret;
397}
Note: See TracBrowser for help on using the repository browser.