1    package org.bouncycastle.crypto.digests;
2    
3    import org.bouncycastle.crypto.Digest;
4    
5    /**
6     * implementation of MD5 as outlined in "Handbook of Applied Cryptography", pages 346 - 347.
7     */
8    public class MD5Digest
9        extends GeneralDigest
10   {
11       private static final int    DIGEST_LENGTH = 16;
12   
13       private intintintintH1H2H3, H4;         // IV's
14   
15       private int[]   X = new int[16];
16       private int     xOff;
17   
18           /**
19            * Standard constructor
20            */
21       public MD5Digest()
22       {
23           reset();
24       }
25   
26           /**
27            * Copy constructor.  This will copy the state of the provided
28            * message digest.
29            */
30           public MD5Digest(MD5Digest t)
31           {
32                   super(t);
33   
34                   H1 = t.H1;
35                   H2 = t.H2;
36                   H3 = t.H3;
37                   H4 = t.H4;
38   
39                   System.arraycopy(t.X, 0, X, 0, t.X.length);
40                   xOff = t.xOff;
41           }
42   
43       public String getAlgorithmName()
44       {
45           return "MD5";
46       }
47   
48       public int getDigestSize()
49       {
50           return DIGEST_LENGTH;
51       }
52   
53       protected void processWord(
54           byte[]  in,
55           int     inOff)
56       {
57           X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
58               | ((in[inOff + 2] & 0xff) << 16) | ((in[inOff + 3] & 0xff) << 24); 
59   
60           if (xOff == 16)
61           {
62               processBlock();
63           }
64       }
65   
66       protected void processLength(
67           long    bitLength)
68       {
69           if (xOff > 14)
70           {
71               processBlock();
72           }
73   
74           X[14] = (int)(bitLength & 0xffffffff);
75           X[15] = (int)(bitLength >>> 32);
76       }
77   
78       private void unpackWord(
79           int     word,
80           byte[]  out,
81           int     outOff)
82       {
83           out[outOff]     = (byte)word;
84           out[outOff + 1] = (byte)(word >>> 8);
85           out[outOff + 2] = (byte)(word >>> 16);
86           out[outOff + 3] = (byte)(word >>> 24);
87       }
88   
89       public int doFinal(
90           byte[]  out,
91           int     outOff)
92       {
93           finish();
94   
95           unpackWord(H1, out, outOff);
96           unpackWord(H2, out, outOff + 4);
97           unpackWord(H3, out, outOff + 8);
98           unpackWord(H4, out, outOff + 12);
99   
100          reset();
101  
102          return DIGEST_LENGTH;
103      }
104  
105      /**
106       * reset the chaining variables to the IV values.
107       */
108      public void reset()
109      {
110          super.reset();
111  
112          H1 = 0x67452301;
113          H2 = 0xefcdab89;
114          H3 = 0x98badcfe;
115          H4 = 0x10325476;
116  
117          xOff = 0;
118  
119          for (int i = 0; i != X.length; i++)
120          {
121              X[i] = 0;
122          }
123      }
124  
125      //
126      // round 1 left rotates
127      //
128      private static final int S11 = 7;
129      private static final int S12 = 12;
130      private static final int S13 = 17;
131      private static final int S14 = 22;
132  
133      //
134      // round 2 left rotates
135      //
136      private static final int S21 = 5;
137      private static final int S22 = 9;
138      private static final int S23 = 14;
139      private static final int S24 = 20;
140  
141      //
142      // round 3 left rotates
143      //
144      private static final int S31 = 4;
145      private static final int S32 = 11;
146      private static final int S33 = 16;
147      private static final int S34 = 23;
148  
149      //
150      // round 4 left rotates
151      //
152      private static final int S41 = 6;
153      private static final int S42 = 10;
154      private static final int S43 = 15;
155      private static final int S44 = 21;
156  
157      /*
158       * rotate int x left n bits.
159       */
160      private int rotateLeft(
161          int x,
162          int n)
163      {
164          return (x << n) | (x >>> (32 - n));
165      }
166  
167      /*
168       * F, G, H and I are the basic MD5 functions.
169       */
170      private int F(
171          int u,
172          int v,
173          int w)
174      {
175          return (u & v) | (~u & w);
176      }
177  
178      private int G(
179          int u,
180          int v,
181          int w)
182      {
183          return (u & w) | (v & ~w);
184      }
185  
186      private int H(
187          int u,
188          int v,
189          int w)
190      {
191          return u ^ v ^ w;
192      }
193  
194      private int K(
195          int u,
196          int v,
197          int w)
198      {
199          return v ^ (u | ~w);
200      }
201  
202      protected void processBlock()
203      {
204          int a = H1;
205          int b = H2;
206          int c = H3;
207          int d = H4;
208  
209          //
210          // Round 1 - F cycle, 16 times.
211          //
212          a = rotateLeft((a + F(b, c, d) + X[ 0] + 0xd76aa478), S11) + b;
213          d = rotateLeft((d + F(a, b, c) + X[ 1] + 0xe8c7b756), S12) + a;
214          c = rotateLeft((c + F(d, a, b) + X[ 2] + 0x242070db), S13) + d;
215          b = rotateLeft((b + F(c, d, a) + X[ 3] + 0xc1bdceee), S14) + c;
216          a = rotateLeft((a + F(b, c, d) + X[ 4] + 0xf57c0faf), S11) + b;
217          d = rotateLeft((d + F(a, b, c) + X[ 5] + 0x4787c62a), S12) + a;
218          c = rotateLeft((c + F(d, a, b) + X[ 6] + 0xa8304613), S13) + d;
219          b = rotateLeft((b + F(c, d, a) + X[ 7] + 0xfd469501), S14) + c;
220          a = rotateLeft((a + F(b, c, d) + X[ 8] + 0x698098d8), S11) + b;
221          d = rotateLeft((d + F(a, b, c) + X[ 9] + 0x8b44f7af), S12) + a;
222          c = rotateLeft((c + F(d, a, b) + X[10] + 0xffff5bb1), S13) + d;
223          b = rotateLeft((b + F(c, d, a) + X[11] + 0x895cd7be), S14) + c;
224          a = rotateLeft((a + F(b, c, d) + X[12] + 0x6b901122), S11) + b;
225          d = rotateLeft((d + F(a, b, c) + X[13] + 0xfd987193), S12) + a;
226          c = rotateLeft((c + F(d, a, b) + X[14] + 0xa679438e), S13) + d;
227          b = rotateLeft((b + F(c, d, a) + X[15] + 0x49b40821), S14) + c;
228  
229          //
230          // Round 2 - G cycle, 16 times.
231          //
232          a = rotateLeft((a + G(b, c, d) + X[ 1] + 0xf61e2562), S21) + b;
233          d = rotateLeft((d + G(a, b, c) + X[ 6] + 0xc040b340), S22) + a;
234          c = rotateLeft((c + G(d, a, b) + X[11] + 0x265e5a51), S23) + d;
235          b = rotateLeft((b + G(c, d, a) + X[ 0] + 0xe9b6c7aa), S24) + c;
236          a = rotateLeft((a + G(b, c, d) + X[ 5] + 0xd62f105d), S21) + b;
237          d = rotateLeft((d + G(a, b, c) + X[10] + 0x02441453), S22) + a;
238          c = rotateLeft((c + G(d, a, b) + X[15] + 0xd8a1e681), S23) + d;
239          b = rotateLeft((b + G(c, d, a) + X[ 4] + 0xe7d3fbc8), S24) + c;
240          a = rotateLeft((a + G(b, c, d) + X[ 9] + 0x21e1cde6), S21) + b;
241          d = rotateLeft((d + G(a, b, c) + X[14] + 0xc33707d6), S22) + a;
242          c = rotateLeft((c + G(d, a, b) + X[ 3] + 0xf4d50d87), S23) + d;
243          b = rotateLeft((b + G(c, d, a) + X[ 8] + 0x455a14ed), S24) + c;
244          a = rotateLeft((a + G(b, c, d) + X[13] + 0xa9e3e905), S21) + b;
245          d = rotateLeft((d + G(a, b, c) + X[ 2] + 0xfcefa3f8), S22) + a;
246          c = rotateLeft((c + G(d, a, b) + X[ 7] + 0x676f02d9), S23) + d;
247          b = rotateLeft((b + G(c, d, a) + X[12] + 0x8d2a4c8a), S24) + c;
248  
249          //
250          // Round 3 - H cycle, 16 times.
251          //
252          a = rotateLeft((a + H(b, c, d) + X[ 5] + 0xfffa3942), S31) + b;
253          d = rotateLeft((d + H(a, b, c) + X[ 8] + 0x8771f681), S32) + a;
254          c = rotateLeft((c + H(d, a, b) + X[11] + 0x6d9d6122), S33) + d;
255          b = rotateLeft((b + H(c, d, a) + X[14] + 0xfde5380c), S34) + c;
256          a = rotateLeft((a + H(b, c, d) + X[ 1] + 0xa4beea44), S31) + b;
257          d = rotateLeft((d + H(a, b, c) + X[ 4] + 0x4bdecfa9), S32) + a;
258          c = rotateLeft((c + H(d, a, b) + X[ 7] + 0xf6bb4b60), S33) + d;
259          b = rotateLeft((b + H(c, d, a) + X[10] + 0xbebfbc70), S34) + c;
260          a = rotateLeft((a + H(b, c, d) + X[13] + 0x289b7ec6), S31) + b;
261          d = rotateLeft((d + H(a, b, c) + X[ 0] + 0xeaa127fa), S32) + a;
262          c = rotateLeft((c + H(d, a, b) + X[ 3] + 0xd4ef3085), S33) + d;
263          b = rotateLeft((b + H(c, d, a) + X[ 6] + 0x04881d05), S34) + c;
264          a = rotateLeft((a + H(b, c, d) + X[ 9] + 0xd9d4d039), S31) + b;
265          d = rotateLeft((d + H(a, b, c) + X[12] + 0xe6db99e5), S32) + a;
266          c = rotateLeft((c + H(d, a, b) + X[15] + 0x1fa27cf8), S33) + d;
267          b = rotateLeft((b + H(c, d, a) + X[ 2] + 0xc4ac5665), S34) + c;
268  
269          //
270          // Round 4 - K cycle, 16 times.
271          //
272          a = rotateLeft((a + K(b, c, d) + X[ 0] + 0xf4292244), S41) + b;
273          d = rotateLeft((d + K(a, b, c) + X[ 7] + 0x432aff97), S42) + a;
274          c = rotateLeft((c + K(d, a, b) + X[14] + 0xab9423a7), S43) + d;
275          b = rotateLeft((b + K(c, d, a) + X[ 5] + 0xfc93a039), S44) + c;
276          a = rotateLeft((a + K(b, c, d) + X[12] + 0x655b59c3), S41) + b;
277          d = rotateLeft((d + K(a, b, c) + X[ 3] + 0x8f0ccc92), S42) + a;
278          c = rotateLeft((c + K(d, a, b) + X[10] + 0xffeff47d), S43) + d;
279          b = rotateLeft((b + K(c, d, a) + X[ 1] + 0x85845dd1), S44) + c;
280          a = rotateLeft((a + K(b, c, d) + X[ 8] + 0x6fa87e4f), S41) + b;
281          d = rotateLeft((d + K(a, b, c) + X[15] + 0xfe2ce6e0), S42) + a;
282          c = rotateLeft((c + K(d, a, b) + X[ 6] + 0xa3014314), S43) + d;
283          b = rotateLeft((b + K(c, d, a) + X[13] + 0x4e0811a1), S44) + c;
284          a = rotateLeft((a + K(b, c, d) + X[ 4] + 0xf7537e82), S41) + b;
285          d = rotateLeft((d + K(a, b, c) + X[11] + 0xbd3af235), S42) + a;
286          c = rotateLeft((c + K(d, a, b) + X[ 2] + 0x2ad7d2bb), S43) + d;
287          b = rotateLeft((b + K(c, d, a) + X[ 9] + 0xeb86d391), S44) + c;
288  
289          H1 += a;
290          H2 += b;
291          H3 += c;
292          H4 += d;
293  
294          //
295          // reset the offset and clean out the word buffer.
296          //
297          xOff = 0;
298          for (int i = 0; i != X.length; i++)
299          {
300              X[i] = 0;
301          }
302      }
303  }
304