001: /******************************************************************************
002: * Copyright (c) 2000 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
003: * Permission is hereby granted, free of charge, to any person obtaining a
004: * copy of this software and associated documentation files (the "Software"),
005: * to deal in the Software without restriction, including without limitation
006: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
007: * and/or sell copies of the Software, and to permit persons to whom the
008: * Software is furnished to do so, subject to the following conditions:
009: *
010: * The above copyright notice and this permission notice shall be included
011: * in all copies or substantial portions of the Software.
012: *
013: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
014: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
015: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
016: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
017: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
018: * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
019: * USE OR OTHER DEALINGS IN THE SOFTWARE.
020: ******************************************************************************/package net.sourceforge.jtds.util;
021:
022: /**
023: * implementation of MD4 as RFC 1320 by R. Rivest, MIT Laboratory for
024: * Computer Science and RSA Data Security, Inc.
025: * <p>
026: * <b>NOTE</b>: This algorithm is only included for backwards compatability
027: * with legacy applications, it's not secure, don't use it for anything new!
028: *
029: * @version $Id: MD4Digest.java,v 1.2 2004/06/27 17:00:55 bheineman Exp $
030: */
031: public class MD4Digest extends GeneralDigest {
032: private static final int DIGEST_LENGTH = 16;
033:
034: //
035: // round 1 left rotates
036: //
037: private static final int S11 = 3;
038: private static final int S12 = 7;
039: private static final int S13 = 11;
040: private static final int S14 = 19;
041:
042: //
043: // round 2 left rotates
044: //
045: private static final int S21 = 3;
046: private static final int S22 = 5;
047: private static final int S23 = 9;
048: private static final int S24 = 13;
049:
050: //
051: // round 3 left rotates
052: //
053: private static final int S31 = 3;
054: private static final int S32 = 9;
055: private static final int S33 = 11;
056: private static final int S34 = 15;
057:
058: private int H1, H2, H3, H4; // IV's
059:
060: private int[] X = new int[16];
061: private int xOff;
062:
063: /**
064: * Standard constructor
065: */
066: public MD4Digest() {
067: reset();
068: }
069:
070: /**
071: * Copy constructor. This will copy the state of the provided
072: * message digest.
073: */
074: public MD4Digest(MD4Digest t) {
075: super (t);
076:
077: H1 = t.H1;
078: H2 = t.H2;
079: H3 = t.H3;
080: H4 = t.H4;
081:
082: System.arraycopy(t.X, 0, X, 0, t.X.length);
083: xOff = t.xOff;
084: }
085:
086: public String getAlgorithmName() {
087: return "MD4";
088: }
089:
090: public int getDigestSize() {
091: return DIGEST_LENGTH;
092: }
093:
094: protected void processWord(byte[] in, int inOff) {
095: X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
096: | ((in[inOff + 2] & 0xff) << 16)
097: | ((in[inOff + 3] & 0xff) << 24);
098:
099: if (xOff == 16) {
100: processBlock();
101: }
102: }
103:
104: protected void processLength(long bitLength) {
105: if (xOff > 14) {
106: processBlock();
107: }
108:
109: X[14] = (int) (bitLength & 0xffffffff);
110: X[15] = (int) (bitLength >>> 32);
111: }
112:
113: private void unpackWord(int word, byte[] out, int outOff) {
114: out[outOff] = (byte) word;
115: out[outOff + 1] = (byte) (word >>> 8);
116: out[outOff + 2] = (byte) (word >>> 16);
117: out[outOff + 3] = (byte) (word >>> 24);
118: }
119:
120: public int doFinal(byte[] out, int outOff) {
121: finish();
122:
123: unpackWord(H1, out, outOff);
124: unpackWord(H2, out, outOff + 4);
125: unpackWord(H3, out, outOff + 8);
126: unpackWord(H4, out, outOff + 12);
127:
128: reset();
129:
130: return DIGEST_LENGTH;
131: }
132:
133: /**
134: * reset the chaining variables to the IV values.
135: */
136: public void reset() {
137: super .reset();
138:
139: H1 = 0x67452301;
140: H2 = 0xefcdab89;
141: H3 = 0x98badcfe;
142: H4 = 0x10325476;
143:
144: xOff = 0;
145:
146: for (int i = 0; i != X.length; i++) {
147: X[i] = 0;
148: }
149: }
150:
151: /*
152: * rotate int x left n bits.
153: */
154: private int rotateLeft(int x, int n) {
155: return (x << n) | (x >>> (32 - n));
156: }
157:
158: /*
159: * F, G, H and I are the basic MD4 functions.
160: */
161: private int F(int u, int v, int w) {
162: return (u & v) | (~u & w);
163: }
164:
165: private int G(int u, int v, int w) {
166: return (u & v) | (u & w) | (v & w);
167: }
168:
169: private int H(int u, int v, int w) {
170: return u ^ v ^ w;
171: }
172:
173: protected void processBlock() {
174: int a = H1;
175: int b = H2;
176: int c = H3;
177: int d = H4;
178:
179: //
180: // Round 1 - F cycle, 16 times.
181: //
182: a = rotateLeft((a + F(b, c, d) + X[0]), S11);
183: d = rotateLeft((d + F(a, b, c) + X[1]), S12);
184: c = rotateLeft((c + F(d, a, b) + X[2]), S13);
185: b = rotateLeft((b + F(c, d, a) + X[3]), S14);
186: a = rotateLeft((a + F(b, c, d) + X[4]), S11);
187: d = rotateLeft((d + F(a, b, c) + X[5]), S12);
188: c = rotateLeft((c + F(d, a, b) + X[6]), S13);
189: b = rotateLeft((b + F(c, d, a) + X[7]), S14);
190: a = rotateLeft((a + F(b, c, d) + X[8]), S11);
191: d = rotateLeft((d + F(a, b, c) + X[9]), S12);
192: c = rotateLeft((c + F(d, a, b) + X[10]), S13);
193: b = rotateLeft((b + F(c, d, a) + X[11]), S14);
194: a = rotateLeft((a + F(b, c, d) + X[12]), S11);
195: d = rotateLeft((d + F(a, b, c) + X[13]), S12);
196: c = rotateLeft((c + F(d, a, b) + X[14]), S13);
197: b = rotateLeft((b + F(c, d, a) + X[15]), S14);
198:
199: //
200: // Round 2 - G cycle, 16 times.
201: //
202: a = rotateLeft((a + G(b, c, d) + X[0] + 0x5a827999), S21);
203: d = rotateLeft((d + G(a, b, c) + X[4] + 0x5a827999), S22);
204: c = rotateLeft((c + G(d, a, b) + X[8] + 0x5a827999), S23);
205: b = rotateLeft((b + G(c, d, a) + X[12] + 0x5a827999), S24);
206: a = rotateLeft((a + G(b, c, d) + X[1] + 0x5a827999), S21);
207: d = rotateLeft((d + G(a, b, c) + X[5] + 0x5a827999), S22);
208: c = rotateLeft((c + G(d, a, b) + X[9] + 0x5a827999), S23);
209: b = rotateLeft((b + G(c, d, a) + X[13] + 0x5a827999), S24);
210: a = rotateLeft((a + G(b, c, d) + X[2] + 0x5a827999), S21);
211: d = rotateLeft((d + G(a, b, c) + X[6] + 0x5a827999), S22);
212: c = rotateLeft((c + G(d, a, b) + X[10] + 0x5a827999), S23);
213: b = rotateLeft((b + G(c, d, a) + X[14] + 0x5a827999), S24);
214: a = rotateLeft((a + G(b, c, d) + X[3] + 0x5a827999), S21);
215: d = rotateLeft((d + G(a, b, c) + X[7] + 0x5a827999), S22);
216: c = rotateLeft((c + G(d, a, b) + X[11] + 0x5a827999), S23);
217: b = rotateLeft((b + G(c, d, a) + X[15] + 0x5a827999), S24);
218:
219: //
220: // Round 3 - H cycle, 16 times.
221: //
222: a = rotateLeft((a + H(b, c, d) + X[0] + 0x6ed9eba1), S31);
223: d = rotateLeft((d + H(a, b, c) + X[8] + 0x6ed9eba1), S32);
224: c = rotateLeft((c + H(d, a, b) + X[4] + 0x6ed9eba1), S33);
225: b = rotateLeft((b + H(c, d, a) + X[12] + 0x6ed9eba1), S34);
226: a = rotateLeft((a + H(b, c, d) + X[2] + 0x6ed9eba1), S31);
227: d = rotateLeft((d + H(a, b, c) + X[10] + 0x6ed9eba1), S32);
228: c = rotateLeft((c + H(d, a, b) + X[6] + 0x6ed9eba1), S33);
229: b = rotateLeft((b + H(c, d, a) + X[14] + 0x6ed9eba1), S34);
230: a = rotateLeft((a + H(b, c, d) + X[1] + 0x6ed9eba1), S31);
231: d = rotateLeft((d + H(a, b, c) + X[9] + 0x6ed9eba1), S32);
232: c = rotateLeft((c + H(d, a, b) + X[5] + 0x6ed9eba1), S33);
233: b = rotateLeft((b + H(c, d, a) + X[13] + 0x6ed9eba1), S34);
234: a = rotateLeft((a + H(b, c, d) + X[3] + 0x6ed9eba1), S31);
235: d = rotateLeft((d + H(a, b, c) + X[11] + 0x6ed9eba1), S32);
236: c = rotateLeft((c + H(d, a, b) + X[7] + 0x6ed9eba1), S33);
237: b = rotateLeft((b + H(c, d, a) + X[15] + 0x6ed9eba1), S34);
238:
239: H1 += a;
240: H2 += b;
241: H3 += c;
242: H4 += d;
243:
244: //
245: // reset the offset and clean out the word buffer.
246: //
247: xOff = 0;
248:
249: for (int i = 0; i != X.length; i++) {
250: X[i] = 0;
251: }
252: }
253: }
|