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