2 // MD4Managed.cs - Message Digest 4 Managed Implementation
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System.Security.Cryptography;
13 namespace Mono.Security.Cryptography {
16 // a. RFC1320: The MD4 Message-Digest Algorithm
17 // http://www.ietf.org/rfc/rfc1320.txt
19 public class MD4Managed : MD4 {
22 private byte[] buffer;
26 private const int S11 = 3;
27 private const int S12 = 7;
28 private const int S13 = 11;
29 private const int S14 = 19;
30 private const int S21 = 3;
31 private const int S22 = 5;
32 private const int S23 = 9;
33 private const int S24 = 13;
34 private const int S31 = 3;
35 private const int S32 = 9;
36 private const int S33 = 11;
37 private const int S34 = 15;
39 private byte[] digest;
41 //--- constructor -----------------------------------------------------------
43 public MD4Managed () : base ()
45 // we allocate the context memory
48 buffer = new byte [64];
49 digest = new byte [16];
50 // the initialize our context
54 public override void Initialize ()
58 state [0] = 0x67452301;
59 state [1] = 0xefcdab89;
60 state [2] = 0x98badcfe;
61 state [3] = 0x10325476;
62 // temporary buffer in MD4Transform that we don't want to keep allocate on each iteration
64 Array.Clear (buffer, 0, 64);
67 protected override void HashCore (byte[] array, int ibStart, int cbSize)
69 /* Compute number of bytes mod 64 */
70 int index = (int) ((count [0] >> 3) & 0x3F);
71 /* Update number of bits */
72 count [0] += (uint) (cbSize << 3);
73 if (count [0] < (cbSize << 3))
75 count [1] += (uint) (cbSize >> 29);
77 int partLen = 64 - index;
79 /* Transform as many times as possible. */
80 if (cbSize >= partLen) {
81 //MD4_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
82 Buffer.BlockCopy (array, ibStart, buffer, index, partLen);
83 MD4Transform (state, buffer, 0);
85 for (i = partLen; i + 63 < cbSize; i += 64) {
86 // MD4Transform (context->state, &input[i]);
87 MD4Transform (state, array, i);
93 /* Buffer remaining input */
94 //MD4_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
95 Buffer.BlockCopy (array, ibStart + i, buffer, index, (cbSize-i));
98 protected override byte[] HashFinal ()
100 /* Save number of bits */
101 byte[] bits = new byte [8];
102 Encode (bits, count);
104 /* Pad out to 56 mod 64. */
105 uint index = ((count [0] >> 3) & 0x3f);
106 int padLen = (int) ((index < 56) ? (56 - index) : (120 - index));
107 HashCore (Padding (padLen), 0, padLen);
109 /* Append length (before padding) */
110 HashCore (bits, 0, 8);
112 /* Store state in digest */
113 Encode (digest, state);
115 // Zeroize sensitive information.
121 //--- private methods ---------------------------------------------------
123 private byte[] Padding (int nLength)
126 byte[] padding = new byte [nLength];
133 /* F, G and H are basic MD4 functions. */
134 private uint F (uint x, uint y, uint z)
136 return (uint) (((x) & (y)) | ((~x) & (z)));
139 private uint G (uint x, uint y, uint z)
141 return (uint) (((x) & (y)) | ((x) & (z)) | ((y) & (z)));
144 private uint H (uint x, uint y, uint z)
146 return (uint) ((x) ^ (y) ^ (z));
149 /* ROTATE_LEFT rotates x left n bits. */
150 private uint ROL (uint x, byte n)
152 return (uint) (((x) << (n)) | ((x) >> (32-(n))));
155 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
156 /* Rotation is separate from addition to prevent recomputation */
157 private void FF (ref uint a, uint b, uint c, uint d, uint x, byte s)
159 a += F (b, c, d) + x;
163 private void GG (ref uint a, uint b, uint c, uint d, uint x, byte s)
165 a += G (b, c, d) + x + 0x5a827999;
169 private void HH (ref uint a, uint b, uint c, uint d, uint x, byte s)
171 a += H (b, c, d) + x + 0x6ed9eba1;
175 private void Encode (byte[] output, uint[] input)
177 for (int i = 0, j = 0; j < output.Length; i++, j += 4) {
178 output [j] = (byte)(input [i] & 0xff);
179 output [j+1] = (byte)((input [i] >> 8) & 0xff);
180 output [j+2] = (byte)((input [i] >> 16) & 0xff);
181 output [j+3] = (byte)(input [i] >> 24);
185 private void Decode (uint[] output, byte[] input, int index)
187 for (int i = 0, j = index; i < output.Length; i++, j += 4) {
188 output [i] = (uint) ((input [j]) | (input [j+1] << 8) | (input [j+2] << 16) | (input [j+3] << 24));
192 private void MD4Transform (uint[] state, byte[] block, int index)
198 uint[] x = new uint [16];
200 Decode (x, block, index);
203 FF (ref a, b, c, d, x[ 0], S11); /* 1 */
204 FF (ref d, a, b, c, x[ 1], S12); /* 2 */
205 FF (ref c, d, a, b, x[ 2], S13); /* 3 */
206 FF (ref b, c, d, a, x[ 3], S14); /* 4 */
207 FF (ref a, b, c, d, x[ 4], S11); /* 5 */
208 FF (ref d, a, b, c, x[ 5], S12); /* 6 */
209 FF (ref c, d, a, b, x[ 6], S13); /* 7 */
210 FF (ref b, c, d, a, x[ 7], S14); /* 8 */
211 FF (ref a, b, c, d, x[ 8], S11); /* 9 */
212 FF (ref d, a, b, c, x[ 9], S12); /* 10 */
213 FF (ref c, d, a, b, x[10], S13); /* 11 */
214 FF (ref b, c, d, a, x[11], S14); /* 12 */
215 FF (ref a, b, c, d, x[12], S11); /* 13 */
216 FF (ref d, a, b, c, x[13], S12); /* 14 */
217 FF (ref c, d, a, b, x[14], S13); /* 15 */
218 FF (ref b, c, d, a, x[15], S14); /* 16 */
221 GG (ref a, b, c, d, x[ 0], S21); /* 17 */
222 GG (ref d, a, b, c, x[ 4], S22); /* 18 */
223 GG (ref c, d, a, b, x[ 8], S23); /* 19 */
224 GG (ref b, c, d, a, x[12], S24); /* 20 */
225 GG (ref a, b, c, d, x[ 1], S21); /* 21 */
226 GG (ref d, a, b, c, x[ 5], S22); /* 22 */
227 GG (ref c, d, a, b, x[ 9], S23); /* 23 */
228 GG (ref b, c, d, a, x[13], S24); /* 24 */
229 GG (ref a, b, c, d, x[ 2], S21); /* 25 */
230 GG (ref d, a, b, c, x[ 6], S22); /* 26 */
231 GG (ref c, d, a, b, x[10], S23); /* 27 */
232 GG (ref b, c, d, a, x[14], S24); /* 28 */
233 GG (ref a, b, c, d, x[ 3], S21); /* 29 */
234 GG (ref d, a, b, c, x[ 7], S22); /* 30 */
235 GG (ref c, d, a, b, x[11], S23); /* 31 */
236 GG (ref b, c, d, a, x[15], S24); /* 32 */
238 HH (ref a, b, c, d, x[ 0], S31); /* 33 */
239 HH (ref d, a, b, c, x[ 8], S32); /* 34 */
240 HH (ref c, d, a, b, x[ 4], S33); /* 35 */
241 HH (ref b, c, d, a, x[12], S34); /* 36 */
242 HH (ref a, b, c, d, x[ 2], S31); /* 37 */
243 HH (ref d, a, b, c, x[10], S32); /* 38 */
244 HH (ref c, d, a, b, x[ 6], S33); /* 39 */
245 HH (ref b, c, d, a, x[14], S34); /* 40 */
246 HH (ref a, b, c, d, x[ 1], S31); /* 41 */
247 HH (ref d, a, b, c, x[ 9], S32); /* 42 */
248 HH (ref c, d, a, b, x[ 5], S33); /* 43 */
249 HH (ref b, c, d, a, x[13], S34); /* 44 */
250 HH (ref a, b, c, d, x[ 3], S31); /* 45 */
251 HH (ref d, a, b, c, x[11], S32); /* 46 */
252 HH (ref c, d, a, b, x[ 7], S33); /* 47 */
253 HH (ref b, c, d, a, x[15], S34); /* 48 */
260 /* Zeroize sensitive information. */
261 Array.Clear (x, 0, 16);