Remove statement in rsync.1 that a rsync:// URL can only be used if
[rsync/rsync.git] / lib / mdfour.c
CommitLineData
8de330a3
AT
1/*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 a implementation of MD4 designed for use in the SMB authentication protocol
5 Copyright (C) Andrew Tridgell 1997-1998.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include "rsync.h"
23
24/* NOTE: This code makes no attempt to be fast!
25
26 It assumes that a int is at least 32 bits long
27*/
28
29static struct mdfour *m;
30
f3737e06 31static inline uint32 F(uint32 X, uint32 Y, uint32 Z)
8de330a3
AT
32{
33 return (X&Y) | ((~X)&Z);
34}
35
f3737e06 36static inline uint32 G(uint32 X, uint32 Y, uint32 Z)
8de330a3
AT
37{
38 return (X&Y) | (X&Z) | (Y&Z);
39}
40
f3737e06 41static inline uint32 H(uint32 X, uint32 Y, uint32 Z)
8de330a3
AT
42{
43 return X^Y^Z;
44}
45
f3737e06 46static inline uint32 lshift(uint32 x, int s)
8de330a3 47{
f3737e06 48#ifdef LARGE_INT32
8de330a3
AT
49 x &= 0xFFFFFFFF;
50 return ((x<<s)&0xFFFFFFFF) | (x>>(32-s));
f3737e06
AT
51#else
52 return ((x<<s) | (x>>(32-s)));
53#endif
8de330a3
AT
54}
55
56#define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
f3737e06
AT
57#define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)
58#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)
8de330a3
AT
59
60/* this applies md4 to 64 byte chunks */
61static void mdfour64(uint32 *M)
62{
63 int j;
64 uint32 AA, BB, CC, DD;
65 uint32 X[16];
66 uint32 A,B,C,D;
67
68 for (j=0;j<16;j++)
69 X[j] = M[j];
70
71 A = m->A; B = m->B; C = m->C; D = m->D;
72 AA = A; BB = B; CC = C; DD = D;
73
74 ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
75 ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
76 ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
77 ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
78 ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
79 ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
80 ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
81 ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
82
83 ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
84 ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
85 ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
86 ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
87 ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
88 ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
89 ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
90 ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
91
92 ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
93 ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
94 ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
95 ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
96 ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
97 ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
98 ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
99 ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
100
101 A += AA; B += BB; C += CC; D += DD;
102
f3737e06 103#ifdef LARGE_INT32
8de330a3
AT
104 A &= 0xFFFFFFFF; B &= 0xFFFFFFFF;
105 C &= 0xFFFFFFFF; D &= 0xFFFFFFFF;
f3737e06 106#endif
8de330a3
AT
107
108 for (j=0;j<16;j++)
109 X[j] = 0;
110
111 m->A = A; m->B = B; m->C = C; m->D = D;
112}
113
114static void copy64(uint32 *M, unsigned char *in)
115{
116 int i;
117
118 for (i=0;i<16;i++)
119 M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
120 (in[i*4+1]<<8) | (in[i*4+0]<<0);
121}
122
123static void copy4(unsigned char *out,uint32 x)
124{
125 out[0] = x&0xFF;
126 out[1] = (x>>8)&0xFF;
127 out[2] = (x>>16)&0xFF;
128 out[3] = (x>>24)&0xFF;
129}
130
131void mdfour_begin(struct mdfour *md)
132{
133 md->A = 0x67452301;
134 md->B = 0xefcdab89;
135 md->C = 0x98badcfe;
136 md->D = 0x10325476;
137 md->totalN = 0;
138}
139
140
141static void mdfour_tail(unsigned char *in, int n)
142{
143 unsigned char buf[128];
144 uint32 M[16];
145 uint32 b;
146
147 m->totalN += n;
148
149 b = m->totalN * 8;
150
151 memset(buf, 0, 128);
152 if (n) memcpy(buf, in, n);
153 buf[n] = 0x80;
154
155 if (n <= 55) {
156 copy4(buf+56, b);
157 copy64(M, buf);
158 mdfour64(M);
159 } else {
160 copy4(buf+120, b);
161 copy64(M, buf);
162 mdfour64(M);
163 copy64(M, buf+64);
164 mdfour64(M);
165 }
8de330a3
AT
166}
167
168void mdfour_update(struct mdfour *md, unsigned char *in, int n)
169{
170 uint32 M[16];
171
172 if (n == 0) mdfour_tail(in, n);
173
174 m = md;
175
176 while (n >= 64) {
177 copy64(M, in);
178 mdfour64(M);
179 in += 64;
180 n -= 64;
181 m->totalN += 64;
182 }
183
184 if (n) mdfour_tail(in, n);
185}
186
187
188void mdfour_result(struct mdfour *md, unsigned char *out)
189{
190 m = md;
191
192 copy4(out, m->A);
193 copy4(out+4, m->B);
194 copy4(out+8, m->C);
195 copy4(out+12, m->D);
196}
197
198
199void mdfour(unsigned char *out, unsigned char *in, int n)
200{
201 struct mdfour md;
202 mdfour_begin(&md);
203 mdfour_update(&md, in, n);
204 mdfour_result(&md, out);
205}
206
207#ifdef TEST_MDFOUR
208static void file_checksum1(char *fname)
209{
210 int fd, i;
211 struct mdfour md;
212 unsigned char buf[64], sum[16];
213
214 fd = open(fname,O_RDONLY);
215 if (fd == -1) {
216 perror("fname");
217 exit(1);
218 }
219
220 mdfour_begin(&md);
221
222 while (1) {
223 int n = read(fd, buf, sizeof(buf));
224 if (n <= 0) break;
225 mdfour_update(&md, buf, n);
226 }
227
228 close(fd);
229
230 mdfour_result(&md, sum);
231
232 for (i=0;i<16;i++)
233 printf("%02X", sum[i]);
234 printf("\n");
235}
236
237#include "../md4.h"
238
239static void file_checksum2(char *fname)
240{
241 int fd, i;
242 MDstruct md;
243 unsigned char buf[64], sum[16];
244
245 fd = open(fname,O_RDONLY);
246 if (fd == -1) {
247 perror("fname");
248 exit(1);
249 }
250
251 MDbegin(&md);
252
253 while (1) {
254 int n = read(fd, buf, sizeof(buf));
255 if (n <= 0) break;
256 MDupdate(&md, buf, n*8);
257 }
258
259 if (!md.done) {
260 MDupdate(&md, buf, 0);
261 }
262
263 close(fd);
264
265 memcpy(sum, md.buffer, 16);
266
267 for (i=0;i<16;i++)
268 printf("%02X", sum[i]);
269 printf("\n");
270}
271
272 int main(int argc, char *argv[])
273{
274 file_checksum1(argv[1]);
275 file_checksum2(argv[1]);
276 return 0;
277}
278#endif