librsync  2.3.4
checksum.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- the library for network deltas
4  *
5  * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6  * Copyright (C) 1996 by Andrew Tridgell
7  * Copyright (C) 1996 by Paul Mackerras
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 #include "config.h" /* IWYU pragma: keep */
25 #include <stdint.h>
26 #include "checksum.h"
27 #include "blake2.h"
28 #include "librsync_export.h"
29 
30 LIBRSYNC_EXPORT const int RS_MD4_SUM_LENGTH = 16;
31 LIBRSYNC_EXPORT const int RS_BLAKE2_SUM_LENGTH = 32;
32 
33 /** A simple 32bit checksum that can be incrementally updated. */
34 rs_weak_sum_t rs_calc_weak_sum(weaksum_kind_t kind, void const *buf, size_t len)
35 {
36  if (kind == RS_ROLLSUM) {
37  Rollsum sum;
38  RollsumInit(&sum);
39  RollsumUpdate(&sum, buf, len);
40  return RollsumDigest(&sum);
41  } else {
42  rabinkarp_t sum;
43  rabinkarp_init(&sum);
44  rabinkarp_update(&sum, buf, len);
45  return rabinkarp_digest(&sum);
46  }
47 }
48 
49 /** Calculate and store into SUM a strong checksum.
50  *
51  * In plain rsync, the checksum is perturbed by a seed value. This is used when
52  * retrying a failed transmission: we've discovered that the hashes collided at
53  * some point, so we're going to try again with different hashes to see if we
54  * can get it right. (Check tridge's thesis for details and to see if that's
55  * correct.)
56  *
57  * Since we can't retry I'm not sure if it's very useful for librsync, except
58  * perhaps as protection against hash collision attacks. */
59 void rs_calc_strong_sum(strongsum_kind_t kind, void const *buf, size_t len,
60  rs_strong_sum_t *sum)
61 {
62  if (kind == RS_MD4) {
63  rs_mdfour((unsigned char *)sum, buf, len);
64  } else {
65  blake2b_state ctx;
66  blake2b_init(&ctx, RS_MAX_STRONG_SUM_LENGTH);
67  blake2b_update(&ctx, (const uint8_t *)buf, len);
68  blake2b_final(&ctx, (uint8_t *)sum, RS_MAX_STRONG_SUM_LENGTH);
69  }
70 }
void rs_calc_strong_sum(strongsum_kind_t kind, void const *buf, size_t len, rs_strong_sum_t *sum)
Calculate a strongsum.
Definition: checksum.c:59
rs_weak_sum_t rs_calc_weak_sum(weaksum_kind_t kind, void const *buf, size_t len)
Calculate a weaksum.
Definition: checksum.c:34
The rs_mdfour state type.
Definition: mdfour.h:46
strongsum_kind_t
Strongsum implementations.
Definition: checksum.h:41
The Rollsum state type.
Definition: rollsum.h:36
weaksum_kind_t
Weaksum implementations.
Definition: checksum.h:35
Abstract wrappers around different weaksum and strongsum implementations.
The rabinkarp_t state type.
Definition: rabinkarp.h:56