2 * Routines to support checksumming of bytes.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2022 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.
33 # if XXH_VERSION_NUMBER >= 800
34 # define SUPPORT_XXH3 1
39 extern int whole_file;
40 extern int checksum_seed;
41 extern int protocol_version;
42 extern int proper_seed_order;
43 extern const char *checksum_choice;
45 struct name_num_obj valid_checksums = {
46 "checksum", NULL, NULL, 0, 0, {
48 { CSUM_XXH3_128, "xxh128", NULL },
49 { CSUM_XXH3_64, "xxh3", NULL },
52 { CSUM_XXH64, "xxh64", NULL },
53 { CSUM_XXH64, "xxhash", NULL },
55 { CSUM_MD5, "md5", NULL },
56 { CSUM_MD4, "md4", NULL },
57 { CSUM_NONE, "none", NULL },
62 int xfersum_type = 0; /* used for the file transfer checksums */
63 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
65 int parse_csum_name(const char *name, int len)
67 struct name_num_item *nni;
72 if (!name || (len == 4 && strncasecmp(name, "auto", 4) == 0)) {
73 if (protocol_version >= 30)
75 if (protocol_version >= 27)
77 if (protocol_version >= 21)
78 return CSUM_MD4_BUSTED;
79 return CSUM_MD4_ARCHAIC;
82 nni = get_nni_by_name(&valid_checksums, name, len);
85 rprintf(FERROR, "unknown checksum name: %s\n", name);
86 exit_cleanup(RERR_UNSUPPORTED);
92 static const char *checksum_name(int num)
94 struct name_num_item *nni = get_nni_by_num(&valid_checksums, num);
96 return nni ? nni->name : num < CSUM_MD4 ? "md4" : "UNKNOWN";
99 void parse_checksum_choice(int final_call)
101 if (valid_checksums.negotiated_name)
102 xfersum_type = checksum_type = valid_checksums.negotiated_num;
104 char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
106 xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice);
107 checksum_type = parse_csum_name(cp+1, -1);
109 xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1);
110 if (am_server && checksum_choice)
111 validate_choice_vs_env(NSTR_CHECKSUM, xfersum_type, checksum_type);
114 if (xfersum_type == CSUM_NONE)
117 /* Snag the checksum name for both write_batch's option output & the following debug output. */
118 if (valid_checksums.negotiated_name)
119 checksum_choice = valid_checksums.negotiated_name;
120 else if (checksum_choice == NULL)
121 checksum_choice = checksum_name(xfersum_type);
123 if (final_call && DEBUG_GTE(NSTR, am_server ? 3 : 1)) {
124 rprintf(FINFO, "%s%s checksum: %s\n",
125 am_server ? "Server" : "Client",
126 valid_checksums.negotiated_name ? " negotiated" : "",
131 int csum_len_for_type(int cst, BOOL flist_csum)
136 case CSUM_MD4_ARCHAIC:
137 /* The oldest checksum code is rather weird: the file-list code only sent
138 * 2-byte checksums, but all other checksums were full MD4 length. */
139 return flist_csum ? 2 : MD4_DIGEST_LEN;
142 case CSUM_MD4_BUSTED:
143 return MD4_DIGEST_LEN;
145 return MD5_DIGEST_LEN;
151 default: /* paranoia to prevent missing case values */
152 exit_cleanup(RERR_UNSUPPORTED);
157 /* Returns 0 if the checksum is not canonical (i.e. it includes a seed value).
158 * Returns 1 if the public sum order matches our internal sum order.
159 * Returns -1 if the public sum order is the reverse of our internal sum order.
161 int canonical_checksum(int csum_type)
165 case CSUM_MD4_ARCHAIC:
167 case CSUM_MD4_BUSTED:
176 default: /* paranoia to prevent missing case values */
177 exit_cleanup(RERR_UNSUPPORTED);
182 #ifndef USE_ROLL_SIMD /* See simd-checksum-*.cpp. */
184 a simple 32 bit checksum that can be updated from either end
185 (inspired by Mark Adler's Adler-32 checksum)
187 uint32 get_checksum1(char *buf1, int32 len)
191 schar *buf = (schar *)buf1;
194 for (i = 0; i < (len-4); i+=4) {
195 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
196 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
198 for (; i < len; i++) {
199 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
201 return (s1 & 0xffff) + (s2 << 16);
205 void get_checksum2(char *buf, int32 len, char *sum)
207 switch (xfersum_type) {
208 #ifdef SUPPORT_XXHASH
210 SIVAL64(sum, 0, XXH64(buf, len, checksum_seed));
215 SIVAL64(sum, 0, XXH3_64bits_withSeed(buf, len, checksum_seed));
217 case CSUM_XXH3_128: {
218 XXH128_hash_t digest = XXH3_128bits_withSeed(buf, len, checksum_seed);
219 SIVAL64(sum, 0, digest.low64);
220 SIVAL64(sum, 8, digest.high64);
228 if (proper_seed_order) {
230 SIVALu(seedbuf, 0, checksum_seed);
231 md5_update(&m5, seedbuf, 4);
233 md5_update(&m5, (uchar *)buf, len);
235 md5_update(&m5, (uchar *)buf, len);
237 SIVALu(seedbuf, 0, checksum_seed);
238 md5_update(&m5, seedbuf, 4);
241 md5_result(&m5, (uchar *)sum);
249 MD4_Update(&m4, (uchar *)buf, len);
252 SIVALu(seedbuf, 0, checksum_seed);
253 MD4_Update(&m4, seedbuf, 4);
255 MD4_Final((uchar *)sum, &m4);
260 case CSUM_MD4_BUSTED:
261 case CSUM_MD4_ARCHAIC: {
272 buf1 = new_array(char, len+4);
276 memcpy(buf1, buf, len);
278 SIVAL(buf1,len,checksum_seed);
282 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
283 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
286 * Prior to version 27 an incorrect MD4 checksum was computed
287 * by failing to call mdfour_tail() for block sizes that
288 * are multiples of 64. This is fixed by calling mdfour_update()
289 * even when there are no more bytes.
291 if (len - i > 0 || xfersum_type > CSUM_MD4_BUSTED)
292 mdfour_update(&m, (uchar *)(buf1+i), len-i);
294 mdfour_result(&m, (uchar *)sum);
297 default: /* paranoia to prevent missing case values */
298 exit_cleanup(RERR_UNSUPPORTED);
302 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
304 struct map_struct *buf;
305 OFF_T i, len = st_p->st_size;
309 memset(sum, 0, MAX_DIGEST_LEN);
311 fd = do_open(fname, O_RDONLY, 0);
315 buf = map_file(fd, len, MAX_MAP_SIZE, CHUNK_SIZE);
317 switch (checksum_type) {
318 #ifdef SUPPORT_XXHASH
320 static XXH64_state_t* state = NULL;
321 if (!state && !(state = XXH64_createState()))
322 out_of_memory("file_checksum");
324 XXH64_reset(state, 0);
326 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
327 XXH64_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
329 remainder = (int32)(len - i);
331 XXH64_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
333 SIVAL64(sum, 0, XXH64_digest(state));
339 static XXH3_state_t* state = NULL;
340 if (!state && !(state = XXH3_createState()))
341 out_of_memory("file_checksum");
343 XXH3_64bits_reset(state);
345 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
346 XXH3_64bits_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
348 remainder = (int32)(len - i);
350 XXH3_64bits_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
352 SIVAL64(sum, 0, XXH3_64bits_digest(state));
355 case CSUM_XXH3_128: {
356 XXH128_hash_t digest;
357 static XXH3_state_t* state = NULL;
358 if (!state && !(state = XXH3_createState()))
359 out_of_memory("file_checksum");
361 XXH3_128bits_reset(state);
363 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
364 XXH3_128bits_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
366 remainder = (int32)(len - i);
368 XXH3_128bits_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
370 digest = XXH3_128bits_digest(state);
371 SIVAL64(sum, 0, digest.low64);
372 SIVAL64(sum, 8, digest.high64);
381 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
382 md5_update(&m5, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
384 remainder = (int32)(len - i);
386 md5_update(&m5, (uchar *)map_ptr(buf, i, remainder), remainder);
388 md5_result(&m5, (uchar *)sum);
398 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
399 MD4_Update(&m4, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
401 remainder = (int32)(len - i);
403 MD4_Update(&m4, (uchar *)map_ptr(buf, i, remainder), remainder);
405 MD4_Final((uchar *)sum, &m4);
410 case CSUM_MD4_BUSTED:
411 case CSUM_MD4_ARCHAIC: {
416 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
417 mdfour_update(&m, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
419 /* Prior to version 27 an incorrect MD4 checksum was computed
420 * by failing to call mdfour_tail() for block sizes that
421 * are multiples of 64. This is fixed by calling mdfour_update()
422 * even when there are no more bytes. */
423 remainder = (int32)(len - i);
424 if (remainder > 0 || checksum_type > CSUM_MD4_BUSTED)
425 mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
427 mdfour_result(&m, (uchar *)sum);
431 rprintf(FERROR, "Invalid checksum-choice for --checksum: %s (%d)\n",
432 checksum_name(checksum_type), checksum_type);
433 exit_cleanup(RERR_UNSUPPORTED);
440 static int32 sumresidue;
448 #ifdef SUPPORT_XXHASH
449 static XXH64_state_t* xxh64_state;
452 static XXH3_state_t* xxh3_state;
454 static int cursum_type;
456 void sum_init(int csum_type, int seed)
461 csum_type = parse_csum_name(NULL, 0);
462 cursum_type = csum_type;
465 #ifdef SUPPORT_XXHASH
467 if (!xxh64_state && !(xxh64_state = XXH64_createState()))
468 out_of_memory("sum_init");
469 XXH64_reset(xxh64_state, 0);
474 if (!xxh3_state && !(xxh3_state = XXH3_createState()))
475 out_of_memory("sum_init");
476 XXH3_64bits_reset(xxh3_state);
479 if (!xxh3_state && !(xxh3_state = XXH3_createState()))
480 out_of_memory("sum_init");
481 XXH3_128bits_reset(xxh3_state);
491 mdfour_begin(&ctx.md);
496 case CSUM_MD4_BUSTED:
497 case CSUM_MD4_ARCHAIC:
498 mdfour_begin(&ctx.md);
505 default: /* paranoia to prevent missing case values */
506 exit_cleanup(RERR_UNSUPPORTED);
511 * Feed data into an MD4 accumulator, md. The results may be
512 * retrieved using sum_end(). md is used for different purposes at
513 * different points during execution.
515 * @todo Perhaps get rid of md and just pass in the address each time.
516 * Very slightly clearer and slower.
518 void sum_update(const char *p, int32 len)
520 switch (cursum_type) {
521 #ifdef SUPPORT_XXHASH
523 XXH64_update(xxh64_state, p, len);
528 XXH3_64bits_update(xxh3_state, p, len);
531 XXH3_128bits_update(xxh3_state, p, len);
535 md5_update(&ctx.m5, (uchar *)p, len);
539 MD4_Update(&ctx.m4, (uchar *)p, len);
543 case CSUM_MD4_BUSTED:
544 case CSUM_MD4_ARCHAIC:
545 if (len + sumresidue < CSUM_CHUNK) {
546 memcpy(ctx.md.buffer + sumresidue, p, len);
552 int32 i = CSUM_CHUNK - sumresidue;
553 memcpy(ctx.md.buffer + sumresidue, p, i);
554 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, CSUM_CHUNK);
559 while (len >= CSUM_CHUNK) {
560 mdfour_update(&ctx.md, (uchar *)p, CSUM_CHUNK);
567 memcpy(ctx.md.buffer, p, sumresidue);
571 default: /* paranoia to prevent missing case values */
572 exit_cleanup(RERR_UNSUPPORTED);
576 /* NOTE: all the callers of sum_end() pass in a pointer to a buffer that is
577 * MAX_DIGEST_LEN in size, so even if the csum-len is shorter than that (i.e.
578 * CSUM_MD4_ARCHAIC), we don't have to worry about limiting the data we write
579 * into the "sum" buffer. */
580 int sum_end(char *sum)
582 switch (cursum_type) {
583 #ifdef SUPPORT_XXHASH
585 SIVAL64(sum, 0, XXH64_digest(xxh64_state));
590 SIVAL64(sum, 0, XXH3_64bits_digest(xxh3_state));
592 case CSUM_XXH3_128: {
593 XXH128_hash_t digest = XXH3_128bits_digest(xxh3_state);
594 SIVAL64(sum, 0, digest.low64);
595 SIVAL64(sum, 8, digest.high64);
600 md5_result(&ctx.m5, (uchar *)sum);
604 MD4_Final((uchar *)sum, &ctx.m4);
608 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
609 mdfour_result(&ctx.md, (uchar *)sum);
611 case CSUM_MD4_BUSTED:
612 case CSUM_MD4_ARCHAIC:
614 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
615 mdfour_result(&ctx.md, (uchar *)sum);
620 default: /* paranoia to prevent missing case values */
621 exit_cleanup(RERR_UNSUPPORTED);
624 return csum_len_for_type(cursum_type, 0);