der_tests.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #include <tomcrypt_test.h>
  2. #ifndef LTC_DER
  3. int der_tests(void)
  4. {
  5. fprintf(stderr, "NOP");
  6. return 0;
  7. }
  8. #else
  9. /* we are encoding
  10. SEQUENCE {
  11. PRINTABLE "printable"
  12. IA5 "ia5"
  13. SEQUENCE {
  14. INTEGER 12345678
  15. UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
  16. SEQUENCE {
  17. OCTET STRING { 1, 2, 3, 4 }
  18. BIT STRING { 1, 0, 0, 1 }
  19. SEQUENCE {
  20. OID { 1, 2, 840, 113549 }
  21. NULL
  22. }
  23. }
  24. }
  25. }
  26. */
  27. static void der_flexi_test(void)
  28. {
  29. static const char printable_str[] = "printable";
  30. static const char ia5_str[] = "ia5";
  31. static const unsigned long int_val = 12345678UL;
  32. static const ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  33. static const unsigned char oct_str[] = { 1, 2, 3, 4 };
  34. static const unsigned char bit_str[] = { 1, 0, 0, 1 };
  35. static const unsigned long oid_str[] = { 1, 2, 840, 113549 };
  36. unsigned char encode_buf[128];
  37. unsigned long encode_buf_len, decode_len;
  38. int err;
  39. ltc_asn1_list static_list[4][3], *decoded_list, *l;
  40. /* build list */
  41. LTC_SET_ASN1(static_list[0], 0, LTC_ASN1_PRINTABLE_STRING, (void *)printable_str, strlen(printable_str));
  42. LTC_SET_ASN1(static_list[0], 1, LTC_ASN1_IA5_STRING, (void *)ia5_str, strlen(ia5_str));
  43. LTC_SET_ASN1(static_list[0], 2, LTC_ASN1_SEQUENCE, static_list[1], 3);
  44. LTC_SET_ASN1(static_list[1], 0, LTC_ASN1_SHORT_INTEGER, (void *)&int_val, 1);
  45. LTC_SET_ASN1(static_list[1], 1, LTC_ASN1_UTCTIME, (void *)&utctime, 1);
  46. LTC_SET_ASN1(static_list[1], 2, LTC_ASN1_SEQUENCE, static_list[2], 3);
  47. LTC_SET_ASN1(static_list[2], 0, LTC_ASN1_OCTET_STRING, (void *)oct_str, 4);
  48. LTC_SET_ASN1(static_list[2], 1, LTC_ASN1_BIT_STRING, (void *)bit_str, 4);
  49. LTC_SET_ASN1(static_list[2], 2, LTC_ASN1_SEQUENCE, static_list[3], 2);
  50. LTC_SET_ASN1(static_list[3], 0, LTC_ASN1_OBJECT_IDENTIFIER,(void *)oid_str, 4);
  51. LTC_SET_ASN1(static_list[3], 1, LTC_ASN1_NULL, NULL, 0);
  52. /* encode it */
  53. encode_buf_len = sizeof(encode_buf);
  54. if ((err = der_encode_sequence(&static_list[0][0], 3, encode_buf, &encode_buf_len)) != CRYPT_OK) {
  55. fprintf(stderr, "Encoding static_list: %s\n", error_to_string(err));
  56. exit(EXIT_FAILURE);
  57. }
  58. #if 0
  59. {
  60. FILE *f;
  61. f = fopen("t.bin", "wb");
  62. fwrite(encode_buf, 1, encode_buf_len, f);
  63. fclose(f);
  64. }
  65. #endif
  66. /* decode with flexi */
  67. decode_len = encode_buf_len;
  68. if ((err = der_decode_sequence_flexi(encode_buf, &decode_len, &decoded_list)) != CRYPT_OK) {
  69. fprintf(stderr, "decoding static_list: %s\n", error_to_string(err));
  70. exit(EXIT_FAILURE);
  71. }
  72. if (decode_len != encode_buf_len) {
  73. fprintf(stderr, "Decode len of %lu does not match encode len of %lu \n", decode_len, encode_buf_len);
  74. exit(EXIT_FAILURE);
  75. }
  76. /* we expect l->next to be NULL and l->child to not be */
  77. l = decoded_list;
  78. if (l->next != NULL || l->child == NULL) {
  79. 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);
  80. exit(EXIT_FAILURE);
  81. }
  82. /* we expect a SEQUENCE */
  83. if (l->type != LTC_ASN1_SEQUENCE) {
  84. 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);
  85. exit(EXIT_FAILURE);
  86. }
  87. l = l->child;
  88. /* PRINTABLE STRING */
  89. /* we expect printable_str */
  90. if (l->next == NULL || l->child != NULL) {
  91. 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);
  92. exit(EXIT_FAILURE);
  93. }
  94. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  95. 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);
  96. exit(EXIT_FAILURE);
  97. }
  98. if (l->size != strlen(printable_str) || memcmp(printable_str, l->data, l->size)) {
  99. 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);
  100. exit(EXIT_FAILURE);
  101. }
  102. /* move to next */
  103. l = l->next;
  104. /* IA5 STRING */
  105. /* we expect ia5_str */
  106. if (l->next == NULL || l->child != NULL) {
  107. 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);
  108. exit(EXIT_FAILURE);
  109. }
  110. if (l->type != LTC_ASN1_IA5_STRING) {
  111. 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);
  112. exit(EXIT_FAILURE);
  113. }
  114. if (l->size != strlen(ia5_str) || memcmp(ia5_str, l->data, l->size)) {
  115. 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);
  116. exit(EXIT_FAILURE);
  117. }
  118. /* move to next */
  119. l = l->next;
  120. /* expect child anve move down */
  121. if (l->next != NULL || l->child == NULL) {
  122. 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);
  123. exit(EXIT_FAILURE);
  124. }
  125. if (l->type != LTC_ASN1_SEQUENCE) {
  126. 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);
  127. exit(EXIT_FAILURE);
  128. }
  129. l = l->child;
  130. /* INTEGER */
  131. if (l->next == NULL || l->child != NULL) {
  132. 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);
  133. exit(EXIT_FAILURE);
  134. }
  135. if (l->type != LTC_ASN1_INTEGER) {
  136. 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);
  137. exit(EXIT_FAILURE);
  138. }
  139. if (mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) {
  140. 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);
  141. exit(EXIT_FAILURE);
  142. }
  143. /* move to next */
  144. l = l->next;
  145. /* UTCTIME */
  146. if (l->next == NULL || l->child != NULL) {
  147. 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);
  148. exit(EXIT_FAILURE);
  149. }
  150. if (l->type != LTC_ASN1_UTCTIME) {
  151. 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);
  152. exit(EXIT_FAILURE);
  153. }
  154. if (memcmp(l->data, &utctime, sizeof(utctime))) {
  155. 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);
  156. exit(EXIT_FAILURE);
  157. }
  158. /* move to next */
  159. l = l->next;
  160. /* expect child anve move down */
  161. if (l->next != NULL || l->child == NULL) {
  162. 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);
  163. exit(EXIT_FAILURE);
  164. }
  165. if (l->type != LTC_ASN1_SEQUENCE) {
  166. 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);
  167. exit(EXIT_FAILURE);
  168. }
  169. l = l->child;
  170. /* OCTET STRING */
  171. /* we expect oct_str */
  172. if (l->next == NULL || l->child != NULL) {
  173. 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);
  174. exit(EXIT_FAILURE);
  175. }
  176. if (l->type != LTC_ASN1_OCTET_STRING) {
  177. 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);
  178. exit(EXIT_FAILURE);
  179. }
  180. if (l->size != sizeof(oct_str) || memcmp(oct_str, l->data, l->size)) {
  181. 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);
  182. exit(EXIT_FAILURE);
  183. }
  184. /* move to next */
  185. l = l->next;
  186. /* BIT STRING */
  187. /* we expect oct_str */
  188. if (l->next == NULL || l->child != NULL) {
  189. 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);
  190. exit(EXIT_FAILURE);
  191. }
  192. if (l->type != LTC_ASN1_BIT_STRING) {
  193. 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);
  194. exit(EXIT_FAILURE);
  195. }
  196. if (l->size != sizeof(bit_str) || memcmp(bit_str, l->data, l->size)) {
  197. 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);
  198. exit(EXIT_FAILURE);
  199. }
  200. /* move to next */
  201. l = l->next;
  202. /* expect child anve move down */
  203. if (l->next != NULL || l->child == NULL) {
  204. 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. exit(EXIT_FAILURE);
  206. }
  207. if (l->type != LTC_ASN1_SEQUENCE) {
  208. 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);
  209. exit(EXIT_FAILURE);
  210. }
  211. l = l->child;
  212. /* OID STRING */
  213. /* we expect oid_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);
  216. exit(EXIT_FAILURE);
  217. }
  218. if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) {
  219. 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);
  220. exit(EXIT_FAILURE);
  221. }
  222. if (l->size != sizeof(oid_str)/sizeof(oid_str[0]) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
  223. 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. exit(EXIT_FAILURE);
  225. }
  226. /* move to next */
  227. l = l->next;
  228. /* NULL */
  229. if (l->type != LTC_ASN1_NULL) {
  230. 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);
  231. exit(EXIT_FAILURE);
  232. }
  233. der_sequence_free(l);
  234. }
  235. static int der_choice_test(void)
  236. {
  237. ltc_asn1_list types[7], host[1];
  238. unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256];
  239. unsigned long integer, oidbuf[10], outlen, inlen, x, y;
  240. void *mpinteger;
  241. ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  242. /* setup variables */
  243. for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; }
  244. for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; }
  245. for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; }
  246. for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; }
  247. integer = 1;
  248. for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; }
  249. DO(mp_init(&mpinteger));
  250. for (x = 0; x < 14; x++) {
  251. /* setup list */
  252. LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
  253. LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
  254. LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
  255. LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
  256. if (x > 7) {
  257. LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
  258. } else {
  259. LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, mpinteger, 1);
  260. }
  261. LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
  262. LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
  263. LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7);
  264. /* encode */
  265. outlen = sizeof(outbuf);
  266. DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen));
  267. /* decode it */
  268. inlen = outlen;
  269. DO(der_decode_sequence(outbuf, inlen, &host[0], 1));
  270. for (y = 0; y < 7; y++) {
  271. if (types[y].used && y != (x>6?x-7:x)) {
  272. fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y, x);
  273. return 1;
  274. }
  275. if (!types[y].used && y == (x>6?x-7:x)) {
  276. fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y, x);
  277. return 1;
  278. }
  279. }
  280. }
  281. mp_clear(mpinteger);
  282. return 0;
  283. }
  284. int der_tests(void)
  285. {
  286. unsigned long x, y, z, zz, oid[2][32];
  287. unsigned char buf[3][2048];
  288. void *a, *b, *c, *d, *e, *f, *g;
  289. static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
  290. static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 };
  291. static const unsigned char rsa_ia5[] = "[email protected]";
  292. static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
  293. 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
  294. static const unsigned char rsa_printable[] = "Test User 1";
  295. static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
  296. 0x73, 0x65, 0x72, 0x20, 0x31 };
  297. static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  298. static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
  299. ltc_utctime tmp_time;
  300. 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 };
  301. static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
  302. der_flexi_test();
  303. DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
  304. for (zz = 0; zz < 16; zz++) {
  305. #ifdef USE_TFM
  306. for (z = 0; z < 256; z++) {
  307. #else
  308. for (z = 0; z < 1024; z++) {
  309. #endif
  310. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  311. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  312. return 1;
  313. }
  314. DO(mp_read_unsigned_bin(a, buf[0], z));
  315. /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
  316. x = sizeof(buf[0]);
  317. DO(der_encode_integer(a, buf[0], &x));
  318. DO(der_length_integer(a, &y));
  319. if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
  320. mp_set_int(b, 0);
  321. DO(der_decode_integer(buf[0], y, b));
  322. if (y != x || mp_cmp(a, b) != LTC_MP_EQ) {
  323. fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
  324. #ifdef BN_MP_TORADIX_C
  325. mp_todecimal(a, buf[0]);
  326. mp_todecimal(b, buf[1]);
  327. fprintf(stderr, "a == %s\nb == %s\n", buf[0], buf[1]);
  328. #endif
  329. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  330. return 1;
  331. }
  332. }
  333. }
  334. /* test short integer */
  335. for (zz = 0; zz < 256; zz++) {
  336. for (z = 1; z < 4; z++) {
  337. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  338. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  339. return 1;
  340. }
  341. /* encode with normal */
  342. DO(mp_read_unsigned_bin(a, buf[0], z));
  343. x = sizeof(buf[0]);
  344. DO(der_encode_integer(a, buf[0], &x));
  345. /* encode with short */
  346. y = sizeof(buf[1]);
  347. DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
  348. if (x != y || memcmp(buf[0], buf[1], x)) {
  349. fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
  350. for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
  351. for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
  352. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  353. return 1;
  354. }
  355. /* decode it */
  356. x = 0;
  357. DO(der_decode_short_integer(buf[1], y, &x));
  358. if (x != mp_get_int(a)) {
  359. fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a));
  360. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  361. return 1;
  362. }
  363. }
  364. }
  365. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  366. /* Test bit string */
  367. for (zz = 1; zz < 1536; zz++) {
  368. yarrow_read(buf[0], zz, &yarrow_prng);
  369. for (z = 0; z < zz; z++) {
  370. buf[0][z] &= 0x01;
  371. }
  372. x = sizeof(buf[1]);
  373. DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
  374. DO(der_length_bit_string(zz, &y));
  375. if (y != x) {
  376. fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  377. return 1;
  378. }
  379. y = sizeof(buf[2]);
  380. DO(der_decode_bit_string(buf[1], x, buf[2], &y));
  381. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  382. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  383. return 1;
  384. }
  385. }
  386. /* Test octet string */
  387. for (zz = 1; zz < 1536; zz++) {
  388. yarrow_read(buf[0], zz, &yarrow_prng);
  389. x = sizeof(buf[1]);
  390. DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
  391. DO(der_length_octet_string(zz, &y));
  392. if (y != x) {
  393. fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  394. return 1;
  395. }
  396. y = sizeof(buf[2]);
  397. DO(der_decode_octet_string(buf[1], x, buf[2], &y));
  398. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  399. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  400. return 1;
  401. }
  402. }
  403. /* test OID */
  404. x = sizeof(buf[0]);
  405. DO(der_encode_object_identifier(rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
  406. if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
  407. fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
  408. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  409. fprintf(stderr, "\n");
  410. return 1;
  411. }
  412. y = sizeof(oid[0])/sizeof(oid[0][0]);
  413. DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
  414. if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
  415. fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
  416. for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
  417. fprintf(stderr, "\n");
  418. return 1;
  419. }
  420. /* do random strings */
  421. for (zz = 0; zz < 5000; zz++) {
  422. /* pick a random number of words */
  423. yarrow_read(buf[0], 4, &yarrow_prng);
  424. LOAD32L(z, buf[0]);
  425. z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
  426. /* fill them in */
  427. oid[0][0] = buf[0][0] % 3;
  428. oid[0][1] = buf[0][1] % 40;
  429. for (y = 2; y < z; y++) {
  430. yarrow_read(buf[0], 4, &yarrow_prng);
  431. LOAD32L(oid[0][y], buf[0]);
  432. }
  433. /* encode it */
  434. x = sizeof(buf[0]);
  435. DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
  436. DO(der_length_object_identifier(oid[0], z, &y));
  437. if (x != y) {
  438. fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
  439. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  440. return 1;
  441. }
  442. /* decode it */
  443. y = sizeof(oid[0])/sizeof(oid[0][0]);
  444. DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
  445. if (y != z) {
  446. fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
  447. return 1;
  448. }
  449. if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
  450. fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
  451. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]); fprintf(stderr, "\n\n Got \n\n");
  452. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
  453. return 1;
  454. }
  455. }
  456. /* IA5 string */
  457. x = sizeof(buf[0]);
  458. DO(der_encode_ia5_string(rsa_ia5, strlen(rsa_ia5), buf[0], &x));
  459. if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
  460. fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
  461. return 1;
  462. }
  463. DO(der_length_ia5_string(rsa_ia5, strlen(rsa_ia5), &y));
  464. if (y != x) {
  465. fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
  466. return 1;
  467. }
  468. y = sizeof(buf[1]);
  469. DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
  470. if (y != strlen(rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen(rsa_ia5))) {
  471. fprintf(stderr, "DER IA5 failed test vector\n");
  472. return 1;
  473. }
  474. /* Printable string */
  475. x = sizeof(buf[0]);
  476. DO(der_encode_printable_string(rsa_printable, strlen(rsa_printable), buf[0], &x));
  477. if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
  478. fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
  479. return 1;
  480. }
  481. DO(der_length_printable_string(rsa_printable, strlen(rsa_printable), &y));
  482. if (y != x) {
  483. fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
  484. return 1;
  485. }
  486. y = sizeof(buf[1]);
  487. DO(der_decode_printable_string(buf[0], x, buf[1], &y));
  488. if (y != strlen(rsa_printable) || memcmp(buf[1], rsa_printable, strlen(rsa_printable))) {
  489. fprintf(stderr, "DER printable failed test vector\n");
  490. return 1;
  491. }
  492. /* Test UTC time */
  493. x = sizeof(buf[0]);
  494. DO(der_encode_utctime(&rsa_time1, buf[0], &x));
  495. if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
  496. fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  497. fprintf(stderr, "\n\n");
  498. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  499. return 1;
  500. }
  501. DO(der_length_utctime(&rsa_time1, &y));
  502. if (y != x) {
  503. fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
  504. return 1;
  505. }
  506. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  507. if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
  508. fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
  509. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  510. tmp_time.YY,
  511. tmp_time.MM,
  512. tmp_time.DD,
  513. tmp_time.hh,
  514. tmp_time.mm,
  515. tmp_time.ss,
  516. tmp_time.off_dir,
  517. tmp_time.off_mm,
  518. tmp_time.off_hh);
  519. return 1;
  520. }
  521. x = sizeof(buf[0]);
  522. DO(der_encode_utctime(&rsa_time2, buf[0], &x));
  523. if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
  524. fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  525. fprintf(stderr, "\n\n");
  526. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  527. return 1;
  528. }
  529. DO(der_length_utctime(&rsa_time2, &y));
  530. if (y != x) {
  531. fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
  532. return 1;
  533. }
  534. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  535. if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
  536. fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
  537. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  538. tmp_time.YY,
  539. tmp_time.MM,
  540. tmp_time.DD,
  541. tmp_time.hh,
  542. tmp_time.mm,
  543. tmp_time.ss,
  544. tmp_time.off_dir,
  545. tmp_time.off_mm,
  546. tmp_time.off_hh);
  547. return 1;
  548. }
  549. return der_choice_test();
  550. }
  551. #endif
  552. /* $Source$ */
  553. /* $Revision$ */
  554. /* $Date$ */