dh_tests.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "test.h"
  2. int dh_tests (void)
  3. {
  4. unsigned char buf[3][4096];
  5. unsigned long x, y, z;
  6. int stat, stat2;
  7. dh_key usera, userb;
  8. DO(dh_test());
  9. /* make up two keys */
  10. DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &usera));
  11. DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &userb));
  12. /* make the shared secret */
  13. x = 4096;
  14. DO(dh_shared_secret (&usera, &userb, buf[0], &x));
  15. y = 4096;
  16. DO(dh_shared_secret (&userb, &usera, buf[1], &y));
  17. if (y != x) {
  18. printf ("DH Shared keys are not same size.\n");
  19. return 1;
  20. }
  21. if (memcmp (buf[0], buf[1], x)) {
  22. printf ("DH Shared keys not same contents.\n");
  23. return 1;
  24. }
  25. /* now export userb */
  26. y = 4096;
  27. DO(dh_export (buf[1], &y, PK_PUBLIC, &userb));
  28. dh_free (&userb);
  29. /* import and make the shared secret again */
  30. DO(dh_import (buf[1], y, &userb));
  31. z = 4096;
  32. DO(dh_shared_secret (&usera, &userb, buf[2], &z));
  33. if (z != x) {
  34. printf ("failed. Size don't match?\n");
  35. return 1;
  36. }
  37. if (memcmp (buf[0], buf[2], x)) {
  38. printf ("Failed. Content didn't match.\n");
  39. return 1;
  40. }
  41. dh_free (&usera);
  42. dh_free (&userb);
  43. /* test encrypt_key */
  44. dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &usera);
  45. for (x = 0; x < 16; x++) {
  46. buf[0][x] = x;
  47. }
  48. y = sizeof (buf[1]);
  49. DO(dh_encrypt_key (buf[0], 16, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("md5"), &usera));
  50. zeromem (buf[0], sizeof (buf[0]));
  51. x = sizeof (buf[0]);
  52. DO(dh_decrypt_key (buf[1], y, buf[0], &x, &usera));
  53. if (x != 16) {
  54. printf ("Failed (length)\n");
  55. return 1;
  56. }
  57. for (x = 0; x < 16; x++)
  58. if (buf[0][x] != x) {
  59. printf ("Failed (contents)\n");
  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(dh_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow , find_prng ("yarrow"), &usera));
  68. DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
  69. buf[0][0] ^= 1;
  70. DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
  71. if (!(stat == 1 && stat2 == 0)) {
  72. printf("dh_sign/verify_hash %d %d", stat, stat2);
  73. return 1;
  74. }
  75. dh_free (&usera);
  76. return 0;
  77. }