test-md5.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Test program for MD5 (test vectors from RFC 1321)
  3. * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/crypto.h"
  11. int main(int argc, char *argv[])
  12. {
  13. struct {
  14. char *data;
  15. char *hash;
  16. } tests[] = {
  17. {
  18. "",
  19. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04"
  20. "\xe9\x80\x09\x98\xec\xf8\x42\x7e"
  21. },
  22. {
  23. "a",
  24. "\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8"
  25. "\x31\xc3\x99\xe2\x69\x77\x26\x61"
  26. },
  27. {
  28. "abc",
  29. "\x90\x01\x50\x98\x3c\xd2\x4f\xb0"
  30. "\xd6\x96\x3f\x7d\x28\xe1\x7f\x72"
  31. },
  32. {
  33. "message digest",
  34. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d"
  35. "\x52\x5a\x2f\x31\xaa\xf1\x61\xd0"
  36. },
  37. {
  38. "abcdefghijklmnopqrstuvwxyz",
  39. "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00"
  40. "\x7d\xfb\x49\x6c\xca\x67\xe1\x3b"
  41. },
  42. {
  43. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  44. "0123456789",
  45. "\xd1\x74\xab\x98\xd2\x77\xd9\xf5"
  46. "\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f"
  47. },
  48. {
  49. "12345678901234567890123456789012345678901234567890"
  50. "123456789012345678901234567890",
  51. "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55"
  52. "\xac\x49\xda\x2e\x21\x07\xb6\x7a"
  53. }
  54. };
  55. unsigned int i;
  56. u8 hash[16];
  57. const u8 *addr[2];
  58. size_t len[2];
  59. int errors = 0;
  60. for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
  61. printf("MD5 test case %d:", i);
  62. addr[0] = (u8 *) tests[i].data;
  63. len[0] = strlen(tests[i].data);
  64. md5_vector(1, addr, len, hash);
  65. if (memcmp(hash, tests[i].hash, 16) != 0) {
  66. printf(" FAIL");
  67. errors++;
  68. } else
  69. printf(" OK");
  70. if (len[0]) {
  71. addr[0] = (u8 *) tests[i].data;
  72. len[0] = strlen(tests[i].data);
  73. addr[1] = (u8 *) tests[i].data + 1;
  74. len[1] = strlen(tests[i].data) - 1;
  75. md5_vector(1, addr, len, hash);
  76. if (memcmp(hash, tests[i].hash, 16) != 0) {
  77. printf(" FAIL");
  78. errors++;
  79. } else
  80. printf(" OK");
  81. }
  82. printf("\n");
  83. }
  84. return errors;
  85. }