der_tests.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. void *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(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[0], 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. void *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(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
  77. for (zz = 0; zz < 16; zz++) {
  78. #ifdef USE_TFM
  79. for (z = 0; z < 256; z++) {
  80. #else
  81. for (z = 0; z < 1024; z++) {
  82. #endif
  83. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  84. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  85. return 1;
  86. }
  87. DO(mp_read_unsigned_bin(a, buf[0], z));
  88. /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
  89. x = sizeof(buf[0]);
  90. DO(der_encode_integer(a, buf[0], &x));
  91. DO(der_length_integer(a, &y));
  92. if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
  93. mp_set_int(b, 0);
  94. DO(der_decode_integer(buf[0], y, b));
  95. if (y != x || mp_cmp(a, b) != LTC_MP_EQ) {
  96. fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
  97. #ifdef BN_MP_TORADIX_C
  98. mp_todecimal(a, buf[0]);
  99. mp_todecimal(b, buf[1]);
  100. fprintf(stderr, "a == %s\nb == %s\n", buf[0], buf[1]);
  101. #endif
  102. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  103. return 1;
  104. }
  105. }
  106. }
  107. /* test short integer */
  108. for (zz = 0; zz < 256; zz++) {
  109. for (z = 1; z < 4; z++) {
  110. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  111. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  112. return 1;
  113. }
  114. /* encode with normal */
  115. DO(mp_read_unsigned_bin(a, buf[0], z));
  116. x = sizeof(buf[0]);
  117. DO(der_encode_integer(a, buf[0], &x));
  118. /* encode with short */
  119. y = sizeof(buf[1]);
  120. DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
  121. if (x != y || memcmp(buf[0], buf[1], x)) {
  122. fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
  123. for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
  124. for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
  125. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  126. return 1;
  127. }
  128. /* decode it */
  129. x = 0;
  130. DO(der_decode_short_integer(buf[1], y, &x));
  131. if (x != mp_get_int(a)) {
  132. fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a));
  133. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  134. return 1;
  135. }
  136. }
  137. }
  138. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  139. /* Test bit string */
  140. for (zz = 1; zz < 1536; zz++) {
  141. yarrow_read(buf[0], zz, &yarrow_prng);
  142. for (z = 0; z < zz; z++) {
  143. buf[0][z] &= 0x01;
  144. }
  145. x = sizeof(buf[1]);
  146. DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
  147. DO(der_length_bit_string(zz, &y));
  148. if (y != x) {
  149. fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  150. return 1;
  151. }
  152. y = sizeof(buf[2]);
  153. DO(der_decode_bit_string(buf[1], x, buf[2], &y));
  154. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  155. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  156. return 1;
  157. }
  158. }
  159. /* Test octet string */
  160. for (zz = 1; zz < 1536; zz++) {
  161. yarrow_read(buf[0], zz, &yarrow_prng);
  162. x = sizeof(buf[1]);
  163. DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
  164. DO(der_length_octet_string(zz, &y));
  165. if (y != x) {
  166. fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  167. return 1;
  168. }
  169. y = sizeof(buf[2]);
  170. DO(der_decode_octet_string(buf[1], x, buf[2], &y));
  171. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  172. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  173. return 1;
  174. }
  175. }
  176. /* test OID */
  177. x = sizeof(buf[0]);
  178. DO(der_encode_object_identifier(rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
  179. if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
  180. fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
  181. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  182. fprintf(stderr, "\n");
  183. return 1;
  184. }
  185. y = sizeof(oid[0])/sizeof(oid[0][0]);
  186. DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
  187. if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
  188. fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
  189. for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
  190. fprintf(stderr, "\n");
  191. return 1;
  192. }
  193. /* do random strings */
  194. for (zz = 0; zz < 5000; zz++) {
  195. /* pick a random number of words */
  196. yarrow_read(buf[0], 4, &yarrow_prng);
  197. LOAD32L(z, buf[0]);
  198. z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
  199. /* fill them in */
  200. oid[0][0] = buf[0][0] % 3;
  201. oid[0][1] = buf[0][1] % 40;
  202. for (y = 2; y < z; y++) {
  203. yarrow_read(buf[0], 4, &yarrow_prng);
  204. LOAD32L(oid[0][y], buf[0]);
  205. }
  206. /* encode it */
  207. x = sizeof(buf[0]);
  208. DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
  209. DO(der_length_object_identifier(oid[0], z, &y));
  210. if (x != y) {
  211. fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
  212. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  213. return 1;
  214. }
  215. /* decode it */
  216. y = sizeof(oid[0])/sizeof(oid[0][0]);
  217. DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
  218. if (y != z) {
  219. fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
  220. return 1;
  221. }
  222. if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
  223. fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
  224. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]); fprintf(stderr, "\n\n Got \n\n");
  225. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
  226. return 1;
  227. }
  228. }
  229. /* IA5 string */
  230. x = sizeof(buf[0]);
  231. DO(der_encode_ia5_string(rsa_ia5, strlen(rsa_ia5), buf[0], &x));
  232. if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
  233. fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
  234. return 1;
  235. }
  236. DO(der_length_ia5_string(rsa_ia5, strlen(rsa_ia5), &y));
  237. if (y != x) {
  238. fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
  239. return 1;
  240. }
  241. y = sizeof(buf[1]);
  242. DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
  243. if (y != strlen(rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen(rsa_ia5))) {
  244. fprintf(stderr, "DER IA5 failed test vector\n");
  245. return 1;
  246. }
  247. /* Printable string */
  248. x = sizeof(buf[0]);
  249. DO(der_encode_printable_string(rsa_printable, strlen(rsa_printable), buf[0], &x));
  250. if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
  251. fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
  252. return 1;
  253. }
  254. DO(der_length_printable_string(rsa_printable, strlen(rsa_printable), &y));
  255. if (y != x) {
  256. fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
  257. return 1;
  258. }
  259. y = sizeof(buf[1]);
  260. DO(der_decode_printable_string(buf[0], x, buf[1], &y));
  261. if (y != strlen(rsa_printable) || memcmp(buf[1], rsa_printable, strlen(rsa_printable))) {
  262. fprintf(stderr, "DER printable failed test vector\n");
  263. return 1;
  264. }
  265. /* Test UTC time */
  266. x = sizeof(buf[0]);
  267. DO(der_encode_utctime(&rsa_time1, buf[0], &x));
  268. if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
  269. fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  270. fprintf(stderr, "\n\n");
  271. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  272. return 1;
  273. }
  274. DO(der_length_utctime(&rsa_time1, &y));
  275. if (y != x) {
  276. fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
  277. return 1;
  278. }
  279. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  280. if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
  281. fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
  282. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  283. tmp_time.YY,
  284. tmp_time.MM,
  285. tmp_time.DD,
  286. tmp_time.hh,
  287. tmp_time.mm,
  288. tmp_time.ss,
  289. tmp_time.off_dir,
  290. tmp_time.off_mm,
  291. tmp_time.off_hh);
  292. return 1;
  293. }
  294. x = sizeof(buf[0]);
  295. DO(der_encode_utctime(&rsa_time2, buf[0], &x));
  296. if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
  297. fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  298. fprintf(stderr, "\n\n");
  299. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  300. return 1;
  301. }
  302. DO(der_length_utctime(&rsa_time2, &y));
  303. if (y != x) {
  304. fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
  305. return 1;
  306. }
  307. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  308. if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
  309. fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
  310. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  311. tmp_time.YY,
  312. tmp_time.MM,
  313. tmp_time.DD,
  314. tmp_time.hh,
  315. tmp_time.mm,
  316. tmp_time.ss,
  317. tmp_time.off_dir,
  318. tmp_time.off_mm,
  319. tmp_time.off_hh);
  320. return 1;
  321. }
  322. return der_choice_test();
  323. }
  324. #endif
  325. /* $Source$ */
  326. /* $Revision$ */
  327. /* $Date$ */