usp10: Move the application of pair values to a helper function.
[wine] / dlls / advapi32 / crypt_md4.c
1 /*
2  * Copyright (C) 2001 Nikos Mavroyanopoulos
3  * Copyright (C) 2004 Hans Leidekker
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 /*
21  * This code implements the MD4 message-digest algorithm.
22  * It is based on code in the public domain written by Colin
23  * Plumb in 1993. The algorithm is due to Ron Rivest.
24  *
25  * Equivalent code is available from RSA Data Security, Inc.
26  * This code has been tested against that, and is equivalent,
27  * except that you don't need to include two pages of legalese
28  * with every copy.
29  *
30  * To compute the message digest of a chunk of bytes, declare an
31  * MD4_CTX structure, pass it to MD4Init, call MD4Update as
32  * needed on buffers full of bytes, and then call MD4Final, which
33  * will fill a supplied 16-byte array with the digest.
34  */
35
36 #include <stdarg.h>
37
38 #include "ntstatus.h"
39 #define WIN32_NO_STATUS
40 #include "windef.h"
41 #include "winternl.h"
42
43 typedef struct
44 {
45     unsigned int buf[4];
46     unsigned int i[2];
47     unsigned char in[64];
48     unsigned char digest[16];
49 } MD4_CTX;
50
51 static void MD4Transform( unsigned int buf[4], unsigned int const in[16] );
52
53 /*
54  * Note: this code is harmless on little-endian machines.
55  */
56 static void byteReverse( unsigned char *buf, unsigned longs )
57 {
58     unsigned int t;
59
60     do {
61         t = ((unsigned)buf[3] << 8 | buf[2]) << 16 |
62             ((unsigned)buf[1] << 8 | buf[0]);
63         *(unsigned int *)buf = t;
64         buf += 4;
65     } while (--longs);
66 }
67
68 /*
69  * Start MD4 accumulation.  Set bit count to 0 and buffer to mysterious
70  * initialization constants.
71  */
72 VOID WINAPI MD4Init( MD4_CTX *ctx )
73 {
74     ctx->buf[0] = 0x67452301;
75     ctx->buf[1] = 0xefcdab89;
76     ctx->buf[2] = 0x98badcfe;
77     ctx->buf[3] = 0x10325476;
78
79     ctx->i[0] = ctx->i[1] = 0;
80 }
81
82 /*
83  * Update context to reflect the concatenation of another buffer full
84  * of bytes.
85  */
86 VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len )
87 {
88     register unsigned int t;
89
90     /* Update bitcount */
91     t = ctx->i[0];
92
93     if ((ctx->i[0] = t + (len << 3)) < t)
94         ctx->i[1]++;        /* Carry from low to high */
95
96     ctx->i[1] += len >> 29;
97     t = (t >> 3) & 0x3f;
98
99     /* Handle any leading odd-sized chunks */
100     if (t)
101     {
102         unsigned char *p = (unsigned char *)ctx->in + t;
103         t = 64 - t;
104
105         if (len < t)
106         {
107             memcpy( p, buf, len );
108             return;
109         }
110
111         memcpy( p, buf, t );
112         byteReverse( ctx->in, 16 );
113
114         MD4Transform( ctx->buf, (unsigned int *)ctx->in );
115
116         buf += t;
117         len -= t;
118     }
119
120     /* Process data in 64-byte chunks */
121     while (len >= 64)
122     {
123         memcpy( ctx->in, buf, 64 );
124         byteReverse( ctx->in, 16 );
125
126         MD4Transform( ctx->buf, (unsigned int *)ctx->in );
127
128         buf += 64;
129         len -= 64;
130     }
131
132     /* Handle any remaining bytes of data. */
133     memcpy( ctx->in, buf, len );
134 }
135
136 /*
137  * Final wrapup - pad to 64-byte boundary with the bit pattern 
138  * 1 0* (64-bit count of bits processed, MSB-first)
139  */
140 VOID WINAPI MD4Final( MD4_CTX *ctx )
141 {
142     unsigned int count;
143     unsigned char *p;
144
145     /* Compute number of bytes mod 64 */
146     count = (ctx->i[0] >> 3) & 0x3F;
147
148     /* Set the first char of padding to 0x80.  This is safe since there is
149        always at least one byte free */
150     p = ctx->in + count;
151     *p++ = 0x80;
152
153     /* Bytes of padding needed to make 64 bytes */
154     count = 64 - 1 - count;
155
156     /* Pad out to 56 mod 64 */
157     if (count < 8)
158     {
159         /* Two lots of padding:  Pad the first block to 64 bytes */
160         memset( p, 0, count );
161         byteReverse( ctx->in, 16 );
162         MD4Transform( ctx->buf, (unsigned int *)ctx->in );
163
164         /* Now fill the next block with 56 bytes */
165         memset( ctx->in, 0, 56 );
166     }
167     else
168     {
169         /* Pad block to 56 bytes */
170         memset( p, 0, count - 8 );
171     }
172
173     byteReverse( ctx->in, 14 );
174
175     /* Append length in bits and transform */
176     ((unsigned int *)ctx->in)[14] = ctx->i[0];
177     ((unsigned int *)ctx->in)[15] = ctx->i[1];
178
179     MD4Transform( ctx->buf, (unsigned int *)ctx->in );
180     byteReverse( (unsigned char *)ctx->buf, 4 );
181     memcpy( ctx->digest, ctx->buf, 16 );
182 }
183
184 /* The three core functions */
185
186 #define rotl32(x,n)  (((x) << ((unsigned int)(n))) | ((x) >> (32 - (unsigned int)(n))))
187
188 #define F( x, y, z ) (((x) & (y)) | ((~x) & (z)))
189 #define G( x, y, z ) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
190 #define H( x, y, z ) ((x) ^ (y) ^ (z))
191
192 #define FF( a, b, c, d, x, s ) { \
193     (a) += F( (b), (c), (d) ) + (x); \
194     (a) = rotl32( (a), (s) ); \
195   }
196 #define GG( a, b, c, d, x, s ) { \
197     (a) += G( (b), (c), (d) ) + (x) + (unsigned int)0x5a827999; \
198     (a) = rotl32( (a), (s) ); \
199   }
200 #define HH( a, b, c, d, x, s ) { \
201     (a) += H( (b), (c), (d) ) + (x) + (unsigned int)0x6ed9eba1; \
202     (a) = rotl32( (a), (s) ); \
203   }
204
205 /*
206  * The core of the MD4 algorithm
207  */
208 static void MD4Transform( unsigned int buf[4], const unsigned int in[16] )
209 {
210     register unsigned int a, b, c, d;
211
212     a = buf[0];
213     b = buf[1];
214     c = buf[2];
215     d = buf[3];
216
217     FF( a, b, c, d, in[0], 3 );
218     FF( d, a, b, c, in[1], 7 );
219     FF( c, d, a, b, in[2], 11 );
220     FF( b, c, d, a, in[3], 19 );
221     FF( a, b, c, d, in[4], 3 );
222     FF( d, a, b, c, in[5], 7 );
223     FF( c, d, a, b, in[6], 11 );
224     FF( b, c, d, a, in[7], 19 );
225     FF( a, b, c, d, in[8], 3 );
226     FF( d, a, b, c, in[9], 7 );
227     FF( c, d, a, b, in[10], 11 );
228     FF( b, c, d, a, in[11], 19 );
229     FF( a, b, c, d, in[12], 3 );
230     FF( d, a, b, c, in[13], 7 );
231     FF( c, d, a, b, in[14], 11 );
232     FF( b, c, d, a, in[15], 19 );
233
234     GG( a, b, c, d, in[0], 3 );
235     GG( d, a, b, c, in[4], 5 );
236     GG( c, d, a, b, in[8], 9 );
237     GG( b, c, d, a, in[12], 13 );
238     GG( a, b, c, d, in[1], 3 );
239     GG( d, a, b, c, in[5], 5 );
240     GG( c, d, a, b, in[9], 9 );
241     GG( b, c, d, a, in[13], 13 );
242     GG( a, b, c, d, in[2], 3 );
243     GG( d, a, b, c, in[6], 5 );
244     GG( c, d, a, b, in[10], 9 );
245     GG( b, c, d, a, in[14], 13 );
246     GG( a, b, c, d, in[3], 3 );
247     GG( d, a, b, c, in[7], 5 );
248     GG( c, d, a, b, in[11], 9 );
249     GG( b, c, d, a, in[15], 13 );
250
251     HH( a, b, c, d, in[0], 3 );
252     HH( d, a, b, c, in[8], 9 );
253     HH( c, d, a, b, in[4], 11 );
254     HH( b, c, d, a, in[12], 15 );
255     HH( a, b, c, d, in[2], 3 );
256     HH( d, a, b, c, in[10], 9 );
257     HH( c, d, a, b, in[6], 11 );
258     HH( b, c, d, a, in[14], 15 );
259     HH( a, b, c, d, in[1], 3 );
260     HH( d, a, b, c, in[9], 9 );
261     HH( c, d, a, b, in[5], 11 );
262     HH( b, c, d, a, in[13], 15 );
263     HH( a, b, c, d, in[3], 3 );
264     HH( d, a, b, c, in[11], 9 );
265     HH( c, d, a, b, in[7], 11 );
266     HH( b, c, d, a, in[15], 15 );
267
268     buf[0] += a;
269     buf[1] += b;
270     buf[2] += c;
271     buf[3] += d;
272 }
273
274 /******************************************************************************
275  * SystemFunction007  [ADVAPI32.@]
276  *
277  * MD4 hash a unicode string
278  *
279  * PARAMS
280  *   string  [I] the string to hash
281  *   output  [O] the md4 hash of the string (16 bytes)
282  *
283  * RETURNS
284  *  Success: STATUS_SUCCESS
285  *  Failure: STATUS_UNSUCCESSFUL
286  *
287  */
288 NTSTATUS WINAPI SystemFunction007(const UNICODE_STRING *string, LPBYTE hash)
289 {
290     MD4_CTX ctx;
291
292     MD4Init( &ctx );
293     MD4Update( &ctx, (const BYTE *)string->Buffer, string->Length );
294     MD4Final( &ctx );
295     memcpy( hash, ctx.digest, 0x10 );
296
297     return STATUS_SUCCESS;
298 }
299
300 /******************************************************************************
301  * SystemFunction010  [ADVAPI32.@]
302  * SystemFunction011  [ADVAPI32.@]
303  *
304  * MD4 hashes 16 bytes of data
305  *
306  * PARAMS
307  *   unknown []  seems to have no effect on the output
308  *   data    [I] pointer to data to hash (16 bytes)
309  *   output  [O] the md4 hash of the data (16 bytes)
310  *
311  * RETURNS
312  *  Success: STATUS_SUCCESS
313  *  Failure: STATUS_UNSUCCESSFUL
314  *
315  */
316 NTSTATUS WINAPI SystemFunction010(LPVOID unknown, const BYTE *data, LPBYTE hash)
317 {
318     MD4_CTX ctx;
319
320     MD4Init( &ctx );
321     MD4Update( &ctx, data, 0x10 );
322     MD4Final( &ctx );
323     memcpy( hash, ctx.digest, 0x10 );
324
325     return STATUS_SUCCESS;
326 }