1 /* Functions to compute MD4 message digest of files or memory blocks.
2 according to the definition of MD4 in RFC 1320 from April 1992.
3 Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
4 2008, 2009, 2010 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Adapted by Simon Josefsson from gnulib md5.? and Libgcrypt
31 #include <sys/types.h>
34 # include "unlocked-io.h"
37 #ifdef WORDS_BIGENDIAN
39 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
44 #define BLOCKSIZE 32768
45 #if BLOCKSIZE % 64 != 0
46 # error "invalid BLOCKSIZE"
49 /* This array contains the bytes used to pad the buffer to the next
50 64-byte boundary. (RFC 1320, 3.1: Step 1) */
51 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
54 /* Initialize structure containing state of computation.
55 (RFC 1320, 3.3: Step 3) */
57 md4_init_ctx (struct md4_ctx *ctx)
64 ctx->total[0] = ctx->total[1] = 0;
68 /* Copy the 4 byte value from v into the memory location pointed to by *cp,
69 If your architecture allows unaligned access this is equivalent to
70 * (uint32_t *) cp = v */
72 set_uint32 (char *cp, uint32_t v)
74 memcpy (cp, &v, sizeof v);
77 /* Put result from CTX in first 16 bytes following RESBUF. The result
78 must be in little endian byte order. */
80 md4_read_ctx (const struct md4_ctx *ctx, void *resbuf)
83 set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
84 set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
85 set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
86 set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
91 /* Process the remaining bytes in the internal buffer and the usual
92 prolog according to the standard and write the result to RESBUF. */
94 md4_finish_ctx (struct md4_ctx *ctx, void *resbuf)
96 /* Take yet unprocessed bytes into account. */
97 uint32_t bytes = ctx->buflen;
100 /* Now count remaining bytes. */
101 ctx->total[0] += bytes;
102 if (ctx->total[0] < bytes)
105 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
106 memcpy (&((char*)ctx->buffer)[bytes], fillbuf, pad);
108 /* Put the 64-bit file length in *bits* at the end of the buffer. */
109 ctx->buffer[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
110 ctx->buffer[(bytes + pad) / 4 + 1] = SWAP ((ctx->total[1] << 3) |
111 (ctx->total[0] >> 29));
113 /* Process last bytes. */
114 md4_process_block (ctx->buffer, bytes + pad + 8, ctx);
116 return md4_read_ctx (ctx, resbuf);
119 /* Compute MD4 message digest for bytes read from STREAM. The
120 resulting message digest number will be written into the 16 bytes
121 beginning at RESBLOCK. */
123 md4_stream (FILE * stream, void *resblock)
128 char *buffer = malloc (BLOCKSIZE + 72);
132 /* Initialize the computation context. */
135 /* Iterate over full file contents. */
138 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
139 computation function processes the whole buffer so that with the
140 next round of the loop another block can be read. */
144 /* Read block. Take care for partial reads. */
147 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
151 if (sum == BLOCKSIZE)
156 /* Check for the error flag IFF N == 0, so that we don't
157 exit the loop after a partial read due to e.g., EAGAIN
164 goto process_partial_block;
167 /* We've read at least one byte, so ignore errors. But always
168 check for EOF, since feof may be true even though N > 0.
169 Otherwise, we could end up calling fread after EOF. */
171 goto process_partial_block;
174 /* Process buffer with BLOCKSIZE bytes. Note that
177 md4_process_block (buffer, BLOCKSIZE, &ctx);
180 process_partial_block:;
182 /* Process any remaining bytes. */
184 md4_process_bytes (buffer, sum, &ctx);
186 /* Construct result in desired memory. */
187 md4_finish_ctx (&ctx, resblock);
192 /* Compute MD4 message digest for LEN bytes beginning at BUFFER. The
193 result is always in little endian byte order, so that a byte-wise
194 output yields to the wanted ASCII representation of the message
197 md4_buffer (const char *buffer, size_t len, void *resblock)
201 /* Initialize the computation context. */
204 /* Process whole buffer but last len % 64 bytes. */
205 md4_process_bytes (buffer, len, &ctx);
207 /* Put result in desired memory area. */
208 return md4_finish_ctx (&ctx, resblock);
212 md4_process_bytes (const void *buffer, size_t len, struct md4_ctx *ctx)
214 /* When we already have some bits in our internal buffer concatenate
215 both inputs first. */
216 if (ctx->buflen != 0)
218 size_t left_over = ctx->buflen;
219 size_t add = 128 - left_over > len ? len : 128 - left_over;
221 memcpy (&((char*)ctx->buffer)[left_over], buffer, add);
224 if (ctx->buflen > 64)
226 md4_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
229 /* The regions in the following copy operation cannot overlap. */
230 memcpy (ctx->buffer, &((char*)ctx->buffer)[(left_over + add) & ~63],
234 buffer = (const char *) buffer + add;
238 /* Process available complete blocks. */
241 #if !_STRING_ARCH_unaligned
242 /* To check alignment gcc has an appropriate operator. Other
245 # define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
247 # define alignof(type) offsetof (struct { char c; type x; }, x)
248 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
250 if (UNALIGNED_P (buffer))
253 md4_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
254 buffer = (const char *) buffer + 64;
260 md4_process_block (buffer, len & ~63, ctx);
261 buffer = (const char *) buffer + (len & ~63);
266 /* Move remaining bytes in internal buffer. */
269 size_t left_over = ctx->buflen;
271 memcpy (&((char*)ctx->buffer)[left_over], buffer, len);
275 md4_process_block (ctx->buffer, 64, ctx);
277 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
279 ctx->buflen = left_over;
283 /* --- Code below is the primary difference between md5.c and md4.c --- */
285 /* MD4 round constants */
286 #define K1 0x5a827999
287 #define K2 0x6ed9eba1
289 /* Round functions. */
290 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
291 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
292 #define H(x, y, z) ((x) ^ (y) ^ (z))
293 #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
294 #define R1(a,b,c,d,k,s) a=rol(a+F(b,c,d)+x[k],s);
295 #define R2(a,b,c,d,k,s) a=rol(a+G(b,c,d)+x[k]+K1,s);
296 #define R3(a,b,c,d,k,s) a=rol(a+H(b,c,d)+x[k]+K2,s);
298 /* Process LEN bytes of BUFFER, accumulating context into CTX.
299 It is assumed that LEN % 64 == 0. */
302 md4_process_block (const void *buffer, size_t len, struct md4_ctx *ctx)
304 const uint32_t *words = buffer;
305 size_t nwords = len / sizeof (uint32_t);
306 const uint32_t *endp = words + nwords;
313 /* First increment the byte count. RFC 1320 specifies the possible
314 length of the file up to 2^64 bits. Here we only compute the
315 number of bytes. Do a double word increment. */
316 ctx->total[0] += len;
317 if (ctx->total[0] < len)
320 /* Process all bytes in the buffer with 64 bytes in each round of
325 for (t = 0; t < 16; t++)
327 x[t] = SWAP (*words);
332 R1 (A, B, C, D, 0, 3);
333 R1 (D, A, B, C, 1, 7);
334 R1 (C, D, A, B, 2, 11);
335 R1 (B, C, D, A, 3, 19);
336 R1 (A, B, C, D, 4, 3);
337 R1 (D, A, B, C, 5, 7);
338 R1 (C, D, A, B, 6, 11);
339 R1 (B, C, D, A, 7, 19);
340 R1 (A, B, C, D, 8, 3);
341 R1 (D, A, B, C, 9, 7);
342 R1 (C, D, A, B, 10, 11);
343 R1 (B, C, D, A, 11, 19);
344 R1 (A, B, C, D, 12, 3);
345 R1 (D, A, B, C, 13, 7);
346 R1 (C, D, A, B, 14, 11);
347 R1 (B, C, D, A, 15, 19);
350 R2 (A, B, C, D, 0, 3);
351 R2 (D, A, B, C, 4, 5);
352 R2 (C, D, A, B, 8, 9);
353 R2 (B, C, D, A, 12, 13);
354 R2 (A, B, C, D, 1, 3);
355 R2 (D, A, B, C, 5, 5);
356 R2 (C, D, A, B, 9, 9);
357 R2 (B, C, D, A, 13, 13);
358 R2 (A, B, C, D, 2, 3);
359 R2 (D, A, B, C, 6, 5);
360 R2 (C, D, A, B, 10, 9);
361 R2 (B, C, D, A, 14, 13);
362 R2 (A, B, C, D, 3, 3);
363 R2 (D, A, B, C, 7, 5);
364 R2 (C, D, A, B, 11, 9);
365 R2 (B, C, D, A, 15, 13);
368 R3 (A, B, C, D, 0, 3);
369 R3 (D, A, B, C, 8, 9);
370 R3 (C, D, A, B, 4, 11);
371 R3 (B, C, D, A, 12, 15);
372 R3 (A, B, C, D, 2, 3);
373 R3 (D, A, B, C, 10, 9);
374 R3 (C, D, A, B, 6, 11);
375 R3 (B, C, D, A, 14, 15);
376 R3 (A, B, C, D, 1, 3);
377 R3 (D, A, B, C, 9, 9);
378 R3 (C, D, A, B, 5, 11);
379 R3 (B, C, D, A, 13, 15);
380 R3 (A, B, C, D, 3, 3);
381 R3 (D, A, B, C, 11, 9);
382 R3 (C, D, A, B, 7, 11);
383 R3 (B, C, D, A, 15, 15);