3 This code is from rfc1186.
7 ** ********************************************************************
8 ** md4.c -- Implementation of MD4 Message Digest Algorithm **
9 ** Updated: 2/16/90 by Ronald L. Rivest **
10 ** (C) 1990 RSA Data Security, Inc. **
11 ** ********************************************************************
16 ** -- Include md4.h in your program
17 ** -- Declare an MDstruct MD to hold the state of the digest
19 ** -- Initialize MD using MDbegin(&MD)
20 ** -- For each full block (64 bytes) X you wish to process, call
21 ** MDupdate(&MD,X,512)
22 ** (512 is the number of bits in a full block.)
23 ** -- For the last block (less than 64 bytes) you wish to process,
25 ** where n is the number of bits in the partial block. A partial
26 ** block terminates the computation, so every MD computation
27 ** should terminate by processing a partial block, even if it
29 ** -- The message digest is available in MD.buffer[0] ...
30 ** MD.buffer[3]. (Least-significant byte of each word
31 ** should be output first.)
32 ** -- You can print out the digest using MDprint(&MD)
35 /* Implementation notes:
36 ** This implementation assumes that ints are 32-bit quantities.
37 ** If the machine stores the least-significant byte of an int in the
38 ** least-addressed byte (e.g., VAX and 8086), then LOWBYTEFIRST
39 ** should be set to TRUE. Otherwise (e.g., SUNS), LOWBYTEFIRST
40 ** should be set to FALSE. Note that on machines with LOWBYTEFIRST
41 ** FALSE the routine MDupdate modifies has a side-effect on its input
42 ** array (the order of bytes in each word are reversed). If this is
43 ** undesired a call to MDreverse(X) can reverse the bytes of X back
44 ** into order after each call to MDupdate.
50 /* Compile-time includes
56 #define uchar unsigned char
57 #define int16 unsigned short
58 #define uint32 unsigned int
60 #include "byteorder.h"
62 /* Compile-time declarations of MD4 "magic constants".
64 #define I0 0x67452301 /* Initial values for MD buffer */
68 #define C2 013240474631 /* round 2 constant = sqrt(2) in octal */
69 #define C3 015666365641 /* round 3 constant = sqrt(3) in octal */
70 /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
71 ** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
75 #define fs1 3 /* round 1 shift amounts */
79 #define gs1 3 /* round 2 shift amounts */
83 #define hs1 3 /* round 3 shift amounts */
88 /* Compile-time macro declarations for MD4.
89 ** Note: The "rot" operator uses the variable "tmp".
90 ** It assumes tmp is declared as unsigned int, so that the >>
91 ** operator will shift in zeros rather than extending the sign bit.
93 #define f(X,Y,Z) ((X&Y) | ((~X)&Z))
94 #define g(X,Y,Z) ((X&Y) | (X&Z) | (Y&Z))
95 #define h(X,Y,Z) (X^Y^Z)
96 #define rot(X,S) (tmp=X,(tmp<<S) | (tmp>>(32-S)))
97 #define ff(A,B,C,D,i,s) A = rot((A + f(B,C,D) + X[i]),s)
98 #define gg(A,B,C,D,i,s) A = rot((A + g(B,C,D) + X[i] + C2),s)
99 #define hh(A,B,C,D,i,s) A = rot((A + h(B,C,D) + X[i] + C3),s)
102 ** Print message digest buffer MDp as 32 hexadecimal digits.
103 ** Order is from low-order byte of buffer[0] to high-order byte of
105 ** Each byte is printed with high-order hexadecimal digit first.
106 ** This is a user-callable routine.
114 printf("%02x",(MDp
->buffer
[i
]>>j
) & 0xFF);
118 ** Initialize message digest buffer MDp.
119 ** This is a user-callable routine.
129 for (i
=0;i
<8;i
++) MDp
->count
[i
] = 0;
134 ** Reverse the byte-ordering of every int in X.
135 ** Assumes X is an array of 16 ints.
136 ** The macro revx reverses the byte-ordering of the next word of X.
140 { register unsigned int t
;
141 register unsigned int i
;
143 for(i
= 0; i
< 16; i
++) {
150 ** Update message digest buffer MDp->buffer using 16-word data block X.
151 ** Assumes all 16 words of X are full of data.
152 ** Does not update MDp->count.
153 ** This routine is not user-callable.
160 register unsigned int tmp
, A
, B
, C
, D
;
166 /* Update the message digest buffer */
167 ff(A
, B
, C
, D
, 0 , fs1
); /* Round 1 */
168 ff(D
, A
, B
, C
, 1 , fs2
);
169 ff(C
, D
, A
, B
, 2 , fs3
);
170 ff(B
, C
, D
, A
, 3 , fs4
);
171 ff(A
, B
, C
, D
, 4 , fs1
);
172 ff(D
, A
, B
, C
, 5 , fs2
);
173 ff(C
, D
, A
, B
, 6 , fs3
);
174 ff(B
, C
, D
, A
, 7 , fs4
);
175 ff(A
, B
, C
, D
, 8 , fs1
);
176 ff(D
, A
, B
, C
, 9 , fs2
);
177 ff(C
, D
, A
, B
, 10 , fs3
);
178 ff(B
, C
, D
, A
, 11 , fs4
);
179 ff(A
, B
, C
, D
, 12 , fs1
);
180 ff(D
, A
, B
, C
, 13 , fs2
);
181 ff(C
, D
, A
, B
, 14 , fs3
);
182 ff(B
, C
, D
, A
, 15 , fs4
);
183 gg(A
, B
, C
, D
, 0 , gs1
); /* Round 2 */
184 gg(D
, A
, B
, C
, 4 , gs2
);
185 gg(C
, D
, A
, B
, 8 , gs3
);
186 gg(B
, C
, D
, A
, 12 , gs4
);
187 gg(A
, B
, C
, D
, 1 , gs1
);
188 gg(D
, A
, B
, C
, 5 , gs2
);
189 gg(C
, D
, A
, B
, 9 , gs3
);
190 gg(B
, C
, D
, A
, 13 , gs4
);
191 gg(A
, B
, C
, D
, 2 , gs1
);
192 gg(D
, A
, B
, C
, 6 , gs2
);
193 gg(C
, D
, A
, B
, 10 , gs3
);
194 gg(B
, C
, D
, A
, 14 , gs4
);
195 gg(A
, B
, C
, D
, 3 , gs1
);
196 gg(D
, A
, B
, C
, 7 , gs2
);
197 gg(C
, D
, A
, B
, 11 , gs3
);
198 gg(B
, C
, D
, A
, 15 , gs4
);
199 hh(A
, B
, C
, D
, 0 , hs1
); /* Round 3 */
200 hh(D
, A
, B
, C
, 8 , hs2
);
201 hh(C
, D
, A
, B
, 4 , hs3
);
202 hh(B
, C
, D
, A
, 12 , hs4
);
203 hh(A
, B
, C
, D
, 2 , hs1
);
204 hh(D
, A
, B
, C
, 10 , hs2
);
205 hh(C
, D
, A
, B
, 6 , hs3
);
206 hh(B
, C
, D
, A
, 14 , hs4
);
207 hh(A
, B
, C
, D
, 1 , hs1
);
208 hh(D
, A
, B
, C
, 9 , hs2
);
209 hh(C
, D
, A
, B
, 5 , hs3
);
210 hh(B
, C
, D
, A
, 13 , hs4
);
211 hh(A
, B
, C
, D
, 3 , hs1
);
212 hh(D
, A
, B
, C
, 11 , hs2
);
213 hh(C
, D
, A
, B
, 7 , hs3
);
214 hh(B
, C
, D
, A
, 15 , hs4
);
221 /* MDupdate(MDp,X,count)
222 ** Input: MDp -- an MDptr
223 ** X -- a pointer to an array of unsigned characters.
224 ** count -- the number of bits of X to use.
225 ** (if not a multiple of 8, uses high bits of last byte.)
226 ** Update MDp using the number of bits of X given by count.
227 ** This is the basic input routine for an MD4 user.
228 ** The routine completes the MD computation when count < 512, so
229 ** every MD computation should end with one call to MDupdate with a
230 ** count less than 512. A call with count 0 will be ignored if the
231 ** MD has already been terminated (done != 0), so an extra call with
232 ** count 0 can be given as a "courtesy close" to force termination
236 MDupdate(MDp
,X
,count
)
240 { unsigned int i
, tmp
, bit
, byte
, mask
;
241 unsigned char XX
[64];
243 /* return with no error if this is a courtesy close with count
244 ** zero and MDp->done is true.
246 if (count
== 0 && MDp
->done
) return;
247 /* check to see if MD is already done and report error */
249 { printf("\nError: MDupdate MD already done."); return; }
250 /* Add count to MDp->count */
260 { /* Full block of data to handle */
261 MDblock(MDp
,(unsigned int *)X
);
263 else if (count
> 512) /* Check for count too large */
264 { printf("\nError: MDupdate called with illegal count value %d."
268 else /* partial block -- must be last block so finish up */
269 { /* Find out how many bytes and residual bits there are */
272 /* Copy X into XX since we need to modify it */
273 for (i
=0;i
<=byte
;i
++) XX
[i
] = X
[i
];
274 for (i
=byte
+1;i
<64;i
++) XX
[i
] = 0;
275 /* Add padding '1' bit and low-order zeros in last byte */
276 mask
= 1 << (7 - bit
);
277 XX
[byte
] = (XX
[byte
] | mask
) & ~( mask
- 1);
278 /* If room for bit count, finish up with this block */
280 { for (i
=0;i
<8;i
++) XX
[56+i
] = MDp
->count
[i
];
281 MDblock(MDp
,(unsigned int *)XX
);
283 else /* need to do two blocks to finish up */
284 { MDblock(MDp
,(unsigned int *)XX
);
285 for (i
=0;i
<56;i
++) XX
[i
] = 0;
286 for (i
=0;i
<8;i
++) XX
[56+i
] = MDp
->count
[i
];
287 MDblock(MDp
,(unsigned int *)XX
);
289 /* Set flag saying we're done with MD computation */