ecc_test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_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. if (ltc_mp.name == NULL) return CRYPT_NOP;
  109. DO(ecc_test ());
  110. for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
  111. /* make up two keys */
  112. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  113. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
  114. /* make the shared secret */
  115. x = sizeof(buf[0]);
  116. DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
  117. y = sizeof(buf[1]);
  118. DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
  119. if (y != x) {
  120. fprintf(stderr, "ecc Shared keys are not same size.");
  121. return 1;
  122. }
  123. if (memcmp (buf[0], buf[1], x)) {
  124. fprintf(stderr, "ecc Shared keys not same contents.");
  125. return 1;
  126. }
  127. /* now export userb */
  128. y = sizeof(buf[0]);
  129. DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
  130. ecc_free (&userb);
  131. /* import and make the shared secret again */
  132. DO(ecc_import (buf[1], y, &userb));
  133. z = sizeof(buf[0]);
  134. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  135. if (z != x) {
  136. fprintf(stderr, "failed. Size don't match?");
  137. return 1;
  138. }
  139. if (memcmp (buf[0], buf[2], x)) {
  140. fprintf(stderr, "Failed. Contents didn't match.");
  141. return 1;
  142. }
  143. /* export with ANSI X9.63 */
  144. y = sizeof(buf[1]);
  145. DO(ecc_ansi_x963_export(&userb, buf[1], &y));
  146. ecc_free (&userb);
  147. /* now import the ANSI key */
  148. DO(ecc_ansi_x963_import(buf[1], y, &userb));
  149. /* shared secret */
  150. z = sizeof(buf[0]);
  151. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  152. if (z != x) {
  153. fprintf(stderr, "failed. Size don't match?");
  154. return 1;
  155. }
  156. if (memcmp (buf[0], buf[2], x)) {
  157. fprintf(stderr, "Failed. Contents didn't match.");
  158. return 1;
  159. }
  160. ecc_free (&usera);
  161. ecc_free (&userb);
  162. /* test encrypt_key */
  163. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  164. /* export key */
  165. x = sizeof(buf[0]);
  166. DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
  167. DO(ecc_import(buf[0], x, &pubKey));
  168. x = sizeof(buf[0]);
  169. DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
  170. DO(ecc_import(buf[0], x, &privKey));
  171. for (ch = 0; ch < 32; ch++) {
  172. buf[0][ch] = ch;
  173. }
  174. y = sizeof (buf[1]);
  175. DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
  176. zeromem (buf[0], sizeof (buf[0]));
  177. x = sizeof (buf[0]);
  178. DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
  179. if (x != 32) {
  180. fprintf(stderr, "Failed (length)");
  181. return 1;
  182. }
  183. for (ch = 0; ch < 32; ch++) {
  184. if (buf[0][ch] != ch) {
  185. fprintf(stderr, "Failed (contents)");
  186. return 1;
  187. }
  188. }
  189. /* test sign_hash */
  190. for (ch = 0; ch < 16; ch++) {
  191. buf[0][ch] = ch;
  192. }
  193. x = sizeof (buf[1]);
  194. DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
  195. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
  196. buf[0][0] ^= 1;
  197. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
  198. if (!(stat == 1 && stat2 == 0)) {
  199. fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2);
  200. return 1;
  201. }
  202. /* test sign_hash_rfc7518 */
  203. for (ch = 0; ch < 16; ch++) {
  204. buf[0][ch] = ch;
  205. }
  206. x = sizeof (buf[1]);
  207. DO(ecc_sign_hash_rfc7518(buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
  208. DO(ecc_verify_hash_rfc7518(buf[1], x, buf[0], 16, &stat, &pubKey));
  209. buf[0][0] ^= 1;
  210. DO(ecc_verify_hash_rfc7518(buf[1], x, buf[0], 16, &stat2, &privKey));
  211. if (!(stat == 1 && stat2 == 0)) {
  212. fprintf(stderr, "ecc_verify_hash_rfc7518 failed %d, %d, ", stat, stat2);
  213. return 1;
  214. }
  215. ecc_free (&usera);
  216. ecc_free (&pubKey);
  217. ecc_free (&privKey);
  218. }
  219. #ifdef LTC_ECC_SHAMIR
  220. return ecc_test_shamir();
  221. #else
  222. return 0;
  223. #endif
  224. }
  225. #else
  226. int ecc_tests(void)
  227. {
  228. return CRYPT_NOP;
  229. }
  230. #endif
  231. /* ref: $Format:%D$ */
  232. /* git commit: $Format:%H$ */
  233. /* commit time: $Format:%ai$ */