2 * Unit tests for SystemFunctionXXX (LMHash?)
4 * Copyright 2004 Hans Leidekker
5 * Copyright 2006 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
26 #include "wine/test.h"
34 unsigned char *Buffer;
37 typedef NTSTATUS (WINAPI *fnSystemFunction001)(const LPBYTE, const LPBYTE, LPBYTE);
38 typedef NTSTATUS (WINAPI *fnSystemFunction002)(const LPBYTE, const LPBYTE, LPBYTE);
39 typedef NTSTATUS (WINAPI *fnSystemFunction003)(const LPBYTE, LPBYTE);
40 typedef NTSTATUS (WINAPI *fnSystemFunction004)(const struct ustring *, const struct ustring *, struct ustring *);
41 typedef NTSTATUS (WINAPI *fnSystemFunction005)(const struct ustring *, const struct ustring *, struct ustring *);
42 typedef VOID (WINAPI *fnSystemFunction006)( PCSTR passwd, PSTR lmhash );
43 typedef NTSTATUS (WINAPI *fnSystemFunction008)(const LPBYTE, const LPBYTE, LPBYTE);
44 typedef NTSTATUS (WINAPI *fnSystemFunction009)(const LPBYTE, const LPBYTE, LPBYTE);
45 typedef int (WINAPI *descrypt)(unsigned char *, unsigned char *, unsigned char *);
46 typedef NTSTATUS (WINAPI *fnSystemFunction032)(struct ustring *, struct ustring *);
48 fnSystemFunction001 pSystemFunction001;
49 fnSystemFunction002 pSystemFunction002;
50 fnSystemFunction003 pSystemFunction003;
51 fnSystemFunction004 pSystemFunction004;
52 fnSystemFunction004 pSystemFunction005;
53 fnSystemFunction006 pSystemFunction006;
54 fnSystemFunction008 pSystemFunction008;
55 fnSystemFunction008 pSystemFunction009;
57 /* encrypt two blocks */
58 descrypt pSystemFunction012;
59 descrypt pSystemFunction014;
60 descrypt pSystemFunction016;
61 descrypt pSystemFunction018;
62 descrypt pSystemFunction020;
63 descrypt pSystemFunction022;
65 /* decrypt two blocks */
66 descrypt pSystemFunction013;
67 descrypt pSystemFunction015;
68 descrypt pSystemFunction017;
69 descrypt pSystemFunction019;
70 descrypt pSystemFunction021;
71 descrypt pSystemFunction023;
73 /* encrypt two blocks with a 32bit key */
74 descrypt pSystemFunction024;
75 descrypt pSystemFunction025;
77 /* decrypt two blocks with a 32bit key */
78 descrypt pSystemFunction026;
79 descrypt pSystemFunction027;
81 fnSystemFunction032 pSystemFunction032;
83 static void test_SystemFunction006(void)
87 char passwd[] = { 's','e','c','r','e','t', 0, 0, 0, 0, 0, 0, 0, 0 };
88 unsigned char expect[] =
89 { 0x85, 0xf5, 0x28, 0x9f, 0x09, 0xdc, 0xa7, 0xeb,
90 0xaa, 0xd3, 0xb4, 0x35, 0xb5, 0x14, 0x04, 0xee };
92 pSystemFunction006( passwd, lmhash );
94 ok( !memcmp( lmhash, expect, sizeof(expect) ),
95 "lmhash: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
96 lmhash[0], lmhash[1], lmhash[2], lmhash[3], lmhash[4], lmhash[5],
97 lmhash[6], lmhash[7], lmhash[8], lmhash[9], lmhash[10], lmhash[11],
98 lmhash[12], lmhash[13], lmhash[14], lmhash[15] );
101 static void test_SystemFunction008(void)
103 /* example data from http://davenport.sourceforge.net/ntlm.html */
104 unsigned char hash[0x40] = {
105 0xff, 0x37, 0x50, 0xbc, 0xc2, 0xb2, 0x24, 0x12,
106 0xc2, 0x26, 0x5b, 0x23, 0x73, 0x4e, 0x0d, 0xac };
107 unsigned char challenge[0x40] = {
108 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
109 unsigned char expected[0x18] = {
110 0xc3, 0x37, 0xcd, 0x5c, 0xbd, 0x44, 0xfc, 0x97,
111 0x82, 0xa6, 0x67, 0xaf, 0x6d, 0x42, 0x7c, 0x6d,
112 0xe6, 0x7c, 0x20, 0xc2, 0xd3, 0xe7, 0x7c, 0x56 };
113 unsigned char output[0x18];
116 r = pSystemFunction008(0,0,0);
117 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
119 r = pSystemFunction008(challenge,0,0);
120 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
122 r = pSystemFunction008(challenge, hash, 0);
123 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
128 r = pSystemFunction008(challenge, 0, output);
129 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
132 r = pSystemFunction008(0, 0, output);
133 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
135 memset(output, 0, sizeof output);
136 r = pSystemFunction008(challenge, hash, output);
137 ok( r == STATUS_SUCCESS, "wrong error code\n");
139 ok( !memcmp(output, expected, sizeof expected), "response wrong\n");
142 static void test_SystemFunction001(void)
144 unsigned char key[8] = { 0xff, 0x37, 0x50, 0xbc, 0xc2, 0xb2, 0x24, 0 };
145 unsigned char data[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
146 unsigned char expected[8] = { 0xc3, 0x37, 0xcd, 0x5c, 0xbd, 0x44, 0xfc, 0x97 };
147 unsigned char output[16];
150 r = pSystemFunction001(0,0,0);
151 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
153 memset(output, 0, sizeof output);
155 r = pSystemFunction001(data,key,output);
156 ok( r == STATUS_SUCCESS, "wrong error code\n");
158 ok(!memcmp(output, expected, sizeof expected), "response wrong\n");
161 static void test_SystemFunction002(void)
163 /* reverse of SystemFunction001 */
164 unsigned char key[8] = { 0xff, 0x37, 0x50, 0xbc, 0xc2, 0xb2, 0x24, 0 };
165 unsigned char expected[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
166 unsigned char data[8] = { 0xc3, 0x37, 0xcd, 0x5c, 0xbd, 0x44, 0xfc, 0x97 };
167 unsigned char output[8];
170 memset(output, 0, sizeof output);
171 r = pSystemFunction002(data, key, output);
172 ok(r == STATUS_SUCCESS, "function failed\n");
173 ok(!memcmp(output, expected, sizeof expected), "response wrong\n");
176 static void test_SystemFunction032(void)
178 struct ustring key, data;
179 unsigned char szKey[] = { 'f','o','o',0 };
180 unsigned char szData[8] = { 'b','a','r',0 };
181 unsigned char expected[] = {0x28, 0xb9, 0xf8, 0xe1};
184 /* crashes: pSystemFunction032(NULL,NULL); */
187 key.Length = sizeof szKey;
188 key.MaximumLength = key.Length;
190 data.Buffer = szData;
192 data.MaximumLength = 8;
194 r = pSystemFunction032(&data, &key);
195 ok(r == STATUS_SUCCESS, "function failed\n");
197 ok(!memcmp(expected, data.Buffer, data.Length), "wrong result\n");
200 static void test_SystemFunction003(void)
202 unsigned char output[8], data[8];
203 unsigned char key[7] = { 0xff, 0x37, 0x50, 0xbc, 0xc2, 0xb2, 0x24 };
204 unsigned char exp1[8] = { 0x9d, 0x21, 0xc8, 0x86, 0x6c, 0x21, 0xcf, 0x43 };
205 char exp2[] = "KGS!@#$%";
208 r = pSystemFunction003(NULL, NULL);
209 ok(r == STATUS_UNSUCCESSFUL, "function failed\n");
211 r = pSystemFunction003(key, NULL);
212 ok(r == STATUS_UNSUCCESSFUL, "function failed\n");
214 memset(data, 0, sizeof data);
215 r = pSystemFunction003(key, data);
216 ok(r == STATUS_SUCCESS, "function failed\n");
217 ok( !memcmp(exp1, data, sizeof data), "decrypted message wrong\n");
219 memset(output, 0, sizeof output);
220 r = pSystemFunction002(data, key, output);
222 ok( !memcmp(exp2, output, sizeof output), "decrypted message wrong\n");
225 static void test_SystemFunction004(void)
227 unsigned char inbuf[0x100], keybuf[0x100], resbuf[0x100];
228 unsigned char output[8];
230 struct ustring in, key, out;
233 r = pSystemFunction004(NULL, NULL, NULL);
234 ok(r == STATUS_UNSUCCESSFUL, "function failed\n");
237 memset(inbuf, 0, sizeof inbuf);
238 memset(keybuf, 0, sizeof keybuf);
239 memset(resbuf, 0, sizeof resbuf);
242 in.Length = in.MaximumLength = 0;
245 key.Length = key.MaximumLength = 0;
248 out.Length = out.MaximumLength = 0;
250 r = pSystemFunction004(&in, &key, &out);
251 ok(r == STATUS_INVALID_PARAMETER_2, "function failed\n");
255 key.MaximumLength = 0x100;
257 r = pSystemFunction004(&in, &key, (struct ustring *)&out);
258 ok(r == STATUS_BUFFER_TOO_SMALL, "function failed\n");
262 in.MaximumLength = 0;
264 /* add two identical blocks... */
275 /* check that the Length field is really obeyed */
280 key.MaximumLength = 0;
286 out.MaximumLength = 0x40;
287 r = pSystemFunction004(&in, &key, &out);
288 ok(r == STATUS_SUCCESS, "function failed\n");
292 memset(output, 0, sizeof output);
293 r = pSystemFunction002(out.Buffer, key.Buffer, output);
295 ok(((unsigned int*)output)[0] == in.Length, "crypted length wrong\n");
296 ok(((unsigned int*)output)[1] == 1, "crypted value wrong\n");
298 memset(output, 0, sizeof output);
299 r = pSystemFunction002(out.Buffer+8, key.Buffer, output);
300 ok(!memcmp(output, inbuf, sizeof output), "crypted data wrong\n");
302 memset(output, 0, sizeof output);
303 r = pSystemFunction002(out.Buffer+16, key.Buffer, output);
304 ok(!memcmp(output, inbuf, sizeof output), "crypted data wrong\n");
307 static void test_SystemFunction005(void)
309 char output[0x40], result[0x40];
311 struct ustring in, key, out, res;
312 char *datastr = "twinkle twinkle little star";
313 char *keystr = "byolnim";
315 in.Buffer = (unsigned char *) datastr;
316 in.Length = strlen(datastr);
317 in.MaximumLength = 0;
319 key.Buffer = (unsigned char *)keystr;
320 key.Length = strlen(keystr);
321 key.MaximumLength = 0;
323 out.Buffer = (unsigned char *)output;
324 out.Length = out.MaximumLength = sizeof output;
326 r = pSystemFunction004(&in, &key, &out);
327 ok(r == STATUS_SUCCESS, "function failed\n");
329 memset(result, 0, sizeof result);
330 res.Buffer = (unsigned char *)result;
332 res.MaximumLength = sizeof result;
334 r = pSystemFunction005(&out, &key, &res);
335 ok(r == STATUS_SUCCESS, "function failed\n");
337 r = pSystemFunction005(&out, &key, &res);
338 ok(r == STATUS_SUCCESS, "function failed\n");
340 ok(res.Length == in.Length, "Length wrong\n");
341 ok(!memcmp(res.Buffer, in.Buffer, in.Length), "data wrong\n");
344 out.MaximumLength = 0;
345 r = pSystemFunction005(&out, &key, &res);
346 ok(r == STATUS_SUCCESS, "function failed\n");
348 ok(res.Length == in.Length, "Length wrong\n");
349 ok(!memcmp(res.Buffer, in.Buffer, in.Length), "data wrong\n");
351 res.MaximumLength = 0;
352 r = pSystemFunction005(&out, &key, &res);
353 ok(r == STATUS_BUFFER_TOO_SMALL, "function failed\n");
356 r = pSystemFunction005(&out, &key, &res);
357 ok(r == STATUS_UNKNOWN_REVISION, "function failed\n");
360 r = pSystemFunction005(&out, &key, &res);
361 ok(r == STATUS_INVALID_PARAMETER_2, "function failed\n");
364 static void test_SystemFunction009(void)
366 unsigned char hash[0x10] = {
367 0xff, 0x37, 0x50, 0xbc, 0xc2, 0xb2, 0x24, 0x12,
368 0xc2, 0x26, 0x5b, 0x23, 0x73, 0x4e, 0x0d, 0xac };
369 unsigned char challenge[8] = {
370 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
371 unsigned char expected[0x18] = {
372 0xc3, 0x37, 0xcd, 0x5c, 0xbd, 0x44, 0xfc, 0x97,
373 0x82, 0xa6, 0x67, 0xaf, 0x6d, 0x42, 0x7c, 0x6d,
374 0xe6, 0x7c, 0x20, 0xc2, 0xd3, 0xe7, 0x7c, 0x56 };
375 unsigned char output[0x18];
378 memset(output, 0, sizeof output);
379 r = pSystemFunction009(challenge, hash, output);
380 ok( r == STATUS_SUCCESS, "wrong error code\n");
381 ok(!memcmp(output, expected, sizeof expected), "response wrong\n");
384 static unsigned char des_key[] = {
385 0xff, 0x37, 0x50, 0xbc, 0xc2, 0xb2, 0x24,
386 0xff, 0x37, 0x50, 0xbc, 0xc2, 0xb2, 0x24,
388 static unsigned char des_plaintext[] = {
389 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
390 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0
392 static unsigned char des_ciphertext[] = {
393 0xc3, 0x37, 0xcd, 0x5c, 0xbd, 0x44, 0xfc, 0x97,
394 0xc3, 0x37, 0xcd, 0x5c, 0xbd, 0x44, 0xfc, 0x97, 0
397 /* test functions that encrypt two DES blocks */
398 static void test_SystemFunction_encrypt(descrypt func, int num)
400 unsigned char output[0x11];
406 r = func(NULL, NULL, NULL);
407 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
409 memset(output, 0, sizeof output);
410 r = func(des_plaintext, des_key, output);
411 ok( r == STATUS_SUCCESS, "wrong error code\n");
412 ok( !memcmp(des_ciphertext, output, sizeof des_ciphertext), "ciphertext wrong (%d)\n", num);
415 /* test functions that decrypt two DES blocks */
416 static void test_SystemFunction_decrypt(descrypt func, int num)
418 unsigned char output[0x11];
424 r = func(NULL, NULL, NULL);
425 ok( r == STATUS_UNSUCCESSFUL, "wrong error code\n");
427 memset(output, 0, sizeof output);
429 r = func(des_ciphertext, des_key, output);
430 ok( r == STATUS_SUCCESS, "wrong error code\n");
431 ok( !memcmp(des_plaintext, output, sizeof des_plaintext), "plaintext wrong (%d)\n", num);
434 static unsigned char des_ciphertext32[] = {
435 0x69, 0x51, 0x35, 0x69, 0x0d, 0x29, 0x24, 0xad,
436 0x23, 0x6d, 0xfd, 0x43, 0x0d, 0xd3, 0x25, 0x81, 0
439 static void test_SystemFunction_enc32(descrypt func, int num)
441 unsigned char key[4], output[0x11];
447 memset(output, 0, sizeof output);
449 /* two keys are generated using 4 bytes, repeated 4 times ... */
450 memcpy(key, "foo", 4);
452 r = func(des_plaintext, key, output);
453 ok( r == STATUS_SUCCESS, "wrong error code (%d)\n", num);
455 ok( !memcmp( output, des_ciphertext32, sizeof des_ciphertext32), "ciphertext wrong (%d)\n", num);
458 static void test_SystemFunction_dec32(descrypt func, int num)
460 unsigned char key[4], output[0x11];
466 memset(output, 0, sizeof output);
468 /* two keys are generated using 4 bytes, repeated 4 times ... */
469 memcpy(key, "foo", 4);
471 r = func(des_ciphertext32, key, output);
472 ok( r == STATUS_SUCCESS, "wrong error code (%d)\n", num);
474 ok( !memcmp( output, des_plaintext, sizeof des_plaintext), "plaintext wrong (%d)\n", num);
477 START_TEST(crypt_lmhash)
481 if (!(module = LoadLibrary("advapi32.dll"))) return;
483 pSystemFunction001 = (fnSystemFunction001)GetProcAddress( module, "SystemFunction001" );
484 if (pSystemFunction001)
485 test_SystemFunction001();
487 pSystemFunction002 = (fnSystemFunction002)GetProcAddress( module, "SystemFunction002" );
488 if (pSystemFunction002)
489 test_SystemFunction002();
491 pSystemFunction003 = (fnSystemFunction003)GetProcAddress( module, "SystemFunction003" );
492 if (pSystemFunction003)
493 test_SystemFunction003();
495 pSystemFunction004 = (fnSystemFunction004)GetProcAddress( module, "SystemFunction004" );
496 if (pSystemFunction004)
497 test_SystemFunction004();
499 pSystemFunction005 = (fnSystemFunction005)GetProcAddress( module, "SystemFunction005" );
500 if (pSystemFunction005)
501 test_SystemFunction005();
503 pSystemFunction006 = (fnSystemFunction006)GetProcAddress( module, "SystemFunction006" );
504 if (pSystemFunction006)
505 test_SystemFunction006();
507 pSystemFunction008 = (fnSystemFunction008)GetProcAddress( module, "SystemFunction008" );
508 if (pSystemFunction008)
509 test_SystemFunction008();
511 pSystemFunction009 = (fnSystemFunction009)GetProcAddress( module, "SystemFunction009" );
512 if (pSystemFunction009)
513 test_SystemFunction009();
515 pSystemFunction032 = (fnSystemFunction032)GetProcAddress( module, "SystemFunction032" );
516 if (pSystemFunction032)
517 test_SystemFunction032();
519 pSystemFunction012 = (descrypt) GetProcAddress( module, "SystemFunction012");
520 pSystemFunction013 = (descrypt) GetProcAddress( module, "SystemFunction013");
521 pSystemFunction014 = (descrypt) GetProcAddress( module, "SystemFunction014");
522 pSystemFunction015 = (descrypt) GetProcAddress( module, "SystemFunction015");
523 pSystemFunction016 = (descrypt) GetProcAddress( module, "SystemFunction016");
524 pSystemFunction017 = (descrypt) GetProcAddress( module, "SystemFunction017");
525 pSystemFunction018 = (descrypt) GetProcAddress( module, "SystemFunction018");
526 pSystemFunction019 = (descrypt) GetProcAddress( module, "SystemFunction019");
527 pSystemFunction020 = (descrypt) GetProcAddress( module, "SystemFunction020");
528 pSystemFunction021 = (descrypt) GetProcAddress( module, "SystemFunction021");
529 pSystemFunction022 = (descrypt) GetProcAddress( module, "SystemFunction022");
530 pSystemFunction023 = (descrypt) GetProcAddress( module, "SystemFunction023");
532 /* these all encrypt two DES blocks */
533 test_SystemFunction_encrypt(pSystemFunction012, 12);
534 test_SystemFunction_encrypt(pSystemFunction014, 14);
535 test_SystemFunction_encrypt(pSystemFunction016, 16);
536 test_SystemFunction_encrypt(pSystemFunction018, 18);
537 test_SystemFunction_encrypt(pSystemFunction020, 20);
538 test_SystemFunction_encrypt(pSystemFunction022, 22);
540 /* these all decrypt two DES blocks */
541 test_SystemFunction_decrypt(pSystemFunction013, 13);
542 test_SystemFunction_decrypt(pSystemFunction015, 15);
543 test_SystemFunction_decrypt(pSystemFunction017, 17);
544 test_SystemFunction_decrypt(pSystemFunction019, 19);
545 test_SystemFunction_decrypt(pSystemFunction021, 21);
546 test_SystemFunction_decrypt(pSystemFunction023, 23);
548 pSystemFunction024 = (descrypt) GetProcAddress( module, "SystemFunction024");
549 pSystemFunction025 = (descrypt) GetProcAddress( module, "SystemFunction025");
550 pSystemFunction026 = (descrypt) GetProcAddress( module, "SystemFunction026");
551 pSystemFunction027 = (descrypt) GetProcAddress( module, "SystemFunction027");
553 /* these encrypt two DES blocks with a short key */
554 test_SystemFunction_enc32(pSystemFunction024, 24);
555 test_SystemFunction_enc32(pSystemFunction026, 26);
557 /* these descrypt two DES blocks with a short key */
558 test_SystemFunction_dec32(pSystemFunction025, 25);
559 test_SystemFunction_dec32(pSystemFunction027, 27);