dh_test.c 2.8 KB

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