1 #include "crypt-port.h"
2 #include "alg-md4.h"
3
4 #include <stdio.h>
5
6 #if INCLUDE_nt
7
8 static const struct
9 {
10 const char *input;
11 const char result[16];
12 } tests[] =
13 {
14 /* Test vectors as defined in RFC 1320, appendix A, section 5.
15 https://tools.ietf.org/html/rfc1320#appendix-A.5 */
16 {
17 "",
18 "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0"
19 },
20 {
21 "a",
22 "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb\x24"
23 },
24 {
25 "abc",
26 "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d"
27 },
28 {
29 "message digest",
30 "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01\x4b"
31 },
32 {
33 "abcdefghijklmnopqrstuvwxyz",
34 "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d\xa9"
35 },
36 {
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
38 "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0\xe4"
39 },
40 {
41 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
42 "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05\x36"
43 }
44 };
45
46 static void
47 report_failure(int n, const char *tag,
48 const char expected[16], uint8_t actual[16])
49 {
50 int i;
51 printf ("FAIL: test %d (%s):\n exp:", n, tag);
52 for (i = 0; i < 16; i++)
53 {
54 if (i % 4 == 0)
55 putchar (' ');
56 printf ("%02x", (unsigned int)(unsigned char)expected[i]);
57 }
58 printf ("\n got:");
59 for (i = 0; i < 16; i++)
60 {
61 if (i % 4 == 0)
62 putchar (' ');
63 printf ("%02x", (unsigned int)(unsigned char)actual[i]);
64 }
65 putchar ('\n');
66 putchar ('\n');
67 }
68
69 int
70 main (void)
71 {
72 MD4_CTX ctx;
73 uint8_t sum[16];
74 int result = 0;
75 int cnt;
76 int i;
77
78 for (cnt = 0; cnt < (int) ARRAY_SIZE (tests); ++cnt)
79 {
80 MD4_Init (&ctx);
81 MD4_Update (&ctx, tests[cnt].input, strlen (tests[cnt].input));
82 MD4_Final (sum, &ctx);
83 if (memcmp (tests[cnt].result, sum, 16))
84 {
85 report_failure (cnt, "all at once", tests[cnt].result, sum);
86 result = 1;
87 }
88
89 MD4_Init (&ctx);
90 for (i = 0; tests[cnt].input[i] != '\0'; ++i)
91 MD4_Update (&ctx, &tests[cnt].input[i], 1);
92 MD4_Final (sum, &ctx);
93 if (memcmp (tests[cnt].result, sum, 16))
94 {
95 report_failure (cnt, "byte by byte", tests[cnt].result, sum);
96 result = 1;
97 }
98 }
99
100 return result;
101 }
102
103 #else
104
105 int
106 main (void)
107 {
108 return 77; /* UNSUPPORTED */
109 }
110
111 #endif