1 /*
2  * Unit tests for MD4 functions
3  *
4  * Copyright 2004 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "precomp.h"
22 
23 typedef struct
24 {
25     unsigned int buf[4];
26     unsigned int i[2];
27     unsigned char in[64];
28     unsigned char digest[16];
29 } MD4_CTX;
30 
31 static VOID (WINAPI *pMD4Init)( MD4_CTX *ctx );
32 static VOID (WINAPI *pMD4Update)( MD4_CTX *ctx, const unsigned char *src, const int len );
33 static VOID (WINAPI *pMD4Final)( MD4_CTX *ctx );
34 static int (WINAPI *pSystemFunction007)(const UNICODE_STRING *, LPBYTE);
35 typedef int (WINAPI *md4hashfunc)(LPVOID, const LPBYTE, LPBYTE);
36 
37 static md4hashfunc pSystemFunction010;
38 static md4hashfunc pSystemFunction011;
39 
40 #define ctxcmp( a, b ) memcmp( a, b, FIELD_OFFSET( MD4_CTX, in ) )
41 
42 static void test_md4_ctx(void)
43 {
44     static unsigned char message[] =
45         "In our Life there's If"
46         "In our beliefs there's Lie"
47         "In our business there is Sin"
48         "In our bodies, there is Die";
49 
50     int size = sizeof(message) - 1;
51 
52     MD4_CTX ctx;
53     MD4_CTX ctx_initialized =
54     {
55         { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 },
56         { 0, 0 }
57     };
58 
59     MD4_CTX ctx_update1 =
60     {
61         { 0x5e592ef7, 0xbdcb1567, 0x2b626d17, 0x7d1198bd },
62         { 0x00000338, 0 }
63     };
64 
65     MD4_CTX ctx_update2 =
66     {
67         { 0x05dcfd65, 0xb3711c0d, 0x9e3369c2, 0x903ead11 },
68         { 0x00000670, 0 }
69     };
70 
71     unsigned char expect[16] =
72         { 0x5f, 0xd3, 0x9b, 0x29, 0x47, 0x53, 0x47, 0xaf,
73           0xa5, 0xba, 0x0c, 0x05, 0xff, 0xc0, 0xc7, 0xda };
74 
75 
76     memset( &ctx, 0, sizeof(ctx) );
77     pMD4Init( &ctx );
78     ok( !ctxcmp( &ctx, &ctx_initialized ), "invalid initialization\n" );
79 
80     pMD4Update( &ctx, message, size );
81     ok( !ctxcmp( &ctx, &ctx_update1 ), "update doesn't work correctly\n" );
82 
83     pMD4Update( &ctx, message, size );
84     ok( !ctxcmp( &ctx, &ctx_update2 ), "update doesn't work correctly\n" );
85 
86     pMD4Final( &ctx );
87     ok( ctxcmp( &ctx, &ctx_initialized ), "context has changed\n" );
88     ok( !memcmp( ctx.digest, expect, sizeof(expect) ), "incorrect result\n" );
89 
90 }
91 
92 static void test_SystemFunction007(void)
93 {
94     int r;
95     UNICODE_STRING str;
96     BYTE output[0x10];
97     BYTE expected[0x10] = { 0x24, 0x0a, 0xf0, 0x9d, 0x84, 0x1c, 0xda, 0xcf,
98                             0x56, 0xeb, 0x6b, 0x96, 0x55, 0xec, 0xcf, 0x0a };
99     WCHAR szFoo[] = {'f','o','o',0 };
100 
101     if (0)
102     {
103     /* crashes on Windows */
104     r = pSystemFunction007(NULL, NULL);
105     ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
106     }
107 
108     str.Buffer = szFoo;
109     str.Length = 4*sizeof(WCHAR);
110     str.MaximumLength = str.Length;
111 
112     memset(output, 0, sizeof output);
113     r = pSystemFunction007(&str, output);
114     ok( r == STATUS_SUCCESS, "wrong error code\n");
115 
116     ok(!memcmp(output, expected, sizeof expected), "response wrong\n");
117 }
118 
119 static void test_md4hashfunc(md4hashfunc func)
120 {
121     unsigned char expected[0x10] = {
122         0x48, 0x7c, 0x3f, 0x5e, 0x2b, 0x0d, 0x6a, 0x79,
123         0x32, 0x4e, 0xcd, 0xbe, 0x9c, 0x15, 0x16, 0x6f };
124     unsigned char in[0x10], output[0x10];
125     int r;
126 
127     memset(in, 0, sizeof in);
128     memset(output, 0, sizeof output);
129     r = func(0, in, output);
130     ok( r == STATUS_SUCCESS, "wrong error code\n");
131     ok( !memcmp(expected, output, sizeof output), "output wrong\n");
132 }
133 
134 START_TEST(crypt_md4)
135 {
136     HMODULE module;
137 
138     module = GetModuleHandleA( "advapi32.dll" );
139 
140     pMD4Init = (void *)GetProcAddress( module, "MD4Init" );
141     pMD4Update = (void *)GetProcAddress( module, "MD4Update" );
142     pMD4Final = (void *)GetProcAddress( module, "MD4Final" );
143 
144     if (pMD4Init && pMD4Update && pMD4Final)
145         test_md4_ctx();
146     else
147         win_skip("MD4Init and/or MD4Update and/or MD4Final are not available\n");
148 
149     pSystemFunction007 = (void *)GetProcAddress( module, "SystemFunction007" );
150     if (pSystemFunction007)
151         test_SystemFunction007();
152     else
153         win_skip("SystemFunction007 is not available\n");
154 
155     pSystemFunction010 = (md4hashfunc)GetProcAddress( module, "SystemFunction010" );
156     if (pSystemFunction010)
157         test_md4hashfunc(pSystemFunction010);
158     else
159         win_skip("SystemFunction010 is not available\n");
160 
161     pSystemFunction011 = (md4hashfunc)GetProcAddress( module, "SystemFunction011" );
162     if (pSystemFunction011)
163         test_md4hashfunc(pSystemFunction011);
164     else
165         win_skip("SystemFunction011 is not available\n");
166 }
167