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