wined3d: Use the FBO cache in stretch_rect_fbo().
[wine/hramrach.git] / dlls / advapi32 / crypt_md4.c
blob3341f084b5ca42dac1c7f14357fa625b03fef35e
1 /*
2 * Copyright (C) 2001 Nikos Mavroyanopoulos
3 * Copyright (C) 2004 Hans Leidekker
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.
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.
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
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.
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.
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.
36 #include <stdarg.h>
38 #include "ntstatus.h"
39 #define WIN32_NO_STATUS
40 #include "windef.h"
41 #include "winternl.h"
43 typedef struct
45 unsigned int buf[4];
46 unsigned int i[2];
47 unsigned char in[64];
48 unsigned char digest[16];
49 } MD4_CTX;
51 static void MD4Transform( unsigned int buf[4], unsigned int const in[16] );
54 * Note: this code is harmless on little-endian machines.
56 static void byteReverse( unsigned char *buf, unsigned longs )
58 unsigned int t;
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);
69 * Start MD4 accumulation. Set bit count to 0 and buffer to mysterious
70 * initialization constants.
72 VOID WINAPI MD4Init( MD4_CTX *ctx )
74 ctx->buf[0] = 0x67452301;
75 ctx->buf[1] = 0xefcdab89;
76 ctx->buf[2] = 0x98badcfe;
77 ctx->buf[3] = 0x10325476;
79 ctx->i[0] = ctx->i[1] = 0;
83 * Update context to reflect the concatenation of another buffer full
84 * of bytes.
86 VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len )
88 register unsigned int t;
90 /* Update bitcount */
91 t = ctx->i[0];
93 if ((ctx->i[0] = t + (len << 3)) < t)
94 ctx->i[1]++; /* Carry from low to high */
96 ctx->i[1] += len >> 29;
97 t = (t >> 3) & 0x3f;
99 /* Handle any leading odd-sized chunks */
100 if (t)
102 unsigned char *p = (unsigned char *)ctx->in + t;
103 t = 64 - t;
105 if (len < t)
107 memcpy( p, buf, len );
108 return;
111 memcpy( p, buf, t );
112 byteReverse( ctx->in, 16 );
114 MD4Transform( ctx->buf, (unsigned int *)ctx->in );
116 buf += t;
117 len -= t;
120 /* Process data in 64-byte chunks */
121 while (len >= 64)
123 memcpy( ctx->in, buf, 64 );
124 byteReverse( ctx->in, 16 );
126 MD4Transform( ctx->buf, (unsigned int *)ctx->in );
128 buf += 64;
129 len -= 64;
132 /* Handle any remaining bytes of data. */
133 memcpy( ctx->in, buf, len );
137 * Final wrapup - pad to 64-byte boundary with the bit pattern
138 * 1 0* (64-bit count of bits processed, MSB-first)
140 VOID WINAPI MD4Final( MD4_CTX *ctx )
142 unsigned int count;
143 unsigned char *p;
145 /* Compute number of bytes mod 64 */
146 count = (ctx->i[0] >> 3) & 0x3F;
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;
153 /* Bytes of padding needed to make 64 bytes */
154 count = 64 - 1 - count;
156 /* Pad out to 56 mod 64 */
157 if (count < 8)
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 );
164 /* Now fill the next block with 56 bytes */
165 memset( ctx->in, 0, 56 );
167 else
169 /* Pad block to 56 bytes */
170 memset( p, 0, count - 8 );
173 byteReverse( ctx->in, 14 );
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];
179 MD4Transform( ctx->buf, (unsigned int *)ctx->in );
180 byteReverse( (unsigned char *)ctx->buf, 4 );
181 memcpy( ctx->digest, ctx->buf, 16 );
184 /* The three core functions */
186 #define rotl32(x,n) (((x) << ((unsigned int)(n))) | ((x) >> (32 - (unsigned int)(n))))
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))
192 #define FF( a, b, c, d, x, s ) { \
193 (a) += F( (b), (c), (d) ) + (x); \
194 (a) = rotl32( (a), (s) ); \
196 #define GG( a, b, c, d, x, s ) { \
197 (a) += G( (b), (c), (d) ) + (x) + (unsigned int)0x5a827999; \
198 (a) = rotl32( (a), (s) ); \
200 #define HH( a, b, c, d, x, s ) { \
201 (a) += H( (b), (c), (d) ) + (x) + (unsigned int)0x6ed9eba1; \
202 (a) = rotl32( (a), (s) ); \
206 * The core of the MD4 algorithm
208 static void MD4Transform( unsigned int buf[4], const unsigned int in[16] )
210 register unsigned int a, b, c, d;
212 a = buf[0];
213 b = buf[1];
214 c = buf[2];
215 d = buf[3];
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 );
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 );
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 );
268 buf[0] += a;
269 buf[1] += b;
270 buf[2] += c;
271 buf[3] += d;
274 /******************************************************************************
275 * SystemFunction007 [ADVAPI32.@]
277 * MD4 hash a unicode string
279 * PARAMS
280 * string [I] the string to hash
281 * output [O] the md4 hash of the string (16 bytes)
283 * RETURNS
284 * Success: STATUS_SUCCESS
285 * Failure: STATUS_UNSUCCESSFUL
288 NTSTATUS WINAPI SystemFunction007(const UNICODE_STRING *string, LPBYTE hash)
290 MD4_CTX ctx;
292 MD4Init( &ctx );
293 MD4Update( &ctx, (const BYTE *)string->Buffer, string->Length );
294 MD4Final( &ctx );
295 memcpy( hash, ctx.digest, 0x10 );
297 return STATUS_SUCCESS;
300 /******************************************************************************
301 * SystemFunction010 [ADVAPI32.@]
302 * SystemFunction011 [ADVAPI32.@]
304 * MD4 hashes 16 bytes of data
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)
311 * RETURNS
312 * Success: STATUS_SUCCESS
313 * Failure: STATUS_UNSUCCESSFUL
316 NTSTATUS WINAPI SystemFunction010(LPVOID unknown, const BYTE *data, LPBYTE hash)
318 MD4_CTX ctx;
320 MD4Init( &ctx );
321 MD4Update( &ctx, data, 0x10 );
322 MD4Final( &ctx );
323 memcpy( hash, ctx.digest, 0x10 );
325 return STATUS_SUCCESS;