fixed md4 on 64 bit boxes
[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
a7f8404e
AT
31typedef unsigned long long uint64;
32
33#define MASK32 (0xffffffff)
34
35#define F(X,Y,Z) ((((X)&(Y)) | ((~(X))&(Z))))
36#define G(X,Y,Z) ((((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z))))
37#define H(X,Y,Z) (((X)^(Y)^(Z)))
38#define lshift(x,s) (((((x)<<(s))&MASK32) | (((x)>>(32-(s)))&MASK32)))
39
40#define ROUND1(a,b,c,d,k,s) a = lshift((a + F(b,c,d) + M[k])&MASK32, s)
41#define ROUND2(a,b,c,d,k,s) a = lshift((a + G(b,c,d) + M[k] + 0x5A827999)&MASK32,s)
42#define ROUND3(a,b,c,d,k,s) a = lshift((a + H(b,c,d) + M[k] + 0x6ED9EBA1)&MASK32,s)
8de330a3
AT
43
44/* this applies md4 to 64 byte chunks */
45static void mdfour64(uint32 *M)
46{
8de330a3 47 uint32 AA, BB, CC, DD;
8de330a3
AT
48 uint32 A,B,C,D;
49
8de330a3
AT
50 A = m->A; B = m->B; C = m->C; D = m->D;
51 AA = A; BB = B; CC = C; DD = D;
52
53 ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
54 ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
55 ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
56 ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
57 ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
58 ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
59 ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
60 ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
61
a7f8404e 62
8de330a3
AT
63 ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
64 ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
65 ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
66 ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
67 ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
68 ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
69 ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
70 ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
71
72 ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
73 ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
74 ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
75 ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
76 ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
77 ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
78 ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
79 ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
80
a7f8404e
AT
81 A += AA; B += BB;
82 C += CC; D += DD;
8de330a3 83
a7f8404e
AT
84 A &= MASK32; B &= MASK32;
85 C &= MASK32; D &= MASK32;
8de330a3
AT
86
87 m->A = A; m->B = B; m->C = C; m->D = D;
88}
89
90static void copy64(uint32 *M, unsigned char *in)
91{
92 int i;
93
94 for (i=0;i<16;i++)
95 M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
96 (in[i*4+1]<<8) | (in[i*4+0]<<0);
97}
98
99static void copy4(unsigned char *out,uint32 x)
100{
101 out[0] = x&0xFF;
102 out[1] = (x>>8)&0xFF;
103 out[2] = (x>>16)&0xFF;
104 out[3] = (x>>24)&0xFF;
105}
106
107void mdfour_begin(struct mdfour *md)
108{
109 md->A = 0x67452301;
110 md->B = 0xefcdab89;
111 md->C = 0x98badcfe;
112 md->D = 0x10325476;
113 md->totalN = 0;
114}
115
116
117static void mdfour_tail(unsigned char *in, int n)
118{
119 unsigned char buf[128];
120 uint32 M[16];
121 uint32 b;
122
123 m->totalN += n;
124
125 b = m->totalN * 8;
126
127 memset(buf, 0, 128);
128 if (n) memcpy(buf, in, n);
129 buf[n] = 0x80;
130
131 if (n <= 55) {
132 copy4(buf+56, b);
133 copy64(M, buf);
134 mdfour64(M);
135 } else {
136 copy4(buf+120, b);
137 copy64(M, buf);
138 mdfour64(M);
139 copy64(M, buf+64);
140 mdfour64(M);
141 }
8de330a3
AT
142}
143
144void mdfour_update(struct mdfour *md, unsigned char *in, int n)
145{
146 uint32 M[16];
147
148 if (n == 0) mdfour_tail(in, n);
149
150 m = md;
151
152 while (n >= 64) {
153 copy64(M, in);
154 mdfour64(M);
155 in += 64;
156 n -= 64;
157 m->totalN += 64;
158 }
159
160 if (n) mdfour_tail(in, n);
161}
162
163
164void mdfour_result(struct mdfour *md, unsigned char *out)
165{
166 m = md;
167
168 copy4(out, m->A);
169 copy4(out+4, m->B);
170 copy4(out+8, m->C);
171 copy4(out+12, m->D);
172}
173
174
175void mdfour(unsigned char *out, unsigned char *in, int n)
176{
177 struct mdfour md;
178 mdfour_begin(&md);
179 mdfour_update(&md, in, n);
180 mdfour_result(&md, out);
181}
182
183#ifdef TEST_MDFOUR
184static void file_checksum1(char *fname)
185{
186 int fd, i;
187 struct mdfour md;
2fb27e91
AT
188 unsigned char buf[64*1024], sum[16];
189
8de330a3
AT
190 fd = open(fname,O_RDONLY);
191 if (fd == -1) {
192 perror("fname");
193 exit(1);
194 }
195
196 mdfour_begin(&md);
197
198 while (1) {
199 int n = read(fd, buf, sizeof(buf));
200 if (n <= 0) break;
201 mdfour_update(&md, buf, n);
202 }
203
204 close(fd);
205
206 mdfour_result(&md, sum);
207
208 for (i=0;i<16;i++)
209 printf("%02X", sum[i]);
210 printf("\n");
211}
212
2fb27e91 213#if 0
8de330a3
AT
214#include "../md4.h"
215
216static void file_checksum2(char *fname)
217{
218 int fd, i;
219 MDstruct md;
220 unsigned char buf[64], sum[16];
221
222 fd = open(fname,O_RDONLY);
223 if (fd == -1) {
224 perror("fname");
225 exit(1);
226 }
227
228 MDbegin(&md);
229
230 while (1) {
231 int n = read(fd, buf, sizeof(buf));
232 if (n <= 0) break;
233 MDupdate(&md, buf, n*8);
234 }
235
236 if (!md.done) {
237 MDupdate(&md, buf, 0);
238 }
239
240 close(fd);
241
242 memcpy(sum, md.buffer, 16);
243
244 for (i=0;i<16;i++)
245 printf("%02X", sum[i]);
246 printf("\n");
247}
2fb27e91 248#endif
8de330a3
AT
249
250 int main(int argc, char *argv[])
251{
252 file_checksum1(argv[1]);
2fb27e91 253#if 0
8de330a3 254 file_checksum2(argv[1]);
2fb27e91 255#endif
8de330a3
AT
256 return 0;
257}
258#endif