1 package org.bouncycastle.crypto.digests;
4 * implementation of MD4 as RFC 1320 by R. Rivest, MIT Laboratory for
5 * Computer Science and RSA Data Security, Inc.
7 * <b>NOTE</b>: This algorithm is only included for backwards compatability
8 * with legacy applications, it's not secure, don't use it for anything new!
10 public class MD4Digest
13 private static final int DIGEST_LENGTH = 16;
15 private int H1, H2, H3, H4; // IV's
17 private int[] X = new int[16];
21 * Standard constructor
29 * Copy constructor. This will copy the state of the provided
32 public MD4Digest(MD4Digest t)
41 System.arraycopy(t.X, 0, X, 0, t.X.length);
45 public String getAlgorithmName()
50 public int getDigestSize()
55 protected void processWord(
59 X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
60 | ((in[inOff + 2] & 0xff) << 16) | ((in[inOff + 3] & 0xff) << 24);
68 protected void processLength(
76 X[14] = (int)(bitLength & 0xffffffff);
77 X[15] = (int)(bitLength >>> 32);
80 private void unpackWord(
85 out[outOff] = (byte)word;
86 out[outOff + 1] = (byte)(word >>> 8);
87 out[outOff + 2] = (byte)(word >>> 16);
88 out[outOff + 3] = (byte)(word >>> 24);
97 unpackWord(H1, out, outOff);
98 unpackWord(H2, out, outOff + 4);
99 unpackWord(H3, out, outOff + 8);
100 unpackWord(H4, out, outOff + 12);
104 return DIGEST_LENGTH;
108 * reset the chaining variables to the IV values.
121 for (int i = 0; i != X.length; i++)
128 // round 1 left rotates
130 private static final int S11 = 3;
131 private static final int S12 = 7;
132 private static final int S13 = 11;
133 private static final int S14 = 19;
136 // round 2 left rotates
138 private static final int S21 = 3;
139 private static final int S22 = 5;
140 private static final int S23 = 9;
141 private static final int S24 = 13;
144 // round 3 left rotates
146 private static final int S31 = 3;
147 private static final int S32 = 9;
148 private static final int S33 = 11;
149 private static final int S34 = 15;
152 * rotate int x left n bits.
154 private int rotateLeft(
158 return (x << n) | (x >>> (32 - n));
162 * F, G, H and I are the basic MD4 functions.
169 return (u & v) | (~u & w);
177 return (u & v) | (u & w) | (v & w);
188 protected void processBlock()
196 // Round 1 - F cycle, 16 times.
198 a = rotateLeft((a + F(b, c, d) + X[ 0]), S11);
199 d = rotateLeft((d + F(a, b, c) + X[ 1]), S12);
200 c = rotateLeft((c + F(d, a, b) + X[ 2]), S13);
201 b = rotateLeft((b + F(c, d, a) + X[ 3]), S14);
202 a = rotateLeft((a + F(b, c, d) + X[ 4]), S11);
203 d = rotateLeft((d + F(a, b, c) + X[ 5]), S12);
204 c = rotateLeft((c + F(d, a, b) + X[ 6]), S13);
205 b = rotateLeft((b + F(c, d, a) + X[ 7]), S14);
206 a = rotateLeft((a + F(b, c, d) + X[ 8]), S11);
207 d = rotateLeft((d + F(a, b, c) + X[ 9]), S12);
208 c = rotateLeft((c + F(d, a, b) + X[10]), S13);
209 b = rotateLeft((b + F(c, d, a) + X[11]), S14);
210 a = rotateLeft((a + F(b, c, d) + X[12]), S11);
211 d = rotateLeft((d + F(a, b, c) + X[13]), S12);
212 c = rotateLeft((c + F(d, a, b) + X[14]), S13);
213 b = rotateLeft((b + F(c, d, a) + X[15]), S14);
216 // Round 2 - G cycle, 16 times.
218 a = rotateLeft((a + G(b, c, d) + X[ 0] + 0x5a827999), S21);
219 d = rotateLeft((d + G(a, b, c) + X[ 4] + 0x5a827999), S22);
220 c = rotateLeft((c + G(d, a, b) + X[ 8] + 0x5a827999), S23);
221 b = rotateLeft((b + G(c, d, a) + X[12] + 0x5a827999), S24);
222 a = rotateLeft((a + G(b, c, d) + X[ 1] + 0x5a827999), S21);
223 d = rotateLeft((d + G(a, b, c) + X[ 5] + 0x5a827999), S22);
224 c = rotateLeft((c + G(d, a, b) + X[ 9] + 0x5a827999), S23);
225 b = rotateLeft((b + G(c, d, a) + X[13] + 0x5a827999), S24);
226 a = rotateLeft((a + G(b, c, d) + X[ 2] + 0x5a827999), S21);
227 d = rotateLeft((d + G(a, b, c) + X[ 6] + 0x5a827999), S22);
228 c = rotateLeft((c + G(d, a, b) + X[10] + 0x5a827999), S23);
229 b = rotateLeft((b + G(c, d, a) + X[14] + 0x5a827999), S24);
230 a = rotateLeft((a + G(b, c, d) + X[ 3] + 0x5a827999), S21);
231 d = rotateLeft((d + G(a, b, c) + X[ 7] + 0x5a827999), S22);
232 c = rotateLeft((c + G(d, a, b) + X[11] + 0x5a827999), S23);
233 b = rotateLeft((b + G(c, d, a) + X[15] + 0x5a827999), S24);
236 // Round 3 - H cycle, 16 times.
238 a = rotateLeft((a + H(b, c, d) + X[ 0] + 0x6ed9eba1), S31);
239 d = rotateLeft((d + H(a, b, c) + X[ 8] + 0x6ed9eba1), S32);
240 c = rotateLeft((c + H(d, a, b) + X[ 4] + 0x6ed9eba1), S33);
241 b = rotateLeft((b + H(c, d, a) + X[12] + 0x6ed9eba1), S34);
242 a = rotateLeft((a + H(b, c, d) + X[ 2] + 0x6ed9eba1), S31);
243 d = rotateLeft((d + H(a, b, c) + X[10] + 0x6ed9eba1), S32);
244 c = rotateLeft((c + H(d, a, b) + X[ 6] + 0x6ed9eba1), S33);
245 b = rotateLeft((b + H(c, d, a) + X[14] + 0x6ed9eba1), S34);
246 a = rotateLeft((a + H(b, c, d) + X[ 1] + 0x6ed9eba1), S31);
247 d = rotateLeft((d + H(a, b, c) + X[ 9] + 0x6ed9eba1), S32);
248 c = rotateLeft((c + H(d, a, b) + X[ 5] + 0x6ed9eba1), S33);
249 b = rotateLeft((b + H(c, d, a) + X[13] + 0x6ed9eba1), S34);
250 a = rotateLeft((a + H(b, c, d) + X[ 3] + 0x6ed9eba1), S31);
251 d = rotateLeft((d + H(a, b, c) + X[11] + 0x6ed9eba1), S32);
252 c = rotateLeft((c + H(d, a, b) + X[ 7] + 0x6ed9eba1), S33);
253 b = rotateLeft((b + H(c, d, a) + X[15] + 0x6ed9eba1), S34);
261 // reset the offset and clean out the word buffer.
264 for (int i = 0; i != X.length; i++)