001: package ch.ethz.ssh2.crypto.digest;
002:
003: /**
004: * MD5. Based on the example code in RFC 1321. Optimized (...a little).
005: *
006: * @author Christian Plattner, plattner@inf.ethz.ch
007: * @version $Id: MD5.java,v 1.2 2006/02/02 09:11:03 cplattne Exp $
008: */
009:
010: /*
011: * The following disclaimer has been copied from RFC 1321:
012: *
013: * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
014: * reserved.
015: *
016: * License to copy and use this software is granted provided that it is
017: * identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in
018: * all material mentioning or referencing this software or this function.
019: *
020: * License is also granted to make and use derivative works provided that such
021: * works are identified as "derived from the RSA Data Security, Inc. MD5
022: * Message-Digest Algorithm" in all material mentioning or referencing the
023: * derived work.
024: *
025: * RSA Data Security, Inc. makes no representations concerning either the
026: * merchantability of this software or the suitability of this software for any
027: * particular purpose. It is provided "as is" without express or implied
028: * warranty of any kind.
029: *
030: * These notices must be retained in any copies of any part of this
031: * documentation and/or software.
032: *
033: */
034:
035: public final class MD5 implements Digest {
036: private int state0, state1, state2, state3;
037: private long count;
038: private final byte[] block = new byte[64];
039: private final int x[] = new int[16];
040:
041: private static final byte[] padding = new byte[] { (byte) 128, 0,
042: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
043: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
044: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
045: 0, 0 };
046:
047: public MD5() {
048: reset();
049: }
050:
051: private static final int FF(int a, int b, int c, int d, int x,
052: int s, int ac) {
053: a += ((b & c) | ((~b) & d)) + x + ac;
054: return ((a << s) | (a >>> (32 - s))) + b;
055: }
056:
057: private static final int GG(int a, int b, int c, int d, int x,
058: int s, int ac) {
059: a += ((b & d) | (c & (~d))) + x + ac;
060: return ((a << s) | (a >>> (32 - s))) + b;
061: }
062:
063: private static final int HH(int a, int b, int c, int d, int x,
064: int s, int ac) {
065: a += (b ^ c ^ d) + x + ac;
066: return ((a << s) | (a >>> (32 - s))) + b;
067: }
068:
069: private static final int II(int a, int b, int c, int d, int x,
070: int s, int ac) {
071: a += (c ^ (b | (~d))) + x + ac;
072: return ((a << s) | (a >>> (32 - s))) + b;
073: }
074:
075: private static final void encode(byte[] dst, int dstoff, int word) {
076: dst[dstoff] = (byte) (word);
077: dst[dstoff + 1] = (byte) (word >> 8);
078: dst[dstoff + 2] = (byte) (word >> 16);
079: dst[dstoff + 3] = (byte) (word >> 24);
080: }
081:
082: private final void transform(byte[] src, int pos) {
083: int a = state0;
084: int b = state1;
085: int c = state2;
086: int d = state3;
087:
088: for (int i = 0; i < 16; i++, pos += 4) {
089: x[i] = (src[pos] & 0xff) | ((src[pos + 1] & 0xff) << 8)
090: | ((src[pos + 2] & 0xff) << 16)
091: | ((src[pos + 3] & 0xff) << 24);
092: }
093:
094: /* Round 1 */
095:
096: a = FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */
097: d = FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */
098: c = FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */
099: b = FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */
100: a = FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */
101: d = FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */
102: c = FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */
103: b = FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */
104: a = FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */
105: d = FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */
106: c = FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
107: b = FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
108: a = FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
109: d = FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
110: c = FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
111: b = FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
112:
113: /* Round 2 */
114: a = GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */
115: d = GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */
116: c = GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
117: b = GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */
118: a = GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */
119: d = GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
120: c = GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
121: b = GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */
122: a = GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */
123: d = GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
124: c = GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */
125: b = GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */
126: a = GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
127: d = GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */
128: c = GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */
129: b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
130:
131: /* Round 3 */
132: a = HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */
133: d = HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */
134: c = HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
135: b = HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
136: a = HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */
137: d = HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */
138: c = HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */
139: b = HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
140: a = HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
141: d = HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */
142: c = HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */
143: b = HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */
144: a = HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */
145: d = HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
146: c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
147: b = HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */
148:
149: /* Round 4 */
150: a = II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */
151: d = II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */
152: c = II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
153: b = II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */
154: a = II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
155: d = II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */
156: c = II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
157: b = II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */
158: a = II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */
159: d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
160: c = II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */
161: b = II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
162: a = II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */
163: d = II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
164: c = II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */
165: b = II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */
166:
167: state0 += a;
168: state1 += b;
169: state2 += c;
170: state3 += d;
171: }
172:
173: public final void reset() {
174: count = 0;
175:
176: state0 = 0x67452301;
177: state1 = 0xefcdab89;
178: state2 = 0x98badcfe;
179: state3 = 0x10325476;
180:
181: /* Clear traces in memory... */
182:
183: for (int i = 0; i < 16; i++)
184: x[i] = 0;
185: }
186:
187: public final void update(byte b) {
188: final int space = 64 - ((int) (count & 0x3f));
189:
190: count++;
191:
192: block[64 - space] = b;
193:
194: if (space == 1)
195: transform(block, 0);
196: }
197:
198: public final void update(byte[] buff, int pos, int len) {
199: int space = 64 - ((int) (count & 0x3f));
200:
201: count += len;
202:
203: while (len > 0) {
204: if (len < space) {
205: System.arraycopy(buff, pos, block, 64 - space, len);
206: break;
207: }
208:
209: if (space == 64) {
210: transform(buff, pos);
211: } else {
212: System.arraycopy(buff, pos, block, 64 - space, space);
213: transform(block, 0);
214: }
215:
216: pos += space;
217: len -= space;
218: space = 64;
219: }
220: }
221:
222: public final void update(byte[] b) {
223: update(b, 0, b.length);
224: }
225:
226: public final void digest(byte[] dst, int pos) {
227: byte[] bits = new byte[8];
228:
229: encode(bits, 0, (int) (count << 3));
230: encode(bits, 4, (int) (count >> 29));
231:
232: int idx = (int) count & 0x3f;
233: int padLen = (idx < 56) ? (56 - idx) : (120 - idx);
234:
235: update(padding, 0, padLen);
236: update(bits, 0, 8);
237:
238: encode(dst, pos, state0);
239: encode(dst, pos + 4, state1);
240: encode(dst, pos + 8, state2);
241: encode(dst, pos + 12, state3);
242:
243: reset();
244: }
245:
246: public final void digest(byte[] dst) {
247: digest(dst, 0);
248: }
249:
250: public final int getDigestLength() {
251: return 16;
252: }
253: }
|