2 * Routines to support checksumming of bytes.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2015 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
24 extern int checksum_seed;
25 extern int protocol_version;
26 extern int proper_seed_order;
27 extern char *checksum_choice;
30 #define CSUM_ARCHAIC 1
31 #define CSUM_MD4_BUSTED 2
32 #define CSUM_MD4_OLD 3
36 int xfersum_type = 0; /* used for the file transfer checksums */
37 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
39 /* Returns 1 if --whole-file must be enabled. */
40 int parse_checksum_choice(void)
42 char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
44 xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice);
45 checksum_type = parse_csum_name(cp+1, -1);
47 xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1);
48 return xfersum_type == CSUM_NONE;
51 int parse_csum_name(const char *name, int len)
56 if (!name || (len == 4 && strncasecmp(name, "auto", 4) == 0)) {
57 if (protocol_version >= 30)
59 if (protocol_version >= 27)
61 if (protocol_version >= 21)
62 return CSUM_MD4_BUSTED;
65 if (len == 3 && strncasecmp(name, "md4", 3) == 0)
67 if (len == 3 && strncasecmp(name, "md5", 3) == 0)
69 if (len == 4 && strncasecmp(name, "none", 4) == 0)
72 rprintf(FERROR, "unknown checksum name: %s\n", name);
73 exit_cleanup(RERR_UNSUPPORTED);
76 int csum_len_for_type(int cst)
86 return MD4_DIGEST_LEN;
88 return MD5_DIGEST_LEN;
93 int canonical_checksum(int csum_type)
95 return csum_type >= CSUM_MD4 ? 1 : 0;
99 a simple 32 bit checksum that can be upadted from either end
100 (inspired by Mark Adler's Adler-32 checksum)
102 uint32 get_checksum1(char *buf1, int32 len)
106 schar *buf = (schar *)buf1;
109 for (i = 0; i < (len-4); i+=4) {
110 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] +
112 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
114 for (; i < len; i++) {
115 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
117 return (s1 & 0xffff) + (s2 << 16);
120 void get_checksum2(char *buf, int32 len, char *sum)
124 switch (xfersum_type) {
128 if (proper_seed_order) {
130 SIVALu(seedbuf, 0, checksum_seed);
131 md5_update(&m, seedbuf, 4);
133 md5_update(&m, (uchar *)buf, len);
135 md5_update(&m, (uchar *)buf, len);
137 SIVALu(seedbuf, 0, checksum_seed);
138 md5_update(&m, seedbuf, 4);
141 md5_result(&m, (uchar *)sum);
146 case CSUM_MD4_BUSTED: {
156 buf1 = new_array(char, len+4);
159 out_of_memory("get_checksum2");
162 memcpy(buf1, buf, len);
164 SIVAL(buf1,len,checksum_seed);
168 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
169 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
172 * Prior to version 27 an incorrect MD4 checksum was computed
173 * by failing to call mdfour_tail() for block sizes that
174 * are multiples of 64. This is fixed by calling mdfour_update()
175 * even when there are no more bytes.
177 if (len - i > 0 || xfersum_type != CSUM_MD4_BUSTED)
178 mdfour_update(&m, (uchar *)(buf1+i), len-i);
180 mdfour_result(&m, (uchar *)sum);
186 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
188 struct map_struct *buf;
189 OFF_T i, len = st_p->st_size;
194 memset(sum, 0, MAX_DIGEST_LEN);
196 fd = do_open(fname, O_RDONLY, 0);
200 buf = map_file(fd, len, MAX_MAP_SIZE, CSUM_CHUNK);
202 switch (checksum_type) {
206 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
207 md5_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
211 remainder = (int32)(len - i);
213 md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
215 md5_result(&m, (uchar *)sum);
219 case CSUM_MD4_BUSTED:
222 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
223 mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
227 /* Prior to version 27 an incorrect MD4 checksum was computed
228 * by failing to call mdfour_tail() for block sizes that
229 * are multiples of 64. This is fixed by calling mdfour_update()
230 * even when there are no more bytes. */
231 remainder = (int32)(len - i);
232 if (remainder > 0 || checksum_type != CSUM_MD4_BUSTED)
233 mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
235 mdfour_result(&m, (uchar *)sum);
238 rprintf(FERROR, "invalid checksum-choice for the --checksum option (%d)\n", checksum_type);
239 exit_cleanup(RERR_UNSUPPORTED);
246 static int32 sumresidue;
247 static md_context md;
248 static int cursum_type;
250 void sum_init(int csum_type, int seed)
255 csum_type = parse_csum_name(NULL, 0);
256 cursum_type = csum_type;
267 case CSUM_MD4_BUSTED:
279 * Feed data into an MD4 accumulator, md. The results may be
280 * retrieved using sum_end(). md is used for different purposes at
281 * different points during execution.
283 * @todo Perhaps get rid of md and just pass in the address each time.
284 * Very slightly clearer and slower.
286 void sum_update(const char *p, int32 len)
288 switch (cursum_type) {
290 md5_update(&md, (uchar *)p, len);
294 case CSUM_MD4_BUSTED:
295 if (len + sumresidue < CSUM_CHUNK) {
296 memcpy(md.buffer + sumresidue, p, len);
302 int32 i = CSUM_CHUNK - sumresidue;
303 memcpy(md.buffer + sumresidue, p, i);
304 mdfour_update(&md, (uchar *)md.buffer, CSUM_CHUNK);
309 while (len >= CSUM_CHUNK) {
310 mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
317 memcpy(md.buffer, p, sumresidue);
324 int sum_end(char *sum)
326 switch (cursum_type) {
328 md5_result(&md, (uchar *)sum);
332 mdfour_update(&md, (uchar *)md.buffer, sumresidue);
333 mdfour_result(&md, (uchar *)sum);
335 case CSUM_MD4_BUSTED:
337 mdfour_update(&md, (uchar *)md.buffer, sumresidue);
338 mdfour_result(&md, (uchar *)sum);
345 return csum_len_for_type(cursum_type);