ecc_test.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include <tomcrypt_test.h>
  10. #if defined(LTC_MECC)
  11. static unsigned int sizes[] = {
  12. #ifdef LTC_ECC_SECP112R1
  13. 14,
  14. #endif
  15. #ifdef LTC_ECC_SECP128R1
  16. 16,
  17. #endif
  18. #ifdef LTC_ECC_SECP160R1
  19. 20,
  20. #endif
  21. #ifdef LTC_ECC_SECP192R1
  22. 24,
  23. #endif
  24. #ifdef LTC_ECC_SECP224R1
  25. 28,
  26. #endif
  27. #ifdef LTC_ECC_SECP256R1
  28. 32,
  29. #endif
  30. #ifdef LTC_ECC_SECP384R1
  31. 48,
  32. #endif
  33. #ifdef LTC_ECC_SECP512R1
  34. 66
  35. #endif
  36. };
  37. #ifdef LTC_ECC_SHAMIR
  38. static int _ecc_test_shamir(void)
  39. {
  40. void *a, *modulus, *mp, *kA, *kB, *rA, *rB;
  41. void *mu, *ma;
  42. ecc_point *G, *A, *B, *C1, *C2;
  43. int x, y, z;
  44. unsigned char buf[ECC_BUF_SIZE];
  45. DO(mp_init_multi(&kA, &kB, &rA, &rB, &modulus, &a, &mu, &ma, NULL));
  46. LTC_ARGCHK((G = ltc_ecc_new_point()) != NULL);
  47. LTC_ARGCHK((A = ltc_ecc_new_point()) != NULL);
  48. LTC_ARGCHK((B = ltc_ecc_new_point()) != NULL);
  49. LTC_ARGCHK((C1 = ltc_ecc_new_point()) != NULL);
  50. LTC_ARGCHK((C2 = ltc_ecc_new_point()) != NULL);
  51. for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
  52. /* get the base point */
  53. for (z = 0; ltc_ecc_curves[z].prime != NULL; z++) {
  54. DO(mp_read_radix(modulus, ltc_ecc_curves[z].prime, 16));
  55. if (sizes[x] <= mp_unsigned_bin_size(modulus)) break;
  56. }
  57. LTC_ARGCHK(ltc_ecc_curves[z].prime != NULL);
  58. /* load it */
  59. DO(mp_read_radix(G->x, ltc_ecc_curves[z].Gx, 16));
  60. DO(mp_read_radix(G->y, ltc_ecc_curves[z].Gy, 16));
  61. DO(mp_set(G->z, 1));
  62. DO(mp_read_radix(a, ltc_ecc_curves[z].A, 16));
  63. DO(mp_montgomery_setup(modulus, &mp));
  64. DO(mp_montgomery_normalization(mu, modulus));
  65. DO(mp_mulmod(a, mu, modulus, ma));
  66. /* do 100 random tests */
  67. for (y = 0; y < 100; y++) {
  68. /* pick a random r1, r2 */
  69. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  70. DO(mp_read_unsigned_bin(rA, buf, sizes[x]));
  71. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  72. DO(mp_read_unsigned_bin(rB, buf, sizes[x]));
  73. /* compute rA * G = A */
  74. DO(ltc_mp.ecc_ptmul(rA, G, A, a, modulus, 1));
  75. /* compute rB * G = B */
  76. DO(ltc_mp.ecc_ptmul(rB, G, B, a, modulus, 1));
  77. /* pick a random kA, kB */
  78. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  79. DO(mp_read_unsigned_bin(kA, buf, sizes[x]));
  80. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  81. DO(mp_read_unsigned_bin(kB, buf, sizes[x]));
  82. /* now, compute kA*A + kB*B = C1 using the older method */
  83. DO(ltc_mp.ecc_ptmul(kA, A, C1, a, modulus, 0));
  84. DO(ltc_mp.ecc_ptmul(kB, B, C2, a, modulus, 0));
  85. DO(ltc_mp.ecc_ptadd(C1, C2, C1, a, modulus, mp));
  86. DO(ltc_mp.ecc_map(C1, modulus, mp));
  87. /* now compute using mul2add */
  88. DO(ltc_mp.ecc_mul2add(A, kA, B, kB, C2, ma, modulus));
  89. /* is they the sames? */
  90. if ((mp_cmp(C1->x, C2->x) != LTC_MP_EQ) || (mp_cmp(C1->y, C2->y) != LTC_MP_EQ) || (mp_cmp(C1->z, C2->z) != LTC_MP_EQ)) {
  91. fprintf(stderr, "ECC failed shamir test: size=%d, testno=%d\n", sizes[x], y);
  92. return 1;
  93. }
  94. }
  95. mp_montgomery_free(mp);
  96. }
  97. ltc_ecc_del_point(C2);
  98. ltc_ecc_del_point(C1);
  99. ltc_ecc_del_point(B);
  100. ltc_ecc_del_point(A);
  101. ltc_ecc_del_point(G);
  102. mp_clear_multi(kA, kB, rA, rB, modulus, a, mu, ma, NULL);
  103. return 0;
  104. }
  105. #endif
  106. static int _ecc_issue108(void)
  107. {
  108. void *a, *modulus, *order;
  109. ecc_point *Q, *Result;
  110. int err;
  111. const ltc_ecc_curve* dp;
  112. /* init */
  113. if ((err = mp_init_multi(&modulus, &order, &a, NULL)) != CRYPT_OK) { return err; }
  114. Q = ltc_ecc_new_point();
  115. Result = ltc_ecc_new_point();
  116. /* ECC-224 AKA SECP224R1 */
  117. if ((err = ecc_find_curve("SECP224R1", &dp)) != CRYPT_OK) { goto done; }
  118. /* read A */
  119. if ((err = mp_read_radix(a, (char *)dp->A, 16)) != CRYPT_OK) { goto done; }
  120. /* read modulus */
  121. if ((err = mp_read_radix(modulus, (char *)dp->prime, 16)) != CRYPT_OK) { goto done; }
  122. /* read order */
  123. if ((err = mp_read_radix(order, (char *)dp->order, 16)) != CRYPT_OK) { goto done; }
  124. /* read Q */
  125. if ((err = mp_read_radix(Q->x, (char *)"EA3745501BBC6A70BBFDD8AEEDB18CF5073C6DC9AA7CBB5915170D60", 16)) != CRYPT_OK) { goto done; }
  126. if ((err = mp_read_radix(Q->y, (char *)"6C9CB8E68AABFEC989CAC5E2326E0448B7E69C3E56039BA21A44FDAC", 16)) != CRYPT_OK) { goto done; }
  127. mp_set(Q->z, 1);
  128. /* calculate nQ */
  129. if ((err = ltc_mp.ecc_ptmul(order, Q, Result, a, modulus, 1)) != CRYPT_OK) { goto done; }
  130. done:
  131. ltc_ecc_del_point(Result);
  132. ltc_ecc_del_point(Q);
  133. mp_clear_multi(modulus, order, a, NULL);
  134. return err;
  135. }
  136. static int _ecc_test_mp(void)
  137. {
  138. void *a, *modulus, *order;
  139. ecc_point *G, *GG;
  140. int i, err, primality;
  141. if ((err = mp_init_multi(&modulus, &order, &a, NULL)) != CRYPT_OK) {
  142. return err;
  143. }
  144. G = ltc_ecc_new_point();
  145. GG = ltc_ecc_new_point();
  146. if (G == NULL || GG == NULL) {
  147. mp_clear_multi(modulus, order, NULL);
  148. ltc_ecc_del_point(G);
  149. ltc_ecc_del_point(GG);
  150. return CRYPT_MEM;
  151. }
  152. for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) {
  153. if ((err = mp_read_radix(a, (char *)ltc_ecc_curves[i].A, 16)) != CRYPT_OK) { goto done; }
  154. if ((err = mp_read_radix(modulus, (char *)ltc_ecc_curves[i].prime, 16)) != CRYPT_OK) { goto done; }
  155. if ((err = mp_read_radix(order, (char *)ltc_ecc_curves[i].order, 16)) != CRYPT_OK) { goto done; }
  156. /* is prime actually prime? */
  157. if ((err = mp_prime_is_prime(modulus, 8, &primality)) != CRYPT_OK) { goto done; }
  158. if (primality == 0) {
  159. err = CRYPT_FAIL_TESTVECTOR;
  160. goto done;
  161. }
  162. /* is order prime ? */
  163. if ((err = mp_prime_is_prime(order, 8, &primality)) != CRYPT_OK) { goto done; }
  164. if (primality == 0) {
  165. err = CRYPT_FAIL_TESTVECTOR;
  166. goto done;
  167. }
  168. if ((err = mp_read_radix(G->x, (char *)ltc_ecc_curves[i].Gx, 16)) != CRYPT_OK) { goto done; }
  169. if ((err = mp_read_radix(G->y, (char *)ltc_ecc_curves[i].Gy, 16)) != CRYPT_OK) { goto done; }
  170. mp_set(G->z, 1);
  171. /* then we should have G == (order + 1)G */
  172. if ((err = mp_add_d(order, 1, order)) != CRYPT_OK) { goto done; }
  173. if ((err = ltc_mp.ecc_ptmul(order, G, GG, a, modulus, 1)) != CRYPT_OK) { goto done; }
  174. if (mp_cmp(G->x, GG->x) != LTC_MP_EQ || mp_cmp(G->y, GG->y) != LTC_MP_EQ) {
  175. err = CRYPT_FAIL_TESTVECTOR;
  176. goto done;
  177. }
  178. }
  179. err = CRYPT_OK;
  180. done:
  181. ltc_ecc_del_point(GG);
  182. ltc_ecc_del_point(G);
  183. mp_clear_multi(order, modulus, a, NULL);
  184. return err;
  185. }
  186. static int _ecc_old_api(void)
  187. {
  188. unsigned char buf[4][4096], ch;
  189. unsigned long x, y, z, s;
  190. int stat, stat2;
  191. ecc_key usera, userb, pubKey, privKey;
  192. int low, high;
  193. ecc_sizes(&low, &high);
  194. if (low < 14 || high < 14 || low > 100 || high > 100 || high < low) return CRYPT_FAIL_TESTVECTOR;
  195. for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
  196. /* make up two keys */
  197. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  198. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
  199. if (ecc_get_size(&usera) != (int)sizes[s]) return CRYPT_FAIL_TESTVECTOR;
  200. if (ecc_get_size(&userb) != (int)sizes[s]) return CRYPT_FAIL_TESTVECTOR;
  201. /* make the shared secret */
  202. x = sizeof(buf[0]);
  203. DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
  204. y = sizeof(buf[1]);
  205. DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
  206. if (y != x) {
  207. fprintf(stderr, "ecc Shared keys are not same size.");
  208. return 1;
  209. }
  210. if (memcmp (buf[0], buf[1], x)) {
  211. fprintf(stderr, "ecc Shared keys not same contents.");
  212. return 1;
  213. }
  214. /* now export userb */
  215. y = sizeof(buf[0]);
  216. DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
  217. ecc_free (&userb);
  218. /* import and make the shared secret again */
  219. DO(ecc_import (buf[1], y, &userb));
  220. z = sizeof(buf[0]);
  221. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  222. if (z != x) {
  223. fprintf(stderr, "failed. Size don't match?");
  224. return 1;
  225. }
  226. if (memcmp (buf[0], buf[2], x)) {
  227. fprintf(stderr, "Failed. Contents didn't match.");
  228. return 1;
  229. }
  230. /* export with ANSI X9.63 */
  231. y = sizeof(buf[1]);
  232. DO(ecc_ansi_x963_export(&userb, buf[1], &y));
  233. ecc_free (&userb);
  234. /* now import the ANSI key */
  235. DO(ecc_ansi_x963_import(buf[1], y, &userb));
  236. /* shared secret */
  237. z = sizeof(buf[0]);
  238. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  239. if (z != x) {
  240. fprintf(stderr, "failed. Size don't match?");
  241. return 1;
  242. }
  243. if (memcmp (buf[0], buf[2], x)) {
  244. fprintf(stderr, "Failed. Contents didn't match.");
  245. return 1;
  246. }
  247. ecc_free (&usera);
  248. ecc_free (&userb);
  249. /* test encrypt_key */
  250. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  251. /* export key */
  252. x = sizeof(buf[0]);
  253. DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
  254. DO(ecc_import(buf[0], x, &pubKey));
  255. x = sizeof(buf[0]);
  256. DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
  257. DO(ecc_import(buf[0], x, &privKey));
  258. for (ch = 0; ch < 32; ch++) {
  259. buf[0][ch] = ch;
  260. }
  261. y = sizeof (buf[1]);
  262. DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
  263. zeromem (buf[0], sizeof (buf[0]));
  264. x = sizeof (buf[0]);
  265. DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
  266. if (x != 32) {
  267. fprintf(stderr, "Failed (length)");
  268. return 1;
  269. }
  270. for (ch = 0; ch < 32; ch++) {
  271. if (buf[0][ch] != ch) {
  272. fprintf(stderr, "Failed (contents)");
  273. return 1;
  274. }
  275. }
  276. /* test sign_hash */
  277. for (ch = 0; ch < 16; ch++) {
  278. buf[0][ch] = ch;
  279. }
  280. x = sizeof (buf[1]);
  281. DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
  282. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
  283. buf[0][0] ^= 1;
  284. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
  285. if (!(stat == 1 && stat2 == 0)) {
  286. fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2);
  287. return 1;
  288. }
  289. /* test sign_hash_rfc7518 */
  290. for (ch = 0; ch < 16; ch++) {
  291. buf[0][ch] = ch;
  292. }
  293. x = sizeof (buf[1]);
  294. DO(ecc_sign_hash_rfc7518(buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
  295. DO(ecc_verify_hash_rfc7518(buf[1], x, buf[0], 16, &stat, &pubKey));
  296. buf[0][0] ^= 1;
  297. DO(ecc_verify_hash_rfc7518(buf[1], x, buf[0], 16, &stat2, &privKey));
  298. if (!(stat == 1 && stat2 == 0)) {
  299. fprintf(stderr, "ecc_verify_hash_rfc7518 failed %d, %d, ", stat, stat2);
  300. return 1;
  301. }
  302. ecc_free (&usera);
  303. ecc_free (&pubKey);
  304. ecc_free (&privKey);
  305. }
  306. return CRYPT_OK;
  307. }
  308. static int _ecc_new_api(void)
  309. {
  310. const char* names[] = {
  311. #ifdef LTC_ECC_SECP112R1
  312. "SECP112R1", "ECC-112",
  313. "secp112r1", /* name is case-insensitive */
  314. "S E C-P-1_1_2r1", /* should pass fuzzy matching */
  315. #endif
  316. #ifdef LTC_ECC_SECP112R2
  317. "SECP112R2",
  318. #endif
  319. #ifdef LTC_ECC_SECP128R1
  320. "SECP128R1", "ECC-128",
  321. #endif
  322. #ifdef LTC_ECC_SECP128R2
  323. "SECP128R2",
  324. #endif
  325. #ifdef LTC_ECC_SECP160R1
  326. "SECP160R1", "ECC-160",
  327. #endif
  328. #ifdef LTC_ECC_SECP160R2
  329. "SECP160R2",
  330. #endif
  331. #ifdef LTC_ECC_SECP160K1
  332. "SECP160K1",
  333. #endif
  334. #ifdef LTC_ECC_BRAINPOOLP160R1
  335. "BRAINPOOLP160R1",
  336. #endif
  337. #ifdef LTC_ECC_SECP192R1
  338. "SECP192R1", "NISTP192", "PRIME192V1", "ECC-192", "P-192",
  339. #endif
  340. #ifdef LTC_ECC_PRIME192V2
  341. "PRIME192V2",
  342. #endif
  343. #ifdef LTC_ECC_PRIME192V3
  344. "PRIME192V3",
  345. #endif
  346. #ifdef LTC_ECC_SECP192K1
  347. "SECP192K1",
  348. #endif
  349. #ifdef LTC_ECC_BRAINPOOLP192R1
  350. "BRAINPOOLP192R1",
  351. #endif
  352. #ifdef LTC_ECC_SECP224R1
  353. "SECP224R1", "NISTP224", "ECC-224", "P-224",
  354. #endif
  355. #ifdef LTC_ECC_SECP224K1
  356. "SECP224K1",
  357. #endif
  358. #ifdef LTC_ECC_BRAINPOOLP224R1
  359. "BRAINPOOLP224R1",
  360. #endif
  361. #ifdef LTC_ECC_PRIME239V1
  362. "PRIME239V1",
  363. #endif
  364. #ifdef LTC_ECC_PRIME239V2
  365. "PRIME239V2",
  366. #endif
  367. #ifdef LTC_ECC_PRIME239V3
  368. "PRIME239V3",
  369. #endif
  370. #ifdef LTC_ECC_SECP256R1
  371. "SECP256R1", "NISTP256", "PRIME256V1", "ECC-256", "P-256",
  372. #endif
  373. #ifdef LTC_ECC_SECP256K1
  374. "SECP256K1",
  375. #endif
  376. #ifdef LTC_ECC_BRAINPOOLP256R1
  377. "BRAINPOOLP256R1",
  378. #endif
  379. #ifdef LTC_ECC_BRAINPOOLP320R1
  380. "BRAINPOOLP320R1",
  381. #endif
  382. #ifdef LTC_ECC_SECP384R1
  383. "SECP384R1", "NISTP384", "ECC-384", "P-384",
  384. #endif
  385. #ifdef LTC_ECC_BRAINPOOLP384R1
  386. "BRAINPOOLP384R1",
  387. #endif
  388. #ifdef LTC_ECC_BRAINPOOLP512R1
  389. "BRAINPOOLP512R1",
  390. #endif
  391. #ifdef LTC_ECC_SECP521R1
  392. "SECP521R1", "NISTP521", "ECC-521", "P-521",
  393. #endif
  394. };
  395. int i, j, stat;
  396. const ltc_ecc_curve* dp;
  397. ecc_key key, privkey, pubkey;
  398. unsigned char buf[1000];
  399. unsigned long len;
  400. unsigned char data16[16] = { 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1 };
  401. unsigned long len16;
  402. for (i = 0; i < (int)(sizeof(names)/sizeof(names[0])); i++) {
  403. DO(ecc_find_curve(names[i], &dp));
  404. /* make new key */
  405. DO(ecc_make_key_ex(&yarrow_prng, find_prng ("yarrow"), &key, dp));
  406. len = sizeof(buf);
  407. DO(ecc_export(buf, &len, PK_PRIVATE, &key));
  408. DO(ecc_import_ex(buf, len, &privkey, dp));
  409. ecc_free(&privkey);
  410. len = sizeof(buf);
  411. DO(ecc_export(buf, &len, PK_PUBLIC, &key));
  412. DO(ecc_import_ex(buf, len, &pubkey, dp));
  413. ecc_free(&pubkey);
  414. len = sizeof(buf);
  415. DO(ecc_ansi_x963_export(&key, buf, &len));
  416. ecc_free(&key);
  417. DO(ecc_ansi_x963_import_ex(buf, len, &pubkey, dp));
  418. ecc_free(&pubkey);
  419. /* generate new key */
  420. DO(ecc_set_curve(dp, &key));
  421. DO(ecc_generate_key(&yarrow_prng, find_prng ("yarrow"), &key));
  422. len = sizeof(buf);
  423. DO(ecc_get_key(buf, &len, PK_PRIVATE, &key));
  424. ecc_free(&key);
  425. /* load exported private key */
  426. DO(ecc_set_curve(dp, &privkey));
  427. DO(ecc_set_key(buf, len, PK_PRIVATE, &privkey));
  428. #ifndef USE_TFM
  429. /* XXX-FIXME: TFM does not support sqrtmod_prime */
  430. /* export compressed public key */
  431. len = sizeof(buf);
  432. DO(ecc_get_key(buf, &len, PK_PUBLIC|PK_COMPRESSED, &privkey));
  433. if (len != 1 + (unsigned)ecc_get_size(&privkey)) return CRYPT_FAIL_TESTVECTOR;
  434. /* load exported public+compressed key */
  435. DO(ecc_set_curve(dp, &pubkey));
  436. DO(ecc_set_key(buf, len, PK_PUBLIC, &pubkey));
  437. ecc_free(&pubkey);
  438. #endif
  439. /* export long public key */
  440. len = sizeof(buf);
  441. DO(ecc_get_key(buf, &len, PK_PUBLIC, &privkey));
  442. if (len != 1 + 2 * (unsigned)ecc_get_size(&privkey)) return CRYPT_FAIL_TESTVECTOR;
  443. /* load exported public key */
  444. DO(ecc_set_curve(dp, &pubkey));
  445. DO(ecc_set_key(buf, len, PK_PUBLIC, &pubkey));
  446. /* test signature */
  447. len = sizeof(buf);
  448. DO(ecc_sign_hash(data16, 16, buf, &len, &yarrow_prng, find_prng ("yarrow"), &privkey));
  449. stat = 0;
  450. DO(ecc_verify_hash(buf, len, data16, 16, &stat, &pubkey));
  451. if (stat != 1) return CRYPT_FAIL_TESTVECTOR;
  452. /* test encryption */
  453. len = sizeof(buf);
  454. DO(ecc_encrypt_key(data16, 16, buf, &len, &yarrow_prng, find_prng("yarrow"), find_hash("sha256"), &pubkey));
  455. zeromem(data16, 16);
  456. len16 = 16;
  457. DO(ecc_decrypt_key(buf, len, data16, &len16, &privkey));
  458. if (len16 != 16) return CRYPT_FAIL_TESTVECTOR;
  459. for (j = 0; j < 16; j++) if (data16[j] != 0xd1) return CRYPT_FAIL_TESTVECTOR;
  460. /* cleanup */
  461. ecc_free(&privkey);
  462. ecc_free(&pubkey);
  463. }
  464. return CRYPT_OK;
  465. }
  466. static int _ecc_key_cmp(const int should_type, const ecc_key *should, const ecc_key *is)
  467. {
  468. if (should_type != is->type) return CRYPT_ERROR;
  469. if (should_type == PK_PRIVATE) {
  470. if (mp_cmp(should->k, is->k) != LTC_MP_EQ) return CRYPT_ERROR;
  471. }
  472. if (mp_cmp(should->dp.prime, is->dp.prime) != LTC_MP_EQ) return CRYPT_ERROR;
  473. if (mp_cmp(should->dp.A, is->dp.A) != LTC_MP_EQ) return CRYPT_ERROR;
  474. if (mp_cmp(should->dp.B, is->dp.B) != LTC_MP_EQ) return CRYPT_ERROR;
  475. if (mp_cmp(should->dp.order, is->dp.order) != LTC_MP_EQ) return CRYPT_ERROR;
  476. if (mp_cmp(should->dp.base.x, is->dp.base.x) != LTC_MP_EQ) return CRYPT_ERROR;
  477. if (mp_cmp(should->dp.base.y, is->dp.base.y) != LTC_MP_EQ) return CRYPT_ERROR;
  478. if (mp_cmp(should->pubkey.x, is->pubkey.x) != LTC_MP_EQ) return CRYPT_ERROR;
  479. if (mp_cmp(should->pubkey.y, is->pubkey.y) != LTC_MP_EQ) return CRYPT_ERROR;
  480. if (should->dp.size != is->dp.size) return CRYPT_ERROR;
  481. if (should->dp.cofactor != is->dp.cofactor) return CRYPT_ERROR;
  482. return CRYPT_OK;
  483. }
  484. static int _ecc_import_export(void) {
  485. const ltc_ecc_curve *cu;
  486. ecc_key key, pri, pub;
  487. unsigned char out[300];
  488. unsigned long outlen;
  489. /* the following test keys were generated by:
  490. # no password
  491. openssl ecparam -name secp256k1 -genkey -out main-key.pem
  492. openssl ec -in main-key.pem -param_enc explicit -out long_pri.der -outform DER
  493. openssl ec -in main-key.pem -param_enc explicit -conv_form compressed -out long_pric.der -outform DER
  494. openssl ec -in main-key.pem -param_enc explicit -pubout -out long_pub.der -outform DER
  495. openssl ec -in main-key.pem -param_enc explicit -pubout -conv_form compressed -out long_pubc.der -outform DER
  496. openssl ec -in main-key.pem -param_enc named_curve -out short_pri.der -outform DER
  497. openssl ec -in main-key.pem -param_enc named_curve -conv_form compressed -out short_pric.der -outform DER
  498. openssl ec -in main-key.pem -param_enc named_curve -pubout -out short_pub.der -outform DER
  499. openssl ec -in main-key.pem -param_enc named_curve -pubout -conv_form compressed -out short_pubc.der -outform DER
  500. # X.509 EC certificates
  501. openssl req -new -x509 -keyform der -key long_pri.der -sha512 -subj '/CN=Test Cert EC' -out x509_cert_long.der -outform der -days 365000
  502. openssl req -new -x509 -keyform der -key long_pric.der -sha512 -subj '/CN=Test Cert EC' -out x509_cert_longc.der -outform der -days 365000
  503. openssl req -new -x509 -keyform der -key short_pri.der -sha512 -subj '/CN=Test Cert EC' -out x509_cert_short.der -outform der -days 365000
  504. openssl req -new -x509 -keyform der -key short_pric.der -sha512 -subj '/CN=Test Cert EC' -out x509_cert_shortc.der -outform der -days 365000
  505. */
  506. static const unsigned char long_pri[] = { /* private + long public, explicit curve params */
  507. 0x30, 0x82, 0x01, 0x13, 0x02, 0x01, 0x01, 0x04, 0x20, 0x0c, 0xf1, 0xad, 0x2f, 0x03, 0xf7, 0x91,
  508. 0x1b, 0xba, 0x03, 0xcf, 0x23, 0x37, 0xc8, 0xf2, 0xf7, 0x36, 0xce, 0x65, 0xf1, 0x84, 0x2d, 0x7d,
  509. 0x9f, 0x5f, 0x9e, 0x21, 0xd9, 0x5e, 0x49, 0xbd, 0x23, 0xa0, 0x81, 0xa5, 0x30, 0x81, 0xa2, 0x02,
  510. 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00,
  511. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  512. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2f,
  513. 0x30, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe, 0x66, 0x7e, 0xf9,
  514. 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d,
  515. 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a, 0xda, 0x77, 0x26,
  516. 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17, 0xb4, 0x48, 0xa6,
  517. 0x85, 0x54, 0x19, 0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21, 0x00, 0xff, 0xff,
  518. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xba, 0xae,
  519. 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41, 0x02, 0x01,
  520. 0x01, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1,
  521. 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5,
  522. 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16, 0x91, 0xbd, 0xb2, 0xb9, 0x1b, 0x40, 0x10, 0x5a, 0xb7,
  523. 0x6c, 0x6e, 0x32, 0x5b, 0xf7, 0x63, 0x62, 0x94, 0x24, 0x24, 0xdb, 0xec, 0x3f, 0x8b, 0xe5, 0x6e,
  524. 0x4b, 0x64, 0x37, 0x31, 0x24, 0x79, 0x4d
  525. };
  526. static const unsigned char long_pric[] = { /* private + compressed public, explicit curve params */
  527. 0x30, 0x81, 0xd3, 0x02, 0x01, 0x01, 0x04, 0x20, 0x0c, 0xf1, 0xad, 0x2f, 0x03, 0xf7, 0x91, 0x1b,
  528. 0xba, 0x03, 0xcf, 0x23, 0x37, 0xc8, 0xf2, 0xf7, 0x36, 0xce, 0x65, 0xf1, 0x84, 0x2d, 0x7d, 0x9f,
  529. 0x5f, 0x9e, 0x21, 0xd9, 0x5e, 0x49, 0xbd, 0x23, 0xa0, 0x81, 0x85, 0x30, 0x81, 0x82, 0x02, 0x01,
  530. 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xff,
  531. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  532. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2f, 0x30,
  533. 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04, 0x21, 0x02, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc,
  534. 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce,
  535. 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff,
  536. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xba, 0xae, 0xdc,
  537. 0xe6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41, 0x02, 0x01, 0x01,
  538. 0xa1, 0x24, 0x03, 0x22, 0x00, 0x03, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf,
  539. 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9,
  540. 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16
  541. };
  542. static const unsigned char long_pub[] = { /* long public, explicit curve params */
  543. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  544. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  545. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  546. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  547. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  548. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  549. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  550. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  551. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  552. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  553. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  554. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e,
  555. 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e,
  556. 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16, 0x91, 0xbd, 0xb2, 0xb9, 0x1b, 0x40, 0x10, 0x5a,
  557. 0xb7, 0x6c, 0x6e, 0x32, 0x5b, 0xf7, 0x63, 0x62, 0x94, 0x24, 0x24, 0xdb, 0xec, 0x3f, 0x8b, 0xe5,
  558. 0x6e, 0x4b, 0x64, 0x37, 0x31, 0x24, 0x79, 0x4d
  559. };
  560. static const unsigned char long_pubc[] = { /* compressed public, explicit curve params */
  561. 0x30, 0x81, 0xb5, 0x30, 0x81, 0x8e, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  562. 0x81, 0x82, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  563. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  564. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  565. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04, 0x21, 0x02, 0x79, 0xbe,
  566. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  567. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x02, 0x21,
  568. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  569. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  570. 0x41, 0x02, 0x01, 0x01, 0x03, 0x22, 0x00, 0x03, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e,
  571. 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e,
  572. 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16
  573. };
  574. static const unsigned char short_pri[] = { /* private + long public, curve by OID */
  575. 0x30, 0x74, 0x02, 0x01, 0x01, 0x04, 0x20, 0x0c, 0xf1, 0xad, 0x2f, 0x03, 0xf7, 0x91, 0x1b, 0xba,
  576. 0x03, 0xcf, 0x23, 0x37, 0xc8, 0xf2, 0xf7, 0x36, 0xce, 0x65, 0xf1, 0x84, 0x2d, 0x7d, 0x9f, 0x5f,
  577. 0x9e, 0x21, 0xd9, 0x5e, 0x49, 0xbd, 0x23, 0xa0, 0x07, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a,
  578. 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf,
  579. 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9,
  580. 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16, 0x91, 0xbd, 0xb2, 0xb9, 0x1b, 0x40, 0x10, 0x5a, 0xb7, 0x6c,
  581. 0x6e, 0x32, 0x5b, 0xf7, 0x63, 0x62, 0x94, 0x24, 0x24, 0xdb, 0xec, 0x3f, 0x8b, 0xe5, 0x6e, 0x4b,
  582. 0x64, 0x37, 0x31, 0x24, 0x79, 0x4d
  583. };
  584. static const unsigned char short_pric[] = { /* private + compressed public, curve by OID */
  585. 0x30, 0x54, 0x02, 0x01, 0x01, 0x04, 0x20, 0x0c, 0xf1, 0xad, 0x2f, 0x03, 0xf7, 0x91, 0x1b, 0xba,
  586. 0x03, 0xcf, 0x23, 0x37, 0xc8, 0xf2, 0xf7, 0x36, 0xce, 0x65, 0xf1, 0x84, 0x2d, 0x7d, 0x9f, 0x5f,
  587. 0x9e, 0x21, 0xd9, 0x5e, 0x49, 0xbd, 0x23, 0xa0, 0x07, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a,
  588. 0xa1, 0x24, 0x03, 0x22, 0x00, 0x03, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf,
  589. 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9,
  590. 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16
  591. };
  592. static const unsigned char short_pub[] = { /* long public, curve by OID */
  593. 0x30, 0x56, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b,
  594. 0x81, 0x04, 0x00, 0x0a, 0x03, 0x42, 0x00, 0x04, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e,
  595. 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e,
  596. 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16, 0x91, 0xbd, 0xb2, 0xb9, 0x1b, 0x40, 0x10, 0x5a,
  597. 0xb7, 0x6c, 0x6e, 0x32, 0x5b, 0xf7, 0x63, 0x62, 0x94, 0x24, 0x24, 0xdb, 0xec, 0x3f, 0x8b, 0xe5,
  598. 0x6e, 0x4b, 0x64, 0x37, 0x31, 0x24, 0x79, 0x4d
  599. };
  600. static const unsigned char short_pubc[] = { /* compressed public, curve by OID */
  601. 0x30, 0x36, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b,
  602. 0x81, 0x04, 0x00, 0x0a, 0x03, 0x22, 0x00, 0x03, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e,
  603. 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e,
  604. 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16
  605. };
  606. static const unsigned char raw_pri[] = { /* raw private key */
  607. 0x0c, 0xf1, 0xad, 0x2f, 0x03, 0xf7, 0x91, 0x1b, 0xba, 0x03, 0xcf, 0x23, 0x37, 0xc8, 0xf2, 0xf7,
  608. 0x36, 0xce, 0x65, 0xf1, 0x84, 0x2d, 0x7d, 0x9f, 0x5f, 0x9e, 0x21, 0xd9, 0x5e, 0x49, 0xbd, 0x23
  609. };
  610. static const unsigned char raw_pub[] = { /* raw public key - long form */
  611. 0x04, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba,
  612. 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5,
  613. 0x16, 0x91, 0xbd, 0xb2, 0xb9, 0x1b, 0x40, 0x10, 0x5a, 0xb7, 0x6c, 0x6e, 0x32, 0x5b, 0xf7, 0x63,
  614. 0x62, 0x94, 0x24, 0x24, 0xdb, 0xec, 0x3f, 0x8b, 0xe5, 0x6e, 0x4b, 0x64, 0x37, 0x31, 0x24, 0x79,
  615. 0x4d
  616. };
  617. static const unsigned char raw_pubc[] = { /* raw public key - compressed form */
  618. 0x03, 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba,
  619. 0xb6, 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5,
  620. 0x16
  621. };
  622. static const unsigned char x509_cert_long[] = { /* X.509 cert, long pubkey, explicit curve params */
  623. 0x30, 0x82, 0x02, 0x13, 0x30, 0x82, 0x01, 0xba, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
  624. 0xaf, 0x14, 0xe3, 0x53, 0x36, 0x06, 0x79, 0x34, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
  625. 0x3d, 0x04, 0x03, 0x04, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
  626. 0x0c, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x20, 0x17,
  627. 0x0d, 0x31, 0x37, 0x31, 0x32, 0x33, 0x30, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x18, 0x0f,
  628. 0x33, 0x30, 0x31, 0x37, 0x30, 0x35, 0x30, 0x32, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x30,
  629. 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x54, 0x65, 0x73, 0x74,
  630. 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  631. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30, 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  632. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  633. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  634. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  635. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  636. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  637. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a, 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  638. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17, 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  639. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  640. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  641. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  642. 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6,
  643. 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16,
  644. 0x91, 0xbd, 0xb2, 0xb9, 0x1b, 0x40, 0x10, 0x5a, 0xb7, 0x6c, 0x6e, 0x32, 0x5b, 0xf7, 0x63, 0x62,
  645. 0x94, 0x24, 0x24, 0xdb, 0xec, 0x3f, 0x8b, 0xe5, 0x6e, 0x4b, 0x64, 0x37, 0x31, 0x24, 0x79, 0x4d,
  646. 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x66,
  647. 0xc9, 0x90, 0x3c, 0x8a, 0x81, 0xa3, 0x1c, 0x20, 0x61, 0xd2, 0xf3, 0xf5, 0xae, 0xa8, 0x85, 0x70,
  648. 0xf9, 0x1f, 0x2c, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14,
  649. 0x66, 0xc9, 0x90, 0x3c, 0x8a, 0x81, 0xa3, 0x1c, 0x20, 0x61, 0xd2, 0xf3, 0xf5, 0xae, 0xa8, 0x85,
  650. 0x70, 0xf9, 0x1f, 0x2c, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  651. 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04, 0x03, 0x47,
  652. 0x00, 0x30, 0x44, 0x02, 0x1f, 0x2a, 0x62, 0x64, 0x05, 0x67, 0xb0, 0x2c, 0xa0, 0xa3, 0xb8, 0x61,
  653. 0x4e, 0x87, 0x06, 0x69, 0xf2, 0xda, 0x78, 0xd6, 0x0e, 0x8d, 0x9b, 0xf1, 0x43, 0x5f, 0xf6, 0x40,
  654. 0x9d, 0x9d, 0xbd, 0xce, 0x02, 0x21, 0x00, 0xe9, 0x6f, 0x79, 0xb4, 0x4a, 0x00, 0xf7, 0xfa, 0x81,
  655. 0x25, 0x29, 0xec, 0x79, 0xb2, 0xfa, 0x86, 0xf8, 0x84, 0xd1, 0x78, 0xe7, 0xf8, 0xfd, 0x76, 0x2d,
  656. 0x4f, 0xfe, 0x02, 0x72, 0xba, 0x6c, 0xca
  657. };
  658. static const unsigned char x509_cert_longc[] = { /* X.509 cert, compressed pubkey, explicit curve params */
  659. 0x30, 0x82, 0x01, 0xd3, 0x30, 0x82, 0x01, 0x7a, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
  660. 0x90, 0x5b, 0x48, 0x32, 0x37, 0x4b, 0x72, 0x54, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
  661. 0x3d, 0x04, 0x03, 0x04, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
  662. 0x0c, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x20, 0x17,
  663. 0x0d, 0x31, 0x37, 0x31, 0x32, 0x33, 0x30, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x18, 0x0f,
  664. 0x33, 0x30, 0x31, 0x37, 0x30, 0x35, 0x30, 0x32, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x30,
  665. 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x54, 0x65, 0x73, 0x74,
  666. 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x81, 0xb5, 0x30, 0x81, 0x8e, 0x06, 0x07,
  667. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30, 0x81, 0x82, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  668. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  669. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  670. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  671. 0x04, 0x01, 0x07, 0x04, 0x21, 0x02, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  672. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  673. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  674. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  675. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41, 0x02, 0x01, 0x01, 0x03, 0x22, 0x00, 0x03,
  676. 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6,
  677. 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16,
  678. 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xca,
  679. 0x2a, 0xa1, 0x12, 0x97, 0x96, 0x2c, 0x85, 0xd3, 0x1f, 0xb1, 0x34, 0x7c, 0x26, 0xe9, 0xd6, 0x49,
  680. 0x9f, 0x98, 0xcf, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14,
  681. 0xca, 0x2a, 0xa1, 0x12, 0x97, 0x96, 0x2c, 0x85, 0xd3, 0x1f, 0xb1, 0x34, 0x7c, 0x26, 0xe9, 0xd6,
  682. 0x49, 0x9f, 0x98, 0xcf, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  683. 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04, 0x03, 0x47,
  684. 0x00, 0x30, 0x44, 0x02, 0x20, 0x24, 0x7a, 0xc1, 0xb4, 0x7d, 0x1c, 0x3c, 0x23, 0xc6, 0xad, 0xea,
  685. 0x04, 0x27, 0x27, 0x65, 0xb8, 0x72, 0x93, 0x46, 0xc9, 0xe9, 0x60, 0x8f, 0xca, 0x96, 0x30, 0x60,
  686. 0xb3, 0x22, 0xf7, 0x3b, 0x01, 0x02, 0x20, 0x48, 0x30, 0x2a, 0x58, 0x18, 0x46, 0xdb, 0x50, 0x3e,
  687. 0xad, 0xc3, 0xca, 0xcd, 0x6d, 0x83, 0xd4, 0xc3, 0xc4, 0xa4, 0x8f, 0x37, 0xc3, 0x1d, 0x83, 0x3c,
  688. 0xd3, 0x1f, 0x8f, 0x38, 0x29, 0x75, 0x2c
  689. };
  690. static const unsigned char x509_cert_short[] = { /* X.509 cert, long pubkey, curve by OID */
  691. 0x30, 0x82, 0x01, 0x74, 0x30, 0x82, 0x01, 0x1a, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
  692. 0xbd, 0x81, 0x04, 0x29, 0x43, 0x12, 0x79, 0xce, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
  693. 0x3d, 0x04, 0x03, 0x04, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c,
  694. 0x0c, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x20, 0x17,
  695. 0x0d, 0x31, 0x37, 0x31, 0x32, 0x33, 0x30, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x18, 0x0f,
  696. 0x33, 0x30, 0x31, 0x37, 0x30, 0x35, 0x30, 0x32, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x30,
  697. 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x54, 0x65, 0x73, 0x74,
  698. 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x56, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86,
  699. 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a, 0x03, 0x42, 0x00, 0x04,
  700. 0x2a, 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6,
  701. 0x7f, 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16,
  702. 0x91, 0xbd, 0xb2, 0xb9, 0x1b, 0x40, 0x10, 0x5a, 0xb7, 0x6c, 0x6e, 0x32, 0x5b, 0xf7, 0x63, 0x62,
  703. 0x94, 0x24, 0x24, 0xdb, 0xec, 0x3f, 0x8b, 0xe5, 0x6e, 0x4b, 0x64, 0x37, 0x31, 0x24, 0x79, 0x4d,
  704. 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x66,
  705. 0xc9, 0x90, 0x3c, 0x8a, 0x81, 0xa3, 0x1c, 0x20, 0x61, 0xd2, 0xf3, 0xf5, 0xae, 0xa8, 0x85, 0x70,
  706. 0xf9, 0x1f, 0x2c, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14,
  707. 0x66, 0xc9, 0x90, 0x3c, 0x8a, 0x81, 0xa3, 0x1c, 0x20, 0x61, 0xd2, 0xf3, 0xf5, 0xae, 0xa8, 0x85,
  708. 0x70, 0xf9, 0x1f, 0x2c, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  709. 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04, 0x03, 0x48,
  710. 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0x9b, 0x4e, 0xb2, 0x6a, 0xcc, 0xfa, 0x02, 0x69, 0x22, 0x6a,
  711. 0x32, 0x9c, 0x0c, 0xaa, 0x4c, 0xdf, 0xbb, 0x9b, 0x22, 0xfb, 0xd6, 0xec, 0x5d, 0xf9, 0x87, 0x82,
  712. 0xeb, 0x37, 0xb8, 0x32, 0x7c, 0xd6, 0x02, 0x20, 0x50, 0x8b, 0x9f, 0xc1, 0xa8, 0x4a, 0xff, 0x49,
  713. 0x0d, 0x7e, 0x04, 0x2d, 0x93, 0x3e, 0xdb, 0x30, 0xbc, 0x93, 0xd1, 0x16, 0x1d, 0x99, 0xbd, 0x3f,
  714. 0xfa, 0x2a, 0x6d, 0xe0, 0x2a, 0x83, 0x55, 0x5d
  715. };
  716. static const unsigned char x509_cert_shortc[] = { /* X.509 cert, compressed pubkey, curve by OID */
  717. 0x30, 0x82, 0x01, 0x54, 0x30, 0x81, 0xfa, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00, 0x85,
  718. 0x45, 0x77, 0x75, 0x02, 0x95, 0xf7, 0x06, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  719. 0x04, 0x03, 0x04, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c,
  720. 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x20, 0x17, 0x0d,
  721. 0x31, 0x37, 0x31, 0x32, 0x33, 0x30, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x18, 0x0f, 0x33,
  722. 0x30, 0x31, 0x37, 0x30, 0x35, 0x30, 0x32, 0x32, 0x30, 0x33, 0x33, 0x34, 0x31, 0x5a, 0x30, 0x17,
  723. 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x54, 0x65, 0x73, 0x74, 0x20,
  724. 0x43, 0x65, 0x72, 0x74, 0x20, 0x45, 0x43, 0x30, 0x36, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48,
  725. 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a, 0x03, 0x22, 0x00, 0x03, 0x2a,
  726. 0xf9, 0x0b, 0xda, 0xbe, 0x71, 0x66, 0x9e, 0xd1, 0xcf, 0x12, 0xd0, 0x24, 0xaf, 0xba, 0xb6, 0x7f,
  727. 0xfb, 0x96, 0x27, 0x3e, 0x2f, 0xbd, 0x1e, 0xd5, 0xf9, 0x8d, 0x6c, 0x73, 0x9d, 0xc5, 0x16, 0xa3,
  728. 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xca, 0x2a,
  729. 0xa1, 0x12, 0x97, 0x96, 0x2c, 0x85, 0xd3, 0x1f, 0xb1, 0x34, 0x7c, 0x26, 0xe9, 0xd6, 0x49, 0x9f,
  730. 0x98, 0xcf, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xca,
  731. 0x2a, 0xa1, 0x12, 0x97, 0x96, 0x2c, 0x85, 0xd3, 0x1f, 0xb1, 0x34, 0x7c, 0x26, 0xe9, 0xd6, 0x49,
  732. 0x9f, 0x98, 0xcf, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01,
  733. 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04, 0x03, 0x49, 0x00,
  734. 0x30, 0x46, 0x02, 0x21, 0x00, 0xa4, 0xf9, 0x41, 0x2b, 0x4b, 0x56, 0xa5, 0xd4, 0x8c, 0xdf, 0xb0,
  735. 0x14, 0xe3, 0xe7, 0xed, 0xcc, 0xc4, 0x46, 0x42, 0x04, 0xec, 0x15, 0x9f, 0xe1, 0xb2, 0x00, 0x07,
  736. 0x8c, 0xc1, 0xf9, 0x25, 0xed, 0x02, 0x21, 0x00, 0x81, 0xd8, 0xc4, 0x3a, 0x9f, 0xdf, 0xc1, 0x70,
  737. 0x9d, 0x7b, 0x70, 0x3e, 0xf5, 0x7d, 0xa4, 0xfd, 0x3c, 0xc6, 0x49, 0x93, 0xd3, 0x5b, 0xef, 0xc9,
  738. 0xae, 0x97, 0xaf, 0x64, 0x64, 0xf9, 0x69, 0xd8
  739. };
  740. if (ltc_mp.sqrtmod_prime == NULL) return CRYPT_NOP; /* we need compressed points which requires sqrtmod_prime */
  741. DO(ecc_import_openssl(short_pub, sizeof(short_pub), &pub));
  742. DO(ecc_import_openssl(short_pri, sizeof(short_pri), &pri));
  743. DO(ecc_find_curve("SECP256K1", &cu));
  744. /* import - raw keys */
  745. DO(ecc_set_curve(cu, &key));
  746. DO(ecc_set_key(raw_pri, sizeof(raw_pri), PK_PRIVATE, &key));
  747. DO(_ecc_key_cmp(PK_PRIVATE, &pri, &key));
  748. ecc_free(&key);
  749. DO(ecc_set_curve(cu, &key));
  750. DO(ecc_set_key(raw_pub, sizeof(raw_pub), PK_PUBLIC, &key));
  751. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  752. ecc_free(&key);
  753. DO(ecc_set_curve(cu, &key));
  754. DO(ecc_set_key(raw_pubc, sizeof(raw_pubc), PK_PUBLIC, &key));
  755. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  756. ecc_free(&key);
  757. /* import - openssl compatible DER format */
  758. DO(ecc_import_openssl(long_pri, sizeof(long_pri), &key));
  759. DO(_ecc_key_cmp(PK_PRIVATE, &pri, &key));
  760. ecc_free(&key);
  761. DO(ecc_import_openssl(long_pric, sizeof(long_pric), &key));
  762. DO(_ecc_key_cmp(PK_PRIVATE, &pri, &key));
  763. ecc_free(&key);
  764. DO(ecc_import_openssl(long_pub, sizeof(long_pub), &key));
  765. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  766. ecc_free(&key);
  767. DO(ecc_import_openssl(long_pubc, sizeof(long_pubc), &key));
  768. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  769. ecc_free(&key);
  770. DO(ecc_import_openssl(short_pri, sizeof(short_pri), &key));
  771. DO(_ecc_key_cmp(PK_PRIVATE, &pri, &key));
  772. ecc_free(&key);
  773. DO(ecc_import_openssl(short_pric, sizeof(short_pric), &key));
  774. DO(_ecc_key_cmp(PK_PRIVATE, &pri, &key));
  775. ecc_free(&key);
  776. DO(ecc_import_openssl(short_pub, sizeof(short_pub), &key));
  777. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  778. ecc_free(&key);
  779. DO(ecc_import_openssl(short_pubc, sizeof(short_pubc), &key));
  780. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  781. ecc_free(&key);
  782. /* import - X.509 EC certificates */
  783. DO(ecc_import_x509(x509_cert_long, sizeof(x509_cert_long), &key));
  784. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  785. ecc_free(&key);
  786. DO(ecc_import_x509(x509_cert_longc, sizeof(x509_cert_longc), &key));
  787. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  788. ecc_free(&key);
  789. DO(ecc_import_x509(x509_cert_short, sizeof(x509_cert_short), &key));
  790. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  791. ecc_free(&key);
  792. DO(ecc_import_x509(x509_cert_shortc, sizeof(x509_cert_shortc), &key));
  793. DO(_ecc_key_cmp(PK_PUBLIC, &pub, &key));
  794. ecc_free(&key);
  795. /* export - openssl compatible DER format */
  796. outlen = sizeof(out);
  797. DO(ecc_export_openssl(out, &outlen, PK_PRIVATE, &pri));
  798. if (compare_testvector(out, outlen, long_pri, sizeof(long_pri), "e-long_pri", 0)) return CRYPT_ERROR;
  799. outlen = sizeof(out);
  800. DO(ecc_export_openssl(out, &outlen, PK_PRIVATE|PK_COMPRESSED, &pri));
  801. if (compare_testvector(out, outlen, long_pric, sizeof(long_pric), "e-long_pric", 0)) return CRYPT_ERROR;
  802. outlen = sizeof(out);
  803. DO(ecc_export_openssl(out, &outlen, PK_PUBLIC, &pub));
  804. if (compare_testvector(out, outlen, long_pub, sizeof(long_pub), "e-long_pub", 0)) return CRYPT_ERROR;
  805. outlen = sizeof(out);
  806. DO(ecc_export_openssl(out, &outlen, PK_PUBLIC|PK_COMPRESSED, &pub));
  807. if (compare_testvector(out, outlen, long_pubc, sizeof(long_pubc), "e-long_pubc", 0)) return CRYPT_ERROR;
  808. outlen = sizeof(out);
  809. DO(ecc_export_openssl(out, &outlen, PK_PRIVATE|PK_CURVEOID, &pri));
  810. if (compare_testvector(out, outlen, short_pri, sizeof(short_pri), "e-short_pri", 0)) return CRYPT_ERROR;
  811. outlen = sizeof(out);
  812. DO(ecc_export_openssl(out, &outlen, PK_PRIVATE|PK_CURVEOID|PK_COMPRESSED, &pri));
  813. if (compare_testvector(out, outlen, short_pric, sizeof(short_pric), "e-short_pric", 0)) return CRYPT_ERROR;
  814. outlen = sizeof(out);
  815. DO(ecc_export_openssl(out, &outlen, PK_PUBLIC|PK_CURVEOID, &pub));
  816. if (compare_testvector(out, outlen, short_pub, sizeof(short_pub), "e-short_pub", 0)) return CRYPT_ERROR;
  817. outlen = sizeof(out);
  818. DO(ecc_export_openssl(out, &outlen, PK_PUBLIC|PK_CURVEOID|PK_COMPRESSED, &pub));
  819. if (compare_testvector(out, outlen, short_pubc, sizeof(short_pubc), "e-short_pubc", 0)) return CRYPT_ERROR;
  820. /* export - raw keys */
  821. outlen = sizeof(out);
  822. DO(ecc_get_key(out, &outlen, PK_PRIVATE, &pri));
  823. if (compare_testvector(out, outlen, raw_pri, sizeof(raw_pri), "e-raw_pri", 0)) return CRYPT_ERROR;
  824. outlen = sizeof(out);
  825. DO(ecc_get_key(out, &outlen, PK_PUBLIC, &pub));
  826. if (compare_testvector(out, outlen, raw_pub, sizeof(raw_pub), "e-raw_pub", 0)) return CRYPT_ERROR;
  827. outlen = sizeof(out);
  828. DO(ecc_get_key(out, &outlen, PK_PUBLIC|PK_COMPRESSED, &pub));
  829. if (compare_testvector(out, outlen, raw_pubc, sizeof(raw_pubc), "e-raw_pubc", 0)) return CRYPT_ERROR;
  830. ecc_free(&pri);
  831. ecc_free(&pub);
  832. return CRYPT_OK;
  833. }
  834. int ecc_tests(void)
  835. {
  836. if (ltc_mp.name == NULL) return CRYPT_NOP;
  837. DO(_ecc_old_api()); /* up to 1.18 */
  838. DO(_ecc_new_api());
  839. DO(_ecc_import_export());
  840. DO(_ecc_test_mp());
  841. DO(_ecc_issue108());
  842. #ifdef LTC_ECC_SHAMIR
  843. DO(_ecc_test_shamir());
  844. #endif
  845. return CRYPT_OK;
  846. }
  847. #endif
  848. /* ref: $Format:%D$ */
  849. /* git commit: $Format:%H$ */
  850. /* commit time: $Format:%ai$ */