annotate lib/swig/swigwin-2.0.11/CCache/mdfour.c @ 2222:66bd74aa9f5e

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