Unify the checksum context memory, since we only use one at a time.
[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 #ifdef USE_OPENSSL
34 #include "openssl/md4.h"
35 #include "openssl/md5.h"
36 #endif
37
38 extern int am_server;
39 extern int local_server;
40 extern int whole_file;
41 extern int read_batch;
42 extern int checksum_seed;
43 extern int protocol_version;
44 extern int proper_seed_order;
45 extern char *checksum_choice;
46
47 #define CSUM_NONE 0
48 #define CSUM_MD4_ARCHAIC 1
49 #define CSUM_MD4_BUSTED 2
50 #define CSUM_MD4_OLD 3
51 #define CSUM_MD4 4
52 #define CSUM_MD5 5
53 #define CSUM_XXHASH 6
54
55 #define CSUM_SAW_BUFLEN 10
56
57 struct csum_struct {
58         int num;
59         const char *name;
60 } valid_checksums[] = {
61 #ifdef SUPPORT_XXHASH
62         { CSUM_XXHASH, "xxhash" },
63 #endif
64         { CSUM_MD5, "md5" },
65         { CSUM_MD4, "md4" },
66         { CSUM_NONE, "none" },
67         { -1, NULL }
68 };
69
70 #define MAX_CHECKSUM_LIST 1024
71
72 #ifndef USE_OPENSSL
73 #define MD5_CTX md_context
74 #define MD5_Init md5_begin
75 #define MD5_Update md5_update
76 #define MD5_Final(digest, cptr) md5_result(cptr, digest)
77 #endif
78
79 int xfersum_type = 0; /* used for the file transfer checksums */
80 int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */
81 const char *negotiated_csum_name = NULL;
82
83 static int parse_csum_name(const char *name, int len, int allow_auto)
84 {
85         struct csum_struct *cs;
86
87         if (len < 0 && name)
88                 len = strlen(name);
89
90         if (!name || (allow_auto && len == 4 && strncasecmp(name, "auto", 4) == 0)) {
91                 if (protocol_version >= 30)
92                         return CSUM_MD5;
93                 if (protocol_version >= 27)
94                         return CSUM_MD4_OLD;
95                 if (protocol_version >= 21)
96                         return CSUM_MD4_BUSTED;
97                 return CSUM_MD4_ARCHAIC;
98         }
99
100         for (cs = valid_checksums; cs->name; cs++) {
101                 if (strncasecmp(name, cs->name, len) == 0 && cs->name[len] == '\0')
102                         return cs->num;
103         }
104
105         if (allow_auto) {
106                 rprintf(FERROR, "unknown checksum name: %s\n", name);
107                 exit_cleanup(RERR_UNSUPPORTED);
108         }
109
110         return -1;
111 }
112
113 static const char *checksum_name(int num)
114 {
115         struct csum_struct *cs;
116
117         for (cs = valid_checksums; cs->name; cs++) {
118                 if (num == cs->num)
119                         return cs->name;
120         }
121
122         if (num < CSUM_MD4)
123                 return "MD4";
124
125         return "UNKNOWN"; /* IMPOSSIBLE */
126 }
127
128 void parse_checksum_choice(int final_call)
129 {
130         if (!negotiated_csum_name) {
131                 char *cp = checksum_choice ? strchr(checksum_choice, ',') : NULL;
132                 if (cp) {
133                         xfersum_type = parse_csum_name(checksum_choice, cp - checksum_choice, 1);
134                         checksum_type = parse_csum_name(cp+1, -1, 1);
135                 } else
136                         xfersum_type = checksum_type = parse_csum_name(checksum_choice, -1, 1);
137         }
138
139         if (xfersum_type == CSUM_NONE)
140                 whole_file = 1;
141
142         if (final_call && DEBUG_GTE(CSUM, 1)) {
143                 if (negotiated_csum_name)
144                         rprintf(FINFO, "[%s] negotiated checksum: %s\n", who_am_i(), negotiated_csum_name);
145                 else if (xfersum_type == checksum_type) {
146                         rprintf(FINFO, "[%s] %s checksum: %s\n", who_am_i(),
147                                 checksum_choice ? "chosen" : "protocol-based",
148                                 checksum_name(xfersum_type));
149                 } else {
150                         rprintf(FINFO, "[%s] chosen transfer checksum: %s\n",
151                                 who_am_i(), checksum_name(xfersum_type));
152                         rprintf(FINFO, "[%s] chosen pre-transfer checksum: %s\n",
153                                 who_am_i(), checksum_name(checksum_type));
154                 }
155         }
156 }
157
158 static int parse_checksum_list(const char *from, char *sumbuf, int sumbuf_len, char *saw)
159 {
160         char *to = sumbuf, *tok = NULL;
161         int cnt = 0;
162
163         memset(saw, 0, CSUM_SAW_BUFLEN);
164
165         while (1) {
166                 if (*from == ' ' || !*from) {
167                         if (tok) {
168                                 int sum_type = parse_csum_name(tok, to - tok, 0);
169                                 if (sum_type >= 0 && !saw[sum_type])
170                                         saw[sum_type] = ++cnt;
171                                 else
172                                         to = tok - (tok != sumbuf);
173                                 tok = NULL;
174                         }
175                         if (!*from++)
176                                 break;
177                         continue;
178                 }
179                 if (!tok) {
180                         if (to != sumbuf)
181                                 *to++ = ' ';
182                         tok = to;
183                 }
184                 if (to - sumbuf >= sumbuf_len - 1) {
185                         to = tok - (tok != sumbuf);
186                         break;
187                 }
188                 *to++ = *from++;
189         }
190         *to = '\0';
191
192         return to - sumbuf;
193 }
194
195 void negotiate_checksum(int f_in, int f_out, const char *csum_list, int saw_fail)
196 {
197         char *tok, sumbuf[MAX_CHECKSUM_LIST], saw[CSUM_SAW_BUFLEN];
198         int sum_type, len;
199
200         /* Simplify the user-provided string so that it contains valid
201          * checksum names without any duplicates. The client side also
202          * makes use of the saw values when scanning the server's list. */
203         if (csum_list && *csum_list && (!am_server || local_server)) {
204                 len = parse_checksum_list(csum_list, sumbuf, sizeof sumbuf, saw);
205                 if (saw_fail && !len)
206                         len = strlcpy(sumbuf, "FAIL", sizeof sumbuf);
207                 csum_list = sumbuf;
208         } else {
209                 memset(saw, 0, CSUM_SAW_BUFLEN);
210                 csum_list = NULL;
211         }
212
213         if (!csum_list || !*csum_list) {
214                 struct csum_struct *cs;
215                 int cnt = 0;
216                 for (cs = valid_checksums, len = 0; cs->name; cs++) {
217                         if (cs->num == CSUM_NONE)
218                                 continue;
219                         if (len)
220                                 sumbuf[len++]= ' ';
221                         len += strlcpy(sumbuf+len, cs->name, sizeof sumbuf - len);
222                         if (len >= (int)sizeof sumbuf - 1)
223                                 exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE... */
224                         saw[cs->num] = ++cnt;
225                 }
226         }
227
228         /* Each side sends their list of valid checksum names to the other side and
229          * then both sides pick the first name in the client's list that is also in
230          * the server's list. */
231         if (!local_server)
232                 write_vstring(f_out, sumbuf, len);
233
234         if (!local_server || read_batch)
235                 len = read_vstring(f_in, sumbuf, sizeof sumbuf);
236
237         if (len > 0) {
238                 int best = CSUM_SAW_BUFLEN; /* We want best == 1 from the client list */
239                 if (am_server)
240                         memset(saw, 1, CSUM_SAW_BUFLEN); /* The first client's choice is the best choice */
241                 for (tok = strtok(sumbuf, " \t"); tok; tok = strtok(NULL, " \t")) {
242                         sum_type = parse_csum_name(tok, -1, 0);
243                         if (sum_type < 0 || !saw[sum_type] || best < saw[sum_type])
244                                 continue;
245                         xfersum_type = checksum_type = sum_type;
246                         negotiated_csum_name = tok;
247                         best = saw[sum_type];
248                         if (best == 1)
249                                 break;
250                 }
251                 if (negotiated_csum_name) {
252                         negotiated_csum_name = strdup(negotiated_csum_name);
253                         return;
254                 }
255         }
256
257         if (!am_server)
258                 msleep(20);
259         rprintf(FERROR, "Failed to negotiate a common checksum\n");
260         exit_cleanup(RERR_UNSUPPORTED);
261 }
262
263 int csum_len_for_type(int cst, BOOL flist_csum)
264 {
265         switch (cst) {
266           case CSUM_NONE:
267                 return 1;
268           case CSUM_MD4_ARCHAIC:
269                 /* The oldest checksum code is rather weird: the file-list code only sent
270                  * 2-byte checksums, but all other checksums were full MD4 length. */
271                 return flist_csum ? 2 : MD4_DIGEST_LEN;
272           case CSUM_MD4:
273           case CSUM_MD4_OLD:
274           case CSUM_MD4_BUSTED:
275                 return MD4_DIGEST_LEN;
276           case CSUM_MD5:
277                 return MD5_DIGEST_LEN;
278 #ifdef SUPPORT_XXHASH
279           case CSUM_XXHASH:
280                 return sizeof (XXH64_hash_t);
281 #endif
282           default: /* paranoia to prevent missing case values */
283                 exit_cleanup(RERR_UNSUPPORTED);
284         }
285         return 0;
286 }
287
288 int canonical_checksum(int csum_type)
289 {
290         return csum_type >= CSUM_MD4 ? 1 : 0;
291 }
292
293 #ifndef HAVE_SIMD /* See simd-checksum-*.cpp. */
294 /*
295   a simple 32 bit checksum that can be updated from either end
296   (inspired by Mark Adler's Adler-32 checksum)
297   */
298 uint32 get_checksum1(char *buf1, int32 len)
299 {
300         int32 i;
301         uint32 s1, s2;
302         schar *buf = (schar *)buf1;
303
304         s1 = s2 = 0;
305         for (i = 0; i < (len-4); i+=4) {
306                 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
307                 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
308         }
309         for (; i < len; i++) {
310                 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
311         }
312         return (s1 & 0xffff) + (s2 << 16);
313 }
314 #endif
315
316 void get_checksum2(char *buf, int32 len, char *sum)
317 {
318         switch (xfersum_type) {
319           case CSUM_MD5: {
320                 MD5_CTX m5;
321                 uchar seedbuf[4];
322                 MD5_Init(&m5);
323                 if (proper_seed_order) {
324                         if (checksum_seed) {
325                                 SIVALu(seedbuf, 0, checksum_seed);
326                                 MD5_Update(&m5, seedbuf, 4);
327                         }
328                         MD5_Update(&m5, (uchar *)buf, len);
329                 } else {
330                         MD5_Update(&m5, (uchar *)buf, len);
331                         if (checksum_seed) {
332                                 SIVALu(seedbuf, 0, checksum_seed);
333                                 MD5_Update(&m5, seedbuf, 4);
334                         }
335                 }
336                 MD5_Final((uchar *)sum, &m5);
337                 break;
338           }
339           case CSUM_MD4:
340 #ifdef USE_OPENSSL
341           {
342                 MD4_CTX m4;
343                 MD4_Init(&m4);
344                 MD4_Update(&m4, (uchar *)buf, len);
345                 if (checksum_seed) {
346                         uchar seedbuf[4];
347                         SIVALu(seedbuf, 0, checksum_seed);
348                         MD4_Update(&m4, seedbuf, 4);
349                 }
350                 MD4_Final((uchar *)sum, &m4);
351                 break;
352           }
353 #endif
354           case CSUM_MD4_OLD:
355           case CSUM_MD4_BUSTED:
356           case CSUM_MD4_ARCHAIC: {
357                 md_context m;
358                 int32 i;
359                 static char *buf1;
360                 static int32 len1;
361
362                 mdfour_begin(&m);
363
364                 if (len > len1) {
365                         if (buf1)
366                                 free(buf1);
367                         buf1 = new_array(char, len+4);
368                         len1 = len;
369                         if (!buf1)
370                                 out_of_memory("get_checksum2");
371                 }
372
373                 memcpy(buf1, buf, len);
374                 if (checksum_seed) {
375                         SIVAL(buf1,len,checksum_seed);
376                         len += 4;
377                 }
378
379                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
380                         mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK);
381
382                 /*
383                  * Prior to version 27 an incorrect MD4 checksum was computed
384                  * by failing to call mdfour_tail() for block sizes that
385                  * are multiples of 64.  This is fixed by calling mdfour_update()
386                  * even when there are no more bytes.
387                  */
388                 if (len - i > 0 || xfersum_type > CSUM_MD4_BUSTED)
389                         mdfour_update(&m, (uchar *)(buf1+i), len-i);
390
391                 mdfour_result(&m, (uchar *)sum);
392                 break;
393           }
394 #ifdef SUPPORT_XXHASH
395           case CSUM_XXHASH: 
396                 SIVAL64(sum, 0, XXH64(buf, len, checksum_seed));
397                 break;
398 #endif
399           default: /* paranoia to prevent missing case values */
400                 exit_cleanup(RERR_UNSUPPORTED);
401         }
402 }
403
404 void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
405 {
406         struct map_struct *buf;
407         OFF_T i, len = st_p->st_size;
408         int32 remainder;
409         int fd;
410
411         memset(sum, 0, MAX_DIGEST_LEN);
412
413         fd = do_open(fname, O_RDONLY, 0);
414         if (fd == -1)
415                 return;
416
417         buf = map_file(fd, len, MAX_MAP_SIZE, CSUM_CHUNK);
418
419         switch (checksum_type) {
420           case CSUM_MD5: {
421                 MD5_CTX m5;
422
423                 MD5_Init(&m5);
424
425                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
426                         MD5_Update(&m5, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
427
428                 remainder = (int32)(len - i);
429                 if (remainder > 0)
430                         MD5_Update(&m5, (uchar *)map_ptr(buf, i, remainder), remainder);
431
432                 MD5_Final((uchar *)sum, &m5);
433                 break;
434           }
435           case CSUM_MD4:
436 #ifdef USE_OPENSSL
437           {
438                 MD4_CTX m4;
439
440                 MD4_Init(&m4);
441
442                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
443                         MD4_Update(&m4, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
444
445                 remainder = (int32)(len - i);
446                 if (remainder > 0)
447                         MD4_Update(&m4, (uchar *)map_ptr(buf, i, remainder), remainder);
448
449                 MD4_Final((uchar *)sum, &m4);
450                 break;
451           }
452 #endif
453           case CSUM_MD4_OLD:
454           case CSUM_MD4_BUSTED:
455           case CSUM_MD4_ARCHAIC: {
456                 md_context m;
457
458                 mdfour_begin(&m);
459
460                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK)
461                         mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
462
463                 /* Prior to version 27 an incorrect MD4 checksum was computed
464                  * by failing to call mdfour_tail() for block sizes that
465                  * are multiples of 64.  This is fixed by calling mdfour_update()
466                  * even when there are no more bytes. */
467                 remainder = (int32)(len - i);
468                 if (remainder > 0 || checksum_type > CSUM_MD4_BUSTED)
469                         mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
470
471                 mdfour_result(&m, (uchar *)sum);
472                 break;
473           }
474 #ifdef SUPPORT_XXHASH
475           case CSUM_XXHASH: {
476                 XXH64_state_t* state = XXH64_createState();
477                 if (state == NULL)
478                         out_of_memory("file_checksum xx64");
479
480                 if (XXH64_reset(state, 0) == XXH_ERROR) {
481                         rprintf(FERROR, "error resetting XXH64 seed");
482                         exit_cleanup(RERR_STREAMIO);
483                 }
484
485                 for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) {
486                         XXH_errorcode const updateResult =
487                             XXH64_update(state, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK);
488                         if (updateResult == XXH_ERROR) {
489                                 rprintf(FERROR, "error computing XX64 hash");
490                                 exit_cleanup(RERR_STREAMIO);
491                         }
492                 }
493                 remainder = (int32)(len - i);
494                 if (remainder > 0)
495                         XXH64_update(state, (uchar *)map_ptr(buf, i, CSUM_CHUNK), remainder);
496                 SIVAL64(sum, 0, XXH64_digest(state));
497
498                 XXH64_freeState(state);
499                 break;
500           }
501 #endif
502           default:
503                 rprintf(FERROR, "invalid checksum-choice for the --checksum option (%d)\n", checksum_type);
504                 exit_cleanup(RERR_UNSUPPORTED);
505         }
506
507         close(fd);
508         unmap_file(buf);
509 }
510
511 static int32 sumresidue;
512 static union {
513         md_context md;
514 #ifdef USE_OPENSSL
515         MD4_CTX m4;
516 #endif
517         MD5_CTX m5;
518 } ctx;
519 static int cursum_type;
520 #ifdef SUPPORT_XXHASH
521 XXH64_state_t* xxh64_state = NULL;
522 #endif
523
524 void sum_init(int csum_type, int seed)
525 {
526         char s[4];
527
528         if (csum_type < 0)
529                 csum_type = parse_csum_name(NULL, 0, 1);
530         cursum_type = csum_type;
531
532         switch (csum_type) {
533           case CSUM_MD5:
534                 MD5_Init(&ctx.m5);
535                 break;
536           case CSUM_MD4:
537 #ifdef USE_OPENSSL
538                 MD4_Init(&ctx.m4);
539 #else
540                 mdfour_begin(&ctx.md);
541                 sumresidue = 0;
542 #endif
543                 break;
544           case CSUM_MD4_OLD:
545           case CSUM_MD4_BUSTED:
546           case CSUM_MD4_ARCHAIC:
547                 mdfour_begin(&ctx.md);
548                 sumresidue = 0;
549                 SIVAL(s, 0, seed);
550                 sum_update(s, 4);
551                 break;
552 #ifdef SUPPORT_XXHASH
553           case CSUM_XXHASH:
554                 if (xxh64_state == NULL) {
555                         xxh64_state = XXH64_createState();
556                         if (xxh64_state == NULL)
557                                 out_of_memory("sum_init xxh64");
558                 }
559                 if (XXH64_reset(xxh64_state, 0) == XXH_ERROR) {
560                         rprintf(FERROR, "error resetting XXH64 state");
561                         exit_cleanup(RERR_STREAMIO);
562                 }
563                 break;
564 #endif
565           case CSUM_NONE:
566                 break;
567           default: /* paranoia to prevent missing case values */
568                 exit_cleanup(RERR_UNSUPPORTED);
569         }
570 }
571
572 /**
573  * Feed data into an MD4 accumulator, md.  The results may be
574  * retrieved using sum_end().  md is used for different purposes at
575  * different points during execution.
576  *
577  * @todo Perhaps get rid of md and just pass in the address each time.
578  * Very slightly clearer and slower.
579  **/
580 void sum_update(const char *p, int32 len)
581 {
582         switch (cursum_type) {
583           case CSUM_MD5:
584                 MD5_Update(&ctx.m5, (uchar *)p, len);
585                 break;
586           case CSUM_MD4:
587 #ifdef USE_OPENSSL
588                 MD4_Update(&ctx.m4, (uchar *)p, len);
589                 break;
590 #endif
591           case CSUM_MD4_OLD:
592           case CSUM_MD4_BUSTED:
593           case CSUM_MD4_ARCHAIC:
594                 if (len + sumresidue < CSUM_CHUNK) {
595                         memcpy(ctx.md.buffer + sumresidue, p, len);
596                         sumresidue += len;
597                         break;
598                 }
599
600                 if (sumresidue) {
601                         int32 i = CSUM_CHUNK - sumresidue;
602                         memcpy(ctx.md.buffer + sumresidue, p, i);
603                         mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, CSUM_CHUNK);
604                         len -= i;
605                         p += i;
606                 }
607
608                 while (len >= CSUM_CHUNK) {
609                         mdfour_update(&ctx.md, (uchar *)p, CSUM_CHUNK);
610                         len -= CSUM_CHUNK;
611                         p += CSUM_CHUNK;
612                 }
613
614                 sumresidue = len;
615                 if (sumresidue)
616                         memcpy(ctx.md.buffer, p, sumresidue);
617                 break;
618 #ifdef SUPPORT_XXHASH
619           case CSUM_XXHASH:
620                 if (XXH64_update(xxh64_state, p, len) == XXH_ERROR) {
621                         rprintf(FERROR, "error computing XX64 hash");
622                         exit_cleanup(RERR_STREAMIO);
623                 }
624                 break;
625 #endif
626           case CSUM_NONE:
627                 break;
628           default: /* paranoia to prevent missing case values */
629                 exit_cleanup(RERR_UNSUPPORTED);
630         }
631 }
632
633 /* NOTE: all the callers of sum_end() pass in a pointer to a buffer that is
634  * MAX_DIGEST_LEN in size, so even if the csum-len is shorter that that (i.e.
635  * CSUM_MD4_ARCHAIC), we don't have to worry about limiting the data we write
636  * into the "sum" buffer. */
637 int sum_end(char *sum)
638 {
639         switch (cursum_type) {
640           case CSUM_MD5:
641                 MD5_Final((uchar *)sum, &ctx.m5);
642                 break;
643           case CSUM_MD4:
644 #ifdef USE_OPENSSL
645                 MD4_Final((uchar *)sum, &ctx.m4);
646                 break;
647 #endif
648           case CSUM_MD4_OLD:
649                 mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
650                 mdfour_result(&ctx.md, (uchar *)sum);
651                 break;
652           case CSUM_MD4_BUSTED:
653           case CSUM_MD4_ARCHAIC:
654                 if (sumresidue)
655                         mdfour_update(&ctx.md, (uchar *)ctx.md.buffer, sumresidue);
656                 mdfour_result(&ctx.md, (uchar *)sum);
657                 break;
658 #ifdef SUPPORT_XXHASH
659           case CSUM_XXHASH:
660                 SIVAL64(sum, 0, XXH64_digest(xxh64_state));
661                 break;
662 #endif
663           case CSUM_NONE:
664                 *sum = '\0';
665                 break;
666           default: /* paranoia to prevent missing case values */
667                 exit_cleanup(RERR_UNSUPPORTED);
668         }
669
670         return csum_len_for_type(cursum_type, 0);
671 }