1 package org.bouncycastle.crypto.digests;
3 import org.bouncycastle.crypto.Digest;
6 * implementation of MD4 as RFC 1320 by R. Rivest, MIT Laboratory for
7 * Computer Science and RSA Data Security, Inc.
9 * <b>NOTE</b>: This algorithm is only included for backwards compatability
10 * with legacy applications, it's not secure, don't use it for anything new!
12 public class MD4Digest
15 private static final int DIGEST_LENGTH = 16;
17 private int H1, H2, H3, H4; // IV's
19 private int[] X = new int[16];
23 * Standard constructor
31 * Copy constructor. This will copy the state of the provided
34 public MD4Digest(MD4Digest t)
43 System.arraycopy(t.X, 0, X, 0, t.X.length);
47 public String getAlgorithmName()
52 public int getDigestSize()
57 protected void processWord(
61 X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
62 | ((in[inOff + 2] & 0xff) << 16) | ((in[inOff + 3] & 0xff) << 24);
70 protected void processLength(
78 X[14] = (int)(bitLength & 0xffffffff);
79 X[15] = (int)(bitLength >>> 32);
82 private void unpackWord(
87 out[outOff] = (byte)word;
88 out[outOff + 1] = (byte)(word >>> 8);
89 out[outOff + 2] = (byte)(word >>> 16);
90 out[outOff + 3] = (byte)(word >>> 24);
99 unpackWord(H1, out, outOff);
100 unpackWord(H2, out, outOff + 4);
101 unpackWord(H3, out, outOff + 8);
102 unpackWord(H4, out, outOff + 12);
106 return DIGEST_LENGTH;
110 * reset the chaining variables to the IV values.
123 for (int i = 0; i != X.length; i++)
130 // round 1 left rotates
132 private static final int S11 = 3;
133 private static final int S12 = 7;
134 private static final int S13 = 11;
135 private static final int S14 = 19;
138 // round 2 left rotates
140 private static final int S21 = 3;
141 private static final int S22 = 5;
142 private static final int S23 = 9;
143 private static final int S24 = 13;
146 // round 3 left rotates
148 private static final int S31 = 3;
149 private static final int S32 = 9;
150 private static final int S33 = 11;
151 private static final int S34 = 15;
154 * rotate int x left n bits.
156 private int rotateLeft(
160 return (x << n) | (x >>> (32 - n));
164 * F, G, H and I are the basic MD4 functions.
171 return (u & v) | (~u & w);
179 return (u & v) | (u & w) | (v & w);
190 protected void processBlock()
198 // Round 1 - F cycle, 16 times.
200 a = rotateLeft((a + F(b, c, d) + X[ 0]), S11);
201 d = rotateLeft((d + F(a, b, c) + X[ 1]), S12);
202 c = rotateLeft((c + F(d, a, b) + X[ 2]), S13);
203 b = rotateLeft((b + F(c, d, a) + X[ 3]), S14);
204 a = rotateLeft((a + F(b, c, d) + X[ 4]), S11);
205 d = rotateLeft((d + F(a, b, c) + X[ 5]), S12);
206 c = rotateLeft((c + F(d, a, b) + X[ 6]), S13);
207 b = rotateLeft((b + F(c, d, a) + X[ 7]), S14);
208 a = rotateLeft((a + F(b, c, d) + X[ 8]), S11);
209 d = rotateLeft((d + F(a, b, c) + X[ 9]), S12);
210 c = rotateLeft((c + F(d, a, b) + X[10]), S13);
211 b = rotateLeft((b + F(c, d, a) + X[11]), S14);
212 a = rotateLeft((a + F(b, c, d) + X[12]), S11);
213 d = rotateLeft((d + F(a, b, c) + X[13]), S12);
214 c = rotateLeft((c + F(d, a, b) + X[14]), S13);
215 b = rotateLeft((b + F(c, d, a) + X[15]), S14);
218 // Round 2 - G cycle, 16 times.
220 a = rotateLeft((a + G(b, c, d) + X[ 0] + 0x5a827999), S21);
221 d = rotateLeft((d + G(a, b, c) + X[ 4] + 0x5a827999), S22);
222 c = rotateLeft((c + G(d, a, b) + X[ 8] + 0x5a827999), S23);
223 b = rotateLeft((b + G(c, d, a) + X[12] + 0x5a827999), S24);
224 a = rotateLeft((a + G(b, c, d) + X[ 1] + 0x5a827999), S21);
225 d = rotateLeft((d + G(a, b, c) + X[ 5] + 0x5a827999), S22);
226 c = rotateLeft((c + G(d, a, b) + X[ 9] + 0x5a827999), S23);
227 b = rotateLeft((b + G(c, d, a) + X[13] + 0x5a827999), S24);
228 a = rotateLeft((a + G(b, c, d) + X[ 2] + 0x5a827999), S21);
229 d = rotateLeft((d + G(a, b, c) + X[ 6] + 0x5a827999), S22);
230 c = rotateLeft((c + G(d, a, b) + X[10] + 0x5a827999), S23);
231 b = rotateLeft((b + G(c, d, a) + X[14] + 0x5a827999), S24);
232 a = rotateLeft((a + G(b, c, d) + X[ 3] + 0x5a827999), S21);
233 d = rotateLeft((d + G(a, b, c) + X[ 7] + 0x5a827999), S22);
234 c = rotateLeft((c + G(d, a, b) + X[11] + 0x5a827999), S23);
235 b = rotateLeft((b + G(c, d, a) + X[15] + 0x5a827999), S24);
238 // Round 3 - H cycle, 16 times.
240 a = rotateLeft((a + H(b, c, d) + X[ 0] + 0x6ed9eba1), S31);
241 d = rotateLeft((d + H(a, b, c) + X[ 8] + 0x6ed9eba1), S32);
242 c = rotateLeft((c + H(d, a, b) + X[ 4] + 0x6ed9eba1), S33);
243 b = rotateLeft((b + H(c, d, a) + X[12] + 0x6ed9eba1), S34);
244 a = rotateLeft((a + H(b, c, d) + X[ 2] + 0x6ed9eba1), S31);
245 d = rotateLeft((d + H(a, b, c) + X[10] + 0x6ed9eba1), S32);
246 c = rotateLeft((c + H(d, a, b) + X[ 6] + 0x6ed9eba1), S33);
247 b = rotateLeft((b + H(c, d, a) + X[14] + 0x6ed9eba1), S34);
248 a = rotateLeft((a + H(b, c, d) + X[ 1] + 0x6ed9eba1), S31);
249 d = rotateLeft((d + H(a, b, c) + X[ 9] + 0x6ed9eba1), S32);
250 c = rotateLeft((c + H(d, a, b) + X[ 5] + 0x6ed9eba1), S33);
251 b = rotateLeft((b + H(c, d, a) + X[13] + 0x6ed9eba1), S34);
252 a = rotateLeft((a + H(b, c, d) + X[ 3] + 0x6ed9eba1), S31);
253 d = rotateLeft((d + H(a, b, c) + X[11] + 0x6ed9eba1), S32);
254 c = rotateLeft((c + H(d, a, b) + X[ 7] + 0x6ed9eba1), S33);
255 b = rotateLeft((b + H(c, d, a) + X[15] + 0x6ed9eba1), S34);
263 // reset the offset and clean out the word buffer.
266 for (int i = 0; i != X.length; i++)