ecc_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <tomcrypt_test.h>
  2. #ifdef LTC_MECC
  3. static int sizes[] = {
  4. #ifdef ECC112
  5. 14,
  6. #endif
  7. #ifdef ECC128
  8. 16,
  9. #endif
  10. #ifdef ECC160
  11. 20,
  12. #endif
  13. #ifdef ECC192
  14. 24,
  15. #endif
  16. #ifdef ECC224
  17. 28,
  18. #endif
  19. #ifdef ECC256
  20. 32,
  21. #endif
  22. #ifdef ECC384
  23. 48,
  24. #endif
  25. #ifdef ECC521
  26. 65
  27. #endif
  28. };
  29. #ifdef LTC_ECC_SHAMIR
  30. int ecc_test_shamir(void)
  31. {
  32. void *modulus, *mp, *kA, *kB, *rA, *rB;
  33. ecc_point *G, *A, *B, *C1, *C2;
  34. int x, y, z;
  35. unsigned char buf[ECC_BUF_SIZE];
  36. DO(mp_init_multi(&kA, &kB, &rA, &rB, &modulus, NULL));
  37. LTC_ARGCHK((G = ltc_ecc_new_point()) != NULL);
  38. LTC_ARGCHK((A = ltc_ecc_new_point()) != NULL);
  39. LTC_ARGCHK((B = ltc_ecc_new_point()) != NULL);
  40. LTC_ARGCHK((C1 = ltc_ecc_new_point()) != NULL);
  41. LTC_ARGCHK((C2 = ltc_ecc_new_point()) != NULL);
  42. for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
  43. /* get the base point */
  44. for (z = 0; ltc_ecc_sets[z].name; z++) {
  45. if (sizes[z] < ltc_ecc_sets[z].size) break;
  46. }
  47. LTC_ARGCHK(ltc_ecc_sets[z].name != NULL);
  48. /* load it */
  49. DO(mp_read_radix(G->x, ltc_ecc_sets[z].Gx, 16));
  50. DO(mp_read_radix(G->y, ltc_ecc_sets[z].Gy, 16));
  51. DO(mp_set(G->z, 1));
  52. DO(mp_read_radix(modulus, ltc_ecc_sets[z].prime, 16));
  53. DO(mp_montgomery_setup(modulus, &mp));
  54. /* do 100 random tests */
  55. for (y = 0; y < 100; y++) {
  56. /* pick a random r1, r2 */
  57. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  58. DO(mp_read_unsigned_bin(rA, buf, sizes[x]));
  59. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  60. DO(mp_read_unsigned_bin(rB, buf, sizes[x]));
  61. /* compute rA * G = A */
  62. DO(ltc_mp.ecc_ptmul(rA, G, A, modulus, 1));
  63. /* compute rB * G = B */
  64. DO(ltc_mp.ecc_ptmul(rB, G, B, modulus, 1));
  65. /* pick a random kA, kB */
  66. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  67. DO(mp_read_unsigned_bin(kA, buf, sizes[x]));
  68. LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
  69. DO(mp_read_unsigned_bin(kB, buf, sizes[x]));
  70. /* now, compute kA*A + kB*B = C1 using the older method */
  71. DO(ltc_mp.ecc_ptmul(kA, A, C1, modulus, 0));
  72. DO(ltc_mp.ecc_ptmul(kB, B, C2, modulus, 0));
  73. DO(ltc_mp.ecc_ptadd(C1, C2, C1, modulus, mp));
  74. DO(ltc_mp.ecc_map(C1, modulus, mp));
  75. /* now compute using mul2add */
  76. DO(ltc_mp.ecc_mul2add(A, kA, B, kB, C2, modulus));
  77. /* is they the sames? */
  78. 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)) {
  79. fprintf(stderr, "ECC failed shamir test: size=%d, testno=%d\n", sizes[x], y);
  80. return 1;
  81. }
  82. }
  83. mp_montgomery_free(mp);
  84. }
  85. ltc_ecc_del_point(C2);
  86. ltc_ecc_del_point(C1);
  87. ltc_ecc_del_point(B);
  88. ltc_ecc_del_point(A);
  89. ltc_ecc_del_point(G);
  90. mp_clear_multi(kA, kB, rA, rB, modulus, NULL);
  91. return 0;
  92. }
  93. #endif
  94. int ecc_tests (void)
  95. {
  96. unsigned char buf[4][4096];
  97. unsigned long x, y, z, s;
  98. int stat, stat2;
  99. ecc_key usera, userb, pubKey, privKey;
  100. DO(ecc_test ());
  101. DO(ecc_test ());
  102. DO(ecc_test ());
  103. DO(ecc_test ());
  104. DO(ecc_test ());
  105. for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
  106. /* make up two keys */
  107. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  108. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
  109. /* make the shared secret */
  110. x = sizeof(buf[0]);
  111. DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
  112. y = sizeof(buf[1]);
  113. DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
  114. if (y != x) {
  115. fprintf(stderr, "ecc Shared keys are not same size.");
  116. return 1;
  117. }
  118. if (memcmp (buf[0], buf[1], x)) {
  119. fprintf(stderr, "ecc Shared keys not same contents.");
  120. return 1;
  121. }
  122. /* now export userb */
  123. y = sizeof(buf[0]);
  124. DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
  125. ecc_free (&userb);
  126. /* import and make the shared secret again */
  127. DO(ecc_import (buf[1], y, &userb));
  128. z = sizeof(buf[0]);
  129. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  130. if (z != x) {
  131. fprintf(stderr, "failed. Size don't match?");
  132. return 1;
  133. }
  134. if (memcmp (buf[0], buf[2], x)) {
  135. fprintf(stderr, "Failed. Contents didn't match.");
  136. return 1;
  137. }
  138. /* export with ANSI X9.63 */
  139. y = sizeof(buf[1]);
  140. DO(ecc_ansi_x963_export(&userb, buf[1], &y));
  141. ecc_free (&userb);
  142. /* now import the ANSI key */
  143. DO(ecc_ansi_x963_import(buf[1], y, &userb));
  144. /* shared secret */
  145. z = sizeof(buf[0]);
  146. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  147. if (z != x) {
  148. fprintf(stderr, "failed. Size don't match?");
  149. return 1;
  150. }
  151. if (memcmp (buf[0], buf[2], x)) {
  152. fprintf(stderr, "Failed. Contents didn't match.");
  153. return 1;
  154. }
  155. ecc_free (&usera);
  156. ecc_free (&userb);
  157. /* test encrypt_key */
  158. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  159. /* export key */
  160. x = sizeof(buf[0]);
  161. DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
  162. DO(ecc_import(buf[0], x, &pubKey));
  163. x = sizeof(buf[0]);
  164. DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
  165. DO(ecc_import(buf[0], x, &privKey));
  166. for (x = 0; x < 32; x++) {
  167. buf[0][x] = x;
  168. }
  169. y = sizeof (buf[1]);
  170. DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
  171. zeromem (buf[0], sizeof (buf[0]));
  172. x = sizeof (buf[0]);
  173. DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
  174. if (x != 32) {
  175. fprintf(stderr, "Failed (length)");
  176. return 1;
  177. }
  178. for (x = 0; x < 32; x++) {
  179. if (buf[0][x] != x) {
  180. fprintf(stderr, "Failed (contents)");
  181. return 1;
  182. }
  183. }
  184. /* test sign_hash */
  185. for (x = 0; x < 16; x++) {
  186. buf[0][x] = x;
  187. }
  188. x = sizeof (buf[1]);
  189. DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
  190. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
  191. buf[0][0] ^= 1;
  192. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
  193. if (!(stat == 1 && stat2 == 0)) {
  194. fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2);
  195. return 1;
  196. }
  197. ecc_free (&usera);
  198. ecc_free (&pubKey);
  199. ecc_free (&privKey);
  200. }
  201. #ifdef LTC_ECC_SHAMIR
  202. return ecc_test_shamir();
  203. #else
  204. return 0;
  205. #endif
  206. }
  207. #else
  208. int ecc_tests(void)
  209. {
  210. fprintf(stderr, "NOP");
  211. return 0;
  212. }
  213. #endif
  214. /* $Source$ */
  215. /* $Revision$ */
  216. /* $Date$ */