ecc_test.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "test.h"
  2. #ifdef MECC
  3. int ecc_tests (void)
  4. {
  5. unsigned char buf[4][4096];
  6. unsigned long x, y, z;
  7. int stat, stat2;
  8. ecc_key usera, userb, pubKey, privKey;
  9. DO(ecc_test ());
  10. /* make up two keys */
  11. DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 65, &usera));
  12. DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 65, &userb));
  13. /* make the shared secret */
  14. x = 4096;
  15. DO(ecc_shared_secret (&usera, &userb, buf[0], &x));
  16. y = 4096;
  17. DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
  18. if (y != x) {
  19. printf ("ecc Shared keys are not same size.");
  20. return 1;
  21. }
  22. if (memcmp (buf[0], buf[1], x)) {
  23. printf ("ecc Shared keys not same contents.");
  24. return 1;
  25. }
  26. /* now export userb */
  27. y = 4096;
  28. DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb));
  29. ecc_free (&userb);
  30. /* import and make the shared secret again */
  31. DO(ecc_import (buf[1], y, &userb));
  32. z = 4096;
  33. DO(ecc_shared_secret (&usera, &userb, buf[2], &z));
  34. if (z != x) {
  35. printf ("failed. Size don't match?");
  36. return 1;
  37. }
  38. if (memcmp (buf[0], buf[2], x)) {
  39. printf ("Failed. Content didn't match.");
  40. return 1;
  41. }
  42. ecc_free (&usera);
  43. ecc_free (&userb);
  44. /* test encrypt_key */
  45. DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 65, &usera));
  46. /* export key */
  47. x = sizeof(buf[0]);
  48. DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera));
  49. DO(ecc_import(buf[0], x, &pubKey));
  50. x = sizeof(buf[0]);
  51. DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera));
  52. DO(ecc_import(buf[0], x, &privKey));
  53. for (x = 0; x < 32; x++) {
  54. buf[0][x] = x;
  55. }
  56. y = sizeof (buf[1]);
  57. DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("sha256"), &pubKey));
  58. zeromem (buf[0], sizeof (buf[0]));
  59. x = sizeof (buf[0]);
  60. DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey));
  61. if (x != 32) {
  62. printf ("Failed (length)");
  63. return 1;
  64. }
  65. for (x = 0; x < 32; x++)
  66. if (buf[0][x] != x) {
  67. printf ("Failed (contents)");
  68. return 1;
  69. }
  70. /* test sign_hash */
  71. for (x = 0; x < 16; x++) {
  72. buf[0][x] = x;
  73. }
  74. x = sizeof (buf[1]);
  75. DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow, find_prng ("yarrow"), &privKey));
  76. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey));
  77. buf[0][0] ^= 1;
  78. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey));
  79. if (!(stat == 1 && stat2 == 0)) {
  80. printf("ecc_verify_hash failed");
  81. return 1;
  82. }
  83. ecc_free (&usera);
  84. ecc_free (&pubKey);
  85. ecc_free (&privKey);
  86. return 0;
  87. }
  88. #else
  89. int ecc_tests(void)
  90. {
  91. printf("NOP");
  92. return 0;
  93. }
  94. #endif