Move OpenSSL-related MD4/5 defines and imports to lib/mdigest.h
[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-2020 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 #ifdef SUPPORT_XXHASH
31 #include "xxhash.h"
32 #endif
33
34 extern int am_server;
35 extern int whole_file;
36 extern int checksum_seed;
37 extern int protocol_version;
38 extern int proper_seed_order;
39 extern const char *checksum_choice;
40
41 #define CSUM_NONE 0
42 #define CSUM_MD4_ARCHAIC 1
43 #define CSUM_MD4_BUSTED 2
44 #define CSUM_MD4_OLD 3
45 #define CSUM_MD4 4
46 #define CSUM_MD5 5
47 #define CSUM_XXH64 6
48
49 struct name_num_obj valid_checksums = {
50         "checksum", NULL, NULL, 0, 0, {
51 #ifdef SUPPORT_XXHASH
52                 { CSUM_XXH64, "xxh64", NULL },
53                 { CSUM_XXH64, "xxhash", NULL },
54 #endif
55                 { CSUM_MD5, "md5", NULL },
56                 { CSUM_MD4, "md4", NULL },
57                 { CSUM_NONE, "none", NULL },
58                 { 0, NULL, NULL }
59         }
60 };
61
62 int xfersum_type = 0; /* used for the file transfer checksums */
63 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
64
65 static int parse_csum_name(const char *name, int len)
66 {
67         struct name_num_item *nni;
68
69         if (len < 0 && name)
70                 len = strlen(name);
71
72         if (!name || (len == 4 && strncasecmp(name, "auto", 4) == 0)) {
73                 if (protocol_version >= 30)
74                         return CSUM_MD5;
75                 if (protocol_version >= 27)
76                         return CSUM_MD4_OLD;
77                 if (protocol_version >= 21)
78                         return CSUM_MD4_BUSTED;
79                 return CSUM_MD4_ARCHAIC;
80         }
81
82         nni = get_nni_by_name(&valid_checksums, name, len);
83
84         if (!nni) {
85                 rprintf(FERROR, "unknown checksum name: %s\n", name);
86                 exit_cleanup(RERR_UNSUPPORTED);
87         }
88
89         return nni->num;
90 }
91
92 static const char *checksum_name(int num)
93 {
94         struct name_num_item *nni = get_nni_by_num(&valid_checksums, num);
95
96         return nni ? nni->name : num < CSUM_MD4 ? "MD4" : "UNKNOWN";
97 }
98
99 void parse_checksum_choice(int final_call)
100 {
101         if (valid_checksums.negotiated_name)
102                 xfersum_type = checksum_type = valid_checksums.negotiated_num;
103         else {
104                 char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
105                 if (cp) {
106                         xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice);
107                         checksum_type = parse_csum_name(cp+1, -1);
108                 } else
109                         xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1);
110         }
111
112         if (xfersum_type == CSUM_NONE)
113                 whole_file = 1;
114
115         /* Snag the checksum name for both write_batch's option output & the following debug output. */
116         if (valid_checksums.negotiated_name)
117                 checksum_choice = valid_checksums.negotiated_name;
118         else if (checksum_choice == NULL)
119                 checksum_choice = checksum_name(xfersum_type);
120
121         if (final_call && DEBUG_GTE(NSTR, am_server ? 3 : 1)) {
122                 rprintf(FINFO, "%s%s checksum: %s\n",
123                         am_server ? "Server" : "Client",
124                         valid_checksums.negotiated_name ? " negotiated" : "",
125                         checksum_choice);
126         }
127 }
128
129 int csum_len_for_type(int cst, BOOL flist_csum)
130 {
131         switch (cst) {
132           case CSUM_NONE:
133                 return 1;
134           case CSUM_MD4_ARCHAIC:
135                 /* The oldest checksum code is rather weird: the file-list code only sent
136                  * 2-byte checksums, but all other checksums were full MD4 length. */
137                 return flist_csum ? 2 : MD4_DIGEST_LEN;
138           case CSUM_MD4:
139           case CSUM_MD4_OLD:
140           case CSUM_MD4_BUSTED:
141                 return MD4_DIGEST_LEN;
142           case CSUM_MD5:
143                 return MD5_DIGEST_LEN;
144 #ifdef SUPPORT_XXHASH
145           case CSUM_XXH64:
146                 return 64/8;
147 #endif
148           default: /* paranoia to prevent missing case values */
149                 exit_cleanup(RERR_UNSUPPORTED);
150         }
151         return 0;
152 }
153
154 /* Returns 0 if the checksum is not canonical (i.e. it includes a seed value).
155  * Returns 1 if the public sum order matches our internal sum order.
156  * Returns -1 if the public sum order is the reverse of our internal sum order.
157  */
158 int canonical_checksum(int csum_type)
159 {
160         switch (csum_type) {
161           case CSUM_NONE:
162           case CSUM_MD4_ARCHAIC:
163           case CSUM_MD4_OLD:
164           case CSUM_MD4_BUSTED:
165                 break;
166           case CSUM_MD4:
167           case CSUM_MD5:
168                 return -1;
169 #ifdef SUPPORT_XXHASH
170           case CSUM_XXH64:
171                 return 1;
172 #endif
173           default: /* paranoia to prevent missing case values */
174                 exit_cleanup(RERR_UNSUPPORTED);
175         }
176         return 0;
177 }
178
179 #ifndef HAVE_SIMD /* See simd-checksum-*.cpp. */
180 /*
181   a simple 32 bit checksum that can be updated from either end
182   (inspired by Mark Adler's Adler-32 checksum)
183   */
184 uint32 get_checksum1(char *buf1, int32 len)
185 {
186         int32 i;
187         uint32 s1, s2;
188         schar *buf = (schar *)buf1;
189
190         s1 = s2 = 0;
191         for (i = 0; i < (len-4); i+=4) {
192                 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
193                 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
194         }
195         for (; i < len; i++) {
196                 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
197         }
198         return (s1 & 0xffff) + (s2 << 16);
199 }
200 #endif
201
202 void get_checksum2(char *buf, int32 len, char *sum)
203 {
204         switch (xfersum_type) {
205 #ifdef SUPPORT_XXHASH
206           case CSUM_XXH64:
207                 SIVAL64(sum, 0, XXH64(buf, len, checksum_seed));
208                 break;
209 #endif
210           case CSUM_MD5: {
211                 MD5_CTX m5;
212                 uchar seedbuf[4];
213                 MD5_Init(&m5);
214                 if (proper_seed_order) {
215                         if (checksum_seed) {
216                                 SIVALu(seedbuf, 0, checksum_seed);
217                                 MD5_Update(&m5, seedbuf, 4);
218                         }
219                         MD5_Update(&m5, (uchar *)buf, len);
220                 } else {
221                         MD5_Update(&m5, (uchar *)buf, len);
222                         if (checksum_seed) {
223                                 SIVALu(seedbuf, 0, checksum_seed);
224                                 MD5_Update(&m5, seedbuf, 4);
225                         }
226                 }
227                 MD5_Final((uchar *)sum, &m5);
228                 break;
229           }
230           case CSUM_MD4:
231 #ifdef USE_OPENSSL
232           {
233                 MD4_CTX m4;
234                 MD4_Init(&m4);
235                 MD4_Update(&m4, (uchar *)buf, len);
236                 if (checksum_seed) {
237                         uchar seedbuf[4];
238                         SIVALu(seedbuf, 0, checksum_seed);
239                         MD4_Update(&m4, seedbuf, 4);
240                 }
241                 MD4_Final((uchar *)sum, &m4);
242                 break;
243           }
244 #endif
245           case CSUM_MD4_OLD:
246           case CSUM_MD4_BUSTED:
247           case CSUM_MD4_ARCHAIC: {
248                 md_context m;
249                 int32 i;
250                 static char *buf1;
251                 static int32 len1;
252
253                 mdfour_begin(&m);
254
255                 if (len > len1) {
256                         if (buf1)
257                                 free(buf1);
258                         buf1 = new_array(char, len+4);
259                         len1 = len;
260                         if (!buf1)
261                                 out_of_memory("get_checksum2");
262                 }
263
264                 memcpy(buf1, buf, len);
265                 if (checksum_seed) {
266                         SIVAL(buf1,len,checksum_seed);
267                         len += 4;
268                 }
269
270                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
271                         mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
272
273                 /*
274                  * Prior to version 27 an incorrect MD4 checksum was computed
275                  * by failing to call mdfour_tail() for block sizes that
276                  * are multiples of 64.  This is fixed by calling mdfour_update()
277                  * even when there are no more bytes.
278                  */
279                 if (len - i > 0 || xfersum_type > CSUM_MD4_BUSTED)
280                         mdfour_update(&m, (uchar *)(buf1+i), len-i);
281
282                 mdfour_result(&m, (uchar *)sum);
283                 break;
284           }
285           default: /* paranoia to prevent missing case values */
286                 exit_cleanup(RERR_UNSUPPORTED);
287         }
288 }
289
290 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
291 {
292         struct map_struct *buf;
293         OFF_T i, len = st_p->st_size;
294         int32 remainder;
295         int fd;
296
297         memset(sum, 0, MAX_DIGEST_LEN);
298
299         fd = do_open(fname, O_RDONLY, 0);
300         if (fd == -1)
301                 return;
302
303         buf = map_file(fd, len, MAX_MAP_SIZE, CHUNK_SIZE);
304
305         switch (checksum_type) {
306 #ifdef SUPPORT_XXHASH
307           case CSUM_XXH64: {
308                 static XXH64_state_t* state = NULL;
309                 if (!state && !(state = XXH64_createState()))
310                         out_of_memory("file_checksum");
311
312                 XXH64_reset(state, 0);
313
314                 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
315                         XXH64_update(state, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
316
317                 remainder = (int32)(len - i);
318                 if (remainder > 0)
319                         XXH64_update(state, (uchar *)map_ptr(buf, i, remainder), remainder);
320
321                 SIVAL64(sum, 0, XXH64_digest(state));
322                 break;
323           }
324 #endif
325           case CSUM_MD5: {
326                 MD5_CTX m5;
327
328                 MD5_Init(&m5);
329
330                 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
331                         MD5_Update(&m5, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
332
333                 remainder = (int32)(len - i);
334                 if (remainder > 0)
335                         MD5_Update(&m5, (uchar *)map_ptr(buf, i, remainder), remainder);
336
337                 MD5_Final((uchar *)sum, &m5);
338                 break;
339           }
340           case CSUM_MD4:
341 #ifdef USE_OPENSSL
342           {
343                 MD4_CTX m4;
344
345                 MD4_Init(&m4);
346
347                 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
348                         MD4_Update(&m4, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
349
350                 remainder = (int32)(len - i);
351                 if (remainder > 0)
352                         MD4_Update(&m4, (uchar *)map_ptr(buf, i, remainder), remainder);
353
354                 MD4_Final((uchar *)sum, &m4);
355                 break;
356           }
357 #endif
358           case CSUM_MD4_OLD:
359           case CSUM_MD4_BUSTED:
360           case CSUM_MD4_ARCHAIC: {
361                 md_context m;
362
363                 mdfour_begin(&m);
364
365                 for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
366                         mdfour_update(&m, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
367
368                 /* Prior to version 27 an incorrect MD4 checksum was computed
369                  * by failing to call mdfour_tail() for block sizes that
370                  * are multiples of 64.  This is fixed by calling mdfour_update()
371                  * even when there are no more bytes. */
372                 remainder = (int32)(len - i);
373                 if (remainder > 0 || checksum_type > CSUM_MD4_BUSTED)
374                         mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
375
376                 mdfour_result(&m, (uchar *)sum);
377                 break;
378           }
379           default:
380                 rprintf(FERROR, "Invalid checksum-choice for --checksum: %s (%d)\n",
381                         checksum_name(checksum_type), checksum_type);
382                 exit_cleanup(RERR_UNSUPPORTED);
383         }
384
385         close(fd);
386         unmap_file(buf);
387 }
388
389 static int32 sumresidue;
390 static union {
391         md_context md;
392 #ifdef USE_OPENSSL
393         MD4_CTX m4;
394 #endif
395         MD5_CTX m5;
396 } ctx;
397 #ifdef SUPPORT_XXHASH
398 static XXH64_state_t* xxh64_state;
399 #endif
400 static int cursum_type;
401
402 void sum_init(int csum_type, int seed)
403 {
404         char s[4];
405
406         if (csum_type < 0)
407                 csum_type = parse_csum_name(NULL, 0);
408         cursum_type = csum_type;
409
410         switch (csum_type) {
411 #ifdef SUPPORT_XXHASH
412           case CSUM_XXH64:
413                 if (!xxh64_state && !(xxh64_state = XXH64_createState()))
414                         out_of_memory("sum_init");
415                 XXH64_reset(xxh64_state, 0);
416                 break;
417 #endif
418           case CSUM_MD5:
419                 MD5_Init(&ctx.m5);
420                 break;
421           case CSUM_MD4:
422 #ifdef USE_OPENSSL
423                 MD4_Init(&ctx.m4);
424 #else
425                 mdfour_begin(&ctx.md);
426                 sumresidue = 0;
427 #endif
428                 break;
429           case CSUM_MD4_OLD:
430           case CSUM_MD4_BUSTED:
431           case CSUM_MD4_ARCHAIC:
432                 mdfour_begin(&ctx.md);
433                 sumresidue = 0;
434                 SIVAL(s, 0, seed);
435                 sum_update(s, 4);
436                 break;
437           case CSUM_NONE:
438                 break;
439           default: /* paranoia to prevent missing case values */
440                 exit_cleanup(RERR_UNSUPPORTED);
441         }
442 }
443
444 /**
445  * Feed data into an MD4 accumulator, md.  The results may be
446  * retrieved using sum_end().  md is used for different purposes at
447  * different points during execution.
448  *
449  * @todo Perhaps get rid of md and just pass in the address each time.
450  * Very slightly clearer and slower.
451  **/
452 void sum_update(const char *p, int32 len)
453 {
454         switch (cursum_type) {
455 #ifdef SUPPORT_XXHASH
456           case CSUM_XXH64:
457                 XXH64_update(xxh64_state, p, len);
458                 break;
459 #endif
460           case CSUM_MD5:
461                 MD5_Update(&ctx.m5, (uchar *)p, len);
462                 break;
463           case CSUM_MD4:
464 #ifdef USE_OPENSSL
465                 MD4_Update(&ctx.m4, (uchar *)p, len);
466                 break;
467 #endif
468           case CSUM_MD4_OLD:
469           case CSUM_MD4_BUSTED:
470           case CSUM_MD4_ARCHAIC:
471                 if (len + sumresidue < CSUM_CHUNK) {
472                         memcpy(ctx.md.buffer + sumresidue, p, len);
473                         sumresidue += len;
474                         break;
475                 }
476
477                 if (sumresidue) {
478                         int32 i = CSUM_CHUNK - sumresidue;
479                         memcpy(ctx.md.buffer + sumresidue, p, i);
480                         mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, CSUM_CHUNK);
481                         len -= i;
482                         p += i;
483                 }
484
485                 while (len >= CSUM_CHUNK) {
486                         mdfour_update(&ctx.md, (uchar *)p, CSUM_CHUNK);
487                         len -= CSUM_CHUNK;
488                         p += CSUM_CHUNK;
489                 }
490
491                 sumresidue = len;
492                 if (sumresidue)
493                         memcpy(ctx.md.buffer, p, sumresidue);
494                 break;
495           case CSUM_NONE:
496                 break;
497           default: /* paranoia to prevent missing case values */
498                 exit_cleanup(RERR_UNSUPPORTED);
499         }
500 }
501
502 /* NOTE: all the callers of sum_end() pass in a pointer to a buffer that is
503  * MAX_DIGEST_LEN in size, so even if the csum-len is shorter that that (i.e.
504  * CSUM_MD4_ARCHAIC), we don't have to worry about limiting the data we write
505  * into the "sum" buffer. */
506 int sum_end(char *sum)
507 {
508         switch (cursum_type) {
509 #ifdef SUPPORT_XXHASH
510           case CSUM_XXH64:
511                 SIVAL64(sum, 0, XXH64_digest(xxh64_state));
512                 break;
513 #endif
514           case CSUM_MD5:
515                 MD5_Final((uchar *)sum, &ctx.m5);
516                 break;
517           case CSUM_MD4:
518 #ifdef USE_OPENSSL
519                 MD4_Final((uchar *)sum, &ctx.m4);
520                 break;
521 #endif
522           case CSUM_MD4_OLD:
523                 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
524                 mdfour_result(&ctx.md, (uchar *)sum);
525                 break;
526           case CSUM_MD4_BUSTED:
527           case CSUM_MD4_ARCHAIC:
528                 if (sumresidue)
529                         mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
530                 mdfour_result(&ctx.md, (uchar *)sum);
531                 break;
532           case CSUM_NONE:
533                 *sum = '\0';
534                 break;
535           default: /* paranoia to prevent missing case values */
536                 exit_cleanup(RERR_UNSUPPORTED);
537         }
538
539         return csum_len_for_type(cursum_type, 0);
540 }