Debian Codegraph @

checksum.c

raw symbols SCIP
1 /*
2 * Routines to support checksumming of bytes.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2023 Wayne Davison
7 *
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.
12 *
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.
19 *
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.
24 *
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.
27 */
28
29 #include "rsync.h"
30
31 #ifdef SUPPORT_XXHASH
32 #include <xxhash.h>
33 # if XXH_VERSION_NUMBER >= 800
34 # define SUPPORT_XXH3 1
35 # endif
36 #endif
37
38 extern int am_server;
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;
44
45 #define NNI_BUILTIN (1<<0)
46 #define NNI_EVP (1<<1)
47 #define NNI_EVP_OK (1<<2)
48
49 struct name_num_item valid_checksums_items[] = {
50 #ifdef SUPPORT_XXH3
51 { CSUM_XXH3_128, 0, "xxh128", NULL },
52 { CSUM_XXH3_64, 0, "xxh3", NULL },
53 #endif
54 #ifdef SUPPORT_XXHASH
55 { CSUM_XXH64, 0, "xxh64", NULL },
56 { CSUM_XXH64, 0, "xxhash", NULL },
57 #endif
58 { CSUM_MD5, NNI_BUILTIN|NNI_EVP, "md5", NULL },
59 { CSUM_MD4, NNI_BUILTIN|NNI_EVP, "md4", NULL },
60 #ifdef SHA_DIGEST_LENGTH
61 { CSUM_SHA1, NNI_EVP, "sha1", NULL },
62 #endif
63 { CSUM_NONE, 0, "none", NULL },
64 { 0, 0, NULL, NULL }
65 };
66
67 struct name_num_obj valid_checksums = {
68 "checksum", NULL, 0, 0, valid_checksums_items
69 };
70
71 struct name_num_item valid_auth_checksums_items[] = {
72 #ifdef SHA512_DIGEST_LENGTH
73 { CSUM_SHA512, NNI_EVP, "sha512", NULL },
74 #endif
75 #ifdef SHA256_DIGEST_LENGTH
76 { CSUM_SHA256, NNI_EVP, "sha256", NULL },
77 #endif
78 #ifdef SHA_DIGEST_LENGTH
79 { CSUM_SHA1, NNI_EVP, "sha1", NULL },
80 #endif
81 { CSUM_MD5, NNI_BUILTIN|NNI_EVP, "md5", NULL },
82 { CSUM_MD4, NNI_BUILTIN|NNI_EVP, "md4", NULL },
83 { 0, 0, NULL, NULL }
84 };
85
86 struct name_num_obj valid_auth_checksums = {
87 "daemon auth checksum", NULL, 0, 0, valid_auth_checksums_items
88 };
89
90 /* Return the strength rank (0 = strongest) of a daemon-auth digest by name in
91 * valid_auth_checksums_items[], which is listed strongest-first; -1 if the name
92 * is not a supported auth digest on this build. Used by the daemon's
93 * "auth digest" floor to compare the negotiated digest against the minimum. */
94 int auth_digest_rank(const char *name)
95 {
96 struct name_num_item *nni;
97 int rank = 0;
98
99 if (!name || !*name)
100 return -1;
101 for (nni = valid_auth_checksums_items; nni->name; nni++, rank++) {
102 if (strcasecmp(nni->name, name) == 0)
103 return rank;
104 }
105 return -1;
106 }
107
108 /* These cannot make use of openssl, so they're marked just as built-in */
109 struct name_num_item implied_checksum_md4 =
110 { CSUM_MD4, NNI_BUILTIN, "md4", NULL };
111 struct name_num_item implied_checksum_md5 =
112 { CSUM_MD5, NNI_BUILTIN, "md5", NULL };
113
114 struct name_num_item *xfer_sum_nni; /* used for the transfer checksum2 computations */
115 int xfer_sum_len;
116 struct name_num_item *file_sum_nni; /* used for the pre-transfer --checksum computations */
117 int file_sum_len, file_sum_extra_cnt;
118
119 #ifdef USE_OPENSSL
120 const EVP_MD *xfer_sum_evp_md;
121 const EVP_MD *file_sum_evp_md;
122 EVP_MD_CTX *ctx_evp = NULL;
123 #endif
124
125 static int initialized_choices = 0;
126
127 struct name_num_item *parse_csum_name(const char *name, int len)
128 {
129 struct name_num_item *nni;
130
131 if (len < 0 && name)
132 len = strlen(name);
133
134 init_checksum_choices();
135
136 if (!name || (len == 4 && strncasecmp(name, "auto", 4) == 0)) {
137 if (protocol_version >= 30) {
138 if (!proper_seed_order)
139 return &implied_checksum_md5;
140 name = "md5";
141 len = 3;
142 } else {
143 if (protocol_version >= 27)
144 implied_checksum_md4.num = CSUM_MD4_OLD;
145 else if (protocol_version >= 21)
146 implied_checksum_md4.num = CSUM_MD4_BUSTED;
147 else
148 implied_checksum_md4.num = CSUM_MD4_ARCHAIC;
149 return &implied_checksum_md4;
150 }
151 }
152
153 nni = get_nni_by_name(&valid_checksums, name, len);
154
155 if (!nni) {
156 rprintf(FERROR, "unknown checksum name: %s\n", name);
157 exit_cleanup(RERR_UNSUPPORTED);
158 }
159
160 return nni;
161 }
162
163 #ifdef USE_OPENSSL
164 static const EVP_MD *csum_evp_md(struct name_num_item *nni)
165 {
166 const EVP_MD *emd;
167 if (!(nni->flags & NNI_EVP))
168 return NULL;
169
170 #ifdef USE_MD5_ASM
171 if (nni->num == CSUM_MD5)
172 emd = NULL;
173 else
174 #endif
175 emd = EVP_get_digestbyname(nni->name);
176 if (emd && !(nni->flags & NNI_EVP_OK)) { /* Make sure it works before we advertise it */
177 if (!ctx_evp && !(ctx_evp = EVP_MD_CTX_create()))
178 out_of_memory("csum_evp_md");
179 /* Some routines are marked as legacy and are not enabled in the openssl.cnf file.
180 * If we can't init the emd, we'll fall back to our built-in code. */
181 if (EVP_DigestInit_ex(ctx_evp, emd, NULL) == 0)
182 emd = NULL;
183 else
184 nni->flags = (nni->flags & ~NNI_BUILTIN) | NNI_EVP_OK;
185 }
186 if (!emd)
187 nni->flags &= ~NNI_EVP;
188 return emd;
189 }
190 #endif
191
192 void parse_checksum_choice(int final_call)
193 {
194 if (valid_checksums.negotiated_nni)
195 xfer_sum_nni = file_sum_nni = valid_checksums.negotiated_nni;
196 else {
197 const char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
198 if (cp) {
199 xfer_sum_nni = parse_csum_name(checksum_choice, cp - checksum_choice);
200 file_sum_nni = parse_csum_name(cp+1, -1);
201 } else
202 xfer_sum_nni = file_sum_nni = parse_csum_name(checksum_choice, -1);
203 if (am_server && checksum_choice)
204 validate_choice_vs_env(NSTR_CHECKSUM, xfer_sum_nni->num, file_sum_nni->num);
205 }
206 xfer_sum_len = csum_len_for_type(xfer_sum_nni->num, 0);
207 file_sum_len = csum_len_for_type(file_sum_nni->num, 0);
208 #ifdef USE_OPENSSL
209 xfer_sum_evp_md = csum_evp_md(xfer_sum_nni);
210 file_sum_evp_md = csum_evp_md(file_sum_nni);
211 #endif
212
213 file_sum_extra_cnt = (file_sum_len + EXTRA_LEN - 1) / EXTRA_LEN;
214
215 if (xfer_sum_nni->num == CSUM_NONE)
216 whole_file = 1;
217
218 /* Snag the checksum name for both write_batch's option output & the following debug output. */
219 if (valid_checksums.negotiated_nni)
220 checksum_choice = valid_checksums.negotiated_nni->name;
221 else if (checksum_choice == NULL)
222 checksum_choice = xfer_sum_nni->name;
223
224 if (final_call && DEBUG_GTE(NSTR, am_server ? 3 : 1)) {
225 rprintf(FINFO, "%s%s checksum: %s\n",
226 am_server ? "Server" : "Client",
227 valid_checksums.negotiated_nni ? " negotiated" : "",
228 checksum_choice);
229 }
230 }
231
232 int csum_len_for_type(int cst, BOOL flist_csum)
233 {
234 switch (cst) {
235 case CSUM_NONE:
236 return 1;
237 case CSUM_MD4_ARCHAIC:
238 /* The oldest checksum code is rather weird: the file-list code only sent
239 * 2-byte checksums, but all other checksums were full MD4 length. */
240 return flist_csum ? 2 : MD4_DIGEST_LEN;
241 case CSUM_MD4:
242 case CSUM_MD4_OLD:
243 case CSUM_MD4_BUSTED:
244 return MD4_DIGEST_LEN;
245 case CSUM_MD5:
246 return MD5_DIGEST_LEN;
247 #ifdef SHA_DIGEST_LENGTH
248 case CSUM_SHA1:
249 return SHA_DIGEST_LENGTH;
250 #endif
251 #ifdef SHA256_DIGEST_LENGTH
252 case CSUM_SHA256:
253 return SHA256_DIGEST_LENGTH;
254 #endif
255 #ifdef SHA512_DIGEST_LENGTH
256 case CSUM_SHA512:
257 return SHA512_DIGEST_LENGTH;
258 #endif
259 case CSUM_XXH64:
260 case CSUM_XXH3_64:
261 return 64/8;
262 case CSUM_XXH3_128:
263 return 128/8;
264 default: /* paranoia to prevent missing case values */
265 exit_cleanup(RERR_UNSUPPORTED);
266 }
267 return 0;
268 }
269
270 /* Returns 0 if the checksum is not canonical (i.e. it includes a seed value).
271 * Returns 1 if the public sum order matches our internal sum order.
272 * Returns -1 if the public sum order is the reverse of our internal sum order.
273 */
274 int canonical_checksum(int csum_type)
275 {
276 switch (csum_type) {
277 case CSUM_NONE:
278 case CSUM_MD4_ARCHAIC:
279 case CSUM_MD4_OLD:
280 case CSUM_MD4_BUSTED:
281 break;
282 case CSUM_MD4:
283 case CSUM_MD5:
284 case CSUM_SHA1:
285 case CSUM_SHA256:
286 case CSUM_SHA512:
287 return -1;
288 case CSUM_XXH64:
289 case CSUM_XXH3_64:
290 case CSUM_XXH3_128:
291 return 1;
292 default: /* paranoia to prevent missing case values */
293 exit_cleanup(RERR_UNSUPPORTED);
294 }
295 return 0;
296 }
297
298 #ifndef USE_ROLL_SIMD /* See simd-checksum-*.cpp. */
299 /*
300 a simple 32 bit checksum that can be updated from either end
301 (inspired by Mark Adler's Adler-32 checksum)
302 */
303 uint32 get_checksum1(char *buf1, int32 len)
304 {
305 int32 i;
306 uint32 s1, s2;
307 schar *buf = (schar *)buf1;
308
309 s1 = s2 = 0;
310 for (i = 0; i < (len-4); i+=4) {
311 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
312 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
313 }
314 for (; i < len; i++) {
315 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
316 }
317 return (s1 & 0xffff) + (s2 << 16);
318 }
319 #endif
320
321 /* The "sum" buffer must be at least MAX_DIGEST_LEN bytes! */
322 void get_checksum2(char *buf, int32 len, char *sum)
323 {
324 #ifdef USE_OPENSSL
325 if (xfer_sum_evp_md) {
326 static EVP_MD_CTX *evp = NULL;
327 uchar seedbuf[4];
328 if (!evp && !(evp = EVP_MD_CTX_create()))
329 out_of_memory("get_checksum2");
330 EVP_DigestInit_ex(evp, xfer_sum_evp_md, NULL);
331 if (checksum_seed) {
332 SIVALu(seedbuf, 0, checksum_seed);
333 EVP_DigestUpdate(evp, seedbuf, 4);
334 }
335 EVP_DigestUpdate(evp, (uchar *)buf, len);
336 EVP_DigestFinal_ex(evp, (uchar *)sum, NULL);
337 } else
338 #endif
339 switch (xfer_sum_nni->num) {
340 #ifdef SUPPORT_XXHASH
341 case CSUM_XXH64:
342 SIVAL64(sum, 0, XXH64(buf, len, checksum_seed));
343 break;
344 #endif
345 #ifdef SUPPORT_XXH3
346 case CSUM_XXH3_64:
347 SIVAL64(sum, 0, XXH3_64bits_withSeed(buf, len, checksum_seed));
348 break;
349 case CSUM_XXH3_128: {
350 XXH128_hash_t digest = XXH3_128bits_withSeed(buf, len, checksum_seed);
351 SIVAL64(sum, 0, digest.low64);
352 SIVAL64(sum, 8, digest.high64);
353 break;
354 }
355 #endif
356 case CSUM_MD5: {
357 md_context m5;
358 uchar seedbuf[4];
359 md5_begin(&m5);
360 if (proper_seed_order) {
361 if (checksum_seed) {
362 SIVALu(seedbuf, 0, checksum_seed);
363 md5_update(&m5, seedbuf, 4);
364 }
365 md5_update(&m5, (uchar *)buf, len);
366 } else {
367 md5_update(&m5, (uchar *)buf, len);
368 if (checksum_seed) {
369 SIVALu(seedbuf, 0, checksum_seed);
370 md5_update(&m5, seedbuf, 4);
371 }
372 }
373 md5_result(&m5, (uchar *)sum);
374 break;
375 }
376 case CSUM_MD4:
377 case CSUM_MD4_OLD:
378 case CSUM_MD4_BUSTED:
379 case CSUM_MD4_ARCHAIC: {
380 md_context m;
381 int32 i;
382 static char *buf1;
383 static int32 len1;
384
385 mdfour_begin(&m);
386
387 if (len > len1 || !buf1) {
388 free(buf1);
389 buf1 = new_array(char, len+4);
390 len1 = len;
391 }
392
393 memcpy(buf1, buf, len);
394 if (checksum_seed) {
395 SIVAL(buf1,len,checksum_seed);
396 len += 4;
397 }
398
399 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
400 mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
401
402 /*
403 * Prior to version 27 an incorrect MD4 checksum was computed
404 * by failing to call mdfour_tail() for block sizes that
405 * are multiples of 64. This is fixed by calling mdfour_update()
406 * even when there are no more bytes.
407 */
408 if (len - i > 0 || xfer_sum_nni->num > CSUM_MD4_BUSTED)
409 mdfour_update(&m, (uchar *)(buf1+i), len-i);
410
411 mdfour_result(&m, (uchar *)sum);
412 break;
413 }
414 default: /* paranoia to prevent missing case values */
415 exit_cleanup(RERR_UNSUPPORTED);
416 }
417 }
418
419 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
420 {
421 struct map_struct *buf;
422 OFF_T i, len = st_p->st_size;
423 int32 remainder;
424 int fd;
425
426 fd = do_open_checklinks(fname);
427 if (fd == -1) {
428 memset(sum, 0, file_sum_len);
429 return;
430 }
431
432 buf = map_file(fd, len, MAX_MAP_SIZE, CHUNK_SIZE);
433
434 #ifdef USE_OPENSSL
435 if (file_sum_evp_md) {
436 static EVP_MD_CTX *evp = NULL;
437 if (!evp && !(evp = EVP_MD_CTX_create()))
438 out_of_memory("file_checksum");
439
440 EVP_DigestInit_ex(evp, file_sum_evp_md, NULL);
441
442 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
443 EVP_DigestUpdate(evp, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
444
445 remainder = (int32)(len - i);
446 if (remainder > 0)
447 EVP_DigestUpdate(evp, (uchar *)map_ptr(buf, i, remainder), remainder);
448
449 EVP_DigestFinal_ex(evp, (uchar *)sum, NULL);
450 } else
451 #endif
452 switch (file_sum_nni->num) {
453 #ifdef SUPPORT_XXHASH
454 case CSUM_XXH64: {
455 static XXH64_state_t* state = NULL;
456 if (!state && !(state = XXH64_createState()))
457 out_of_memory("file_checksum");
458
459 XXH64_reset(state, 0);
460
461 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
462 XXH64_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
463
464 remainder = (int32)(len - i);
465 if (remainder > 0)
466 XXH64_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
467
468 SIVAL64(sum, 0, XXH64_digest(state));
469 break;
470 }
471 #endif
472 #ifdef SUPPORT_XXH3
473 case CSUM_XXH3_64: {
474 static XXH3_state_t* state = NULL;
475 if (!state && !(state = XXH3_createState()))
476 out_of_memory("file_checksum");
477
478 XXH3_64bits_reset(state);
479
480 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
481 XXH3_64bits_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
482
483 remainder = (int32)(len - i);
484 if (remainder > 0)
485 XXH3_64bits_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
486
487 SIVAL64(sum, 0, XXH3_64bits_digest(state));
488 break;
489 }
490 case CSUM_XXH3_128: {
491 XXH128_hash_t digest;
492 static XXH3_state_t* state = NULL;
493 if (!state && !(state = XXH3_createState()))
494 out_of_memory("file_checksum");
495
496 XXH3_128bits_reset(state);
497
498 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
499 XXH3_128bits_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
500
501 remainder = (int32)(len - i);
502 if (remainder > 0)
503 XXH3_128bits_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
504
505 digest = XXH3_128bits_digest(state);
506 SIVAL64(sum, 0, digest.low64);
507 SIVAL64(sum, 8, digest.high64);
508 break;
509 }
510 #endif
511 case CSUM_MD5: {
512 md_context m5;
513
514 md5_begin(&m5);
515
516 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
517 md5_update(&m5, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
518
519 remainder = (int32)(len - i);
520 if (remainder > 0)
521 md5_update(&m5, (uchar *)map_ptr(buf, i, remainder), remainder);
522
523 md5_result(&m5, (uchar *)sum);
524 break;
525 }
526 case CSUM_MD4:
527 case CSUM_MD4_OLD:
528 case CSUM_MD4_BUSTED:
529 case CSUM_MD4_ARCHAIC: {
530 md_context m;
531
532 mdfour_begin(&m);
533
534 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
535 mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
536
537 /* Prior to version 27 an incorrect MD4 checksum was computed
538 * by failing to call mdfour_tail() for block sizes that
539 * are multiples of 64. This is fixed by calling mdfour_update()
540 * even when there are no more bytes. */
541 remainder = (int32)(len - i);
542 if (remainder > 0 || file_sum_nni->num > CSUM_MD4_BUSTED)
543 mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
544
545 mdfour_result(&m, (uchar *)sum);
546 break;
547 }
548 default:
549 rprintf(FERROR, "Invalid checksum-choice for --checksum: %s (%d)\n",
550 file_sum_nni->name, file_sum_nni->num);
551 exit_cleanup(RERR_UNSUPPORTED);
552 }
553
554 close(fd);
555 unmap_file(buf);
556 }
557
558 static int32 sumresidue;
559 static md_context ctx_md;
560 #ifdef SUPPORT_XXHASH
561 static XXH64_state_t* xxh64_state;
562 #endif
563 #ifdef SUPPORT_XXH3
564 static XXH3_state_t* xxh3_state;
565 #endif
566 static struct name_num_item *cur_sum_nni;
567 int cur_sum_len;
568
569 #ifdef USE_OPENSSL
570 static const EVP_MD *cur_sum_evp_md;
571 #endif
572
573 /* Initialize a hash digest accumulator. Data is supplied via
574 * sum_update() and the resulting binary digest is retrieved via
575 * sum_end(). This only supports one active sum at a time. */
576 int sum_init(struct name_num_item *nni, int seed)
577 {
578 char s[4];
579
580 if (!nni)
581 nni = parse_csum_name(NULL, 0);
582 cur_sum_nni = nni;
583 cur_sum_len = csum_len_for_type(nni->num, 0);
584 #ifdef USE_OPENSSL
585 cur_sum_evp_md = csum_evp_md(nni);
586 #endif
587
588 #ifdef USE_OPENSSL
589 if (cur_sum_evp_md) {
590 if (!ctx_evp && !(ctx_evp = EVP_MD_CTX_create()))
591 out_of_memory("file_checksum");
592 EVP_DigestInit_ex(ctx_evp, cur_sum_evp_md, NULL);
593 } else
594 #endif
595 switch (cur_sum_nni->num) {
596 #ifdef SUPPORT_XXHASH
597 case CSUM_XXH64:
598 if (!xxh64_state && !(xxh64_state = XXH64_createState()))
599 out_of_memory("sum_init");
600 XXH64_reset(xxh64_state, 0);
601 break;
602 #endif
603 #ifdef SUPPORT_XXH3
604 case CSUM_XXH3_64:
605 if (!xxh3_state && !(xxh3_state = XXH3_createState()))
606 out_of_memory("sum_init");
607 XXH3_64bits_reset(xxh3_state);
608 break;
609 case CSUM_XXH3_128:
610 if (!xxh3_state && !(xxh3_state = XXH3_createState()))
611 out_of_memory("sum_init");
612 XXH3_128bits_reset(xxh3_state);
613 break;
614 #endif
615 case CSUM_MD5:
616 md5_begin(&ctx_md);
617 break;
618 case CSUM_MD4:
619 mdfour_begin(&ctx_md);
620 sumresidue = 0;
621 break;
622 case CSUM_MD4_OLD:
623 case CSUM_MD4_BUSTED:
624 case CSUM_MD4_ARCHAIC:
625 mdfour_begin(&ctx_md);
626 sumresidue = 0;
627 SIVAL(s, 0, seed);
628 sum_update(s, 4);
629 break;
630 case CSUM_NONE:
631 break;
632 default: /* paranoia to prevent missing case values */
633 exit_cleanup(RERR_UNSUPPORTED);
634 }
635
636 return cur_sum_len;
637 }
638
639 /* Feed data into a hash digest accumulator. */
640 void sum_update(const char *p, int32 len)
641 {
642 #ifdef USE_OPENSSL
643 if (cur_sum_evp_md) {
644 EVP_DigestUpdate(ctx_evp, (uchar *)p, len);
645 } else
646 #endif
647 switch (cur_sum_nni->num) {
648 #ifdef SUPPORT_XXHASH
649 case CSUM_XXH64:
650 XXH64_update(xxh64_state, p, len);
651 break;
652 #endif
653 #ifdef SUPPORT_XXH3
654 case CSUM_XXH3_64:
655 XXH3_64bits_update(xxh3_state, p, len);
656 break;
657 case CSUM_XXH3_128:
658 XXH3_128bits_update(xxh3_state, p, len);
659 break;
660 #endif
661 case CSUM_MD5:
662 md5_update(&ctx_md, (uchar *)p, len);
663 break;
664 case CSUM_MD4:
665 case CSUM_MD4_OLD:
666 case CSUM_MD4_BUSTED:
667 case CSUM_MD4_ARCHAIC:
668 if (len + sumresidue < CSUM_CHUNK) {
669 memcpy(ctx_md.buffer + sumresidue, p, len);
670 sumresidue += len;
671 break;
672 }
673
674 if (sumresidue) {
675 int32 i = CSUM_CHUNK - sumresidue;
676 memcpy(ctx_md.buffer + sumresidue, p, i);
677 mdfour_update(&ctx_md, (uchar *)ctx_md.buffer, CSUM_CHUNK);
678 len -= i;
679 p += i;
680 }
681
682 while (len >= CSUM_CHUNK) {
683 mdfour_update(&ctx_md, (uchar *)p, CSUM_CHUNK);
684 len -= CSUM_CHUNK;
685 p += CSUM_CHUNK;
686 }
687
688 sumresidue = len;
689 if (sumresidue)
690 memcpy(ctx_md.buffer, p, sumresidue);
691 break;
692 case CSUM_NONE:
693 break;
694 default: /* paranoia to prevent missing case values */
695 exit_cleanup(RERR_UNSUPPORTED);
696 }
697 }
698
699 /* The sum buffer only needs to be as long as the current checksum's digest
700 * len, not MAX_DIGEST_LEN. Note that for CSUM_MD4_ARCHAIC that is the full
701 * MD4_DIGEST_LEN even if the file-list code is going to ignore all but the
702 * first 2 bytes of it. */
703 void sum_end(char *sum)
704 {
705 #ifdef USE_OPENSSL
706 if (cur_sum_evp_md) {
707 EVP_DigestFinal_ex(ctx_evp, (uchar *)sum, NULL);
708 } else
709 #endif
710 switch (cur_sum_nni->num) {
711 #ifdef SUPPORT_XXHASH
712 case CSUM_XXH64:
713 SIVAL64(sum, 0, XXH64_digest(xxh64_state));
714 break;
715 #endif
716 #ifdef SUPPORT_XXH3
717 case CSUM_XXH3_64:
718 SIVAL64(sum, 0, XXH3_64bits_digest(xxh3_state));
719 break;
720 case CSUM_XXH3_128: {
721 XXH128_hash_t digest = XXH3_128bits_digest(xxh3_state);
722 SIVAL64(sum, 0, digest.low64);
723 SIVAL64(sum, 8, digest.high64);
724 break;
725 }
726 #endif
727 case CSUM_MD5:
728 md5_result(&ctx_md, (uchar *)sum);
729 break;
730 case CSUM_MD4:
731 case CSUM_MD4_OLD:
732 mdfour_update(&ctx_md, (uchar *)ctx_md.buffer, sumresidue);
733 mdfour_result(&ctx_md, (uchar *)sum);
734 break;
735 case CSUM_MD4_BUSTED:
736 case CSUM_MD4_ARCHAIC:
737 if (sumresidue)
738 mdfour_update(&ctx_md, (uchar *)ctx_md.buffer, sumresidue);
739 mdfour_result(&ctx_md, (uchar *)sum);
740 break;
741 case CSUM_NONE:
742 *sum = '\0';
743 break;
744 default: /* paranoia to prevent missing case values */
745 exit_cleanup(RERR_UNSUPPORTED);
746 }
747 }
748
749 #if defined SUPPORT_XXH3 || defined USE_OPENSSL
750 static void verify_digest(struct name_num_item *nni, BOOL check_auth_list)
751 {
752 #ifdef SUPPORT_XXH3
753 static int xxh3_result = 0;
754 #endif
755 #ifdef USE_OPENSSL
756 static int prior_num = 0, prior_flags = 0, prior_result = 0;
757 #endif
758
759 #ifdef SUPPORT_XXH3
760 if (nni->num == CSUM_XXH3_64 || nni->num == CSUM_XXH3_128) {
761 if (!xxh3_result) {
762 char buf[32816];
763 int j;
764 for (j = 0; j < (int)sizeof buf; j++)
765 buf[j] = ' ' + (j % 96);
766 sum_init(nni, 0);
767 sum_update(buf, 32816);
768 sum_update(buf, 31152);
769 sum_update(buf, 32474);
770 sum_update(buf, 9322);
771 xxh3_result = XXH3_64bits_digest(xxh3_state) != 0xadbcf16d4678d1de ? -1 : 1;
772 }
773 if (xxh3_result < 0)
774 nni->num = CSUM_gone;
775 return;
776 }
777 #endif
778
779 #ifdef USE_OPENSSL
780 if (BITS_SETnUNSET(nni->flags, NNI_EVP, NNI_BUILTIN|NNI_EVP_OK)) {
781 if (nni->num == prior_num && nni->flags == prior_flags) {
782 nni->flags = prior_result;
783 if (!(nni->flags & NNI_EVP))
784 nni->num = CSUM_gone;
785 } else {
786 prior_num = nni->num;
787 prior_flags = nni->flags;
788 if (!csum_evp_md(nni))
789 nni->num = CSUM_gone;
790 prior_result = nni->flags;
791 if (check_auth_list && (nni = get_nni_by_num(&valid_auth_checksums, prior_num)) != NULL)
792 verify_digest(nni, False);
793 }
794 }
795 #endif
796 }
797 #endif
798
799 void init_checksum_choices()
800 {
801 #if defined SUPPORT_XXH3 || defined USE_OPENSSL
802 struct name_num_item *nni;
803 #endif
804
805 if (initialized_choices)
806 return;
807
808 #if defined USE_OPENSSL && OPENSSL_VERSION_NUMBER < 0x10100000L
809 OpenSSL_add_all_algorithms();
810 #endif
811
812 #if defined SUPPORT_XXH3 || defined USE_OPENSSL
813 for (nni = valid_checksums.list; nni->name; nni++)
814 verify_digest(nni, True);
815
816 for (nni = valid_auth_checksums.list; nni->name; nni++)
817 verify_digest(nni, False);
818 #endif
819
820 initialized_choices = 1;
821 }