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