ecc_test.c 3.2 KB

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