2003/10/01 04:29:09
[org.ibex.core.git] / src / org / bouncycastle / crypto / digests / MD4Digest.java
1 package org.bouncycastle.crypto.digests;
2
3 import org.bouncycastle.crypto.Digest;
4
5 /**
6  * implementation of MD4 as RFC 1320 by R. Rivest, MIT Laboratory for
7  * Computer Science and RSA Data Security, Inc.
8  * <p>
9  * <b>NOTE</b>: This algorithm is only included for backwards compatability
10  * with legacy applications, it's not secure, don't use it for anything new!
11  */
12 public class MD4Digest
13     extends GeneralDigest
14 {
15     private static final int    DIGEST_LENGTH = 16;
16
17     private int     H1, H2, H3, H4;         // IV's
18
19     private int[]   X = new int[16];
20     private int     xOff;
21
22         /**
23          * Standard constructor
24          */
25     public MD4Digest()
26     {
27         reset();
28     }
29
30         /**
31          * Copy constructor.  This will copy the state of the provided
32          * message digest.
33          */
34         public MD4Digest(MD4Digest t)
35         {
36                 super(t);
37
38                 H1 = t.H1;
39                 H2 = t.H2;
40                 H3 = t.H3;
41                 H4 = t.H4;
42
43                 System.arraycopy(t.X, 0, X, 0, t.X.length);
44                 xOff = t.xOff;
45         }
46
47     public String getAlgorithmName()
48     {
49         return "MD4";
50     }
51
52     public int getDigestSize()
53     {
54         return DIGEST_LENGTH;
55     }
56
57     protected void processWord(
58         byte[]  in,
59         int     inOff)
60     {
61         X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
62             | ((in[inOff + 2] & 0xff) << 16) | ((in[inOff + 3] & 0xff) << 24); 
63
64         if (xOff == 16)
65         {
66             processBlock();
67         }
68     }
69
70     protected void processLength(
71         long    bitLength)
72     {
73         if (xOff > 14)
74         {
75             processBlock();
76         }
77
78         X[14] = (int)(bitLength & 0xffffffff);
79         X[15] = (int)(bitLength >>> 32);
80     }
81
82     private void unpackWord(
83         int     word,
84         byte[]  out,
85         int     outOff)
86     {
87         out[outOff]     = (byte)word;
88         out[outOff + 1] = (byte)(word >>> 8);
89         out[outOff + 2] = (byte)(word >>> 16);
90         out[outOff + 3] = (byte)(word >>> 24);
91     }
92
93     public int doFinal(
94         byte[]  out,
95         int     outOff)
96     {
97         finish();
98
99         unpackWord(H1, out, outOff);
100         unpackWord(H2, out, outOff + 4);
101         unpackWord(H3, out, outOff + 8);
102         unpackWord(H4, out, outOff + 12);
103
104         reset();
105
106         return DIGEST_LENGTH;
107     }
108
109     /**
110      * reset the chaining variables to the IV values.
111      */
112     public void reset()
113     {
114         super.reset();
115
116         H1 = 0x67452301;
117         H2 = 0xefcdab89;
118         H3 = 0x98badcfe;
119         H4 = 0x10325476;
120
121         xOff = 0;
122
123         for (int i = 0; i != X.length; i++)
124         {
125             X[i] = 0;
126         }
127     }
128
129     //
130     // round 1 left rotates
131     //
132     private static final int S11 = 3;
133     private static final int S12 = 7;
134     private static final int S13 = 11;
135     private static final int S14 = 19;
136
137     //
138     // round 2 left rotates
139     //
140     private static final int S21 = 3;
141     private static final int S22 = 5;
142     private static final int S23 = 9;
143     private static final int S24 = 13;
144
145     //
146     // round 3 left rotates
147     //
148     private static final int S31 = 3;
149     private static final int S32 = 9;
150     private static final int S33 = 11;
151     private static final int S34 = 15;
152
153     /*
154      * rotate int x left n bits.
155      */
156     private int rotateLeft(
157         int x,
158         int n)
159     {
160         return (x << n) | (x >>> (32 - n));
161     }
162
163     /*
164      * F, G, H and I are the basic MD4 functions.
165      */
166     private int F(
167         int u,
168         int v,
169         int w)
170     {
171         return (u & v) | (~u & w);
172     }
173
174     private int G(
175         int u,
176         int v,
177         int w)
178     {
179         return (u & v) | (u & w) | (v & w);
180     }
181
182     private int H(
183         int u,
184         int v,
185         int w)
186     {
187         return u ^ v ^ w;
188     }
189
190     protected void processBlock()
191     {
192         int a = H1;
193         int b = H2;
194         int c = H3;
195         int d = H4;
196
197         //
198         // Round 1 - F cycle, 16 times.
199         //
200         a = rotateLeft((a + F(b, c, d) + X[ 0]), S11);
201         d = rotateLeft((d + F(a, b, c) + X[ 1]), S12);
202         c = rotateLeft((c + F(d, a, b) + X[ 2]), S13);
203         b = rotateLeft((b + F(c, d, a) + X[ 3]), S14);
204         a = rotateLeft((a + F(b, c, d) + X[ 4]), S11);
205         d = rotateLeft((d + F(a, b, c) + X[ 5]), S12);
206         c = rotateLeft((c + F(d, a, b) + X[ 6]), S13);
207         b = rotateLeft((b + F(c, d, a) + X[ 7]), S14);
208         a = rotateLeft((a + F(b, c, d) + X[ 8]), S11);
209         d = rotateLeft((d + F(a, b, c) + X[ 9]), S12);
210         c = rotateLeft((c + F(d, a, b) + X[10]), S13);
211         b = rotateLeft((b + F(c, d, a) + X[11]), S14);
212         a = rotateLeft((a + F(b, c, d) + X[12]), S11);
213         d = rotateLeft((d + F(a, b, c) + X[13]), S12);
214         c = rotateLeft((c + F(d, a, b) + X[14]), S13);
215         b = rotateLeft((b + F(c, d, a) + X[15]), S14);
216
217         //
218         // Round 2 - G cycle, 16 times.
219         //
220         a = rotateLeft((a + G(b, c, d) + X[ 0] + 0x5a827999), S21);
221         d = rotateLeft((d + G(a, b, c) + X[ 4] + 0x5a827999), S22);
222         c = rotateLeft((c + G(d, a, b) + X[ 8] + 0x5a827999), S23);
223         b = rotateLeft((b + G(c, d, a) + X[12] + 0x5a827999), S24);
224         a = rotateLeft((a + G(b, c, d) + X[ 1] + 0x5a827999), S21);
225         d = rotateLeft((d + G(a, b, c) + X[ 5] + 0x5a827999), S22);
226         c = rotateLeft((c + G(d, a, b) + X[ 9] + 0x5a827999), S23);
227         b = rotateLeft((b + G(c, d, a) + X[13] + 0x5a827999), S24);
228         a = rotateLeft((a + G(b, c, d) + X[ 2] + 0x5a827999), S21);
229         d = rotateLeft((d + G(a, b, c) + X[ 6] + 0x5a827999), S22);
230         c = rotateLeft((c + G(d, a, b) + X[10] + 0x5a827999), S23);
231         b = rotateLeft((b + G(c, d, a) + X[14] + 0x5a827999), S24);
232         a = rotateLeft((a + G(b, c, d) + X[ 3] + 0x5a827999), S21);
233         d = rotateLeft((d + G(a, b, c) + X[ 7] + 0x5a827999), S22);
234         c = rotateLeft((c + G(d, a, b) + X[11] + 0x5a827999), S23);
235         b = rotateLeft((b + G(c, d, a) + X[15] + 0x5a827999), S24);
236
237         //
238         // Round 3 - H cycle, 16 times.
239         //
240         a = rotateLeft((a + H(b, c, d) + X[ 0] + 0x6ed9eba1), S31);
241         d = rotateLeft((d + H(a, b, c) + X[ 8] + 0x6ed9eba1), S32);
242         c = rotateLeft((c + H(d, a, b) + X[ 4] + 0x6ed9eba1), S33);
243         b = rotateLeft((b + H(c, d, a) + X[12] + 0x6ed9eba1), S34);
244         a = rotateLeft((a + H(b, c, d) + X[ 2] + 0x6ed9eba1), S31);
245         d = rotateLeft((d + H(a, b, c) + X[10] + 0x6ed9eba1), S32);
246         c = rotateLeft((c + H(d, a, b) + X[ 6] + 0x6ed9eba1), S33);
247         b = rotateLeft((b + H(c, d, a) + X[14] + 0x6ed9eba1), S34);
248         a = rotateLeft((a + H(b, c, d) + X[ 1] + 0x6ed9eba1), S31);
249         d = rotateLeft((d + H(a, b, c) + X[ 9] + 0x6ed9eba1), S32);
250         c = rotateLeft((c + H(d, a, b) + X[ 5] + 0x6ed9eba1), S33);
251         b = rotateLeft((b + H(c, d, a) + X[13] + 0x6ed9eba1), S34);
252         a = rotateLeft((a + H(b, c, d) + X[ 3] + 0x6ed9eba1), S31);
253         d = rotateLeft((d + H(a, b, c) + X[11] + 0x6ed9eba1), S32);
254         c = rotateLeft((c + H(d, a, b) + X[ 7] + 0x6ed9eba1), S33);
255         b = rotateLeft((b + H(c, d, a) + X[15] + 0x6ed9eba1), S34);
256
257         H1 += a;
258         H2 += b;
259         H3 += c;
260         H4 += d;
261
262         //
263         // reset the offset and clean out the word buffer.
264         //
265         xOff = 0;
266         for (int i = 0; i != X.length; i++)
267         {
268             X[i] = 0;
269         }
270     }
271 }