[xbuild] Actually delete common files (CopyLocal) during Clean.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / MD4Managed.cs
1 //
2 // MD4Managed.cs - Message Digest 4 Managed Implementation
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005,2010 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31
32 namespace Mono.Security.Cryptography { 
33
34         // References:
35         // a.     RFC1320: The MD4 Message-Digest Algorithm
36         //        http://www.ietf.org/rfc/rfc1320.txt
37
38 #if INSIDE_SYSTEM
39         internal
40 #else
41         public
42 #endif
43         class MD4Managed : MD4 {
44
45                 private uint[] state;
46                 private byte[] buffer;
47                 private uint[] count;
48                 private uint[] x;
49
50                 private const int S11 = 3;
51                 private const int S12 = 7;
52                 private const int S13 = 11;
53                 private const int S14 = 19;
54                 private const int S21 = 3;
55                 private const int S22 = 5;
56                 private const int S23 = 9;
57                 private const int S24 = 13;
58                 private const int S31 = 3;
59                 private const int S32 = 9;
60                 private const int S33 = 11;
61                 private const int S34 = 15;
62
63                 private byte[] digest;
64
65                 //--- constructor -----------------------------------------------------------
66                 
67                 public MD4Managed ()
68                 {
69                         // we allocate the context memory
70                         state = new uint [4];
71                         count = new uint [2];
72                         buffer = new byte [64];
73                         digest = new byte [16];
74                         // temporary buffer in MD4Transform that we don't want to keep allocate on each iteration
75                         x = new uint [16];
76                         // the initialize our context
77                         Initialize ();
78                 }
79
80                 public override void Initialize ()
81                 {
82                         count [0] = 0;
83                         count [1] = 0;
84                         state [0] = 0x67452301;
85                         state [1] = 0xefcdab89;
86                         state [2] = 0x98badcfe;
87                         state [3] = 0x10325476;
88                         // Zeroize sensitive information
89                         Array.Clear (buffer, 0, 64);
90                         Array.Clear (x, 0, 16);
91                 }
92
93                 protected override void HashCore (byte[] array, int ibStart, int cbSize)
94                 {
95                         /* Compute number of bytes mod 64 */
96                         int index = (int) ((count [0] >> 3) & 0x3F);
97                         /* Update number of bits */
98                         count [0] += (uint) (cbSize << 3);
99                         if (count [0] < (cbSize << 3))
100                                 count [1]++;
101                         count [1] += (uint) (cbSize >> 29);
102
103                         int partLen = 64 - index;
104                         int i = 0;
105                         /* Transform as many times as possible. */
106                         if (cbSize >= partLen) {
107                                 //MD4_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
108                                 Buffer.BlockCopy (array, ibStart, buffer, index, partLen);
109                                 MD4Transform (state, buffer, 0);
110
111                                 for (i = partLen; i + 63 < cbSize; i += 64) {
112                                         // MD4Transform (context->state, &input[i]);
113                                         MD4Transform (state, array, ibStart + i);
114                                 }
115
116                                 index = 0;
117                         }
118
119                         /* Buffer remaining input */
120                         //MD4_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
121                         Buffer.BlockCopy (array, ibStart + i, buffer, index, (cbSize-i));
122                 }
123
124                 protected override byte[] HashFinal ()
125                 {
126                         /* Save number of bits */
127                         byte[] bits = new byte [8];
128                         Encode (bits, count);
129
130                         /* Pad out to 56 mod 64. */
131                         uint index = ((count [0] >> 3) & 0x3f);
132                         int padLen = (int) ((index < 56) ? (56 - index) : (120 - index));
133                         HashCore (Padding (padLen), 0, padLen);
134
135                         /* Append length (before padding) */
136                         HashCore (bits, 0, 8);
137
138                         /* Store state in digest */
139                         Encode (digest, state);
140
141                         // Zeroize sensitive information.
142                         Initialize ();
143
144                         return digest;
145                 }
146
147                 //--- private methods ---------------------------------------------------
148
149                 private byte[] Padding (int nLength) 
150                 {
151                         if (nLength > 0) {
152                                 byte[] padding = new byte [nLength];
153                                 padding [0] = 0x80;
154                                 return padding;
155                         }
156                         return null;
157                 }
158
159                 /* F, G and H are basic MD4 functions. */
160                 private uint F (uint x, uint y, uint z) 
161                 {
162                         return (uint) (((x) & (y)) | ((~x) & (z)));
163                 }
164
165                 private uint G (uint x, uint y, uint z) 
166                 {
167                         return (uint) (((x) & (y)) | ((x) & (z)) | ((y) & (z)));
168                 }
169
170                 private uint H (uint x, uint y, uint z) 
171                 {
172                         return (uint) ((x) ^ (y) ^ (z));
173                 }
174
175                 /* ROTATE_LEFT rotates x left n bits. */
176                 private uint ROL (uint x, byte n) 
177                 {
178                         return (uint) (((x) << (n)) | ((x) >> (32-(n))));
179                 }
180
181                 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
182                 /* Rotation is separate from addition to prevent recomputation */
183                 private void FF (ref uint a, uint b, uint c, uint d, uint x, byte s) 
184                 {
185                         a += F (b, c, d) + x;
186                         a = ROL (a, s);
187                 }
188
189                 private void GG (ref uint a, uint b, uint c, uint d, uint x, byte s) 
190                 {
191                         a += G (b, c, d) + x + 0x5a827999;
192                         a = ROL (a, s);
193                 }
194
195                 private void HH (ref uint a, uint b, uint c, uint d, uint x, byte s) 
196                 {
197                         a += H (b, c, d) + x + 0x6ed9eba1;
198                         a = ROL (a, s);
199                 }
200
201                 private void Encode (byte[] output, uint[] input)
202                 {
203                         for (int i = 0, j = 0; j < output.Length; i++, j += 4) {
204                                 output [j]   = (byte)(input [i]);
205                                 output [j+1] = (byte)(input [i] >> 8);
206                                 output [j+2] = (byte)(input [i] >> 16);
207                                 output [j+3] = (byte)(input [i] >> 24);
208                         }
209                 }
210
211                 private void Decode (uint[] output, byte[] input, int index) 
212                 {
213                         for (int i = 0, j = index; i < output.Length; i++, j += 4) {
214                                 output [i] = (uint) ((input [j]) | (input [j+1] << 8) | (input [j+2] << 16) | (input [j+3] << 24));
215                         }
216                 }
217
218                 private void MD4Transform (uint[] state, byte[] block, int index)
219                 {
220                         uint a = state [0];
221                         uint b = state [1];
222                         uint c = state [2];
223                         uint d = state [3];
224
225                         Decode (x, block, index);
226
227                         /* Round 1 */
228                         FF (ref a, b, c, d, x[ 0], S11); /* 1 */
229                         FF (ref d, a, b, c, x[ 1], S12); /* 2 */
230                         FF (ref c, d, a, b, x[ 2], S13); /* 3 */
231                         FF (ref b, c, d, a, x[ 3], S14); /* 4 */
232                         FF (ref a, b, c, d, x[ 4], S11); /* 5 */
233                         FF (ref d, a, b, c, x[ 5], S12); /* 6 */
234                         FF (ref c, d, a, b, x[ 6], S13); /* 7 */
235                         FF (ref b, c, d, a, x[ 7], S14); /* 8 */
236                         FF (ref a, b, c, d, x[ 8], S11); /* 9 */
237                         FF (ref d, a, b, c, x[ 9], S12); /* 10 */
238                         FF (ref c, d, a, b, x[10], S13); /* 11 */
239                         FF (ref b, c, d, a, x[11], S14); /* 12 */
240                         FF (ref a, b, c, d, x[12], S11); /* 13 */
241                         FF (ref d, a, b, c, x[13], S12); /* 14 */
242                         FF (ref c, d, a, b, x[14], S13); /* 15 */
243                         FF (ref b, c, d, a, x[15], S14); /* 16 */
244
245                         /* Round 2 */
246                         GG (ref a, b, c, d, x[ 0], S21); /* 17 */
247                         GG (ref d, a, b, c, x[ 4], S22); /* 18 */
248                         GG (ref c, d, a, b, x[ 8], S23); /* 19 */
249                         GG (ref b, c, d, a, x[12], S24); /* 20 */
250                         GG (ref a, b, c, d, x[ 1], S21); /* 21 */
251                         GG (ref d, a, b, c, x[ 5], S22); /* 22 */
252                         GG (ref c, d, a, b, x[ 9], S23); /* 23 */
253                         GG (ref b, c, d, a, x[13], S24); /* 24 */
254                         GG (ref a, b, c, d, x[ 2], S21); /* 25 */
255                         GG (ref d, a, b, c, x[ 6], S22); /* 26 */
256                         GG (ref c, d, a, b, x[10], S23); /* 27 */
257                         GG (ref b, c, d, a, x[14], S24); /* 28 */
258                         GG (ref a, b, c, d, x[ 3], S21); /* 29 */
259                         GG (ref d, a, b, c, x[ 7], S22); /* 30 */
260                         GG (ref c, d, a, b, x[11], S23); /* 31 */
261                         GG (ref b, c, d, a, x[15], S24); /* 32 */
262
263                         HH (ref a, b, c, d, x[ 0], S31); /* 33 */
264                         HH (ref d, a, b, c, x[ 8], S32); /* 34 */
265                         HH (ref c, d, a, b, x[ 4], S33); /* 35 */
266                         HH (ref b, c, d, a, x[12], S34); /* 36 */
267                         HH (ref a, b, c, d, x[ 2], S31); /* 37 */
268                         HH (ref d, a, b, c, x[10], S32); /* 38 */
269                         HH (ref c, d, a, b, x[ 6], S33); /* 39 */
270                         HH (ref b, c, d, a, x[14], S34); /* 40 */
271                         HH (ref a, b, c, d, x[ 1], S31); /* 41 */
272                         HH (ref d, a, b, c, x[ 9], S32); /* 42 */
273                         HH (ref c, d, a, b, x[ 5], S33); /* 43 */
274                         HH (ref b, c, d, a, x[13], S34); /* 44 */
275                         HH (ref a, b, c, d, x[ 3], S31); /* 45 */
276                         HH (ref d, a, b, c, x[11], S32); /* 46 */
277                         HH (ref c, d, a, b, x[ 7], S33); /* 47 */
278                         HH (ref b, c, d, a, x[15], S34); /* 48 */
279
280                         state [0] += a;
281                         state [1] += b;
282                         state [2] += c;
283                         state [3] += d;
284                 }
285         }
286 }