1 #include "crypt-port.h"
2 #include "alg-md5.h"
3
4 #include <stdio.h>
5
6 #if INCLUDE_md5crypt || INCLUDE_sunmd5
7
8 static const struct
9 {
10 const char *input;
11 const char result[16];
12 } tests[] =
13 {
14 /* "Informal" test vectors from
15 https://www.nist.gov/itl/ssd/software-quality-group/nsrl-test-data
16 (these were once in FIPS 180-2, but MD5 has been withdrawn). */
17 {
18 "abc",
19 "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72"
20 },
21 {
22 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
23 "\x82\x15\xef\x07\x96\xa2\x0b\xca\xaa\xe1\x16\xd3\x87\x6c\x66\x4a"
24 },
25 /* Test vectors from the NESSIE project. */
26 {
27 "",
28 "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e"
29 },
30 {
31 "a",
32 "\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8\x31\xc3\x99\xe2\x69\x77\x26\x61"
33 },
34 {
35 "message digest",
36 "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0"
37 },
38 {
39 "abcdefghijklmnopqrstuvwxyz",
40 "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b"
41 },
42 {
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
44 "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f"
45 },
46 {
47 "123456789012345678901234567890123456789012345678901234567890"
48 "12345678901234567890",
49 "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6\x7a"
50 }
51 };
52
53 static void
54 report_failure(int n, const char *tag,
55 const char expected[16], uint8_t actual[16])
56 {
57 int i;
58 printf ("FAIL: test %d (%s):\n exp:", n, tag);
59 for (i = 0; i < 16; i++)
60 {
61 if (i % 4 == 0)
62 putchar (' ');
63 printf ("%02x", (unsigned int)(unsigned char)expected[i]);
64 }
65 printf ("\n got:");
66 for (i = 0; i < 16; i++)
67 {
68 if (i % 4 == 0)
69 putchar (' ');
70 printf ("%02x", (unsigned int)(unsigned char)actual[i]);
71 }
72 putchar ('\n');
73 putchar ('\n');
74 }
75
76 int
77 main (void)
78 {
79 MD5_CTX ctx;
80 uint8_t sum[16];
81 int result = 0;
82 int cnt;
83 int i;
84
85 for (cnt = 0; cnt < (int) ARRAY_SIZE (tests); ++cnt)
86 {
87 MD5_Init (&ctx);
88 MD5_Update (&ctx, tests[cnt].input, strlen (tests[cnt].input));
89 MD5_Final (sum, &ctx);
90 if (memcmp (tests[cnt].result, sum, 16))
91 {
92 report_failure (cnt, "all at once", tests[cnt].result, sum);
93 result = 1;
94 }
95
96 MD5_Init (&ctx);
97 for (i = 0; tests[cnt].input[i] != '\0'; ++i)
98 MD5_Update (&ctx, &tests[cnt].input[i], 1);
99 MD5_Final (sum, &ctx);
100 if (memcmp (tests[cnt].result, sum, 16))
101 {
102 report_failure (cnt, "byte by byte", tests[cnt].result, sum);
103 result = 1;
104 }
105 }
106
107 /* The third "informal" test vector from
108 <https://www.nist.gov/itl/ssd/software-quality-group/nsrl-test-data>. */
109 char buf[1000];
110 memset (buf, 'a', sizeof (buf));
111 MD5_Init (&ctx);
112 for (i = 0; i < 1000; ++i)
113 MD5_Update (&ctx, buf, sizeof (buf));
114 MD5_Final (sum, &ctx);
115 static const char expected[64] =
116 "\x77\x07\xd6\xae\x4e\x02\x7c\x70\xee\xa2\xa9\x35\xc2\x29\x6f\x21";
117 if (memcmp (expected, sum, 16) != 0)
118 {
119 report_failure (cnt, "block by block", expected, sum);
120 result = 1;
121 }
122
123 return result;
124 }
125
126 #else
127
128 int
129 main (void)
130 {
131 return 77; /* UNSUPPORTED */
132 }
133
134 #endif