test_md4.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Test program for MD4 (test vectors from RFC 1320)
  3. * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto.h"
  17. int main(int argc, char *argv[])
  18. {
  19. struct {
  20. char *data;
  21. u8 *hash;
  22. } tests[] = {
  23. {
  24. "",
  25. "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31"
  26. "\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0"
  27. },
  28. {
  29. "a",
  30. "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46"
  31. "\x24\x5e\x05\xfb\xdb\xd6\xfb\x24"
  32. },
  33. {
  34. "abc",
  35. "\xa4\x48\x01\x7a\xaf\x21\xd8\x52"
  36. "\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d"
  37. },
  38. {
  39. "message digest",
  40. "\xd9\x13\x0a\x81\x64\x54\x9f\xe8"
  41. "\x18\x87\x48\x06\xe1\xc7\x01\x4b"
  42. },
  43. {
  44. "abcdefghijklmnopqrstuvwxyz",
  45. "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd"
  46. "\xee\xa8\xed\x63\xdf\x41\x2d\xa9"
  47. },
  48. {
  49. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  50. "0123456789",
  51. "\x04\x3f\x85\x82\xf2\x41\xdb\x35"
  52. "\x1c\xe6\x27\xe1\x53\xe7\xf0\xe4"
  53. },
  54. {
  55. "12345678901234567890123456789012345678901234567890"
  56. "123456789012345678901234567890",
  57. "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19"
  58. "\x9c\x3e\x7b\x16\x4f\xcc\x05\x36"
  59. }
  60. };
  61. unsigned int i;
  62. u8 hash[16];
  63. const u8 *addr[2];
  64. size_t len[2];
  65. int errors = 0;
  66. for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
  67. printf("MD4 test case %d:", i);
  68. addr[0] = tests[i].data;
  69. len[0] = strlen(tests[i].data);
  70. md4_vector(1, addr, len, hash);
  71. if (memcmp(hash, tests[i].hash, 16) != 0) {
  72. printf(" FAIL");
  73. errors++;
  74. } else
  75. printf(" OK");
  76. if (len[0]) {
  77. addr[0] = tests[i].data;
  78. len[0] = strlen(tests[i].data);
  79. addr[1] = tests[i].data + 1;
  80. len[1] = strlen(tests[i].data) - 1;
  81. md4_vector(1, addr, len, hash);
  82. if (memcmp(hash, tests[i].hash, 16) != 0) {
  83. printf(" FAIL");
  84. errors++;
  85. } else
  86. printf(" OK");
  87. }
  88. printf("\n");
  89. }
  90. return errors;
  91. }