der_tests.c 13 KB

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