librsync  2.0.1
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"
25 
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 
30 #include "librsync.h"
31 #include "checksum.h"
32 #include "rollsum.h"
33 #include "blake2.h"
34 
35 
36 /* A simple 32bit checksum that can be incrementally updated. */
37 rs_weak_sum_t rs_calc_weak_sum(void const *buf, size_t len)
38 {
39  Rollsum sum;
40 
41  RollsumInit(&sum);
42  RollsumUpdate(&sum, buf, len);
43  return RollsumDigest(&sum);
44 }
45 
46 
47 /**
48  * Calculate and store into SUM a strong MD4 checksum of the file
49  * blocks seen so far.
50  *
51  * In plain rsync, the checksum is perturbed by a seed value. This is
52  * used when retrying a failed transmission: we've discovered that the
53  * hashes collided at some point, so we're going to try again with
54  * different hashes to see if we can get it right. (Check tridge's
55  * thesis for details and to see if that's correct.)
56  *
57  * Since we can't retry a web transaction I'm not sure if it's very
58  * useful in rproxy.
59  */
60 void rs_calc_md4_sum(void const *buf, size_t len, rs_strong_sum_t *sum)
61 {
62  rs_mdfour((unsigned char *) sum, buf, len);
63 }
64 
65 void rs_calc_blake2_sum(void const *buf, size_t len, rs_strong_sum_t *sum)
66 {
67  blake2b_state ctx;
68  blake2b_init(&ctx, RS_MAX_STRONG_SUM_LENGTH);
69  blake2b_update(&ctx, (const uint8_t *)buf, len);
70  blake2b_final(&ctx, (uint8_t *)sum, RS_MAX_STRONG_SUM_LENGTH);
71 }
Public header for librsync.