ecc_test.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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;
  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. ecc_make_key (&test_yarrow, find_prng ("yarrow"), 65, &usera);
  46. for (x = 0; x < 32; x++) {
  47. buf[0][x] = x;
  48. }
  49. y = sizeof (buf[1]);
  50. DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("sha256"), &usera));
  51. zeromem (buf[0], sizeof (buf[0]));
  52. x = sizeof (buf[0]);
  53. DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &usera));
  54. if (x != 32) {
  55. printf ("Failed (length)");
  56. return 1;
  57. }
  58. for (x = 0; x < 32; x++)
  59. if (buf[0][x] != x) {
  60. printf ("Failed (contents)");
  61. return 1;
  62. }
  63. /* test sign_hash */
  64. for (x = 0; x < 16; x++) {
  65. buf[0][x] = x;
  66. }
  67. x = sizeof (buf[1]);
  68. DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow, find_prng ("yarrow"), &usera));
  69. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
  70. buf[0][0] ^= 1;
  71. DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
  72. if (!(stat == 1 && stat2 == 0)) {
  73. printf("ecc_verify_hash failed");
  74. return 1;
  75. }
  76. ecc_free (&usera);
  77. return 0;
  78. }
  79. #else
  80. int ecc_tests(void)
  81. {
  82. printf("NOP");
  83. return 0;
  84. }
  85. #endif