1 /*
2  * Unit tests for MD5 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 i[2];
26     unsigned int buf[4];
27     unsigned char in[64];
28     unsigned char digest[16];
29 } MD5_CTX;
30 
31 static VOID (WINAPI *pMD5Init)( MD5_CTX *ctx );
32 static VOID (WINAPI *pMD5Update)( MD5_CTX *ctx, const unsigned char *src, const int len );
33 static VOID (WINAPI *pMD5Final)( MD5_CTX *ctx );
34 
35 #define ctxcmp( a, b ) memcmp( a, b, FIELD_OFFSET( MD5_CTX, in ) )
36 
37 static void test_md5_ctx(void)
38 {
39     static unsigned char message[] =
40         "In our Life there's If"
41         "In our beliefs there's Lie"
42         "In our business there is Sin"
43         "In our bodies, there is Die";
44 
45     int size = sizeof(message) - 1;
46     HMODULE module;
47 
48     MD5_CTX ctx;
49     MD5_CTX ctx_initialized =
50     {
51         { 0, 0 },
52         { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }
53     };
54 
55     MD5_CTX ctx_update1 =
56     {
57         { 0x00000338, 0 },
58         { 0x068cb64d, 0xb7a05790, 0x426979ee, 0xed67e221 }
59     };
60 
61     MD5_CTX ctx_update2 =
62     {
63         { 0x00000670, 0 },
64         { 0x2f7afe58, 0xcc3e9315, 0x709c465c, 0xbf6414c8 }
65     };
66 
67     unsigned char expect[16] =
68         { 0x43, 0x03, 0xdd, 0x8c, 0x60, 0xd9, 0x3a, 0x22,
69           0x0b, 0x28, 0xd0, 0xb2, 0x65, 0x93, 0xd0, 0x36 };
70 
71     module = GetModuleHandleA("advapi32.dll");
72 
73     pMD5Init = (void *)GetProcAddress( module, "MD5Init" );
74     pMD5Update = (void *)GetProcAddress( module, "MD5Update" );
75     pMD5Final = (void *)GetProcAddress( module, "MD5Final" );
76 
77     if (!pMD5Init || !pMD5Update || !pMD5Final)
78     {
79         win_skip("Needed functions are not available\n");
80         return;
81     }
82 
83     memset( &ctx, 0, sizeof(ctx) );
84     pMD5Init( &ctx );
85     ok( !ctxcmp( &ctx, &ctx_initialized ), "invalid initialization\n" );
86 
87     pMD5Update( &ctx, message, size );
88     ok( !ctxcmp( &ctx, &ctx_update1 ), "update doesn't work correctly\n" );
89 
90     pMD5Update( &ctx, message, size );
91     ok( !ctxcmp( &ctx, &ctx_update2 ), "update doesn't work correctly\n" );
92 
93     pMD5Final( &ctx );
94     ok( ctxcmp( &ctx, &ctx_initialized ), "context has changed\n" );
95     ok( !memcmp( ctx.digest, expect, sizeof(expect) ), "incorrect result\n" );
96 }
97 
98 START_TEST(crypt_md5)
99 {
100     test_md5_ctx();
101 }
102