1 #include <tomcrypt_test.h>
2 #if defined(GMP_DESC) || defined(USE_GMP)
10 fprintf(stderr
, "NOP");
16 static void der_set_test(void)
18 ltc_asn1_list list
[10];
19 static const unsigned char oct_str
[] = { 1, 2, 3, 4 };
20 static const unsigned char bin_str
[] = { 1, 0, 0, 1 };
21 static const unsigned long int_val
= 12345678UL;
23 unsigned char strs
[10][10], outbuf
[128];
24 unsigned long x
, val
, outlen
;
27 /* make structure and encode it */
28 LTC_SET_ASN1(list
, 0, LTC_ASN1_OCTET_STRING
, oct_str
, sizeof(oct_str
));
29 LTC_SET_ASN1(list
, 1, LTC_ASN1_BIT_STRING
, bin_str
, sizeof(bin_str
));
30 LTC_SET_ASN1(list
, 2, LTC_ASN1_SHORT_INTEGER
, &int_val
, 1);
33 outlen
= sizeof(outbuf
);
34 if ((err
= der_encode_set(list
, 3, outbuf
, &outlen
)) != CRYPT_OK
) {
35 fprintf(stderr
, "error encoding set: %s\n", error_to_string(err
));
40 /* first let's test the set_decoder out of order to see what happens, we should get all the fields we expect even though they're in a diff order */
41 LTC_SET_ASN1(list
, 0, LTC_ASN1_BIT_STRING
, strs
[1], sizeof(strs
[1]));
42 LTC_SET_ASN1(list
, 1, LTC_ASN1_SHORT_INTEGER
, &val
, 1);
43 LTC_SET_ASN1(list
, 2, LTC_ASN1_OCTET_STRING
, strs
[0], sizeof(strs
[0]));
45 if ((err
= der_decode_set(outbuf
, outlen
, list
, 3)) != CRYPT_OK
) {
46 fprintf(stderr
, "error decoding set using der_decode_set: %s\n", error_to_string(err
));
50 /* now compare the items */
51 if (memcmp(strs
[0], oct_str
, sizeof(oct_str
))) {
52 fprintf(stderr
, "error decoding set using der_decode_set (oct_str is wrong):\n");
56 if (memcmp(strs
[1], bin_str
, sizeof(bin_str
))) {
57 fprintf(stderr
, "error decoding set using der_decode_set (bin_str is wrong):\n");
62 fprintf(stderr
, "error decoding set using der_decode_set (int_val is wrong):\n");
66 strcpy((char*)strs
[0], "one");
67 strcpy((char*)strs
[1], "one2");
68 strcpy((char*)strs
[2], "two");
69 strcpy((char*)strs
[3], "aaa");
70 strcpy((char*)strs
[4], "aaaa");
71 strcpy((char*)strs
[5], "aab");
72 strcpy((char*)strs
[6], "aaab");
73 strcpy((char*)strs
[7], "bbb");
74 strcpy((char*)strs
[8], "bbba");
75 strcpy((char*)strs
[9], "bbbb");
77 for (x
= 0; x
< 10; x
++) {
78 LTC_SET_ASN1(list
, x
, LTC_ASN1_PRINTABLE_STRING
, strs
[x
], strlen((char*)strs
[x
]));
81 outlen
= sizeof(outbuf
);
82 if ((err
= der_encode_setof(list
, 10, outbuf
, &outlen
)) != CRYPT_OK
) {
83 fprintf(stderr
, "error encoding SET OF: %s\n", error_to_string(err
));
87 for (x
= 0; x
< 10; x
++) {
88 LTC_SET_ASN1(list
, x
, LTC_ASN1_PRINTABLE_STRING
, strs
[x
], sizeof(strs
[x
]) - 1);
90 XMEMSET(strs
, 0, sizeof(strs
));
92 if ((err
= der_decode_set(outbuf
, outlen
, list
, 10)) != CRYPT_OK
) {
93 fprintf(stderr
, "error decoding SET OF: %s\n", error_to_string(err
));
98 for (x
= 1; x
< 10; x
++) {
99 if (!(strlen((char*)strs
[x
-1]) <= strlen((char*)strs
[x
])) && strcmp((char*)strs
[x
-1], (char*)strs
[x
]) >= 0) {
100 fprintf(stderr
, "error SET OF order at %lu is wrong\n", x
);
111 PRINTABLE "printable"
115 UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
117 OCTET STRING { 1, 2, 3, 4 }
118 BIT STRING { 1, 0, 0, 1 }
120 OID { 1, 2, 840, 113549 }
123 PRINTABLE "333" // WILL GET SORTED
132 static void der_flexi_test(void)
134 static const char printable_str
[] = "printable";
135 static const char set1_str
[] = "333";
136 static const char set2_str
[] = "222";
137 static const char ia5_str
[] = "ia5";
138 static const unsigned long int_val
= 12345678UL;
139 static const ltc_utctime utctime
= { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
140 static const unsigned char oct_str
[] = { 1, 2, 3, 4 };
141 static const unsigned char bit_str
[] = { 1, 0, 0, 1 };
142 static const unsigned long oid_str
[] = { 1, 2, 840, 113549 };
144 unsigned char encode_buf
[192];
145 unsigned long encode_buf_len
, decode_len
;
148 ltc_asn1_list static_list
[5][3], *decoded_list
, *l
;
151 LTC_SET_ASN1(static_list
[0], 0, LTC_ASN1_PRINTABLE_STRING
, (void *)printable_str
, strlen(printable_str
));
152 LTC_SET_ASN1(static_list
[0], 1, LTC_ASN1_IA5_STRING
, (void *)ia5_str
, strlen(ia5_str
));
153 LTC_SET_ASN1(static_list
[0], 2, LTC_ASN1_SEQUENCE
, static_list
[1], 3);
155 LTC_SET_ASN1(static_list
[1], 0, LTC_ASN1_SHORT_INTEGER
, (void *)&int_val
, 1);
156 LTC_SET_ASN1(static_list
[1], 1, LTC_ASN1_UTCTIME
, (void *)&utctime
, 1);
157 LTC_SET_ASN1(static_list
[1], 2, LTC_ASN1_SEQUENCE
, static_list
[2], 3);
159 LTC_SET_ASN1(static_list
[2], 0, LTC_ASN1_OCTET_STRING
, (void *)oct_str
, 4);
160 LTC_SET_ASN1(static_list
[2], 1, LTC_ASN1_BIT_STRING
, (void *)bit_str
, 4);
161 LTC_SET_ASN1(static_list
[2], 2, LTC_ASN1_SEQUENCE
, static_list
[3], 3);
163 LTC_SET_ASN1(static_list
[3], 0, LTC_ASN1_OBJECT_IDENTIFIER
,(void *)oid_str
, 4);
164 LTC_SET_ASN1(static_list
[3], 1, LTC_ASN1_NULL
, NULL
, 0);
165 LTC_SET_ASN1(static_list
[3], 2, LTC_ASN1_SETOF
, static_list
[4], 2);
167 LTC_SET_ASN1(static_list
[4], 0, LTC_ASN1_PRINTABLE_STRING
, set1_str
, strlen(set1_str
));
168 LTC_SET_ASN1(static_list
[4], 1, LTC_ASN1_PRINTABLE_STRING
, set2_str
, strlen(set2_str
));
171 encode_buf_len
= sizeof(encode_buf
);
172 if ((err
= der_encode_sequence(&static_list
[0][0], 3, encode_buf
, &encode_buf_len
)) != CRYPT_OK
) {
173 fprintf(stderr
, "Encoding static_list: %s\n", error_to_string(err
));
180 f
= fopen("t.bin", "wb");
181 fwrite(encode_buf
, 1, encode_buf_len
, f
);
186 /* decode with flexi */
187 decode_len
= encode_buf_len
;
188 if ((err
= der_decode_sequence_flexi(encode_buf
, &decode_len
, &decoded_list
)) != CRYPT_OK
) {
189 fprintf(stderr
, "decoding static_list: %s\n", error_to_string(err
));
193 if (decode_len
!= encode_buf_len
) {
194 fprintf(stderr
, "Decode len of %lu does not match encode len of %lu \n", decode_len
, encode_buf_len
);
198 /* we expect l->next to be NULL and l->child to not be */
200 if (l
->next
!= NULL
|| l
->child
== NULL
) {
201 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
205 /* we expect a SEQUENCE */
206 if (l
->type
!= LTC_ASN1_SEQUENCE
) {
207 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
212 /* PRINTABLE STRING */
213 /* we expect printable_str */
214 if (l
->next
== NULL
|| l
->child
!= NULL
) {
215 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
219 if (l
->type
!= LTC_ASN1_PRINTABLE_STRING
) {
220 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
224 if (l
->size
!= strlen(printable_str
) || memcmp(printable_str
, l
->data
, l
->size
)) {
225 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
233 /* we expect ia5_str */
234 if (l
->next
== NULL
|| l
->child
!= NULL
) {
235 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
239 if (l
->type
!= LTC_ASN1_IA5_STRING
) {
240 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
244 if (l
->size
!= strlen(ia5_str
) || memcmp(ia5_str
, l
->data
, l
->size
)) {
245 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
252 /* expect child anve move down */
254 if (l
->next
!= NULL
|| l
->child
== NULL
) {
255 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
259 if (l
->type
!= LTC_ASN1_SEQUENCE
) {
260 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
268 if (l
->next
== NULL
|| l
->child
!= NULL
) {
269 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
273 if (l
->type
!= LTC_ASN1_INTEGER
) {
274 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
278 if (mp_cmp_d(l
->data
, 12345678UL) != LTC_MP_EQ
) {
279 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
288 if (l
->next
== NULL
|| l
->child
!= NULL
) {
289 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
293 if (l
->type
!= LTC_ASN1_UTCTIME
) {
294 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
298 if (memcmp(l
->data
, &utctime
, sizeof(utctime
))) {
299 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
306 /* expect child anve move down */
308 if (l
->next
!= NULL
|| l
->child
== NULL
) {
309 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
313 if (l
->type
!= LTC_ASN1_SEQUENCE
) {
314 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
321 /* we expect oct_str */
322 if (l
->next
== NULL
|| l
->child
!= NULL
) {
323 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
327 if (l
->type
!= LTC_ASN1_OCTET_STRING
) {
328 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
332 if (l
->size
!= sizeof(oct_str
) || memcmp(oct_str
, l
->data
, l
->size
)) {
333 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
341 /* we expect oct_str */
342 if (l
->next
== NULL
|| l
->child
!= NULL
) {
343 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
347 if (l
->type
!= LTC_ASN1_BIT_STRING
) {
348 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
352 if (l
->size
!= sizeof(bit_str
) || memcmp(bit_str
, l
->data
, l
->size
)) {
353 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
360 /* expect child anve move down */
362 if (l
->next
!= NULL
|| l
->child
== NULL
) {
363 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
367 if (l
->type
!= LTC_ASN1_SEQUENCE
) {
368 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
375 /* we expect oid_str */
376 if (l
->next
== NULL
|| l
->child
!= NULL
) {
377 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
381 if (l
->type
!= LTC_ASN1_OBJECT_IDENTIFIER
) {
382 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
386 if (l
->size
!= sizeof(oid_str
)/sizeof(oid_str
[0]) || memcmp(oid_str
, l
->data
, l
->size
*sizeof(oid_str
[0]))) {
387 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
395 if (l
->type
!= LTC_ASN1_NULL
) {
396 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
403 /* expect child anve move down */
404 if (l
->next
!= NULL
|| l
->child
== NULL
) {
405 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
409 if (l
->type
!= LTC_ASN1_SET
) {
410 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
415 /* PRINTABLE STRING */
416 /* we expect printable_str */
417 if (l
->next
== NULL
|| l
->child
!= NULL
) {
418 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
422 if (l
->type
!= LTC_ASN1_PRINTABLE_STRING
) {
423 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
427 /* note we compare set2_str FIRST because the SET OF is sorted and "222" comes before "333" */
428 if (l
->size
!= strlen(set2_str
) || memcmp(set2_str
, l
->data
, l
->size
)) {
429 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
436 /* PRINTABLE STRING */
437 /* we expect printable_str */
438 if (l
->type
!= LTC_ASN1_PRINTABLE_STRING
) {
439 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
443 if (l
->size
!= strlen(set1_str
) || memcmp(set1_str
, l
->data
, l
->size
)) {
444 fprintf(stderr
, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__
, l
->type
, l
->size
, l
->next
, l
->prev
, l
->parent
, l
->child
);
449 der_sequence_free(l
);
453 static int der_choice_test(void)
455 ltc_asn1_list types
[7], host
[1];
456 unsigned char bitbuf
[10], octetbuf
[10], ia5buf
[10], printbuf
[10], outbuf
[256];
457 unsigned long integer
, oidbuf
[10], outlen
, inlen
, x
, y
;
459 ltc_utctime utctime
= { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
461 /* setup variables */
462 for (x
= 0; x
< sizeof(bitbuf
); x
++) { bitbuf
[x
] = x
& 1; }
463 for (x
= 0; x
< sizeof(octetbuf
); x
++) { octetbuf
[x
] = x
; }
464 for (x
= 0; x
< sizeof(ia5buf
); x
++) { ia5buf
[x
] = 'a'; }
465 for (x
= 0; x
< sizeof(printbuf
); x
++) { printbuf
[x
] = 'a'; }
467 for (x
= 0; x
< sizeof(oidbuf
)/sizeof(oidbuf
[0]); x
++) { oidbuf
[x
] = x
+ 1; }
468 DO(mp_init(&mpinteger
));
470 for (x
= 0; x
< 14; x
++) {
472 LTC_SET_ASN1(types
, 0, LTC_ASN1_PRINTABLE_STRING
, printbuf
, sizeof(printbuf
));
473 LTC_SET_ASN1(types
, 1, LTC_ASN1_BIT_STRING
, bitbuf
, sizeof(bitbuf
));
474 LTC_SET_ASN1(types
, 2, LTC_ASN1_OCTET_STRING
, octetbuf
, sizeof(octetbuf
));
475 LTC_SET_ASN1(types
, 3, LTC_ASN1_IA5_STRING
, ia5buf
, sizeof(ia5buf
));
477 LTC_SET_ASN1(types
, 4, LTC_ASN1_SHORT_INTEGER
, &integer
, 1);
479 LTC_SET_ASN1(types
, 4, LTC_ASN1_INTEGER
, mpinteger
, 1);
481 LTC_SET_ASN1(types
, 5, LTC_ASN1_OBJECT_IDENTIFIER
, oidbuf
, sizeof(oidbuf
)/sizeof(oidbuf
[0]));
482 LTC_SET_ASN1(types
, 6, LTC_ASN1_UTCTIME
, &utctime
, 1);
484 LTC_SET_ASN1(host
, 0, LTC_ASN1_CHOICE
, types
, 7);
488 outlen
= sizeof(outbuf
);
489 DO(der_encode_sequence(&types
[x
>6?x
-7:x
], 1, outbuf
, &outlen
));
493 DO(der_decode_sequence(outbuf
, inlen
, &host
[0], 1));
495 for (y
= 0; y
< 7; y
++) {
496 if (types
[y
].used
&& y
!= (x
>6?x
-7:x
)) {
497 fprintf(stderr
, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y
, x
);
500 if (!types
[y
].used
&& y
== (x
>6?x
-7:x
)) {
501 fprintf(stderr
, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y
, x
);
513 unsigned long x
, y
, z
, zz
, oid
[2][32];
514 unsigned char buf
[3][2048];
515 void *a
, *b
, *c
, *d
, *e
, *f
, *g
;
517 static const unsigned char rsa_oid_der
[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
518 static const unsigned long rsa_oid
[] = { 1, 2, 840, 113549 };
520 static const unsigned char rsa_ia5
[] = "test1@rsa.com";
521 static const unsigned char rsa_ia5_der
[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
522 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
524 static const unsigned char rsa_printable
[] = "Test User 1";
525 static const unsigned char rsa_printable_der
[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
526 0x73, 0x65, 0x72, 0x20, 0x31 };
528 static const ltc_utctime rsa_time1
= { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
529 static const ltc_utctime rsa_time2
= { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
530 ltc_utctime tmp_time
;
532 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 };
533 static const unsigned char rsa_time2_der
[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
535 static const wchar_t utf8_1
[] = { 0x0041, 0x2262, 0x0391, 0x002E };
536 static const unsigned char utf8_1_der
[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E };
537 static const wchar_t utf8_2
[] = { 0xD55C, 0xAD6D, 0xC5B4 };
538 static const unsigned char utf8_2_der
[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 };
540 unsigned char utf8_buf
[32];
541 wchar_t utf8_out
[32];
543 DO(mp_init_multi(&a
, &b
, &c
, &d
, &e
, &f
, &g
, NULL
));
544 for (zz
= 0; zz
< 16; zz
++) {
546 for (z
= 0; z
< 256; z
++) {
548 for (z
= 0; z
< 1024; z
++) {
550 if (yarrow_read(buf
[0], z
, &yarrow_prng
) != z
) {
551 fprintf(stderr
, "Failed to read %lu bytes from yarrow\n", z
);
554 DO(mp_read_unsigned_bin(a
, buf
[0], z
));
555 /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
557 DO(der_encode_integer(a
, buf
[0], &x
));
558 DO(der_length_integer(a
, &y
));
559 if (y
!= x
) { fprintf(stderr
, "DER INTEGER size mismatch\n"); return 1; }
561 DO(der_decode_integer(buf
[0], y
, b
));
562 if (y
!= x
|| mp_cmp(a
, b
) != LTC_MP_EQ
) {
563 fprintf(stderr
, "%lu: %lu vs %lu\n", z
, x
, y
);
564 mp_clear_multi(a
, b
, c
, d
, e
, f
, g
, NULL
);
570 /* test short integer */
571 for (zz
= 0; zz
< 256; zz
++) {
572 for (z
= 1; z
< 4; z
++) {
573 if (yarrow_read(buf
[0], z
, &yarrow_prng
) != z
) {
574 fprintf(stderr
, "Failed to read %lu bytes from yarrow\n", z
);
577 /* encode with normal */
578 DO(mp_read_unsigned_bin(a
, buf
[0], z
));
581 DO(der_encode_integer(a
, buf
[0], &x
));
583 /* encode with short */
585 DO(der_encode_short_integer(mp_get_int(a
), buf
[1], &y
));
586 if (x
!= y
|| memcmp(buf
[0], buf
[1], x
)) {
587 fprintf(stderr
, "DER INTEGER short encoding failed, %lu, %lu\n", x
, y
);
588 for (z
= 0; z
< x
; z
++) fprintf(stderr
, "%02x ", buf
[0][z
]); fprintf(stderr
, "\n");
589 for (z
= 0; z
< y
; z
++) fprintf(stderr
, "%02x ", buf
[1][z
]); fprintf(stderr
, "\n");
590 mp_clear_multi(a
, b
, c
, d
, e
, f
, g
, NULL
);
596 DO(der_decode_short_integer(buf
[1], y
, &x
));
597 if (x
!= mp_get_int(a
)) {
598 fprintf(stderr
, "DER INTEGER short decoding failed, %lu, %lu\n", x
, mp_get_int(a
));
599 mp_clear_multi(a
, b
, c
, d
, e
, f
, g
, NULL
);
604 mp_clear_multi(a
, b
, c
, d
, e
, f
, g
, NULL
);
607 /* Test bit string */
608 for (zz
= 1; zz
< 1536; zz
++) {
609 yarrow_read(buf
[0], zz
, &yarrow_prng
);
610 for (z
= 0; z
< zz
; z
++) {
614 DO(der_encode_bit_string(buf
[0], zz
, buf
[1], &x
));
615 DO(der_length_bit_string(zz
, &y
));
617 fprintf(stderr
, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z
, x
, y
);
622 DO(der_decode_bit_string(buf
[1], x
, buf
[2], &y
));
623 if (y
!= zz
|| memcmp(buf
[0], buf
[2], zz
)) {
624 fprintf(stderr
, "%lu, %lu, %d\n", y
, zz
, memcmp(buf
[0], buf
[2], zz
));
629 /* Test octet string */
630 for (zz
= 1; zz
< 1536; zz
++) {
631 yarrow_read(buf
[0], zz
, &yarrow_prng
);
633 DO(der_encode_octet_string(buf
[0], zz
, buf
[1], &x
));
634 DO(der_length_octet_string(zz
, &y
));
636 fprintf(stderr
, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z
, x
, y
);
640 DO(der_decode_octet_string(buf
[1], x
, buf
[2], &y
));
641 if (y
!= zz
|| memcmp(buf
[0], buf
[2], zz
)) {
642 fprintf(stderr
, "%lu, %lu, %d\n", y
, zz
, memcmp(buf
[0], buf
[2], zz
));
649 DO(der_encode_object_identifier((unsigned long*)rsa_oid
, sizeof(rsa_oid
)/sizeof(rsa_oid
[0]), buf
[0], &x
));
650 if (x
!= sizeof(rsa_oid_der
) || memcmp(rsa_oid_der
, buf
[0], x
)) {
651 fprintf(stderr
, "rsa_oid_der encode failed to match, %lu, ", x
);
652 for (y
= 0; y
< x
; y
++) fprintf(stderr
, "%02x ", buf
[0][y
]);
653 fprintf(stderr
, "\n");
657 y
= sizeof(oid
[0])/sizeof(oid
[0][0]);
658 DO(der_decode_object_identifier(buf
[0], x
, oid
[0], &y
));
659 if (y
!= sizeof(rsa_oid
)/sizeof(rsa_oid
[0]) || memcmp(rsa_oid
, oid
[0], sizeof(rsa_oid
))) {
660 fprintf(stderr
, "rsa_oid_der decode failed to match, %lu, ", y
);
661 for (z
= 0; z
< y
; z
++) fprintf(stderr
, "%lu ", oid
[0][z
]);
662 fprintf(stderr
, "\n");
666 /* do random strings */
667 for (zz
= 0; zz
< 5000; zz
++) {
668 /* pick a random number of words */
669 yarrow_read(buf
[0], 4, &yarrow_prng
);
671 z
= 2 + (z
% ((sizeof(oid
[0])/sizeof(oid
[0][0])) - 2));
674 oid
[0][0] = buf
[0][0] % 3;
675 oid
[0][1] = buf
[0][1] % 40;
677 for (y
= 2; y
< z
; y
++) {
678 yarrow_read(buf
[0], 4, &yarrow_prng
);
679 LOAD32L(oid
[0][y
], buf
[0]);
684 DO(der_encode_object_identifier(oid
[0], z
, buf
[0], &x
));
685 DO(der_length_object_identifier(oid
[0], z
, &y
));
687 fprintf(stderr
, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z
, x
, y
);
688 for (x
= 0; x
< z
; x
++) fprintf(stderr
, "%lu\n", oid
[0][x
]);
693 y
= sizeof(oid
[0])/sizeof(oid
[0][0]);
694 DO(der_decode_object_identifier(buf
[0], x
, oid
[1], &y
));
696 fprintf(stderr
, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z
, x
, y
);
699 if (memcmp(oid
[0], oid
[1], sizeof(oid
[0][0]) * z
)) {
700 fprintf(stderr
, "Random OID %lu test failed, decoded values wrong\n", z
);
701 for (x
= 0; x
< z
; x
++) fprintf(stderr
, "%lu\n", oid
[0][x
]); fprintf(stderr
, "\n\n Got \n\n");
702 for (x
= 0; x
< z
; x
++) fprintf(stderr
, "%lu\n", oid
[1][x
]);
709 DO(der_encode_ia5_string(rsa_ia5
, strlen((char*)rsa_ia5
), buf
[0], &x
));
710 if (x
!= sizeof(rsa_ia5_der
) || memcmp(buf
[0], rsa_ia5_der
, x
)) {
711 fprintf(stderr
, "IA5 encode failed: %lu, %lu\n", x
, (unsigned long)sizeof(rsa_ia5_der
));
714 DO(der_length_ia5_string(rsa_ia5
, strlen((char*)rsa_ia5
), &y
));
716 fprintf(stderr
, "IA5 length failed to match: %lu, %lu\n", x
, y
);
720 DO(der_decode_ia5_string(buf
[0], x
, buf
[1], &y
));
721 if (y
!= strlen((char*)rsa_ia5
) || memcmp(buf
[1], rsa_ia5
, strlen((char*)rsa_ia5
))) {
722 fprintf(stderr
, "DER IA5 failed test vector\n");
726 /* Printable string */
728 DO(der_encode_printable_string(rsa_printable
, strlen((char*)rsa_printable
), buf
[0], &x
));
729 if (x
!= sizeof(rsa_printable_der
) || memcmp(buf
[0], rsa_printable_der
, x
)) {
730 fprintf(stderr
, "PRINTABLE encode failed: %lu, %lu\n", x
, (unsigned long)sizeof(rsa_printable_der
));
733 DO(der_length_printable_string(rsa_printable
, strlen((char*)rsa_printable
), &y
));
735 fprintf(stderr
, "printable length failed to match: %lu, %lu\n", x
, y
);
739 DO(der_decode_printable_string(buf
[0], x
, buf
[1], &y
));
740 if (y
!= strlen((char*)rsa_printable
) || memcmp(buf
[1], rsa_printable
, strlen((char*)rsa_printable
))) {
741 fprintf(stderr
, "DER printable failed test vector\n");
747 DO(der_encode_utctime((ltc_utctime
*)&rsa_time1
, buf
[0], &x
));
748 if (x
!= sizeof(rsa_time1_der
) || memcmp(buf
[0], rsa_time1_der
, x
)) {
749 fprintf(stderr
, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x
, (unsigned long)sizeof(rsa_time1_der
));
750 fprintf(stderr
, "\n\n");
751 for (y
= 0; y
< x
; y
++) fprintf(stderr
, "%02x ", buf
[0][y
]); printf("\n");
755 DO(der_length_utctime((ltc_utctime
*)&rsa_time1
, &y
));
757 fprintf(stderr
, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x
, y
);
760 DO(der_decode_utctime(buf
[0], &y
, &tmp_time
));
761 if (y
!= x
|| memcmp(&rsa_time1
, &tmp_time
, sizeof(ltc_utctime
))) {
762 fprintf(stderr
, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x
, y
);
763 fprintf(stderr
, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
777 DO(der_encode_utctime((ltc_utctime
*)&rsa_time2
, buf
[0], &x
));
778 if (x
!= sizeof(rsa_time2_der
) || memcmp(buf
[0], rsa_time2_der
, x
)) {
779 fprintf(stderr
, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x
, (unsigned long)sizeof(rsa_time1_der
));
780 fprintf(stderr
, "\n\n");
781 for (y
= 0; y
< x
; y
++) fprintf(stderr
, "%02x ", buf
[0][y
]); printf("\n");
785 DO(der_length_utctime((ltc_utctime
*)&rsa_time2
, &y
));
787 fprintf(stderr
, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x
, y
);
790 DO(der_decode_utctime(buf
[0], &y
, &tmp_time
));
791 if (y
!= x
|| memcmp(&rsa_time2
, &tmp_time
, sizeof(ltc_utctime
))) {
792 fprintf(stderr
, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x
, y
);
793 fprintf(stderr
, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
810 x
= sizeof(utf8_buf
);
811 DO(der_encode_utf8_string(utf8_1
, sizeof(utf8_1
) / sizeof(utf8_1
[0]), utf8_buf
, &x
));
812 if (x
!= sizeof(utf8_1_der
) || memcmp(utf8_buf
, utf8_1_der
, x
)) {
813 fprintf(stderr
, "DER UTF8_1 encoded to %lu bytes\n", x
);
814 for (y
= 0; y
< x
; y
++) fprintf(stderr
, "%02x ", (unsigned)utf8_buf
[y
]); fprintf(stderr
, "\n");
818 y
= sizeof(utf8_out
) / sizeof(utf8_out
[0]);
819 DO(der_decode_utf8_string(utf8_buf
, x
, utf8_out
, &y
));
820 if (y
!= (sizeof(utf8_1
) / sizeof(utf8_1
[0])) || memcmp(utf8_1
, utf8_out
, y
* sizeof(wchar_t))) {
821 fprintf(stderr
, "DER UTF8_1 decoded to %lu wchar_t\n", y
);
822 for (x
= 0; x
< y
; x
++) fprintf(stderr
, "%04lx ", (unsigned long)utf8_out
[x
]); fprintf(stderr
, "\n");
827 x
= sizeof(utf8_buf
);
828 DO(der_encode_utf8_string(utf8_2
, sizeof(utf8_2
) / sizeof(utf8_2
[0]), utf8_buf
, &x
));
829 if (x
!= sizeof(utf8_2_der
) || memcmp(utf8_buf
, utf8_2_der
, x
)) {
830 fprintf(stderr
, "DER UTF8_2 encoded to %lu bytes\n", x
);
831 for (y
= 0; y
< x
; y
++) fprintf(stderr
, "%02x ", (unsigned)utf8_buf
[y
]); fprintf(stderr
, "\n");
835 y
= sizeof(utf8_out
) / sizeof(utf8_out
[0]);
836 DO(der_decode_utf8_string(utf8_buf
, x
, utf8_out
, &y
));
837 if (y
!= (sizeof(utf8_2
) / sizeof(utf8_2
[0])) || memcmp(utf8_2
, utf8_out
, y
* sizeof(wchar_t))) {
838 fprintf(stderr
, "DER UTF8_2 decoded to %lu wchar_t\n", y
);
839 for (x
= 0; x
< y
; x
++) fprintf(stderr
, "%04lx ", (unsigned long)utf8_out
[x
]); fprintf(stderr
, "\n");
846 return der_choice_test();
851 /* $Source: /cvs/libtom/libtomcrypt/testprof/der_tests.c,v $ */
852 /* $Revision: 1.49 $ */
853 /* $Date: 2006/11/26 02:10:21 $ */