dh_tests.c 2.4 KB

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