comparison libtomcrypt/testprof/der_tests.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
1 #include <tomcrypt_test.h>
2
3 #ifndef LTC_DER
4
5 int der_tests(void)
6 {
7 fprintf(stderr, "NOP");
8 return 0;
9 }
10
11 #else
12
13 static int der_choice_test(void)
14 {
15 ltc_asn1_list types[7], host[1];
16 unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256];
17 unsigned long integer, oidbuf[10], outlen, inlen, x, y;
18 mp_int mpinteger;
19 ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
20
21 /* setup variables */
22 for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; }
23 for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; }
24 for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; }
25 for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; }
26 integer = 1;
27 for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; }
28 DO(mpi_to_ltc_error(mp_init(&mpinteger)));
29
30 for (x = 0; x < 14; x++) {
31 /* setup list */
32 LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
33 LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
34 LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
35 LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
36 if (x > 7) {
37 LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
38 } else {
39 LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, &mpinteger, 1);
40 }
41 LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
42 LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
43
44 LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7);
45
46
47 /* encode */
48 outlen = sizeof(outbuf);
49 DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen));
50
51 /* decode it */
52 inlen = outlen;
53 DO(der_decode_sequence(outbuf, inlen, &host, 1));
54
55 for (y = 0; y < 7; y++) {
56 if (types[y].used && y != (x>6?x-7:x)) {
57 fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y, x);
58 return 1;
59 }
60 if (!types[y].used && y == (x>6?x-7:x)) {
61 fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y, x);
62 return 1;
63 }
64 }
65 }
66 mp_clear(&mpinteger);
67 return 0;
68 }
69
70
71 int der_tests(void)
72 {
73 unsigned long x, y, z, zz, oid[2][32];
74 unsigned char buf[3][2048];
75 mp_int a, b, c, d, e, f, g;
76
77 static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
78 static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 };
79
80 static const unsigned char rsa_ia5[] = "[email protected]";
81 static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
82 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
83
84 static const unsigned char rsa_printable[] = "Test User 1";
85 static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
86 0x73, 0x65, 0x72, 0x20, 0x31 };
87
88 static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
89 static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
90 ltc_utctime tmp_time;
91
92 static const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 };
93 static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
94
95 DO(mpi_to_ltc_error(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL)));
96 for (zz = 0; zz < 16; zz++) {
97 for (z = 0; z < 1024; z++) {
98 if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
99 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
100 return 1;
101 }
102 DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
103 if (mp_iszero(&a) == MP_NO) { a.sign = buf[0][0] & 1 ? MP_ZPOS : MP_NEG; }
104 x = sizeof(buf[0]);
105 DO(der_encode_integer(&a, buf[0], &x));
106 DO(der_length_integer(&a, &y));
107 if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
108 mp_zero(&b);
109 DO(der_decode_integer(buf[0], y, &b));
110 if (y != x || mp_cmp(&a, &b) != MP_EQ) {
111 fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
112 #ifdef BN_MP_TORADIX_C
113 mp_todecimal(&a, buf[0]);
114 mp_todecimal(&b, buf[1]);
115 fprintf(stderr, "a == %s\nb == %s\n", buf[0], buf[1]);
116 #endif
117 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
118 return 1;
119 }
120 }
121 }
122
123 /* test short integer */
124 for (zz = 0; zz < 256; zz++) {
125 for (z = 1; z < 4; z++) {
126 if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
127 fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
128 return 1;
129 }
130 /* encode with normal */
131 DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
132
133 x = sizeof(buf[0]);
134 DO(der_encode_integer(&a, buf[0], &x));
135
136 /* encode with short */
137 y = sizeof(buf[1]);
138 DO(der_encode_short_integer(mp_get_int(&a), buf[1], &y));
139 if (x != y || memcmp(buf[0], buf[1], x)) {
140 fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
141 for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
142 for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
143 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
144 return 1;
145 }
146
147 /* decode it */
148 x = 0;
149 DO(der_decode_short_integer(buf[1], y, &x));
150 if (x != mp_get_int(&a)) {
151 fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(&a));
152 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
153 return 1;
154 }
155 }
156 }
157 mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
158
159
160 /* Test bit string */
161 for (zz = 1; zz < 1536; zz++) {
162 yarrow_read(buf[0], zz, &yarrow_prng);
163 for (z = 0; z < zz; z++) {
164 buf[0][z] &= 0x01;
165 }
166 x = sizeof(buf[1]);
167 DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
168 DO(der_length_bit_string(zz, &y));
169 if (y != x) {
170 fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
171 return 1;
172 }
173
174 y = sizeof(buf[2]);
175 DO(der_decode_bit_string(buf[1], x, buf[2], &y));
176 if (y != zz || memcmp(buf[0], buf[2], zz)) {
177 fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
178 return 1;
179 }
180 }
181
182 /* Test octet string */
183 for (zz = 1; zz < 1536; zz++) {
184 yarrow_read(buf[0], zz, &yarrow_prng);
185 x = sizeof(buf[1]);
186 DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
187 DO(der_length_octet_string(zz, &y));
188 if (y != x) {
189 fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
190 return 1;
191 }
192 y = sizeof(buf[2]);
193 DO(der_decode_octet_string(buf[1], x, buf[2], &y));
194 if (y != zz || memcmp(buf[0], buf[2], zz)) {
195 fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
196 return 1;
197 }
198 }
199
200 /* test OID */
201 x = sizeof(buf[0]);
202 DO(der_encode_object_identifier(rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
203 if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
204 fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
205 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
206 fprintf(stderr, "\n");
207 return 1;
208 }
209
210 y = sizeof(oid[0])/sizeof(oid[0][0]);
211 DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
212 if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
213 fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
214 for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
215 fprintf(stderr, "\n");
216 return 1;
217 }
218
219 /* do random strings */
220 for (zz = 0; zz < 5000; zz++) {
221 /* pick a random number of words */
222 yarrow_read(buf[0], 4, &yarrow_prng);
223 LOAD32L(z, buf[0]);
224 z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
225
226 /* fill them in */
227 oid[0][0] = buf[0][0] % 3;
228 oid[0][1] = buf[0][1] % 40;
229
230 for (y = 2; y < z; y++) {
231 yarrow_read(buf[0], 4, &yarrow_prng);
232 LOAD32L(oid[0][y], buf[0]);
233 }
234
235 /* encode it */
236 x = sizeof(buf[0]);
237 DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
238 DO(der_length_object_identifier(oid[0], z, &y));
239 if (x != y) {
240 fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
241 for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
242 return 1;
243 }
244
245 /* decode it */
246 y = sizeof(oid[0])/sizeof(oid[0][0]);
247 DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
248 if (y != z) {
249 fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
250 return 1;
251 }
252 if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
253 fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
254 for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]); fprintf(stderr, "\n\n Got \n\n");
255 for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
256 return 1;
257 }
258 }
259
260 /* IA5 string */
261 x = sizeof(buf[0]);
262 DO(der_encode_ia5_string(rsa_ia5, strlen(rsa_ia5), buf[0], &x));
263 if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
264 fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
265 return 1;
266 }
267 DO(der_length_ia5_string(rsa_ia5, strlen(rsa_ia5), &y));
268 if (y != x) {
269 fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
270 return 1;
271 }
272 y = sizeof(buf[1]);
273 DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
274 if (y != strlen(rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen(rsa_ia5))) {
275 fprintf(stderr, "DER IA5 failed test vector\n");
276 return 1;
277 }
278
279 /* Printable string */
280 x = sizeof(buf[0]);
281 DO(der_encode_printable_string(rsa_printable, strlen(rsa_printable), buf[0], &x));
282 if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
283 fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
284 return 1;
285 }
286 DO(der_length_printable_string(rsa_printable, strlen(rsa_printable), &y));
287 if (y != x) {
288 fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
289 return 1;
290 }
291 y = sizeof(buf[1]);
292 DO(der_decode_printable_string(buf[0], x, buf[1], &y));
293 if (y != strlen(rsa_printable) || memcmp(buf[1], rsa_printable, strlen(rsa_printable))) {
294 fprintf(stderr, "DER printable failed test vector\n");
295 return 1;
296 }
297
298 /* Test UTC time */
299 x = sizeof(buf[0]);
300 DO(der_encode_utctime(&rsa_time1, buf[0], &x));
301 if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
302 fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
303 fprintf(stderr, "\n\n");
304 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
305
306 return 1;
307 }
308 DO(der_length_utctime(&rsa_time1, &y));
309 if (y != x) {
310 fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
311 return 1;
312 }
313 DO(der_decode_utctime(buf[0], &y, &tmp_time));
314 if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
315 fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
316 fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
317 tmp_time.YY,
318 tmp_time.MM,
319 tmp_time.DD,
320 tmp_time.hh,
321 tmp_time.mm,
322 tmp_time.ss,
323 tmp_time.off_dir,
324 tmp_time.off_mm,
325 tmp_time.off_hh);
326 return 1;
327 }
328
329 x = sizeof(buf[0]);
330 DO(der_encode_utctime(&rsa_time2, buf[0], &x));
331 if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
332 fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
333 fprintf(stderr, "\n\n");
334 for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
335
336 return 1;
337 }
338 DO(der_length_utctime(&rsa_time2, &y));
339 if (y != x) {
340 fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
341 return 1;
342 }
343 DO(der_decode_utctime(buf[0], &y, &tmp_time));
344 if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
345 fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
346 fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
347 tmp_time.YY,
348 tmp_time.MM,
349 tmp_time.DD,
350 tmp_time.hh,
351 tmp_time.mm,
352 tmp_time.ss,
353 tmp_time.off_dir,
354 tmp_time.off_mm,
355 tmp_time.off_hh);
356
357
358 return 1;
359 }
360
361
362
363 return der_choice_test();
364 }
365
366 #endif
367
368 /* $Source: /cvs/libtom/libtomcrypt/testprof/der_tests.c,v $ */
369 /* $Revision: 1.25 $ */
370 /* $Date: 2005/06/20 20:37:45 $ */