comparison demos/test/der_tests.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
1 #include "test.h"
2
3 int der_tests(void)
4 {
5 unsigned long x, y, z, zz;
6 unsigned char buf[2][4096];
7 mp_int a, b, c, d, e, f, g;
8
9 DO(mpi_to_ltc_error(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL)));
10 for (zz = 0; zz < 16; zz++) {
11 for (z = 0; z < 1024; z++) {
12 if (yarrow_read(buf[0], z, &test_yarrow) != z) {
13 printf("Failed to read %lu bytes from yarrow\n", z);
14 return 1;
15 }
16 DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
17 x = sizeof(buf[0]);
18 DO(der_encode_integer(&a, buf[0], &x));
19 y = x;
20 mp_zero(&b);
21 DO(der_decode_integer(buf[0], &y, &b));
22 if (y != x || mp_cmp(&a, &b) != MP_EQ) {
23 printf("%lu: %lu vs %lu\n", z, x, y);
24 #ifdef BN_MP_TORADIX_C
25 mp_todecimal(&a, buf[0]);
26 mp_todecimal(&b, buf[1]);
27 printf("a == %s\nb == %s\n", buf[0], buf[1]);
28 #endif
29 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
30 return 1;
31 }
32 }
33 }
34
35
36 /* test the multi */
37 mp_set(&a, 1);
38 x = sizeof(buf[0]);
39 DO(der_put_multi_integer(buf[0], &x, &a, NULL));
40 y = x;
41 mp_zero(&a);
42 DO(der_get_multi_integer(buf[0], &y, &a, NULL));
43 if (x != y || mp_cmp_d(&a, 1)) {
44 printf("%lu, %lu, %d\n", x, y, mp_cmp_d(&a, 1));
45 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
46 return 1;
47 }
48
49 mp_set(&a, 1);
50 mp_set(&b, 2);
51 x = sizeof(buf[0]);
52 DO(der_put_multi_integer(buf[0], &x, &a, &b, NULL));
53 y = x;
54 mp_zero(&a);
55 mp_zero(&b);
56 DO(der_get_multi_integer(buf[0], &y, &a, &b, NULL));
57 if (x != y || mp_cmp_d(&a, 1) || mp_cmp_d(&b, 2)) {
58 printf("%lu, %lu, %d, %d\n", x, y, mp_cmp_d(&a, 1), mp_cmp_d(&b, 2));
59 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
60 return 1;
61 }
62
63 mp_set(&a, 1);
64 mp_set(&b, 2);
65 mp_set(&c, 3);
66 x = sizeof(buf[0]);
67 DO(der_put_multi_integer(buf[0], &x, &a, &b, &c, NULL));
68 y = x;
69 mp_zero(&a);
70 mp_zero(&b);
71 mp_zero(&c);
72 DO(der_get_multi_integer(buf[0], &y, &a, &b, &c, NULL));
73 if (x != y || mp_cmp_d(&a, 1) || mp_cmp_d(&b, 2) || mp_cmp_d(&c, 3)) {
74 printf("%lu, %lu, %d, %d, %d\n", x, y, mp_cmp_d(&a, 1), mp_cmp_d(&b, 2), mp_cmp_d(&c, 3));
75 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
76 return 1;
77 }
78
79
80 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
81 return 0;
82 }