ecc_test.c 2.2 KB

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