1 /* $NetBSD: test_bn.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
4 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
48 set_get(unsigned long num
)
53 if (!BN_set_word(bn
, num
))
56 if (BN_get_word(bn
) != num
)
63 #define CHECK(x) do { ret += x; } while(0)
72 CHECK(set_get(0x1ff));
73 CHECK(set_get(0xffff));
74 CHECK(set_get(0xf000));
75 CHECK(set_get(ULONG_MAX
/ 2));
76 CHECK(set_get(ULONG_MAX
- 1));
89 /* test setting and getting of "word" */
90 if (!BN_set_word(bn
, 1))
92 if (!BN_is_bit_set(bn
, 0))
94 if (!BN_is_bit_set(bn
, 0))
97 if (!BN_set_word(bn
, 2))
99 if (!BN_is_bit_set(bn
, 1))
102 if (!BN_set_word(bn
, 3))
104 if (!BN_is_bit_set(bn
, 0))
106 if (!BN_is_bit_set(bn
, 1))
109 if (!BN_set_word(bn
, 0x100))
111 if (!BN_is_bit_set(bn
, 8))
114 if (!BN_set_word(bn
, 0x1000))
116 if (!BN_is_bit_set(bn
, 12))
119 /* test bitsetting */
120 if (!BN_set_word(bn
, 1))
122 if (!BN_set_bit(bn
, 1))
124 if (BN_get_word(bn
) != 3)
126 if (!BN_clear_bit(bn
, 0))
128 if (BN_get_word(bn
) != 2)
131 /* test bitsetting past end of current end */
133 if (!BN_set_bit(bn
, 12))
135 if (BN_get_word(bn
) != 0x1000)
138 /* test bit and byte counting functions */
139 if (BN_num_bits(bn
) != 13)
141 if (BN_num_bytes(bn
) != 2)
157 { "\x01\x00", 2, 256 }
161 test_BN_import_export(void)
169 for (i
= 0; i
< sizeof(ietests
)/sizeof(ietests
[0]); i
++) {
172 if (!BN_bin2bn((unsigned char*)ietests
[i
].data
, ietests
[i
].len
, bn
))
174 if (BN_get_word(bn
) != ietests
[i
].num
)
176 len
= BN_num_bytes(bn
);
177 if (len
!= ietests
[i
].len
)
184 if (memcmp(p
, ietests
[i
].data
, ietests
[i
].len
) != 0)
208 if (BN_get_word(c
) != 3)
213 if (BN_get_word(c
) != 3)
216 BN_set_word(b
, 0xff);
219 if (BN_get_word(c
) != 0x100)
223 if (BN_get_word(c
) != 0x100)
226 BN_set_word(a
, 0xff);
229 if (BN_get_word(c
) != 0x1fe)
233 if (BN_get_word(c
) != 0x1fe)
240 BN_hex2bn(&a
, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
241 BN_hex2bn(&b
, "84B6C7E8D28ACA1614954DA");
245 if (strcmp(p
, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
253 if (strcmp(p
, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
274 if (!BN_set_word(a
, 1))
276 if (!BN_set_word(b
, 1))
279 if (BN_cmp(a
, b
) != 0)
281 if (BN_cmp(b
, a
) != 0)
284 if (!BN_set_word(b
, 2))
287 if (BN_cmp(a
, b
) >= 0)
289 if (BN_cmp(b
, a
) <= 0)
292 BN_set_negative(b
, 1);
294 if (BN_cmp(a
, b
) <= 0)
296 if (BN_cmp(b
, a
) >= 0)
302 BN_hex2bn(&a
, "50212A3B611D46642C825A16A354CE0FD4D85DD1");
303 BN_hex2bn(&b
, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
305 if (BN_cmp(a
, b
) >= 0)
307 if (BN_cmp(b
, a
) <= 0)
310 BN_set_negative(b
, 1);
312 if (BN_cmp(a
, b
) <= 0)
314 if (BN_cmp(b
, a
) >= 0)
327 if (RAND_status() != 1)
334 if (!BN_rand(bn
, 1024, 0, 0))
351 if ((c
= BN_CTX_new()) == NULL
)
354 for (i
= 0; i
< testnum
; i
++) {
359 for (i
= 0; i
< testnum
; i
++)
361 for (i
= 0; i
< testnum
; i
++)
364 for (i
= 0; i
< testnum
; i
++) {
366 if ((bn
= BN_CTX_get(c
)) == NULL
)
371 for (i
= 0; i
< testnum
; i
++) {
373 for (j
= 0; j
< testnum2
; j
++)
374 if ((bn
= BN_CTX_get(c
)) == NULL
)
377 for (i
= 0; i
< testnum
; i
++)
386 main(int argc
, char **argv
)
390 ret
+= test_BN_set_get();
391 ret
+= test_BN_bit();
392 ret
+= test_BN_import_export();
393 ret
+= test_BN_uadd();
394 ret
+= test_BN_cmp();
395 ret
+= test_BN_rand();
396 ret
+= test_BN_CTX();