Commit | Line | Data |
---|---|---|
0d7638ea | 1 | /* |
0f78b815 WD |
2 | * Routines to support checksumming of bytes. |
3 | * | |
4 | * Copyright (C) 1996 Andrew Tridgell | |
5 | * Copyright (C) 1996 Paul Mackerras | |
ba2133d6 | 6 | * Copyright (C) 2004-2007 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
4fd842f9 | 9 | * it under the terms of the GNU General Public License version 3 as |
ba2133d6 | 10 | * published by the Free Software Foundation. |
0f78b815 WD |
11 | * |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
e7c67065 | 17 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 18 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 19 | */ |
c627d613 AT |
20 | |
21 | #include "rsync.h" | |
22 | ||
8ee6eb71 | 23 | extern int checksum_seed; |
d04e9c51 | 24 | extern int protocol_version; |
c627d613 | 25 | |
a0456b9c WD |
26 | int csum_length = SHORT_SUM_LENGTH; /* initial value */ |
27 | ||
c627d613 AT |
28 | /* |
29 | a simple 32 bit checksum that can be upadted from either end | |
30 | (inspired by Mark Adler's Adler-32 checksum) | |
31 | */ | |
deb5bf1d | 32 | uint32 get_checksum1(char *buf1, int32 len) |
c627d613 | 33 | { |
deb5bf1d | 34 | int32 i; |
c627d613 | 35 | uint32 s1, s2; |
debb4505 | 36 | schar *buf = (schar *)buf1; |
c627d613 AT |
37 | |
38 | s1 = s2 = 0; | |
34ccb63e | 39 | for (i = 0; i < (len-4); i+=4) { |
0d7638ea | 40 | s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + |
1b01b295 | 41 | 10*CHAR_OFFSET; |
0d7638ea | 42 | s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET); |
34ccb63e AT |
43 | } |
44 | for (; i < len; i++) { | |
1b01b295 | 45 | s1 += (buf[i]+CHAR_OFFSET); s2 += s1; |
c627d613 AT |
46 | } |
47 | return (s1 & 0xffff) + (s2 << 16); | |
48 | } | |
49 | ||
50 | ||
deb5bf1d | 51 | void get_checksum2(char *buf, int32 len, char *sum) |
c627d613 | 52 | { |
a0456b9c WD |
53 | md_context m; |
54 | ||
55 | if (protocol_version >= 30) { | |
56 | uchar seedbuf[4]; | |
57 | md5_begin(&m); | |
161fba69 | 58 | md5_update(&m, (uchar *)buf, len); |
a0456b9c WD |
59 | if (checksum_seed) { |
60 | SIVAL(seedbuf, 0, checksum_seed); | |
61 | md5_update(&m, seedbuf, 4); | |
62 | } | |
a0456b9c WD |
63 | md5_result(&m, (uchar *)sum); |
64 | } else { | |
65 | int32 i; | |
66 | static char *buf1; | |
67 | static int32 len1; | |
68 | ||
69 | mdfour_begin(&m); | |
70 | ||
71 | if (len > len1) { | |
72 | if (buf1) | |
73 | free(buf1); | |
74 | buf1 = new_array(char, len+4); | |
75 | len1 = len; | |
76 | if (!buf1) | |
77 | out_of_memory("get_checksum2"); | |
78 | } | |
79 | ||
80 | memcpy(buf1, buf, len); | |
81 | if (checksum_seed) { | |
82 | SIVAL(buf1,len,checksum_seed); | |
83 | len += 4; | |
84 | } | |
85 | ||
86 | for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) | |
87 | mdfour_update(&m, (uchar *)(buf1+i), CSUM_CHUNK); | |
88 | ||
89 | /* | |
90 | * Prior to version 27 an incorrect MD4 checksum was computed | |
91 | * by failing to call mdfour_tail() for block sizes that | |
92 | * are multiples of 64. This is fixed by calling mdfour_update() | |
93 | * even when there are no more bytes. | |
94 | */ | |
95 | if (len - i > 0 || protocol_version >= 27) | |
96 | mdfour_update(&m, (uchar *)(buf1+i), len-i); | |
97 | ||
98 | mdfour_result(&m, (uchar *)sum); | |
8de330a3 | 99 | } |
c627d613 AT |
100 | } |
101 | ||
a0456b9c | 102 | void file_checksum(char *fname, char *sum, OFF_T size) |
c627d613 | 103 | { |
8de330a3 | 104 | struct map_struct *buf; |
26404276 | 105 | OFF_T i, len = size; |
a0456b9c | 106 | md_context m; |
26404276 WD |
107 | int32 remainder; |
108 | int fd; | |
0d7638ea | 109 | |
a0456b9c | 110 | memset(sum, 0, MAX_DIGEST_LEN); |
0d7638ea | 111 | |
8c9fd200 | 112 | fd = do_open(fname, O_RDONLY, 0); |
efa95a18 WD |
113 | if (fd == -1) |
114 | return; | |
0d7638ea | 115 | |
bd1a581b | 116 | buf = map_file(fd, size, MAX_MAP_SIZE, CSUM_CHUNK); |
0d7638ea | 117 | |
a0456b9c WD |
118 | if (protocol_version >= 30) { |
119 | md5_begin(&m); | |
8de330a3 | 120 | |
a0456b9c WD |
121 | for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { |
122 | md5_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), | |
123 | CSUM_CHUNK); | |
124 | } | |
8de330a3 | 125 | |
a0456b9c WD |
126 | remainder = (int32)(len - i); |
127 | if (remainder > 0) | |
128 | md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder); | |
8de330a3 | 129 | |
a0456b9c WD |
130 | md5_result(&m, (uchar *)sum); |
131 | } else { | |
132 | mdfour_begin(&m); | |
133 | ||
134 | for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { | |
135 | mdfour_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), | |
136 | CSUM_CHUNK); | |
137 | } | |
138 | ||
139 | /* Prior to version 27 an incorrect MD4 checksum was computed | |
140 | * by failing to call mdfour_tail() for block sizes that | |
141 | * are multiples of 64. This is fixed by calling mdfour_update() | |
142 | * even when there are no more bytes. */ | |
143 | remainder = (int32)(len - i); | |
144 | if (remainder > 0 || protocol_version >= 27) | |
145 | mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder); | |
146 | ||
147 | mdfour_result(&m, (uchar *)sum); | |
148 | } | |
8de330a3 AT |
149 | |
150 | close(fd); | |
151 | unmap_file(buf); | |
c627d613 | 152 | } |
34ccb63e | 153 | |
deb5bf1d | 154 | static int32 sumresidue; |
a0456b9c | 155 | static md_context md; |
9e31c482 | 156 | |
ba582f75 | 157 | void sum_init(int seed) |
9e31c482 | 158 | { |
8de330a3 | 159 | char s[4]; |
a0456b9c WD |
160 | |
161 | if (protocol_version >= 30) | |
162 | md5_begin(&md); | |
163 | else { | |
164 | mdfour_begin(&md); | |
165 | sumresidue = 0; | |
166 | } | |
ba582f75 WD |
167 | SIVAL(s, 0, seed); |
168 | sum_update(s, 4); | |
9e31c482 AT |
169 | } |
170 | ||
d37d8d7b MP |
171 | /** |
172 | * Feed data into an MD4 accumulator, md. The results may be | |
173 | * retrieved using sum_end(). md is used for different purposes at | |
174 | * different points during execution. | |
175 | * | |
176 | * @todo Perhaps get rid of md and just pass in the address each time. | |
177 | * Very slightly clearer and slower. | |
178 | **/ | |
4a19c3b2 | 179 | void sum_update(const char *p, int32 len) |
9e31c482 | 180 | { |
a0456b9c WD |
181 | if (protocol_version >= 30) { |
182 | md5_update(&md, (uchar *)p, len); | |
183 | return; | |
184 | } | |
185 | ||
8de330a3 | 186 | if (len + sumresidue < CSUM_CHUNK) { |
a0456b9c | 187 | memcpy(md.buffer + sumresidue, p, len); |
8de330a3 AT |
188 | sumresidue += len; |
189 | return; | |
190 | } | |
191 | ||
192 | if (sumresidue) { | |
deb5bf1d | 193 | int32 i = CSUM_CHUNK - sumresidue; |
a0456b9c WD |
194 | memcpy(md.buffer + sumresidue, p, i); |
195 | mdfour_update(&md, (uchar *)md.buffer, CSUM_CHUNK); | |
8de330a3 AT |
196 | len -= i; |
197 | p += i; | |
198 | } | |
199 | ||
deb5bf1d WD |
200 | while (len >= CSUM_CHUNK) { |
201 | mdfour_update(&md, (uchar *)p, CSUM_CHUNK); | |
202 | len -= CSUM_CHUNK; | |
203 | p += CSUM_CHUNK; | |
8de330a3 AT |
204 | } |
205 | ||
deb5bf1d WD |
206 | sumresidue = len; |
207 | if (sumresidue) | |
a0456b9c | 208 | memcpy(md.buffer, p, sumresidue); |
9e31c482 AT |
209 | } |
210 | ||
a0456b9c | 211 | int sum_end(char *sum) |
9e31c482 | 212 | { |
a0456b9c WD |
213 | if (protocol_version >= 30) { |
214 | md5_result(&md, (uchar *)sum); | |
215 | return MD5_DIGEST_LEN; | |
216 | } | |
217 | ||
deb5bf1d | 218 | if (sumresidue || protocol_version >= 27) |
a0456b9c | 219 | mdfour_update(&md, (uchar *)md.buffer, sumresidue); |
9e31c482 | 220 | |
7ae359c3 | 221 | mdfour_result(&md, (uchar *)sum); |
a0456b9c WD |
222 | |
223 | return MD4_DIGEST_LEN; | |
9e31c482 | 224 | } |