001: package org.bouncycastle.crypto.digests;
002:
003: /**
004: * implementation of MD4 as RFC 1320 by R. Rivest, MIT Laboratory for
005: * Computer Science and RSA Data Security, Inc.
006: * <p>
007: * <b>NOTE</b>: This algorithm is only included for backwards compatability
008: * with legacy applications, it's not secure, don't use it for anything new!
009: */
010: public class MD4Digest extends GeneralDigest {
011: private static final int DIGEST_LENGTH = 16;
012:
013: private int H1, H2, H3, H4; // IV's
014:
015: private int[] X = new int[16];
016: private int xOff;
017:
018: /**
019: * Standard constructor
020: */
021: public MD4Digest() {
022: reset();
023: }
024:
025: /**
026: * Copy constructor. This will copy the state of the provided
027: * message digest.
028: */
029: public MD4Digest(MD4Digest t) {
030: super (t);
031:
032: H1 = t.H1;
033: H2 = t.H2;
034: H3 = t.H3;
035: H4 = t.H4;
036:
037: System.arraycopy(t.X, 0, X, 0, t.X.length);
038: xOff = t.xOff;
039: }
040:
041: public String getAlgorithmName() {
042: return "MD4";
043: }
044:
045: public int getDigestSize() {
046: return DIGEST_LENGTH;
047: }
048:
049: protected void processWord(byte[] in, int inOff) {
050: X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
051: | ((in[inOff + 2] & 0xff) << 16)
052: | ((in[inOff + 3] & 0xff) << 24);
053:
054: if (xOff == 16) {
055: processBlock();
056: }
057: }
058:
059: protected void processLength(long bitLength) {
060: if (xOff > 14) {
061: processBlock();
062: }
063:
064: X[14] = (int) (bitLength & 0xffffffff);
065: X[15] = (int) (bitLength >>> 32);
066: }
067:
068: private void unpackWord(int word, byte[] out, int outOff) {
069: out[outOff] = (byte) word;
070: out[outOff + 1] = (byte) (word >>> 8);
071: out[outOff + 2] = (byte) (word >>> 16);
072: out[outOff + 3] = (byte) (word >>> 24);
073: }
074:
075: public int doFinal(byte[] out, int outOff) {
076: finish();
077:
078: unpackWord(H1, out, outOff);
079: unpackWord(H2, out, outOff + 4);
080: unpackWord(H3, out, outOff + 8);
081: unpackWord(H4, out, outOff + 12);
082:
083: reset();
084:
085: return DIGEST_LENGTH;
086: }
087:
088: /**
089: * reset the chaining variables to the IV values.
090: */
091: public void reset() {
092: super .reset();
093:
094: H1 = 0x67452301;
095: H2 = 0xefcdab89;
096: H3 = 0x98badcfe;
097: H4 = 0x10325476;
098:
099: xOff = 0;
100:
101: for (int i = 0; i != X.length; i++) {
102: X[i] = 0;
103: }
104: }
105:
106: //
107: // round 1 left rotates
108: //
109: private static final int S11 = 3;
110: private static final int S12 = 7;
111: private static final int S13 = 11;
112: private static final int S14 = 19;
113:
114: //
115: // round 2 left rotates
116: //
117: private static final int S21 = 3;
118: private static final int S22 = 5;
119: private static final int S23 = 9;
120: private static final int S24 = 13;
121:
122: //
123: // round 3 left rotates
124: //
125: private static final int S31 = 3;
126: private static final int S32 = 9;
127: private static final int S33 = 11;
128: private static final int S34 = 15;
129:
130: /*
131: * rotate int x left n bits.
132: */
133: private int rotateLeft(int x, int n) {
134: return (x << n) | (x >>> (32 - n));
135: }
136:
137: /*
138: * F, G, H and I are the basic MD4 functions.
139: */
140: private int F(int u, int v, int w) {
141: return (u & v) | (~u & w);
142: }
143:
144: private int G(int u, int v, int w) {
145: return (u & v) | (u & w) | (v & w);
146: }
147:
148: private int H(int u, int v, int w) {
149: return u ^ v ^ w;
150: }
151:
152: protected void processBlock() {
153: int a = H1;
154: int b = H2;
155: int c = H3;
156: int d = H4;
157:
158: //
159: // Round 1 - F cycle, 16 times.
160: //
161: a = rotateLeft(a + F(b, c, d) + X[0], S11);
162: d = rotateLeft(d + F(a, b, c) + X[1], S12);
163: c = rotateLeft(c + F(d, a, b) + X[2], S13);
164: b = rotateLeft(b + F(c, d, a) + X[3], S14);
165: a = rotateLeft(a + F(b, c, d) + X[4], S11);
166: d = rotateLeft(d + F(a, b, c) + X[5], S12);
167: c = rotateLeft(c + F(d, a, b) + X[6], S13);
168: b = rotateLeft(b + F(c, d, a) + X[7], S14);
169: a = rotateLeft(a + F(b, c, d) + X[8], S11);
170: d = rotateLeft(d + F(a, b, c) + X[9], S12);
171: c = rotateLeft(c + F(d, a, b) + X[10], S13);
172: b = rotateLeft(b + F(c, d, a) + X[11], S14);
173: a = rotateLeft(a + F(b, c, d) + X[12], S11);
174: d = rotateLeft(d + F(a, b, c) + X[13], S12);
175: c = rotateLeft(c + F(d, a, b) + X[14], S13);
176: b = rotateLeft(b + F(c, d, a) + X[15], S14);
177:
178: //
179: // Round 2 - G cycle, 16 times.
180: //
181: a = rotateLeft(a + G(b, c, d) + X[0] + 0x5a827999, S21);
182: d = rotateLeft(d + G(a, b, c) + X[4] + 0x5a827999, S22);
183: c = rotateLeft(c + G(d, a, b) + X[8] + 0x5a827999, S23);
184: b = rotateLeft(b + G(c, d, a) + X[12] + 0x5a827999, S24);
185: a = rotateLeft(a + G(b, c, d) + X[1] + 0x5a827999, S21);
186: d = rotateLeft(d + G(a, b, c) + X[5] + 0x5a827999, S22);
187: c = rotateLeft(c + G(d, a, b) + X[9] + 0x5a827999, S23);
188: b = rotateLeft(b + G(c, d, a) + X[13] + 0x5a827999, S24);
189: a = rotateLeft(a + G(b, c, d) + X[2] + 0x5a827999, S21);
190: d = rotateLeft(d + G(a, b, c) + X[6] + 0x5a827999, S22);
191: c = rotateLeft(c + G(d, a, b) + X[10] + 0x5a827999, S23);
192: b = rotateLeft(b + G(c, d, a) + X[14] + 0x5a827999, S24);
193: a = rotateLeft(a + G(b, c, d) + X[3] + 0x5a827999, S21);
194: d = rotateLeft(d + G(a, b, c) + X[7] + 0x5a827999, S22);
195: c = rotateLeft(c + G(d, a, b) + X[11] + 0x5a827999, S23);
196: b = rotateLeft(b + G(c, d, a) + X[15] + 0x5a827999, S24);
197:
198: //
199: // Round 3 - H cycle, 16 times.
200: //
201: a = rotateLeft(a + H(b, c, d) + X[0] + 0x6ed9eba1, S31);
202: d = rotateLeft(d + H(a, b, c) + X[8] + 0x6ed9eba1, S32);
203: c = rotateLeft(c + H(d, a, b) + X[4] + 0x6ed9eba1, S33);
204: b = rotateLeft(b + H(c, d, a) + X[12] + 0x6ed9eba1, S34);
205: a = rotateLeft(a + H(b, c, d) + X[2] + 0x6ed9eba1, S31);
206: d = rotateLeft(d + H(a, b, c) + X[10] + 0x6ed9eba1, S32);
207: c = rotateLeft(c + H(d, a, b) + X[6] + 0x6ed9eba1, S33);
208: b = rotateLeft(b + H(c, d, a) + X[14] + 0x6ed9eba1, S34);
209: a = rotateLeft(a + H(b, c, d) + X[1] + 0x6ed9eba1, S31);
210: d = rotateLeft(d + H(a, b, c) + X[9] + 0x6ed9eba1, S32);
211: c = rotateLeft(c + H(d, a, b) + X[5] + 0x6ed9eba1, S33);
212: b = rotateLeft(b + H(c, d, a) + X[13] + 0x6ed9eba1, S34);
213: a = rotateLeft(a + H(b, c, d) + X[3] + 0x6ed9eba1, S31);
214: d = rotateLeft(d + H(a, b, c) + X[11] + 0x6ed9eba1, S32);
215: c = rotateLeft(c + H(d, a, b) + X[7] + 0x6ed9eba1, S33);
216: b = rotateLeft(b + H(c, d, a) + X[15] + 0x6ed9eba1, S34);
217:
218: H1 += a;
219: H2 += b;
220: H3 += c;
221: H4 += d;
222:
223: //
224: // reset the offset and clean out the word buffer.
225: //
226: xOff = 0;
227: for (int i = 0; i != X.length; i++) {
228: X[i] = 0;
229: }
230: }
231: }
|