2 // MD4Managed.cs - Message Digest 4 Managed Implementation
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005,2010 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 namespace Mono.Security.Cryptography {
35 // a. RFC1320: The MD4 Message-Digest Algorithm
36 // http://www.ietf.org/rfc/rfc1320.txt
41 class MD4Managed : MD4 {
44 private byte[] buffer;
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;
61 private byte[] digest;
63 //--- constructor -----------------------------------------------------------
67 // we allocate the context memory
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
74 // the initialize our context
78 public override void Initialize ()
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);
91 protected override void HashCore (byte[] array, int ibStart, int cbSize)
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))
99 count [1] += (uint) (cbSize >> 29);
101 int partLen = 64 - index;
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);
109 for (i = partLen; i + 63 < cbSize; i += 64) {
110 // MD4Transform (context->state, &input[i]);
111 MD4Transform (state, array, ibStart + i);
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));
122 protected override byte[] HashFinal ()
124 /* Save number of bits */
125 byte[] bits = new byte [8];
126 Encode (bits, count);
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);
133 /* Append length (before padding) */
134 HashCore (bits, 0, 8);
136 /* Store state in digest */
137 Encode (digest, state);
139 // Zeroize sensitive information.
145 //--- private methods ---------------------------------------------------
147 private byte[] Padding (int nLength)
150 byte[] padding = new byte [nLength];
157 /* F, G and H are basic MD4 functions. */
158 private uint F (uint x, uint y, uint z)
160 return (uint) (((x) & (y)) | ((~x) & (z)));
163 private uint G (uint x, uint y, uint z)
165 return (uint) (((x) & (y)) | ((x) & (z)) | ((y) & (z)));
168 private uint H (uint x, uint y, uint z)
170 return (uint) ((x) ^ (y) ^ (z));
173 /* ROTATE_LEFT rotates x left n bits. */
174 private uint ROL (uint x, byte n)
176 return (uint) (((x) << (n)) | ((x) >> (32-(n))));
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)
183 a += F (b, c, d) + x;
187 private void GG (ref uint a, uint b, uint c, uint d, uint x, byte s)
189 a += G (b, c, d) + x + 0x5a827999;
193 private void HH (ref uint a, uint b, uint c, uint d, uint x, byte s)
195 a += H (b, c, d) + x + 0x6ed9eba1;
199 private void Encode (byte[] output, uint[] input)
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);
209 private void Decode (uint[] output, byte[] input, int index)
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));
216 private void MD4Transform (uint[] state, byte[] block, int index)
223 Decode (x, block, index);
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 */
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 */
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 */