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 MD5 as outlined in "Handbook of Applied Cryptography", pages 346 - 347.
024: */
025: public class MD5Digest extends GeneralDigest {
026: private static final int DIGEST_LENGTH = 16;
027:
028: private int H1, H2, H3, H4; // IV's
029:
030: private int[] X = new int[16];
031: private int xOff;
032:
033: /**
034: * Standard constructor
035: */
036: public MD5Digest() {
037: reset();
038: }
039:
040: /**
041: * Copy constructor. This will copy the state of the provided
042: * message digest.
043: */
044: public MD5Digest(MD5Digest t) {
045: super (t);
046:
047: H1 = t.H1;
048: H2 = t.H2;
049: H3 = t.H3;
050: H4 = t.H4;
051:
052: System.arraycopy(t.X, 0, X, 0, t.X.length);
053: xOff = t.xOff;
054: }
055:
056: public String getAlgorithmName() {
057: return "MD5";
058: }
059:
060: public int getDigestSize() {
061: return DIGEST_LENGTH;
062: }
063:
064: protected void processWord(byte[] in, int inOff) {
065: X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
066: | ((in[inOff + 2] & 0xff) << 16)
067: | ((in[inOff + 3] & 0xff) << 24);
068:
069: if (xOff == 16) {
070: processBlock();
071: }
072: }
073:
074: protected void processLength(long bitLength) {
075: if (xOff > 14) {
076: processBlock();
077: }
078:
079: X[14] = (int) (bitLength & 0xffffffff);
080: X[15] = (int) (bitLength >>> 32);
081: }
082:
083: private void unpackWord(int word, byte[] out, int outOff) {
084: out[outOff] = (byte) word;
085: out[outOff + 1] = (byte) (word >>> 8);
086: out[outOff + 2] = (byte) (word >>> 16);
087: out[outOff + 3] = (byte) (word >>> 24);
088: }
089:
090: public int doFinal(byte[] out, int outOff) {
091: finish();
092:
093: unpackWord(H1, out, outOff);
094: unpackWord(H2, out, outOff + 4);
095: unpackWord(H3, out, outOff + 8);
096: unpackWord(H4, out, outOff + 12);
097:
098: reset();
099:
100: return DIGEST_LENGTH;
101: }
102:
103: /**
104: * reset the chaining variables to the IV values.
105: */
106: public void reset() {
107: super .reset();
108:
109: H1 = 0x67452301;
110: H2 = 0xefcdab89;
111: H3 = 0x98badcfe;
112: H4 = 0x10325476;
113:
114: xOff = 0;
115:
116: for (int i = 0; i != X.length; i++) {
117: X[i] = 0;
118: }
119: }
120:
121: //
122: // round 1 left rotates
123: //
124: private static final int S11 = 7;
125: private static final int S12 = 12;
126: private static final int S13 = 17;
127: private static final int S14 = 22;
128:
129: //
130: // round 2 left rotates
131: //
132: private static final int S21 = 5;
133: private static final int S22 = 9;
134: private static final int S23 = 14;
135: private static final int S24 = 20;
136:
137: //
138: // round 3 left rotates
139: //
140: private static final int S31 = 4;
141: private static final int S32 = 11;
142: private static final int S33 = 16;
143: private static final int S34 = 23;
144:
145: //
146: // round 4 left rotates
147: //
148: private static final int S41 = 6;
149: private static final int S42 = 10;
150: private static final int S43 = 15;
151: private static final int S44 = 21;
152:
153: /*
154: * rotate int x left n bits.
155: */
156: private int rotateLeft(int x, int n) {
157: return (x << n) | (x >>> (32 - n));
158: }
159:
160: /*
161: * F, G, H and I are the basic MD5 functions.
162: */
163: private int F(int u, int v, int w) {
164: return (u & v) | (~u & w);
165: }
166:
167: private int G(int u, int v, int w) {
168: return (u & w) | (v & ~w);
169: }
170:
171: private int H(int u, int v, int w) {
172: return u ^ v ^ w;
173: }
174:
175: private int K(int u, int v, int w) {
176: return v ^ (u | ~w);
177: }
178:
179: protected void processBlock() {
180: int a = H1;
181: int b = H2;
182: int c = H3;
183: int d = H4;
184:
185: //
186: // Round 1 - F cycle, 16 times.
187: //
188: a = rotateLeft(a + F(b, c, d) + X[0] + 0xd76aa478, S11) + b;
189: d = rotateLeft(d + F(a, b, c) + X[1] + 0xe8c7b756, S12) + a;
190: c = rotateLeft(c + F(d, a, b) + X[2] + 0x242070db, S13) + d;
191: b = rotateLeft(b + F(c, d, a) + X[3] + 0xc1bdceee, S14) + c;
192: a = rotateLeft(a + F(b, c, d) + X[4] + 0xf57c0faf, S11) + b;
193: d = rotateLeft(d + F(a, b, c) + X[5] + 0x4787c62a, S12) + a;
194: c = rotateLeft(c + F(d, a, b) + X[6] + 0xa8304613, S13) + d;
195: b = rotateLeft(b + F(c, d, a) + X[7] + 0xfd469501, S14) + c;
196: a = rotateLeft(a + F(b, c, d) + X[8] + 0x698098d8, S11) + b;
197: d = rotateLeft(d + F(a, b, c) + X[9] + 0x8b44f7af, S12) + a;
198: c = rotateLeft(c + F(d, a, b) + X[10] + 0xffff5bb1, S13) + d;
199: b = rotateLeft(b + F(c, d, a) + X[11] + 0x895cd7be, S14) + c;
200: a = rotateLeft(a + F(b, c, d) + X[12] + 0x6b901122, S11) + b;
201: d = rotateLeft(d + F(a, b, c) + X[13] + 0xfd987193, S12) + a;
202: c = rotateLeft(c + F(d, a, b) + X[14] + 0xa679438e, S13) + d;
203: b = rotateLeft(b + F(c, d, a) + X[15] + 0x49b40821, S14) + c;
204:
205: //
206: // Round 2 - G cycle, 16 times.
207: //
208: a = rotateLeft(a + G(b, c, d) + X[1] + 0xf61e2562, S21) + b;
209: d = rotateLeft(d + G(a, b, c) + X[6] + 0xc040b340, S22) + a;
210: c = rotateLeft(c + G(d, a, b) + X[11] + 0x265e5a51, S23) + d;
211: b = rotateLeft(b + G(c, d, a) + X[0] + 0xe9b6c7aa, S24) + c;
212: a = rotateLeft(a + G(b, c, d) + X[5] + 0xd62f105d, S21) + b;
213: d = rotateLeft(d + G(a, b, c) + X[10] + 0x02441453, S22) + a;
214: c = rotateLeft(c + G(d, a, b) + X[15] + 0xd8a1e681, S23) + d;
215: b = rotateLeft(b + G(c, d, a) + X[4] + 0xe7d3fbc8, S24) + c;
216: a = rotateLeft(a + G(b, c, d) + X[9] + 0x21e1cde6, S21) + b;
217: d = rotateLeft(d + G(a, b, c) + X[14] + 0xc33707d6, S22) + a;
218: c = rotateLeft(c + G(d, a, b) + X[3] + 0xf4d50d87, S23) + d;
219: b = rotateLeft(b + G(c, d, a) + X[8] + 0x455a14ed, S24) + c;
220: a = rotateLeft(a + G(b, c, d) + X[13] + 0xa9e3e905, S21) + b;
221: d = rotateLeft(d + G(a, b, c) + X[2] + 0xfcefa3f8, S22) + a;
222: c = rotateLeft(c + G(d, a, b) + X[7] + 0x676f02d9, S23) + d;
223: b = rotateLeft(b + G(c, d, a) + X[12] + 0x8d2a4c8a, S24) + c;
224:
225: //
226: // Round 3 - H cycle, 16 times.
227: //
228: a = rotateLeft(a + H(b, c, d) + X[5] + 0xfffa3942, S31) + b;
229: d = rotateLeft(d + H(a, b, c) + X[8] + 0x8771f681, S32) + a;
230: c = rotateLeft(c + H(d, a, b) + X[11] + 0x6d9d6122, S33) + d;
231: b = rotateLeft(b + H(c, d, a) + X[14] + 0xfde5380c, S34) + c;
232: a = rotateLeft(a + H(b, c, d) + X[1] + 0xa4beea44, S31) + b;
233: d = rotateLeft(d + H(a, b, c) + X[4] + 0x4bdecfa9, S32) + a;
234: c = rotateLeft(c + H(d, a, b) + X[7] + 0xf6bb4b60, S33) + d;
235: b = rotateLeft(b + H(c, d, a) + X[10] + 0xbebfbc70, S34) + c;
236: a = rotateLeft(a + H(b, c, d) + X[13] + 0x289b7ec6, S31) + b;
237: d = rotateLeft(d + H(a, b, c) + X[0] + 0xeaa127fa, S32) + a;
238: c = rotateLeft(c + H(d, a, b) + X[3] + 0xd4ef3085, S33) + d;
239: b = rotateLeft(b + H(c, d, a) + X[6] + 0x04881d05, S34) + c;
240: a = rotateLeft(a + H(b, c, d) + X[9] + 0xd9d4d039, S31) + b;
241: d = rotateLeft(d + H(a, b, c) + X[12] + 0xe6db99e5, S32) + a;
242: c = rotateLeft(c + H(d, a, b) + X[15] + 0x1fa27cf8, S33) + d;
243: b = rotateLeft(b + H(c, d, a) + X[2] + 0xc4ac5665, S34) + c;
244:
245: //
246: // Round 4 - K cycle, 16 times.
247: //
248: a = rotateLeft(a + K(b, c, d) + X[0] + 0xf4292244, S41) + b;
249: d = rotateLeft(d + K(a, b, c) + X[7] + 0x432aff97, S42) + a;
250: c = rotateLeft(c + K(d, a, b) + X[14] + 0xab9423a7, S43) + d;
251: b = rotateLeft(b + K(c, d, a) + X[5] + 0xfc93a039, S44) + c;
252: a = rotateLeft(a + K(b, c, d) + X[12] + 0x655b59c3, S41) + b;
253: d = rotateLeft(d + K(a, b, c) + X[3] + 0x8f0ccc92, S42) + a;
254: c = rotateLeft(c + K(d, a, b) + X[10] + 0xffeff47d, S43) + d;
255: b = rotateLeft(b + K(c, d, a) + X[1] + 0x85845dd1, S44) + c;
256: a = rotateLeft(a + K(b, c, d) + X[8] + 0x6fa87e4f, S41) + b;
257: d = rotateLeft(d + K(a, b, c) + X[15] + 0xfe2ce6e0, S42) + a;
258: c = rotateLeft(c + K(d, a, b) + X[6] + 0xa3014314, S43) + d;
259: b = rotateLeft(b + K(c, d, a) + X[13] + 0x4e0811a1, S44) + c;
260: a = rotateLeft(a + K(b, c, d) + X[4] + 0xf7537e82, S41) + b;
261: d = rotateLeft(d + K(a, b, c) + X[11] + 0xbd3af235, S42) + a;
262: c = rotateLeft(c + K(d, a, b) + X[2] + 0x2ad7d2bb, S43) + d;
263: b = rotateLeft(b + K(c, d, a) + X[9] + 0xeb86d391, S44) + c;
264:
265: H1 += a;
266: H2 += b;
267: H3 += c;
268: H4 += d;
269:
270: //
271: // reset the offset and clean out the word buffer.
272: //
273: xOff = 0;
274: for (int i = 0; i != X.length; i++) {
275: X[i] = 0;
276: }
277: }
278: }
|