[sgen] Scan pinned objects in nursery as part of concurrent mark
[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_CORLIB
39         public
40 #endif
41         class MD4Managed : MD4 {
42
43                 private uint[] state;
44                 private byte[] buffer;
45                 private uint[] count;
46                 private uint[] x;
47
48                 private const int S11 = 3;
49                 private const int S12 = 7;
50                 private const int S13 = 11;
51                 private const int S14 = 19;
52                 private const int S21 = 3;
53                 private const int S22 = 5;
54                 private const int S23 = 9;
55                 private const int S24 = 13;
56                 private const int S31 = 3;
57                 private const int S32 = 9;
58                 private const int S33 = 11;
59                 private const int S34 = 15;
60
61                 private byte[] digest;
62
63                 //--- constructor -----------------------------------------------------------
64                 
65                 public MD4Managed ()
66                 {
67                         // we allocate the context memory
68                         state = new uint [4];
69                         count = new uint [2];
70                         buffer = new byte [64];
71                         digest = new byte [16];
72                         // temporary buffer in MD4Transform that we don't want to keep allocate on each iteration
73                         x = new uint [16];
74                         // the initialize our context
75                         Initialize ();
76                 }
77
78                 public override void Initialize ()
79                 {
80                         count [0] = 0;
81                         count [1] = 0;
82                         state [0] = 0x67452301;
83                         state [1] = 0xefcdab89;
84                         state [2] = 0x98badcfe;
85                         state [3] = 0x10325476;
86                         // Zeroize sensitive information
87                         Array.Clear (buffer, 0, 64);
88                         Array.Clear (x, 0, 16);
89                 }
90
91                 protected override void HashCore (byte[] array, int ibStart, int cbSize)
92                 {
93                         /* Compute number of bytes mod 64 */
94                         int index = (int) ((count [0] >> 3) & 0x3F);
95                         /* Update number of bits */
96                         count [0] += (uint) (cbSize << 3);
97                         if (count [0] < (cbSize << 3))
98                                 count [1]++;
99                         count [1] += (uint) (cbSize >> 29);
100
101                         int partLen = 64 - index;
102                         int i = 0;
103                         /* Transform as many times as possible. */
104                         if (cbSize >= partLen) {
105                                 //MD4_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
106                                 Buffer.BlockCopy (array, ibStart, buffer, index, partLen);
107                                 MD4Transform (state, buffer, 0);
108
109                                 for (i = partLen; i + 63 < cbSize; i += 64) {
110                                         // MD4Transform (context->state, &input[i]);
111                                         MD4Transform (state, array, ibStart + i);
112                                 }
113
114                                 index = 0;
115                         }
116
117                         /* Buffer remaining input */
118                         //MD4_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
119                         Buffer.BlockCopy (array, ibStart + i, buffer, index, (cbSize-i));
120                 }
121
122                 protected override byte[] HashFinal ()
123                 {
124                         /* Save number of bits */
125                         byte[] bits = new byte [8];
126                         Encode (bits, count);
127
128                         /* Pad out to 56 mod 64. */
129                         uint index = ((count [0] >> 3) & 0x3f);
130                         int padLen = (int) ((index < 56) ? (56 - index) : (120 - index));
131                         HashCore (Padding (padLen), 0, padLen);
132
133                         /* Append length (before padding) */
134                         HashCore (bits, 0, 8);
135
136                         /* Store state in digest */
137                         Encode (digest, state);
138
139                         // Zeroize sensitive information.
140                         Initialize ();
141
142                         return digest;
143                 }
144
145                 //--- private methods ---------------------------------------------------
146
147                 private byte[] Padding (int nLength) 
148                 {
149                         if (nLength > 0) {
150                                 byte[] padding = new byte [nLength];
151                                 padding [0] = 0x80;
152                                 return padding;
153                         }
154                         return null;
155                 }
156
157                 /* F, G and H are basic MD4 functions. */
158                 private uint F (uint x, uint y, uint z) 
159                 {
160                         return (uint) (((x) & (y)) | ((~x) & (z)));
161                 }
162
163                 private uint G (uint x, uint y, uint z) 
164                 {
165                         return (uint) (((x) & (y)) | ((x) & (z)) | ((y) & (z)));
166                 }
167
168                 private uint H (uint x, uint y, uint z) 
169                 {
170                         return (uint) ((x) ^ (y) ^ (z));
171                 }
172
173                 /* ROTATE_LEFT rotates x left n bits. */
174                 private uint ROL (uint x, byte n) 
175                 {
176                         return (uint) (((x) << (n)) | ((x) >> (32-(n))));
177                 }
178
179                 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
180                 /* Rotation is separate from addition to prevent recomputation */
181                 private void FF (ref uint a, uint b, uint c, uint d, uint x, byte s) 
182                 {
183                         a += F (b, c, d) + x;
184                         a = ROL (a, s);
185                 }
186
187                 private void GG (ref uint a, uint b, uint c, uint d, uint x, byte s) 
188                 {
189                         a += G (b, c, d) + x + 0x5a827999;
190                         a = ROL (a, s);
191                 }
192
193                 private void HH (ref uint a, uint b, uint c, uint d, uint x, byte s) 
194                 {
195                         a += H (b, c, d) + x + 0x6ed9eba1;
196                         a = ROL (a, s);
197                 }
198
199                 private void Encode (byte[] output, uint[] input)
200                 {
201                         for (int i = 0, j = 0; j < output.Length; i++, j += 4) {
202                                 output [j]   = (byte)(input [i]);
203                                 output [j+1] = (byte)(input [i] >> 8);
204                                 output [j+2] = (byte)(input [i] >> 16);
205                                 output [j+3] = (byte)(input [i] >> 24);
206                         }
207                 }
208
209                 private void Decode (uint[] output, byte[] input, int index) 
210                 {
211                         for (int i = 0, j = index; i < output.Length; i++, j += 4) {
212                                 output [i] = (uint) ((input [j]) | (input [j+1] << 8) | (input [j+2] << 16) | (input [j+3] << 24));
213                         }
214                 }
215
216                 private void MD4Transform (uint[] state, byte[] block, int index)
217                 {
218                         uint a = state [0];
219                         uint b = state [1];
220                         uint c = state [2];
221                         uint d = state [3];
222
223                         Decode (x, block, index);
224
225                         /* Round 1 */
226                         FF (ref a, b, c, d, x[ 0], S11); /* 1 */
227                         FF (ref d, a, b, c, x[ 1], S12); /* 2 */
228                         FF (ref c, d, a, b, x[ 2], S13); /* 3 */
229                         FF (ref b, c, d, a, x[ 3], S14); /* 4 */
230                         FF (ref a, b, c, d, x[ 4], S11); /* 5 */
231                         FF (ref d, a, b, c, x[ 5], S12); /* 6 */
232                         FF (ref c, d, a, b, x[ 6], S13); /* 7 */
233                         FF (ref b, c, d, a, x[ 7], S14); /* 8 */
234                         FF (ref a, b, c, d, x[ 8], S11); /* 9 */
235                         FF (ref d, a, b, c, x[ 9], S12); /* 10 */
236                         FF (ref c, d, a, b, x[10], S13); /* 11 */
237                         FF (ref b, c, d, a, x[11], S14); /* 12 */
238                         FF (ref a, b, c, d, x[12], S11); /* 13 */
239                         FF (ref d, a, b, c, x[13], S12); /* 14 */
240                         FF (ref c, d, a, b, x[14], S13); /* 15 */
241                         FF (ref b, c, d, a, x[15], S14); /* 16 */
242
243                         /* Round 2 */
244                         GG (ref a, b, c, d, x[ 0], S21); /* 17 */
245                         GG (ref d, a, b, c, x[ 4], S22); /* 18 */
246                         GG (ref c, d, a, b, x[ 8], S23); /* 19 */
247                         GG (ref b, c, d, a, x[12], S24); /* 20 */
248                         GG (ref a, b, c, d, x[ 1], S21); /* 21 */
249                         GG (ref d, a, b, c, x[ 5], S22); /* 22 */
250                         GG (ref c, d, a, b, x[ 9], S23); /* 23 */
251                         GG (ref b, c, d, a, x[13], S24); /* 24 */
252                         GG (ref a, b, c, d, x[ 2], S21); /* 25 */
253                         GG (ref d, a, b, c, x[ 6], S22); /* 26 */
254                         GG (ref c, d, a, b, x[10], S23); /* 27 */
255                         GG (ref b, c, d, a, x[14], S24); /* 28 */
256                         GG (ref a, b, c, d, x[ 3], S21); /* 29 */
257                         GG (ref d, a, b, c, x[ 7], S22); /* 30 */
258                         GG (ref c, d, a, b, x[11], S23); /* 31 */
259                         GG (ref b, c, d, a, x[15], S24); /* 32 */
260
261                         HH (ref a, b, c, d, x[ 0], S31); /* 33 */
262                         HH (ref d, a, b, c, x[ 8], S32); /* 34 */
263                         HH (ref c, d, a, b, x[ 4], S33); /* 35 */
264                         HH (ref b, c, d, a, x[12], S34); /* 36 */
265                         HH (ref a, b, c, d, x[ 2], S31); /* 37 */
266                         HH (ref d, a, b, c, x[10], S32); /* 38 */
267                         HH (ref c, d, a, b, x[ 6], S33); /* 39 */
268                         HH (ref b, c, d, a, x[14], S34); /* 40 */
269                         HH (ref a, b, c, d, x[ 1], S31); /* 41 */
270                         HH (ref d, a, b, c, x[ 9], S32); /* 42 */
271                         HH (ref c, d, a, b, x[ 5], S33); /* 43 */
272                         HH (ref b, c, d, a, x[13], S34); /* 44 */
273                         HH (ref a, b, c, d, x[ 3], S31); /* 45 */
274                         HH (ref d, a, b, c, x[11], S32); /* 46 */
275                         HH (ref c, d, a, b, x[ 7], S33); /* 47 */
276                         HH (ref b, c, d, a, x[15], S34); /* 48 */
277
278                         state [0] += a;
279                         state [1] += b;
280                         state [2] += c;
281                         state [3] += d;
282                 }
283         }
284 }