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