ecc_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <tomcrypt_test.h>
  2. #ifdef 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. int ecc_tests (void)
  30. {
  31. unsigned char buf[4][4096];
  32. unsigned long x, y, z, s;
  33. int stat, stat2;
  34. ecc_key usera, userb, pubKey, privKey;
  35. DO(ecc_test ());
  36. DO(ecc_test ());
  37. DO(ecc_test ());
  38. DO(ecc_test ());
  39. DO(ecc_test ());
  40. for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
  41. /* make up two keys */
  42. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  43. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
  44. /* make the shared secret */
  45. x = sizeof(buf[0]);
  46. DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
  47. y = sizeof(buf[1]);
  48. DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
  49. if (y != x) {
  50. fprintf(stderr, "ecc Shared keys are not same size.");
  51. return 1;
  52. }
  53. if (memcmp (buf[0], buf[1], x)) {
  54. fprintf(stderr, "ecc Shared keys not same contents.");
  55. return 1;
  56. }
  57. /* now export userb */
  58. y = sizeof(buf[0]);
  59. DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
  60. ecc_free (&userb);
  61. /* import and make the shared secret again */
  62. DO(ecc_import (buf[1], y, &userb));
  63. z = sizeof(buf[0]);
  64. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  65. if (z != x) {
  66. fprintf(stderr, "failed. Size don't match?");
  67. return 1;
  68. }
  69. if (memcmp (buf[0], buf[2], x)) {
  70. fprintf(stderr, "Failed. Contents didn't match.");
  71. return 1;
  72. }
  73. /* export with ANSI X9.63 */
  74. y = sizeof(buf[1]);
  75. DO(ecc_ansi_x963_export(&userb, buf[1], &y));
  76. ecc_free (&userb);
  77. /* now import the ANSI key */
  78. DO(ecc_ansi_x963_import(buf[1], y, &userb));
  79. /* shared secret */
  80. z = sizeof(buf[0]);
  81. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  82. if (z != x) {
  83. fprintf(stderr, "failed. Size don't match?");
  84. return 1;
  85. }
  86. if (memcmp (buf[0], buf[2], x)) {
  87. fprintf(stderr, "Failed. Contents didn't match.");
  88. return 1;
  89. }
  90. ecc_free (&usera);
  91. ecc_free (&userb);
  92. /* test encrypt_key */
  93. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  94. /* export key */
  95. x = sizeof(buf[0]);
  96. DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
  97. DO(ecc_import(buf[0], x, &pubKey));
  98. x = sizeof(buf[0]);
  99. DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
  100. DO(ecc_import(buf[0], x, &privKey));
  101. for (x = 0; x < 32; x++) {
  102. buf[0][x] = x;
  103. }
  104. y = sizeof (buf[1]);
  105. DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
  106. zeromem (buf[0], sizeof (buf[0]));
  107. x = sizeof (buf[0]);
  108. DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
  109. if (x != 32) {
  110. fprintf(stderr, "Failed (length)");
  111. return 1;
  112. }
  113. for (x = 0; x < 32; x++) {
  114. if (buf[0][x] != x) {
  115. fprintf(stderr, "Failed (contents)");
  116. return 1;
  117. }
  118. }
  119. /* test sign_hash */
  120. for (x = 0; x < 16; x++) {
  121. buf[0][x] = x;
  122. }
  123. x = sizeof (buf[1]);
  124. DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
  125. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
  126. buf[0][0] ^= 1;
  127. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
  128. if (!(stat == 1 && stat2 == 0)) {
  129. fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2);
  130. return 1;
  131. }
  132. ecc_free (&usera);
  133. ecc_free (&pubKey);
  134. ecc_free (&privKey);
  135. }
  136. return 0;
  137. }
  138. #else
  139. int ecc_tests(void)
  140. {
  141. fprintf(stderr, "NOP");
  142. return 0;
  143. }
  144. #endif
  145. /* $Source$ */
  146. /* $Revision$ */
  147. /* $Date$ */