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