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 // (C) 2004 Novell (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 namespace Mono
.Security
.Cryptography
{
37 // a. RFC1320: The MD4 Message-Digest Algorithm
38 // http://www.ietf.org/rfc/rfc1320.txt
40 public class MD4Managed
: MD4
{
43 private byte[] buffer
;
47 private const int S11
= 3;
48 private const int S12
= 7;
49 private const int S13
= 11;
50 private const int S14
= 19;
51 private const int S21
= 3;
52 private const int S22
= 5;
53 private const int S23
= 9;
54 private const int S24
= 13;
55 private const int S31
= 3;
56 private const int S32
= 9;
57 private const int S33
= 11;
58 private const int S34
= 15;
60 private byte[] digest
;
62 //--- constructor -----------------------------------------------------------
64 public MD4Managed () : base ()
66 // we allocate the context memory
69 buffer
= new byte [64];
70 digest
= new byte [16];
71 // the initialize our context
75 public override void Initialize ()
79 state
[0] = 0x67452301;
80 state
[1] = 0xefcdab89;
81 state
[2] = 0x98badcfe;
82 state
[3] = 0x10325476;
83 // temporary buffer in MD4Transform that we don't want to keep allocate on each iteration
85 Array
.Clear (buffer
, 0, 64);
88 protected override void HashCore (byte[] array
, int ibStart
, int cbSize
)
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))
96 count
[1] += (uint) (cbSize
>> 29);
98 int partLen
= 64 - index
;
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);
106 for (i
= partLen
; i
+ 63 < cbSize
; i
+= 64) {
107 // MD4Transform (context->state, &input[i]);
108 MD4Transform (state
, array
, i
);
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
));
119 protected override byte[] HashFinal ()
121 /* Save number of bits */
122 byte[] bits
= new byte [8];
123 Encode (bits
, count
);
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
);
130 /* Append length (before padding) */
131 HashCore (bits
, 0, 8);
133 /* Store state in digest */
134 Encode (digest
, state
);
136 // Zeroize sensitive information.
142 //--- private methods ---------------------------------------------------
144 private byte[] Padding (int nLength
)
147 byte[] padding
= new byte [nLength
];
154 /* F, G and H are basic MD4 functions. */
155 private uint F (uint x
, uint y
, uint z
)
157 return (uint) (((x
) & (y
)) | ((~x
) & (z
)));
160 private uint G (uint x
, uint y
, uint z
)
162 return (uint) (((x
) & (y
)) | ((x
) & (z
)) | ((y
) & (z
)));
165 private uint H (uint x
, uint y
, uint z
)
167 return (uint) ((x
) ^
(y
) ^
(z
));
170 /* ROTATE_LEFT rotates x left n bits. */
171 private uint ROL (uint x
, byte n
)
173 return (uint) (((x
) << (n
)) | ((x
) >> (32-(n
))));
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
)
180 a
+= F (b
, c
, d
) + x
;
184 private void GG (ref uint a
, uint b
, uint c
, uint d
, uint x
, byte s
)
186 a
+= G (b
, c
, d
) + x
+ 0x5a827999;
190 private void HH (ref uint a
, uint b
, uint c
, uint d
, uint x
, byte s
)
192 a
+= H (b
, c
, d
) + x
+ 0x6ed9eba1;
196 private void Encode (byte[] output
, uint[] input
)
198 for (int i
= 0, j
= 0; j
< output
.Length
; i
++, j
+= 4) {
199 output
[j
] = (byte)(input
[i
] & 0xff);
200 output
[j
+1] = (byte)((input
[i
] >> 8) & 0xff);
201 output
[j
+2] = (byte)((input
[i
] >> 16) & 0xff);
202 output
[j
+3] = (byte)(input
[i
] >> 24);
206 private void Decode (uint[] output
, byte[] input
, int index
)
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));
213 private void MD4Transform (uint[] state
, byte[] block
, int index
)
220 Decode (x
, block
, index
);
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 */
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 */
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 */
280 /* Zeroize sensitive information. */
281 Array
.Clear (x
, 0, 16);