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