2 * Routines to support checksumming of bytes.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2020 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 * In addition, as a special exception, the copyright holders give
14 * permission to dynamically link rsync with the OpenSSL and xxhash
15 * libraries when those libraries are being distributed in compliance
16 * with their license terms, and to distribute a dynamically linked
17 * combination of rsync and these libraries. This is also considered
18 * to be covered under the GPL's System Libraries exception.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, visit the http://fsf.org website.
34 #include "openssl/md5.h"
38 extern int local_server;
39 extern int whole_file;
40 extern int read_batch;
41 extern int checksum_seed;
42 extern int protocol_version;
43 extern int proper_seed_order;
44 extern char *checksum_choice;
47 #define CSUM_MD4_ARCHAIC 1
48 #define CSUM_MD4_BUSTED 2
49 #define CSUM_MD4_OLD 3
54 #define CSUM_SAW_BUFLEN 10
59 } valid_checksums[] = {
61 { CSUM_XXHASH, "xxhash" },
65 { CSUM_NONE, "none" },
69 #define MAX_CHECKSUM_LIST 1024
72 #define MD5_CTX md_context
73 #define MD5_Init md5_begin
74 #define MD5_Update md5_update
75 #define MD5_Final(digest, cptr) md5_result(cptr, digest)
78 int xfersum_type = 0; /* used for the file transfer checksums */
79 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
80 const char *negotiated_csum_name = NULL;
82 static int parse_csum_name(const char *name, int len, int allow_auto)
84 struct csum_struct *cs;
89 if (!name || (allow_auto && len == 4 && strncasecmp(name, "auto", 4) == 0)) {
90 if (protocol_version >= 30)
92 if (protocol_version >= 27)
94 if (protocol_version >= 21)
95 return CSUM_MD4_BUSTED;
96 return CSUM_MD4_ARCHAIC;
99 for (cs = valid_checksums; cs->name; cs++) {
100 if (strncasecmp(name, cs->name, len) == 0 && cs->name[len] == '\0')
105 rprintf(FERROR, "unknown checksum name: %s\n", name);
106 exit_cleanup(RERR_UNSUPPORTED);
112 static const char *checksum_name(int num)
114 struct csum_struct *cs;
116 for (cs = valid_checksums; cs->name; cs++) {
127 void parse_checksum_choice(int final_call)
129 if (!negotiated_csum_name) {
130 char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
132 xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice, 1);
133 checksum_type = parse_csum_name(cp+1, -1, 1);
135 xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1, 1);
138 if (xfersum_type == CSUM_NONE)
141 if (final_call && DEBUG_GTE(CSUM, 1)) {
142 if (negotiated_csum_name)
143 rprintf(FINFO, "[%s] negotiated checksum: %s\n", who_am_i(), negotiated_csum_name);
144 else if (xfersum_type == checksum_type) {
145 rprintf(FINFO, "[%s] %s checksum: %s\n", who_am_i(),
146 checksum_choice ? "chosen" : "protocol-based",
147 checksum_name(xfersum_type));
149 rprintf(FINFO, "[%s] chosen transfer checksum: %s\n",
150 who_am_i(), checksum_name(xfersum_type));
151 rprintf(FINFO, "[%s] chosen pre-transfer checksum: %s\n",
152 who_am_i(), checksum_name(checksum_type));
157 static int parse_checksum_list(const char *from, char *sumbuf, int sumbuf_len, char *saw)
159 char *to = sumbuf, *tok = NULL;
162 memset(saw, 0, CSUM_SAW_BUFLEN);
165 if (*from == ' ' || !*from) {
167 int sum_type = parse_csum_name(tok, to - tok, 0);
168 if (sum_type >= 0 && !saw[sum_type])
169 saw[sum_type] = ++cnt;
171 to = tok - (tok != sumbuf);
183 if (to - sumbuf >= sumbuf_len - 1) {
184 to = tok - (tok != sumbuf);
194 void negotiate_checksum(int f_in, int f_out, const char *csum_list, int saw_fail)
196 char *tok, sumbuf[MAX_CHECKSUM_LIST], saw[CSUM_SAW_BUFLEN];
199 /* Simplify the user-provided string so that it contains valid
200 * checksum names without any duplicates. The client side also
201 * makes use of the saw values when scanning the server's list. */
202 if (csum_list && *csum_list && (!am_server || local_server)) {
203 len = parse_checksum_list(csum_list, sumbuf, sizeof sumbuf, saw);
204 if (saw_fail && !len)
205 len = strlcpy(sumbuf, "FAIL", sizeof sumbuf);
208 memset(saw, 0, CSUM_SAW_BUFLEN);
212 if (!csum_list || !*csum_list) {
213 struct csum_struct *cs;
215 for (cs = valid_checksums, len = 0; cs->name; cs++) {
216 if (cs->num == CSUM_NONE)
220 len += strlcpy(sumbuf+len, cs->name, sizeof sumbuf - len);
221 if (len >= (int)sizeof sumbuf - 1)
222 exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE... */
223 saw[cs->num] = ++cnt;
227 /* Each side sends their list of valid checksum names to the other side and
228 * then both sides pick the first name in the client's list that is also in
229 * the server's list. */
231 write_vstring(f_out, sumbuf, len);
233 if (!local_server || read_batch)
234 len = read_vstring(f_in, sumbuf, sizeof sumbuf);
237 int best = CSUM_SAW_BUFLEN; /* We want best == 1 from the client list */
239 memset(saw, 1, CSUM_SAW_BUFLEN); /* The first client's choice is the best choice */
240 for (tok = strtok(sumbuf, " \t"); tok; tok = strtok(NULL, " \t")) {
241 sum_type = parse_csum_name(tok, -1, 0);
242 if (sum_type < 0 || !saw[sum_type] || best < saw[sum_type])
244 xfersum_type = checksum_type = sum_type;
245 negotiated_csum_name = tok;
246 best = saw[sum_type];
250 if (negotiated_csum_name) {
251 negotiated_csum_name = strdup(negotiated_csum_name);
258 rprintf(FERROR, "Failed to negotiate a common checksum\n");
259 exit_cleanup(RERR_UNSUPPORTED);
262 int csum_len_for_type(int cst, BOOL flist_csum)
267 case CSUM_MD4_ARCHAIC:
268 /* The oldest checksum code is rather weird: the file-list code only sent
269 * 2-byte checksums, but all other checksums were full MD4 length. */
270 return flist_csum ? 2 : MD4_DIGEST_LEN;
273 case CSUM_MD4_BUSTED:
274 return MD4_DIGEST_LEN;
276 return MD5_DIGEST_LEN;
277 #ifdef SUPPORT_XXHASH
279 return sizeof (XXH64_hash_t);
281 default: /* paranoia to prevent missing case values */
282 exit_cleanup(RERR_UNSUPPORTED);
287 int canonical_checksum(int csum_type)
289 return csum_type >= CSUM_MD4 ? 1 : 0;
292 #ifndef HAVE_SIMD /* See simd-checksum-*.cpp. */
294 a simple 32 bit checksum that can be updated from either end
295 (inspired by Mark Adler's Adler-32 checksum)
297 uint32 get_checksum1(char *buf1, int32 len)
301 schar *buf = (schar *)buf1;
304 for (i = 0; i < (len-4); i+=4) {
305 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
306 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
308 for (; i < len; i++) {
309 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
311 return (s1 & 0xffff) + (s2 << 16);
315 void get_checksum2(char *buf, int32 len, char *sum)
320 switch (xfersum_type) {
324 if (proper_seed_order) {
326 SIVALu(seedbuf, 0, checksum_seed);
327 MD5_Update(&m5, seedbuf, 4);
329 MD5_Update(&m5, (uchar *)buf, len);
331 MD5_Update(&m5, (uchar *)buf, len);
333 SIVALu(seedbuf, 0, checksum_seed);
334 MD5_Update(&m5, seedbuf, 4);
337 MD5_Final((uchar *)sum, &m5);
342 case CSUM_MD4_BUSTED:
343 case CSUM_MD4_ARCHAIC: {
353 buf1 = new_array(char, len+4);
356 out_of_memory("get_checksum2");
359 memcpy(buf1, buf, len);
361 SIVAL(buf1,len,checksum_seed);
365 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
366 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
369 * Prior to version 27 an incorrect MD4 checksum was computed
370 * by failing to call mdfour_tail() for block sizes that
371 * are multiples of 64. This is fixed by calling mdfour_update()
372 * even when there are no more bytes.
374 if (len - i > 0 || xfersum_type > CSUM_MD4_BUSTED)
375 mdfour_update(&m, (uchar *)(buf1+i), len-i);
377 mdfour_result(&m, (uchar *)sum);
380 #ifdef SUPPORT_XXHASH
382 SIVAL64(sum, 0, XXH64(buf, len, checksum_seed));
385 default: /* paranoia to prevent missing case values */
386 exit_cleanup(RERR_UNSUPPORTED);
390 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
392 struct map_struct *buf;
393 OFF_T i, len = st_p->st_size;
399 memset(sum, 0, MAX_DIGEST_LEN);
401 fd = do_open(fname, O_RDONLY, 0);
405 buf = map_file(fd, len, MAX_MAP_SIZE, CSUM_CHUNK);
407 switch (checksum_type) {
411 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
412 MD5_Update(&m5, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
416 remainder = (int32)(len - i);
418 MD5_Update(&m5, (uchar *)map_ptr(buf, i, remainder), remainder);
420 MD5_Final((uchar *)sum, &m5);
424 case CSUM_MD4_BUSTED:
425 case CSUM_MD4_ARCHAIC:
428 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
429 mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
432 /* Prior to version 27 an incorrect MD4 checksum was computed
433 * by failing to call mdfour_tail() for block sizes that
434 * are multiples of 64. This is fixed by calling mdfour_update()
435 * even when there are no more bytes. */
436 remainder = (int32)(len - i);
437 if (remainder > 0 || checksum_type > CSUM_MD4_BUSTED)
438 mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
440 mdfour_result(&m, (uchar *)sum);
442 #ifdef SUPPORT_XXHASH
444 XXH64_state_t* state = XXH64_createState();
446 out_of_memory("file_checksum xx64");
448 if (XXH64_reset(state, 0) == XXH_ERROR) {
449 rprintf(FERROR, "error resetting XXH64 seed");
450 exit_cleanup(RERR_STREAMIO);
453 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
454 XXH_errorcode const updateResult =
455 XXH64_update(state, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
456 if (updateResult == XXH_ERROR) {
457 rprintf(FERROR, "error computing XX64 hash");
458 exit_cleanup(RERR_STREAMIO);
461 remainder = (int32)(len - i);
463 XXH64_update(state, (uchar *)map_ptr(buf, i, CSUM_CHUNK), remainder);
464 SIVAL64(sum, 0, XXH64_digest(state));
466 XXH64_freeState(state);
471 rprintf(FERROR, "invalid checksum-choice for the --checksum option (%d)\n", checksum_type);
472 exit_cleanup(RERR_UNSUPPORTED);
479 static int32 sumresidue;
480 static md_context md;
482 static int cursum_type;
483 #ifdef SUPPORT_XXHASH
484 XXH64_state_t* xxh64_state = NULL;
487 void sum_init(int csum_type, int seed)
492 csum_type = parse_csum_name(NULL, 0, 1);
493 cursum_type = csum_type;
504 case CSUM_MD4_BUSTED:
505 case CSUM_MD4_ARCHAIC:
511 #ifdef SUPPORT_XXHASH
513 if (xxh64_state == NULL) {
514 xxh64_state = XXH64_createState();
515 if (xxh64_state == NULL)
516 out_of_memory("sum_init xxh64");
518 if (XXH64_reset(xxh64_state, 0) == XXH_ERROR) {
519 rprintf(FERROR, "error resetting XXH64 state");
520 exit_cleanup(RERR_STREAMIO);
526 default: /* paranoia to prevent missing case values */
527 exit_cleanup(RERR_UNSUPPORTED);
532 * Feed data into an MD4 accumulator, md. The results may be
533 * retrieved using sum_end(). md is used for different purposes at
534 * different points during execution.
536 * @todo Perhaps get rid of md and just pass in the address each time.
537 * Very slightly clearer and slower.
539 void sum_update(const char *p, int32 len)
541 switch (cursum_type) {
543 MD5_Update(&m5, (uchar *)p, len);
547 case CSUM_MD4_BUSTED:
548 case CSUM_MD4_ARCHAIC:
549 if (len + sumresidue < CSUM_CHUNK) {
550 memcpy(md.buffer + sumresidue, p, len);
556 int32 i = CSUM_CHUNK - sumresidue;
557 memcpy(md.buffer + sumresidue, p, i);
558 mdfour_update(&md, (uchar *)md.buffer, CSUM_CHUNK);
563 while (len >= CSUM_CHUNK) {
564 mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
571 memcpy(md.buffer, p, sumresidue);
573 #ifdef SUPPORT_XXHASH
575 if (XXH64_update(xxh64_state, p, len) == XXH_ERROR) {
576 rprintf(FERROR, "error computing XX64 hash");
577 exit_cleanup(RERR_STREAMIO);
583 default: /* paranoia to prevent missing case values */
584 exit_cleanup(RERR_UNSUPPORTED);
588 /* NOTE: all the callers of sum_end() pass in a pointer to a buffer that is
589 * MAX_DIGEST_LEN in size, so even if the csum-len is shorter that that (i.e.
590 * CSUM_MD4_ARCHAIC), we don't have to worry about limiting the data we write
591 * into the "sum" buffer. */
592 int sum_end(char *sum)
594 switch (cursum_type) {
596 MD5_Final((uchar *)sum, &m5);
600 mdfour_update(&md, (uchar *)md.buffer, sumresidue);
601 mdfour_result(&md, (uchar *)sum);
603 case CSUM_MD4_BUSTED:
604 case CSUM_MD4_ARCHAIC:
606 mdfour_update(&md, (uchar *)md.buffer, sumresidue);
607 mdfour_result(&md, (uchar *)sum);
609 #ifdef SUPPORT_XXHASH
611 SIVAL64(sum, 0, XXH64_digest(xxh64_state));
617 default: /* paranoia to prevent missing case values */
618 exit_cleanup(RERR_UNSUPPORTED);
621 return csum_len_for_type(cursum_type, 0);