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