Tweak some indentation.
[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-2019 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  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, visit the http://fsf.org website.
20  */
21
22 #include "rsync.h"
23
24 extern int checksum_seed;
25 extern int protocol_version;
26 extern int proper_seed_order;
27 extern char *checksum_choice;
28
29 #define CSUM_NONE 0
30 #define CSUM_MD4_ARCHAIC 1
31 #define CSUM_MD4_BUSTED 2
32 #define CSUM_MD4_OLD 3
33 #define CSUM_MD4 4
34 #define CSUM_MD5 5
35
36 int xfersum_type = 0; /* used for the file transfer checksums */
37 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
38
39 /* Returns 1 if --whole-file must be enabled. */
40 int parse_checksum_choice(void)
41 {
42         char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
43         if (cp) {
44                 xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice);
45                 checksum_type = parse_csum_name(cp+1, -1);
46         } else
47                 xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1);
48         return xfersum_type == CSUM_NONE;
49 }
50
51 int parse_csum_name(const char *name, int len)
52 {
53         if (len < 0 && name)
54                 len = strlen(name);
55
56         if (!name || (len == 4 && strncasecmp(name, "auto", 4) == 0)) {
57                 if (protocol_version >= 30)
58                         return CSUM_MD5;
59                 if (protocol_version >= 27)
60                         return CSUM_MD4_OLD;
61                 if (protocol_version >= 21)
62                         return CSUM_MD4_BUSTED;
63                 return CSUM_MD4_ARCHAIC;
64         }
65         if (len == 3 && strncasecmp(name, "md4", 3) == 0)
66                 return CSUM_MD4;
67         if (len == 3 && strncasecmp(name, "md5", 3) == 0)
68                 return CSUM_MD5;
69         if (len == 4 && strncasecmp(name, "none", 4) == 0)
70                 return CSUM_NONE;
71
72         rprintf(FERROR, "unknown checksum name: %s\n", name);
73         exit_cleanup(RERR_UNSUPPORTED);
74 }
75
76 int csum_len_for_type(int cst, BOOL flist_csum)
77 {
78         switch (cst) {
79           case CSUM_NONE:
80                 return 1;
81           case CSUM_MD4_ARCHAIC:
82                 /* The oldest checksum code is rather weird: the file-list code only sent
83                  * 2-byte checksums, but all other checksums were full MD4 length. */
84                 return flist_csum ? 2 : MD4_DIGEST_LEN;
85           case CSUM_MD4:
86           case CSUM_MD4_OLD:
87           case CSUM_MD4_BUSTED:
88                 return MD4_DIGEST_LEN;
89           case CSUM_MD5:
90                 return MD5_DIGEST_LEN;
91           default: /* paranoia to prevent missing case values */
92                 exit_cleanup(RERR_UNSUPPORTED);
93         }
94         return 0;
95 }
96
97 int canonical_checksum(int csum_type)
98 {
99         return csum_type >= CSUM_MD4 ? 1 : 0;
100 }
101
102 /*
103   a simple 32 bit checksum that can be upadted from either end
104   (inspired by Mark Adler's Adler-32 checksum)
105   */
106 uint32 get_checksum1(char *buf1, int32 len)
107 {
108         int32 i;
109         uint32 s1, s2;
110         schar *buf = (schar *)buf1;
111
112         s1 = s2 = 0;
113         for (i = 0; i < (len-4); i+=4) {
114                 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
115                 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
116         }
117         for (; i < len; i++) {
118                 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
119         }
120         return (s1 & 0xffff) + (s2 << 16);
121 }
122
123 void get_checksum2(char *buf, int32 len, char *sum)
124 {
125         md_context m;
126
127         switch (xfersum_type) {
128           case CSUM_MD5: {
129                 uchar seedbuf[4];
130                 md5_begin(&m);
131                 if (proper_seed_order) {
132                         if (checksum_seed) {
133                                 SIVALu(seedbuf, 0, checksum_seed);
134                                 md5_update(&m, seedbuf, 4);
135                         }
136                         md5_update(&m, (uchar *)buf, len);
137                 } else {
138                         md5_update(&m, (uchar *)buf, len);
139                         if (checksum_seed) {
140                                 SIVALu(seedbuf, 0, checksum_seed);
141                                 md5_update(&m, seedbuf, 4);
142                         }
143                 }
144                 md5_result(&m, (uchar *)sum);
145                 break;
146           }
147           case CSUM_MD4:
148           case CSUM_MD4_OLD:
149           case CSUM_MD4_BUSTED:
150           case CSUM_MD4_ARCHAIC: {
151                 int32 i;
152                 static char *buf1;
153                 static int32 len1;
154
155                 mdfour_begin(&m);
156
157                 if (len > len1) {
158                         if (buf1)
159                                 free(buf1);
160                         buf1 = new_array(char, len+4);
161                         len1 = len;
162                         if (!buf1)
163                                 out_of_memory("get_checksum2");
164                 }
165
166                 memcpy(buf1, buf, len);
167                 if (checksum_seed) {
168                         SIVAL(buf1,len,checksum_seed);
169                         len += 4;
170                 }
171
172                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
173                         mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
174
175                 /*
176                  * Prior to version 27 an incorrect MD4 checksum was computed
177                  * by failing to call mdfour_tail() for block sizes that
178                  * are multiples of 64.  This is fixed by calling mdfour_update()
179                  * even when there are no more bytes.
180                  */
181                 if (len - i > 0 || xfersum_type > CSUM_MD4_BUSTED)
182                         mdfour_update(&m, (uchar *)(buf1+i), len-i);
183
184                 mdfour_result(&m, (uchar *)sum);
185                 break;
186           }
187           default: /* paranoia to prevent missing case values */
188                 exit_cleanup(RERR_UNSUPPORTED);
189         }
190 }
191
192 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
193 {
194         struct map_struct *buf;
195         OFF_T i, len = st_p->st_size;
196         md_context m;
197         int32 remainder;
198         int fd;
199
200         memset(sum, 0, MAX_DIGEST_LEN);
201
202         fd = do_open(fname, O_RDONLY, 0);
203         if (fd == -1)
204                 return;
205
206         buf = map_file(fd, len, MAX_MAP_SIZE, CSUM_CHUNK);
207
208         switch (checksum_type) {
209           case CSUM_MD5:
210                 md5_begin(&m);
211
212                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
213                         md5_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK),
214                                    CSUM_CHUNK);
215                 }
216
217                 remainder = (int32)(len - i);
218                 if (remainder > 0)
219                         md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
220
221                 md5_result(&m, (uchar *)sum);
222                 break;
223           case CSUM_MD4:
224           case CSUM_MD4_OLD:
225           case CSUM_MD4_BUSTED:
226           case CSUM_MD4_ARCHAIC:
227                 mdfour_begin(&m);
228
229                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
230                         mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
231                 }
232
233                 /* Prior to version 27 an incorrect MD4 checksum was computed
234                  * by failing to call mdfour_tail() for block sizes that
235                  * are multiples of 64.  This is fixed by calling mdfour_update()
236                  * even when there are no more bytes. */
237                 remainder = (int32)(len - i);
238                 if (remainder > 0 || checksum_type > CSUM_MD4_BUSTED)
239                         mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
240
241                 mdfour_result(&m, (uchar *)sum);
242                 break;
243           default:
244                 rprintf(FERROR, "invalid checksum-choice for the --checksum option (%d)\n", checksum_type);
245                 exit_cleanup(RERR_UNSUPPORTED);
246         }
247
248         close(fd);
249         unmap_file(buf);
250 }
251
252 static int32 sumresidue;
253 static md_context md;
254 static int cursum_type;
255
256 void sum_init(int csum_type, int seed)
257 {
258         char s[4];
259
260         if (csum_type < 0)
261                 csum_type = parse_csum_name(NULL, 0);
262         cursum_type = csum_type;
263
264         switch (csum_type) {
265           case CSUM_MD5:
266                 md5_begin(&md);
267                 break;
268           case CSUM_MD4:
269                 mdfour_begin(&md);
270                 sumresidue = 0;
271                 break;
272           case CSUM_MD4_OLD:
273           case CSUM_MD4_BUSTED:
274           case CSUM_MD4_ARCHAIC:
275                 mdfour_begin(&md);
276                 sumresidue = 0;
277                 SIVAL(s, 0, seed);
278                 sum_update(s, 4);
279                 break;
280           case CSUM_NONE:
281                 break;
282           default: /* paranoia to prevent missing case values */
283                 exit_cleanup(RERR_UNSUPPORTED);
284         }
285 }
286
287 /**
288  * Feed data into an MD4 accumulator, md.  The results may be
289  * retrieved using sum_end().  md is used for different purposes at
290  * different points during execution.
291  *
292  * @todo Perhaps get rid of md and just pass in the address each time.
293  * Very slightly clearer and slower.
294  **/
295 void sum_update(const char *p, int32 len)
296 {
297         switch (cursum_type) {
298           case CSUM_MD5:
299                 md5_update(&md, (uchar *)p, len);
300                 break;
301           case CSUM_MD4:
302           case CSUM_MD4_OLD:
303           case CSUM_MD4_BUSTED:
304           case CSUM_MD4_ARCHAIC:
305                 if (len + sumresidue < CSUM_CHUNK) {
306                         memcpy(md.buffer + sumresidue, p, len);
307                         sumresidue += len;
308                         break;
309                 }
310
311                 if (sumresidue) {
312                         int32 i = CSUM_CHUNK - sumresidue;
313                         memcpy(md.buffer + sumresidue, p, i);
314                         mdfour_update(&md, (uchar *)md.buffer, CSUM_CHUNK);
315                         len -= i;
316                         p += i;
317                 }
318
319                 while (len >= CSUM_CHUNK) {
320                         mdfour_update(&md, (uchar *)p, CSUM_CHUNK);
321                         len -= CSUM_CHUNK;
322                         p += CSUM_CHUNK;
323                 }
324
325                 sumresidue = len;
326                 if (sumresidue)
327                         memcpy(md.buffer, p, sumresidue);
328                 break;
329           case CSUM_NONE:
330                 break;
331           default: /* paranoia to prevent missing case values */
332                 exit_cleanup(RERR_UNSUPPORTED);
333         }
334 }
335
336 /* NOTE: all the callers of sum_end() pass in a pointer to a buffer that is
337  * MAX_DIGEST_LEN in size, so even if the csum-len is shorter that that (i.e.
338  * CSUM_MD4_ARCHAIC), we don't have to worry about limiting the data we write
339  * into the "sum" buffer. */
340 int sum_end(char *sum)
341 {
342         switch (cursum_type) {
343           case CSUM_MD5:
344                 md5_result(&md, (uchar *)sum);
345                 break;
346           case CSUM_MD4:
347           case CSUM_MD4_OLD:
348                 mdfour_update(&md, (uchar *)md.buffer, sumresidue);
349                 mdfour_result(&md, (uchar *)sum);
350                 break;
351           case CSUM_MD4_BUSTED:
352           case CSUM_MD4_ARCHAIC:
353                 if (sumresidue)
354                         mdfour_update(&md, (uchar *)md.buffer, sumresidue);
355                 mdfour_result(&md, (uchar *)sum);
356                 break;
357           case CSUM_NONE:
358                 *sum = '\0';
359                 break;
360           default: /* paranoia to prevent missing case values */
361                 exit_cleanup(RERR_UNSUPPORTED);
362         }
363
364         return csum_len_for_type(cursum_type, 0);
365 }