ecc_test.c 6.8 KB

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