00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "rsync.h"
00023
00024
00025
00026
00027
00028
00029 static struct mdfour *m;
00030
00031 #define MASK32 (0xffffffff)
00032
00033 #define F(X,Y,Z) ((((X)&(Y)) | ((~(X))&(Z))))
00034 #define G(X,Y,Z) ((((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z))))
00035 #define H(X,Y,Z) (((X)^(Y)^(Z)))
00036 #define lshift(x,s) (((((x)<<(s))&MASK32) | (((x)>>(32-(s)))&MASK32)))
00037
00038 #define ROUND1(a,b,c,d,k,s) a = lshift((a + F(b,c,d) + M[k])&MASK32, s)
00039 #define ROUND2(a,b,c,d,k,s) a = lshift((a + G(b,c,d) + M[k] + 0x5A827999)&MASK32,s)
00040 #define ROUND3(a,b,c,d,k,s) a = lshift((a + H(b,c,d) + M[k] + 0x6ED9EBA1)&MASK32,s)
00041
00042
00043 static void mdfour64(uint32 *M)
00044 {
00045 uint32 AA, BB, CC, DD;
00046 uint32 A,B,C,D;
00047
00048 A = m->A; B = m->B; C = m->C; D = m->D;
00049 AA = A; BB = B; CC = C; DD = D;
00050
00051 ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
00052 ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
00053 ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
00054 ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
00055 ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
00056 ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
00057 ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
00058 ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
00059
00060
00061 ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
00062 ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
00063 ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
00064 ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
00065 ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
00066 ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
00067 ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
00068 ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
00069
00070 ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
00071 ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
00072 ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
00073 ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
00074 ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
00075 ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
00076 ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
00077 ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
00078
00079 A += AA; B += BB;
00080 C += CC; D += DD;
00081
00082 A &= MASK32; B &= MASK32;
00083 C &= MASK32; D &= MASK32;
00084
00085 m->A = A; m->B = B; m->C = C; m->D = D;
00086 }
00087
00088 static void copy64(uint32 *M, unsigned char *in)
00089 {
00090 int i;
00091
00092 for (i=0;i<16;i++)
00093 M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
00094 (in[i*4+1]<<8) | (in[i*4+0]<<0);
00095 }
00096
00097 static void copy4(unsigned char *out,uint32 x)
00098 {
00099 out[0] = x&0xFF;
00100 out[1] = (x>>8)&0xFF;
00101 out[2] = (x>>16)&0xFF;
00102 out[3] = (x>>24)&0xFF;
00103 }
00104
00105 void mdfour_begin(struct mdfour *md)
00106 {
00107 md->A = 0x67452301;
00108 md->B = 0xefcdab89;
00109 md->C = 0x98badcfe;
00110 md->D = 0x10325476;
00111 md->totalN = 0;
00112 md->totalN2 = 0;
00113 }
00114
00115
00116 static void mdfour_tail(unsigned char *in, uint32 n)
00117 {
00118 unsigned char buf[128];
00119 uint32 M[16];
00120 extern int protocol_version;
00121
00122
00123
00124
00125 m->totalN += n << 3;
00126 if (m->totalN < (n << 3)) {
00127 m->totalN2++;
00128 }
00129 m->totalN2 += n >> 29;
00130
00131 memset(buf, 0, 128);
00132 if (n) memcpy(buf, in, n);
00133 buf[n] = 0x80;
00134
00135 if (n <= 55) {
00136 copy4(buf+56, m->totalN);
00137
00138
00139
00140
00141
00142
00143 if (protocol_version >= 27) {
00144 copy4(buf+60, m->totalN2);
00145 }
00146 copy64(M, buf);
00147 mdfour64(M);
00148 } else {
00149 copy4(buf+120, m->totalN);
00150
00151
00152
00153
00154
00155
00156 if (protocol_version >= 27) {
00157 copy4(buf+124, m->totalN2);
00158 }
00159 copy64(M, buf);
00160 mdfour64(M);
00161 copy64(M, buf+64);
00162 mdfour64(M);
00163 }
00164 }
00165
00166 void mdfour_update(struct mdfour *md, unsigned char *in, uint32 n)
00167 {
00168 uint32 M[16];
00169
00170 m = md;
00171
00172 if (n == 0) mdfour_tail(in, n);
00173
00174 while (n >= 64) {
00175 copy64(M, in);
00176 mdfour64(M);
00177 in += 64;
00178 n -= 64;
00179 m->totalN += 64 << 3;
00180 if (m->totalN < 64 << 3) {
00181 m->totalN2++;
00182 }
00183 }
00184
00185 if (n) mdfour_tail(in, n);
00186 }
00187
00188
00189 void mdfour_result(struct mdfour *md, unsigned char *out)
00190 {
00191 m = md;
00192
00193 copy4(out, m->A);
00194 copy4(out+4, m->B);
00195 copy4(out+8, m->C);
00196 copy4(out+12, m->D);
00197 }
00198
00199
00200 void mdfour(unsigned char *out, unsigned char *in, int n)
00201 {
00202 struct mdfour md;
00203 mdfour_begin(&md);
00204 mdfour_update(&md, in, n);
00205 mdfour_result(&md, out);
00206 }
00207
00208 #ifdef TEST_MDFOUR
00209 int protocol_version = 28;
00210
00211 static void file_checksum1(char *fname)
00212 {
00213 int fd, i, was_multiple_of_64 = 1;
00214 struct mdfour md;
00215 unsigned char buf[64*1024], sum[16];
00216
00217 fd = open(fname,O_RDONLY);
00218 if (fd == -1) {
00219 perror("fname");
00220 exit(1);
00221 }
00222
00223 mdfour_begin(&md);
00224
00225 while (1) {
00226 int n = read(fd, buf, sizeof(buf));
00227 if (n <= 0)
00228 break;
00229 was_multiple_of_64 = !(n % 64);
00230 mdfour_update(&md, buf, n);
00231 }
00232 if (was_multiple_of_64 && protocol_version >= 27)
00233 mdfour_update(&md, buf, 0);
00234
00235 close(fd);
00236
00237 mdfour_result(&md, sum);
00238
00239 for (i=0;i<16;i++)
00240 printf("%02X", sum[i]);
00241 printf("\n");
00242 }
00243
00244 #if 0
00245 #include "../md4.h"
00246
00247 static void file_checksum2(char *fname)
00248 {
00249 int fd, i;
00250 MDstruct md;
00251 unsigned char buf[64], sum[16];
00252
00253 fd = open(fname,O_RDONLY);
00254 if (fd == -1) {
00255 perror("fname");
00256 exit(1);
00257 }
00258
00259 MDbegin(&md);
00260
00261 while (1) {
00262 int n = read(fd, buf, sizeof(buf));
00263 if (n <= 0) break;
00264 MDupdate(&md, buf, n*8);
00265 }
00266
00267 if (!md.done) {
00268 MDupdate(&md, buf, 0);
00269 }
00270
00271 close(fd);
00272
00273 memcpy(sum, md.buffer, 16);
00274
00275 for (i=0;i<16;i++)
00276 printf("%02X", sum[i]);
00277 printf("\n");
00278 }
00279 #endif
00280
00281 int main(int argc, char *argv[])
00282 {
00283 file_checksum1(argv[1]);
00284 #if 0
00285 file_checksum2(argv[1]);
00286 #endif
00287 return 0;
00288 }
00289 #endif