dh_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <tomcrypt_test.h>
  2. #ifdef LTC_MDH
  3. #ifdef DH4096
  4. #define KEYSIZE 4096
  5. #else
  6. #define KEYSIZE 2048
  7. #endif
  8. int dh_test (void)
  9. {
  10. unsigned char buf[3][4096], ch;
  11. unsigned long x, y, z;
  12. int stat, stat2;
  13. dh_key usera, userb;
  14. if (register_prng(&yarrow_desc) == -1) {
  15. printf("Error registering yarrow PRNG\n");
  16. exit(-1);
  17. }
  18. if (register_hash(&md5_desc) == -1) {
  19. printf("Error registering md5 hash\n");
  20. exit(-1);
  21. }
  22. DO(dh_compat_test());
  23. /* make up two keys */
  24. DO(dh_make_key (&yarrow_prng, find_prng ("yarrow"), KEYSIZE/8, &usera));
  25. DO(dh_make_key (&yarrow_prng, find_prng ("yarrow"), KEYSIZE/8, &userb));
  26. /* make the shared secret */
  27. x = KEYSIZE;
  28. DO(dh_shared_secret (&usera, &userb, buf[0], &x));
  29. y = KEYSIZE;
  30. DO(dh_shared_secret (&userb, &usera, buf[1], &y));
  31. if (y != x) {
  32. fprintf(stderr, "DH Shared keys are not same size.\n");
  33. dh_free (&usera);
  34. dh_free (&userb);
  35. return 1;
  36. }
  37. if (memcmp (buf[0], buf[1], x)) {
  38. fprintf(stderr, "DH Shared keys not same contents.\n");
  39. dh_free (&usera);
  40. dh_free (&userb);
  41. return 1;
  42. }
  43. /* now export userb */
  44. y = KEYSIZE;
  45. DO(dh_export (buf[1], &y, PK_PUBLIC, &userb));
  46. dh_free (&userb);
  47. /* import and make the shared secret again */
  48. DO(dh_import (buf[1], y, &userb));
  49. z = KEYSIZE;
  50. DO(dh_shared_secret (&usera, &userb, buf[2], &z));
  51. dh_free (&usera);
  52. dh_free (&userb);
  53. if (z != x) {
  54. fprintf(stderr, "failed. Size don't match?\n");
  55. return 1;
  56. }
  57. if (memcmp (buf[0], buf[2], x)) {
  58. fprintf(stderr, "Failed. Content didn't match.\n");
  59. return 1;
  60. }
  61. /* test encrypt_key */
  62. dh_make_key (&yarrow_prng, find_prng ("yarrow"), KEYSIZE/8, &usera);
  63. for (ch = 0; ch < 16; ch++) {
  64. buf[0][ch] = ch;
  65. }
  66. y = sizeof (buf[1]);
  67. DO(dh_encrypt_key (buf[0], 16, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("md5"), &usera));
  68. zeromem (buf[0], sizeof (buf[0]));
  69. x = sizeof (buf[0]);
  70. DO(dh_decrypt_key (buf[1], y, buf[0], &x, &usera));
  71. if (x != 16) {
  72. fprintf(stderr, "Failed (length)\n");
  73. dh_free (&usera);
  74. return 1;
  75. }
  76. for (ch = 0; ch < 16; ch++)
  77. if (buf[0][ch] != ch) {
  78. fprintf(stderr, "Failed (contents)\n");
  79. dh_free (&usera);
  80. return 1;
  81. }
  82. /* test sign_hash */
  83. for (ch = 0; ch < 16; ch++) {
  84. buf[0][ch] = ch;
  85. }
  86. x = sizeof (buf[1]);
  87. DO(dh_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &usera));
  88. DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
  89. buf[0][0] ^= 1;
  90. DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
  91. dh_free (&usera);
  92. if (!(stat == 1 && stat2 == 0)) {
  93. fprintf(stderr, "dh_sign/verify_hash %d %d", stat, stat2);
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. #else
  99. int dh_test(void)
  100. {
  101. return CRYPT_NOP;
  102. }
  103. #endif