ecc_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <tomcrypt_test.h>
  2. #ifdef MECC
  3. static int sizes[] = {
  4. #ifdef ECC192
  5. 24,
  6. #endif
  7. #ifdef ECC224
  8. 28,
  9. #endif
  10. #ifdef ECC256
  11. 32,
  12. #endif
  13. #ifdef ECC384
  14. 48,
  15. #endif
  16. #ifdef ECC521
  17. 65
  18. #endif
  19. };
  20. int ecc_tests (void)
  21. {
  22. unsigned char buf[4][4096];
  23. unsigned long x, y, z, s;
  24. int stat, stat2;
  25. ecc_key usera, userb, pubKey, privKey;
  26. DO(ecc_test ());
  27. DO(ecc_test ());
  28. DO(ecc_test ());
  29. DO(ecc_test ());
  30. DO(ecc_test ());
  31. for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
  32. /* make up two keys */
  33. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  34. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
  35. /* make the shared secret */
  36. x = 4096;
  37. DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
  38. y = 4096;
  39. DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
  40. if (y != x) {
  41. fprintf(stderr, "ecc Shared keys are not same size.");
  42. return 1;
  43. }
  44. if (memcmp (buf[0], buf[1], x)) {
  45. fprintf(stderr, "ecc Shared keys not same contents.");
  46. return 1;
  47. }
  48. /* now export userb */
  49. y = 4096;
  50. DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
  51. ecc_free (&userb);
  52. /* import and make the shared secret again */
  53. DO(ecc_import (buf[1], y, &userb));
  54. z = 4096;
  55. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  56. if (z != x) {
  57. fprintf(stderr, "failed. Size don't match?");
  58. return 1;
  59. }
  60. if (memcmp (buf[0], buf[2], x)) {
  61. fprintf(stderr, "Failed. Contents didn't match.");
  62. return 1;
  63. }
  64. ecc_free (&usera);
  65. ecc_free (&userb);
  66. /* test encrypt_key */
  67. DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
  68. /* export key */
  69. x = sizeof(buf[0]);
  70. DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
  71. DO(ecc_import(buf[0], x, &pubKey));
  72. x = sizeof(buf[0]);
  73. DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
  74. DO(ecc_import(buf[0], x, &privKey));
  75. for (x = 0; x < 32; x++) {
  76. buf[0][x] = x;
  77. }
  78. y = sizeof (buf[1]);
  79. DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
  80. zeromem (buf[0], sizeof (buf[0]));
  81. x = sizeof (buf[0]);
  82. DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
  83. if (x != 32) {
  84. fprintf(stderr, "Failed (length)");
  85. return 1;
  86. }
  87. for (x = 0; x < 32; x++) {
  88. if (buf[0][x] != x) {
  89. fprintf(stderr, "Failed (contents)");
  90. return 1;
  91. }
  92. }
  93. /* test sign_hash */
  94. for (x = 0; x < 16; x++) {
  95. buf[0][x] = x;
  96. }
  97. x = sizeof (buf[1]);
  98. DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey));
  99. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
  100. buf[0][0] ^= 1;
  101. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
  102. if (!(stat == 1 && stat2 == 0)) {
  103. fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2);
  104. return 1;
  105. }
  106. ecc_free (&usera);
  107. ecc_free (&pubKey);
  108. ecc_free (&privKey);
  109. }
  110. return 0;
  111. }
  112. #else
  113. int ecc_tests(void)
  114. {
  115. fprintf(stderr, "NOP");
  116. return 0;
  117. }
  118. #endif
  119. /* $Source$ */
  120. /* $Revision$ */
  121. /* $Date$ */