2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 #include <sys/types.h>
46 set_get(unsigned long num
)
51 if (!BN_set_word(bn
, num
))
54 if (BN_get_word(bn
) != num
)
61 #define CHECK(x) do { ret += x; } while(0)
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));
87 /* test setting and getting of "word" */
88 if (!BN_set_word(bn
, 1))
90 if (!BN_is_bit_set(bn
, 0))
92 if (!BN_is_bit_set(bn
, 0))
95 if (!BN_set_word(bn
, 2))
97 if (!BN_is_bit_set(bn
, 1))
100 if (!BN_set_word(bn
, 3))
102 if (!BN_is_bit_set(bn
, 0))
104 if (!BN_is_bit_set(bn
, 1))
107 if (!BN_set_word(bn
, 0x100))
109 if (!BN_is_bit_set(bn
, 8))
112 if (!BN_set_word(bn
, 0x1000))
114 if (!BN_is_bit_set(bn
, 12))
117 /* test bitsetting */
118 if (!BN_set_word(bn
, 1))
120 if (!BN_set_bit(bn
, 1))
122 if (BN_get_word(bn
) != 3)
124 if (!BN_clear_bit(bn
, 0))
126 if (BN_get_word(bn
) != 2)
129 /* test bitsetting past end of current end */
131 if (!BN_set_bit(bn
, 12))
133 if (BN_get_word(bn
) != 0x1000)
136 /* test bit and byte counting functions */
137 if (BN_num_bits(bn
) != 13)
139 if (BN_num_bytes(bn
) != 2)
155 { "\x01\x00", 2, 256 }
159 test_BN_import_export(void)
167 for (i
= 0; i
< sizeof(ietests
)/sizeof(ietests
[0]); i
++) {
170 if (!BN_bin2bn((unsigned char*)ietests
[i
].data
, ietests
[i
].len
, bn
))
172 if (BN_get_word(bn
) != ietests
[i
].num
)
174 len
= BN_num_bytes(bn
);
175 if (len
!= ietests
[i
].len
)
182 if (memcmp(p
, ietests
[i
].data
, ietests
[i
].len
) != 0)
206 if (BN_get_word(c
) != 3)
211 if (BN_get_word(c
) != 3)
214 BN_set_word(b
, 0xff);
217 if (BN_get_word(c
) != 0x100)
221 if (BN_get_word(c
) != 0x100)
224 BN_set_word(a
, 0xff);
227 if (BN_get_word(c
) != 0x1fe)
231 if (BN_get_word(c
) != 0x1fe)
238 BN_hex2bn(&a
, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
239 BN_hex2bn(&b
, "84B6C7E8D28ACA1614954DA");
243 if (strcmp(p
, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
251 if (strcmp(p
, "50212A3B611D466434CDC695307D7AB13621B2AC") != 0) {
272 if (!BN_set_word(a
, 1))
274 if (!BN_set_word(b
, 1))
277 if (BN_cmp(a
, b
) != 0)
279 if (BN_cmp(b
, a
) != 0)
282 if (!BN_set_word(b
, 2))
285 if (BN_cmp(a
, b
) >= 0)
287 if (BN_cmp(b
, a
) <= 0)
290 BN_set_negative(b
, 1);
292 if (BN_cmp(a
, b
) <= 0)
294 if (BN_cmp(b
, a
) >= 0)
300 BN_hex2bn(&a
, "50212A3B611D46642C825A16A354CE0FD4D85DD1");
301 BN_hex2bn(&b
, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
303 if (BN_cmp(a
, b
) >= 0)
305 if (BN_cmp(b
, a
) <= 0)
308 BN_set_negative(b
, 1);
310 if (BN_cmp(a
, b
) <= 0)
312 if (BN_cmp(b
, a
) >= 0)
325 if (RAND_status() != 1)
332 if (!BN_rand(bn
, 1024, 0, 0))
349 if ((c
= BN_CTX_new()) == NULL
)
352 for (i
= 0; i
< testnum
; i
++) {
357 for (i
= 0; i
< testnum
; i
++)
359 for (i
= 0; i
< testnum
; i
++)
362 for (i
= 0; i
< testnum
; i
++) {
364 if ((bn
= BN_CTX_get(c
)) == NULL
)
369 for (i
= 0; i
< testnum
; i
++) {
371 for (j
= 0; j
< testnum2
; j
++)
372 if ((bn
= BN_CTX_get(c
)) == NULL
)
375 for (i
= 0; i
< testnum
; i
++)
384 main(int argc
, char **argv
)
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();