librsync  2.0.1
mdfour.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- the library for network deltas
4  *
5  * Copyright (C) 2000, 2001 by Martin Pool <[email protected]>
6  * Copyright (C) 1997-1999 by Andrew Tridgell
7  * Copyright (C) 2002, 2003 by Donovan Baarda <[email protected]>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 /* MD4 message digest algorithm.
25  *
26  * TODO: Perhaps use the MD4 routine from OpenSSL if it's installed.
27  * It's probably not worth the trouble.
28  *
29  * This was originally written by Andrew Tridgell for use in Samba.
30  * It was then modified by;
31  *
32  * 2002-06-xx: Robert Weber <[email protected]>
33  * optimisations and fixed >512M support.
34  *
35  * 2002-06-27: Donovan Baarda <[email protected]>
36  * further optimisations and cleanups.
37  *
38  * 2004-09-09: Simon Law <[email protected]>
39  * handle little-endian machines that can't do unaligned access
40  * (e.g. ia64, pa-risc).
41  */
42 
43 #include "config.h"
44 
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48 
49 #include "librsync.h"
50 #include "trace.h"
51 #include "types.h"
52 #include "mdfour.h"
53 
54 
55 #define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z)))
56 #define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
57 #define H(X,Y,Z) ((X)^(Y)^(Z))
58 #define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
59 
60 #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
61 #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)
62 #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)
63 
64 /** padding data used for finalising */
65 static unsigned char PADDING[64] = {
66  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
69 };
70 
71 
72 static void
73 rs_mdfour_block(rs_mdfour_t *md, void const *p);
74 
75 
76 
77 /**
78  * Update an MD4 accumulator from a 64-byte chunk.
79  *
80  * This cannot be used for the last chunk of the file, which must be
81  * padded and contain the file length. rs_mdfour_tail() is used for
82  * that.
83  *
84  * \todo Recode to be fast, and to use system integer types. Perhaps
85  * if we can find an mdfour implementation already on the system
86  * (e.g. in OpenSSL) then we should use it instead of our own?
87  *
88  * \param X A series of integer, read little-endian from the file.
89  */
90 static void
91 rs_mdfour64(rs_mdfour_t * m, const void *p)
92 {
93  uint32_t AA, BB, CC, DD;
94  uint32_t A, B, C, D;
95  const uint32_t *X = (const uint32_t *) p;
96 
97  A = m->A;
98  B = m->B;
99  C = m->C;
100  D = m->D;
101  AA = A;
102  BB = B;
103  CC = C;
104  DD = D;
105 
106  ROUND1(A, B, C, D, 0, 3);
107  ROUND1(D, A, B, C, 1, 7);
108  ROUND1(C, D, A, B, 2, 11);
109  ROUND1(B, C, D, A, 3, 19);
110  ROUND1(A, B, C, D, 4, 3);
111  ROUND1(D, A, B, C, 5, 7);
112  ROUND1(C, D, A, B, 6, 11);
113  ROUND1(B, C, D, A, 7, 19);
114  ROUND1(A, B, C, D, 8, 3);
115  ROUND1(D, A, B, C, 9, 7);
116  ROUND1(C, D, A, B, 10, 11);
117  ROUND1(B, C, D, A, 11, 19);
118  ROUND1(A, B, C, D, 12, 3);
119  ROUND1(D, A, B, C, 13, 7);
120  ROUND1(C, D, A, B, 14, 11);
121  ROUND1(B, C, D, A, 15, 19);
122 
123  ROUND2(A, B, C, D, 0, 3);
124  ROUND2(D, A, B, C, 4, 5);
125  ROUND2(C, D, A, B, 8, 9);
126  ROUND2(B, C, D, A, 12, 13);
127  ROUND2(A, B, C, D, 1, 3);
128  ROUND2(D, A, B, C, 5, 5);
129  ROUND2(C, D, A, B, 9, 9);
130  ROUND2(B, C, D, A, 13, 13);
131  ROUND2(A, B, C, D, 2, 3);
132  ROUND2(D, A, B, C, 6, 5);
133  ROUND2(C, D, A, B, 10, 9);
134  ROUND2(B, C, D, A, 14, 13);
135  ROUND2(A, B, C, D, 3, 3);
136  ROUND2(D, A, B, C, 7, 5);
137  ROUND2(C, D, A, B, 11, 9);
138  ROUND2(B, C, D, A, 15, 13);
139 
140  ROUND3(A, B, C, D, 0, 3);
141  ROUND3(D, A, B, C, 8, 9);
142  ROUND3(C, D, A, B, 4, 11);
143  ROUND3(B, C, D, A, 12, 15);
144  ROUND3(A, B, C, D, 2, 3);
145  ROUND3(D, A, B, C, 10, 9);
146  ROUND3(C, D, A, B, 6, 11);
147  ROUND3(B, C, D, A, 14, 15);
148  ROUND3(A, B, C, D, 1, 3);
149  ROUND3(D, A, B, C, 9, 9);
150  ROUND3(C, D, A, B, 5, 11);
151  ROUND3(B, C, D, A, 13, 15);
152  ROUND3(A, B, C, D, 3, 3);
153  ROUND3(D, A, B, C, 11, 9);
154  ROUND3(C, D, A, B, 7, 11);
155  ROUND3(B, C, D, A, 15, 15);
156 
157  A += AA;
158  B += BB;
159  C += CC;
160  D += DD;
161 
162  m->A = A;
163  m->B = B;
164  m->C = C;
165  m->D = D;
166 }
167 
168 
169 /**
170  * These next routines are necessary because MD4 is specified in terms of
171  * little-endian int32s, but we have a byte buffer. On little-endian
172  * platforms, I think we can just use the buffer pointer directly.
173  *
174  * There are some nice endianness routines in glib, including assembler
175  * variants. If we ever depended on glib, then it could be good to use them
176  * instead. */
177 inline static void
178 copy4( /* @out@ */ unsigned char *out, uint32_t const x)
179 {
180  out[0] = x;
181  out[1] = x >> 8;
182  out[2] = x >> 16;
183  out[3] = x >> 24;
184 }
185 
186 
187 /* We need this if there is a uint64 */
188 /* [email protected] */
189 #ifdef HAVE_UINT64
190 inline static void
191 copy8( /* @out@ */ unsigned char *out, uint64_t const x)
192 {
193  out[0] = x;
194  out[1] = x >> 8;
195  out[2] = x >> 16;
196  out[3] = x >> 24;
197  out[4] = x >> 32;
198  out[5] = x >> 40;
199  out[6] = x >> 48;
200  out[7] = x >> 56;
201 }
202 #endif /* HAVE_UINT64 */
203 
204 
205 /* We only need this if we are big-endian */
206 #ifdef WORDS_BIGENDIAN
207 inline static void
208 copy64( /* @out@ */ uint32_t * M, unsigned char const *in)
209 {
210  int i=16;
211 
212  while (i--) {
213  *M++ = (in[3] << 24) | (in[2] << 16) | (in[1] << 8) | in[0];
214  in += 4;
215  }
216 }
217 
218 
219 /**
220  * Accumulate a block, making appropriate conversions for bigendian
221  * machines.
222  */
223 inline static void
224 rs_mdfour_block(rs_mdfour_t *md, void const *p)
225 {
226  uint32_t M[16];
227 
228  copy64(M, p);
229  rs_mdfour64(md, M);
230 }
231 
232 
233 #else /* WORDS_BIGENDIAN */
234 
235 # ifdef __i386__
236 
237 /* If we are on an IA-32 machine, we can process directly. */
238 inline static void
239 rs_mdfour_block(rs_mdfour_t *md, void const *p)
240 {
241  rs_mdfour64(md,p);
242 }
243 
244 # else /* !WORDS_BIGENDIAN && !__i386__ */
245 
246 /* We are little-endian, but not on i386 and therefore may not be able
247  * to do unaligned access safely/quickly.
248  *
249  * So if the input is not already aligned correctly, copy it to an
250  * aligned buffer first. */
251 inline static void
252 rs_mdfour_block(rs_mdfour_t *md, void const *p)
253 {
254  unsigned long ptrval = (unsigned long) p;
255 
256  if (ptrval & 3) {
257  uint32_t M[16];
258 
259  memcpy(M, p, 16 * sizeof(uint32_t));
260  rs_mdfour64(md, M);
261  } else {
262  rs_mdfour64(md, (const uint32_t *) p);
263  }
264 }
265 
266 # endif /* ! __i386__ */
267 #endif /* WORDS_BIGENDIAN */
268 
269 
270 void
271 rs_mdfour_begin(rs_mdfour_t * md)
272 {
273  memset(md, 0, sizeof(*md));
274  md->A = 0x67452301;
275  md->B = 0xefcdab89;
276  md->C = 0x98badcfe;
277  md->D = 0x10325476;
278 #if HAVE_UINT64
279  md->totalN = 0;
280 #else
281  md->totalN_hi = md->totalN_lo = 0;
282 #endif
283 }
284 
285 
286 /**
287  * Handle special behaviour for processing the last block of a file
288  * when calculating its MD4 checksum.
289  *
290  * This must be called exactly once per file.
291  *
292  * Modified by Robert Weber to use uint64 in order that we can sum files
293  * > 2^29 = 512 MB.
294  * [email protected]
295  */
296 static void
297 rs_mdfour_tail(rs_mdfour_t * m)
298 {
299 #ifdef HAVE_UINT64
300  uint64_t b;
301 #else /* HAVE_UINT64 */
302  uint32_t b[2];
303 #endif /* HAVE_UINT64 */
304  unsigned char buf[8];
305  size_t pad_len;
306 
307  /* convert the totalN byte count into a bit count buffer */
308 #ifdef HAVE_UINT64
309  b = m->totalN << 3;
310  copy8(buf, b);
311 #else /* HAVE_UINT64 */
312  b[0] = m->totalN_lo << 3;
313  b[1] = ((m->totalN_hi << 3) | (m->totalN_lo >> 29));
314  copy4(buf, b[0]);
315  copy4(buf + 4, b[1]);
316 #endif /* HAVE_UINT64 */
317 
318  /* calculate length and process the padding data */
319  pad_len=(m->tail_len <56) ? (56 - m->tail_len) : (120 - m->tail_len);
320  rs_mdfour_update(m,PADDING,pad_len);
321  /* process the bit count */
322  rs_mdfour_update(m,buf,8);
323 }
324 
325 
326 void
327 rs_mdfour_update(rs_mdfour_t * md, void const *in_void, size_t n)
328 {
329  unsigned char const *in = (unsigned char const *) in_void;
330 
331  /* increment totalN */
332 #ifdef HAVE_UINT64
333  md->totalN+=n;
334 #else /* HAVE_UINT64 */
335  if ((md->totalN_lo += n) < n)
336  md->totalN_hi++;
337 #endif /* HAVE_UINT64 */
338 
339  /* If there's any leftover data in the tail buffer, then first we have
340  * to make it up to a whole block to process it. */
341  if (md->tail_len) {
342  size_t tail_gap = 64 - md->tail_len;
343  if (tail_gap <= n) {
344  memcpy(&md->tail[md->tail_len], in, tail_gap);
345  rs_mdfour_block(md, md->tail);
346  in += tail_gap;
347  n -= tail_gap;
348  md->tail_len = 0;
349  }
350  }
351  /* process complete blocks of input */
352  while (n >= 64) {
353  rs_mdfour_block(md, in);
354  in += 64;
355  n -= 64;
356  }
357  /* Put remaining bytes onto tail*/
358  if (n) {
359  memcpy(&md->tail[md->tail_len], in, n);
360  md->tail_len += n;
361  }
362 }
363 
364 
365 void
366 rs_mdfour_result(rs_mdfour_t * md, unsigned char *out)
367 {
368  rs_mdfour_tail(md);
369 
370  copy4(out, md->A);
371  copy4(out + 4, md->B);
372  copy4(out + 8, md->C);
373  copy4(out + 12, md->D);
374 }
375 
376 
377 void
378 rs_mdfour(unsigned char *out, void const *in, size_t n)
379 {
380  rs_mdfour_t md;
381 
382  rs_mdfour_begin(&md);
383  rs_mdfour_update(&md, in, n);
384  rs_mdfour_result(&md, out);
385 }
Public header for librsync.
void rs_mdfour_update(rs_mdfour_t *md, void const *in_void, size_t n)
Feed some data into the MD4 accumulator.
Definition: mdfour.c:327
struct rs_mdfour rs_mdfour_t
MD4 message-digest accumulator.
Definition: librsync.h:266