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/md4.h"
35 #include "openssl/md5.h"
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;
46 #define CSUM_MD4_ARCHAIC 1
47 #define CSUM_MD4_BUSTED 2
48 #define CSUM_MD4_OLD 3
53 struct name_num_obj valid_checksums = {
54 "checksum", NULL, NULL, 0, 0, {
56 { CSUM_XXH64, "xxh64", NULL },
57 { CSUM_XXH64, "xxhash", NULL },
59 { CSUM_MD5, "md5", NULL },
60 { CSUM_MD4, "md4", NULL },
61 { CSUM_NONE, "none", NULL },
67 #define MD5_CTX md_context
68 #define MD5_Init md5_begin
69 #define MD5_Update md5_update
70 #define MD5_Final(digest, cptr) md5_result(cptr, digest)
73 int xfersum_type = 0; /* used for the file transfer checksums */
74 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
76 static int parse_csum_name(const char *name, int len)
78 struct name_num_item *nni;
83 if (!name || (len == 4 && strncasecmp(name, "auto", 4) == 0)) {
84 if (protocol_version >= 30)
86 if (protocol_version >= 27)
88 if (protocol_version >= 21)
89 return CSUM_MD4_BUSTED;
90 return CSUM_MD4_ARCHAIC;
93 nni = get_nni_by_name(&valid_checksums, name, len);
96 rprintf(FERROR, "unknown checksum name: %s\n", name);
97 exit_cleanup(RERR_UNSUPPORTED);
103 static const char *checksum_name(int num)
105 struct name_num_item *nni = get_nni_by_num(&valid_checksums, num);
107 return nni ? nni->name : num < CSUM_MD4 ? "MD4" : "UNKNOWN";
110 void parse_checksum_choice(int final_call)
112 if (valid_checksums.negotiated_name)
113 xfersum_type = checksum_type = valid_checksums.negotiated_num;
115 char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
117 xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice);
118 checksum_type = parse_csum_name(cp+1, -1);
120 xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1);
123 if (xfersum_type == CSUM_NONE)
126 /* Snag the checksum name for both write_batch's option output & the following debug output. */
127 if (valid_checksums.negotiated_name)
128 checksum_choice = valid_checksums.negotiated_name;
129 else if (checksum_choice == NULL)
130 checksum_choice = checksum_name(xfersum_type);
132 if (final_call && DEBUG_GTE(NSTR, am_server ? 3 : 1)) {
133 rprintf(FINFO, "%s%s checksum: %s\n",
134 am_server ? "Server" : "Client",
135 valid_checksums.negotiated_name ? " negotiated" : "",
140 int csum_len_for_type(int cst, BOOL flist_csum)
145 case CSUM_MD4_ARCHAIC:
146 /* The oldest checksum code is rather weird: the file-list code only sent
147 * 2-byte checksums, but all other checksums were full MD4 length. */
148 return flist_csum ? 2 : MD4_DIGEST_LEN;
151 case CSUM_MD4_BUSTED:
152 return MD4_DIGEST_LEN;
154 return MD5_DIGEST_LEN;
155 #ifdef SUPPORT_XXHASH
159 default: /* paranoia to prevent missing case values */
160 exit_cleanup(RERR_UNSUPPORTED);
165 /* Returns 0 if the checksum is not canonical (i.e. it includes a seed value).
166 * Returns 1 if the public sum order matches our internal sum order.
167 * Returns -1 if the public sum order is the reverse of our internal sum order.
169 int canonical_checksum(int csum_type)
173 case CSUM_MD4_ARCHAIC:
175 case CSUM_MD4_BUSTED:
180 #ifdef SUPPORT_XXHASH
184 default: /* paranoia to prevent missing case values */
185 exit_cleanup(RERR_UNSUPPORTED);
190 #ifndef HAVE_SIMD /* See simd-checksum-*.cpp. */
192 a simple 32 bit checksum that can be updated from either end
193 (inspired by Mark Adler's Adler-32 checksum)
195 uint32 get_checksum1(char *buf1, int32 len)
199 schar *buf = (schar *)buf1;
202 for (i = 0; i < (len-4); i+=4) {
203 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
204 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
206 for (; i < len; i++) {
207 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
209 return (s1 & 0xffff) + (s2 << 16);
213 void get_checksum2(char *buf, int32 len, char *sum)
215 switch (xfersum_type) {
220 if (proper_seed_order) {
222 SIVALu(seedbuf, 0, checksum_seed);
223 MD5_Update(&m5, seedbuf, 4);
225 MD5_Update(&m5, (uchar *)buf, len);
227 MD5_Update(&m5, (uchar *)buf, len);
229 SIVALu(seedbuf, 0, checksum_seed);
230 MD5_Update(&m5, seedbuf, 4);
233 MD5_Final((uchar *)sum, &m5);
241 MD4_Update(&m4, (uchar *)buf, len);
244 SIVALu(seedbuf, 0, checksum_seed);
245 MD4_Update(&m4, seedbuf, 4);
247 MD4_Final((uchar *)sum, &m4);
252 case CSUM_MD4_BUSTED:
253 case CSUM_MD4_ARCHAIC: {
264 buf1 = new_array(char, len+4);
267 out_of_memory("get_checksum2");
270 memcpy(buf1, buf, len);
272 SIVAL(buf1,len,checksum_seed);
276 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
277 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
280 * Prior to version 27 an incorrect MD4 checksum was computed
281 * by failing to call mdfour_tail() for block sizes that
282 * are multiples of 64. This is fixed by calling mdfour_update()
283 * even when there are no more bytes.
285 if (len - i > 0 || xfersum_type > CSUM_MD4_BUSTED)
286 mdfour_update(&m, (uchar *)(buf1+i), len-i);
288 mdfour_result(&m, (uchar *)sum);
291 #ifdef SUPPORT_XXHASH
293 SIVAL64(sum, 0, XXH64(buf, len, checksum_seed));
296 default: /* paranoia to prevent missing case values */
297 exit_cleanup(RERR_UNSUPPORTED);
301 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
303 struct map_struct *buf;
304 OFF_T i, len = st_p->st_size;
308 memset(sum, 0, MAX_DIGEST_LEN);
310 fd = do_open(fname, O_RDONLY, 0);
314 buf = map_file(fd, len, MAX_MAP_SIZE, CHUNK_SIZE);
316 switch (checksum_type) {
322 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
323 MD5_Update(&m5, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
325 remainder = (int32)(len - i);
327 MD5_Update(&m5, (uchar *)map_ptr(buf, i, remainder), remainder);
329 MD5_Final((uchar *)sum, &m5);
339 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
340 MD4_Update(&m4, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
342 remainder = (int32)(len - i);
344 MD4_Update(&m4, (uchar *)map_ptr(buf, i, remainder), remainder);
346 MD4_Final((uchar *)sum, &m4);
351 case CSUM_MD4_BUSTED:
352 case CSUM_MD4_ARCHAIC: {
357 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
358 mdfour_update(&m, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
360 /* Prior to version 27 an incorrect MD4 checksum was computed
361 * by failing to call mdfour_tail() for block sizes that
362 * are multiples of 64. This is fixed by calling mdfour_update()
363 * even when there are no more bytes. */
364 remainder = (int32)(len - i);
365 if (remainder > 0 || checksum_type > CSUM_MD4_BUSTED)
366 mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
368 mdfour_result(&m, (uchar *)sum);
371 #ifdef SUPPORT_XXHASH
373 XXH64_state_t* state = XXH64_createState();
375 out_of_memory("file_checksum XXH64");
377 if (XXH64_reset(state, 0) == XXH_ERROR) {
378 rprintf(FERROR, "error resetting XXH64 seed");
379 exit_cleanup(RERR_STREAMIO);
382 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE) {
383 XXH_errorcode const updateResult =
384 XXH64_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
385 if (updateResult == XXH_ERROR) {
386 rprintf(FERROR, "error computing XXH64 hash");
387 exit_cleanup(RERR_STREAMIO);
391 remainder = (int32)(len - i);
393 XXH64_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
394 SIVAL64(sum, 0, XXH64_digest(state));
396 XXH64_freeState(state);
401 rprintf(FERROR, "Invalid checksum-choice for --checksum: %s (%d)\n",
402 checksum_name(checksum_type), checksum_type);
403 exit_cleanup(RERR_UNSUPPORTED);
410 static int32 sumresidue;
418 #ifdef SUPPORT_XXHASH
419 static XXH64_state_t* xxh64_state;
421 static int cursum_type;
423 void sum_init(int csum_type, int seed)
428 csum_type = parse_csum_name(NULL, 0);
429 cursum_type = csum_type;
439 mdfour_begin(&ctx.md);
444 case CSUM_MD4_BUSTED:
445 case CSUM_MD4_ARCHAIC:
446 mdfour_begin(&ctx.md);
451 #ifdef SUPPORT_XXHASH
453 if (xxh64_state == NULL) {
454 xxh64_state = XXH64_createState();
455 if (xxh64_state == NULL)
456 out_of_memory("sum_init xxh64");
458 if (XXH64_reset(xxh64_state, 0) == XXH_ERROR) {
459 rprintf(FERROR, "error resetting XXH64 state");
460 exit_cleanup(RERR_STREAMIO);
466 default: /* paranoia to prevent missing case values */
467 exit_cleanup(RERR_UNSUPPORTED);
472 * Feed data into an MD4 accumulator, md. The results may be
473 * retrieved using sum_end(). md is used for different purposes at
474 * different points during execution.
476 * @todo Perhaps get rid of md and just pass in the address each time.
477 * Very slightly clearer and slower.
479 void sum_update(const char *p, int32 len)
481 switch (cursum_type) {
483 MD5_Update(&ctx.m5, (uchar *)p, len);
487 MD4_Update(&ctx.m4, (uchar *)p, len);
491 case CSUM_MD4_BUSTED:
492 case CSUM_MD4_ARCHAIC:
493 if (len + sumresidue < CSUM_CHUNK) {
494 memcpy(ctx.md.buffer + sumresidue, p, len);
500 int32 i = CSUM_CHUNK - sumresidue;
501 memcpy(ctx.md.buffer + sumresidue, p, i);
502 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, CSUM_CHUNK);
507 while (len >= CSUM_CHUNK) {
508 mdfour_update(&ctx.md, (uchar *)p, CSUM_CHUNK);
515 memcpy(ctx.md.buffer, p, sumresidue);
517 #ifdef SUPPORT_XXHASH
519 if (XXH64_update(xxh64_state, p, len) == XXH_ERROR) {
520 rprintf(FERROR, "error computing XXH64 hash");
521 exit_cleanup(RERR_STREAMIO);
527 default: /* paranoia to prevent missing case values */
528 exit_cleanup(RERR_UNSUPPORTED);
532 /* NOTE: all the callers of sum_end() pass in a pointer to a buffer that is
533 * MAX_DIGEST_LEN in size, so even if the csum-len is shorter that that (i.e.
534 * CSUM_MD4_ARCHAIC), we don't have to worry about limiting the data we write
535 * into the "sum" buffer. */
536 int sum_end(char *sum)
538 switch (cursum_type) {
540 MD5_Final((uchar *)sum, &ctx.m5);
544 MD4_Final((uchar *)sum, &ctx.m4);
548 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
549 mdfour_result(&ctx.md, (uchar *)sum);
551 case CSUM_MD4_BUSTED:
552 case CSUM_MD4_ARCHAIC:
554 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
555 mdfour_result(&ctx.md, (uchar *)sum);
557 #ifdef SUPPORT_XXHASH
559 SIVAL64(sum, 0, XXH64_digest(xxh64_state));
565 default: /* paranoia to prevent missing case values */
566 exit_cleanup(RERR_UNSUPPORTED);
569 return csum_len_for_type(cursum_type, 0);