* FileSystemInfo.cs: corrected COM visibility of UTC properties
[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 (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Security.Cryptography;
12
13 namespace Mono.Security.Cryptography { 
14
15         // References:
16         // a.     RFC1320: The MD4 Message-Digest Algorithm
17         //        http://www.ietf.org/rfc/rfc1320.txt
18
19         public class MD4Managed : MD4 {
20
21                 private uint[] state;
22                 private byte[] buffer;
23                 private uint[] count;
24                 private uint[] x;
25
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;
38
39                 private byte[] digest;
40
41                 //--- constructor -----------------------------------------------------------
42                 
43                 public MD4Managed () : base ()
44                 {
45                         // we allocate the context memory
46                         state = new uint [4];
47                         count = new uint [2];
48                         buffer = new byte [64];
49                         digest = new byte [16];
50                         // the initialize our context
51                         Initialize ();
52                 }
53
54                 public override void Initialize ()
55                 {
56                         count [0] = 0;
57                         count [1] = 0;
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
63                         x = new uint [16];
64                         Array.Clear (buffer, 0, 64);
65                 }
66
67                 protected override void HashCore (byte[] array, int ibStart, int cbSize)
68                 {
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))
74                                 count [1]++;
75                         count [1] += (uint) (cbSize >> 29);
76
77                         int partLen = 64 - index;
78                         int i = 0;
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);
84
85                                 for (i = partLen; i + 63 < cbSize; i += 64) {
86                                         // MD4Transform (context->state, &input[i]);
87                                         MD4Transform (state, array, i);
88                                 }
89
90                                 index = 0;
91                         }
92
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));
96                 }
97
98                 protected override byte[] HashFinal ()
99                 {
100                         /* Save number of bits */
101                         byte[] bits = new byte [8];
102                         Encode (bits, count);
103
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);
108
109                         /* Append length (before padding) */
110                         HashCore (bits, 0, 8);
111
112                         /* Store state in digest */
113                         Encode (digest, state);
114
115                         // Zeroize sensitive information.
116                         Initialize ();
117
118                         return digest;
119                 }
120
121                 //--- private methods ---------------------------------------------------
122
123                 private byte[] Padding (int nLength) 
124                 {
125                         if (nLength > 0) {
126                                 byte[] padding = new byte [nLength];
127                                 padding [0] = 0x80;
128                                 return padding;
129                         }
130                         return null;
131                 }
132
133                 /* F, G and H are basic MD4 functions. */
134                 private uint F (uint x, uint y, uint z) 
135                 {
136                         return (uint) (((x) & (y)) | ((~x) & (z)));
137                 }
138
139                 private uint G (uint x, uint y, uint z) 
140                 {
141                         return (uint) (((x) & (y)) | ((x) & (z)) | ((y) & (z)));
142                 }
143
144                 private uint H (uint x, uint y, uint z) 
145                 {
146                         return (uint) ((x) ^ (y) ^ (z));
147                 }
148
149                 /* ROTATE_LEFT rotates x left n bits. */
150                 private uint ROL (uint x, byte n) 
151                 {
152                         return (uint) (((x) << (n)) | ((x) >> (32-(n))));
153                 }
154
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) 
158                 {
159                         a += F (b, c, d) + x;
160                         a = ROL (a, s);
161                 }
162
163                 private void GG (ref uint a, uint b, uint c, uint d, uint x, byte s) 
164                 {
165                         a += G (b, c, d) + x + 0x5a827999;
166                         a = ROL (a, s);
167                 }
168
169                 private void HH (ref uint a, uint b, uint c, uint d, uint x, byte s) 
170                 {
171                         a += H (b, c, d) + x + 0x6ed9eba1;
172                         a = ROL (a, s);
173                 }
174
175                 private void Encode (byte[] output, uint[] input)
176                 {
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);
182                         }
183                 }
184
185                 private void Decode (uint[] output, byte[] input, int index) 
186                 {
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));
189                         }
190                 }
191
192                 private void MD4Transform (uint[] state, byte[] block, int index)
193                 {
194                         uint a = state [0];
195                         uint b = state [1];
196                         uint c = state [2];
197                         uint d = state [3];
198                         uint[] x = new uint [16];
199
200                         Decode (x, block, index);
201
202                         /* Round 1 */
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 */
219
220                         /* Round 2 */
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 */
237
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 */
254
255                         state [0] += a;
256                         state [1] += b;
257                         state [2] += c;
258                         state [3] += d;
259
260                         /* Zeroize sensitive information. */
261                         Array.Clear (x, 0, 16);
262                 }
263         }
264 }