ecc_test.c 7.5 KB

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