2 // MD2Managed.cs - Message Digest 2 Managed Implementation
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2001-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. RFC1319: The MD2 Message-Digest Algorithm
36 // http://www.ietf.org/rfc/rfc1319.txt
41 class MD2Managed : MD2 {
44 private byte[] checksum;
45 private byte[] buffer;
50 /// Permutation of 0..255 constructed from the digits of pi. It gives a
51 /// "random" nonlinear byte substitution operation.
53 private static readonly byte[] PI_SUBST = {
54 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
55 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
56 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
57 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
58 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
59 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
60 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
61 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
62 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
63 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
64 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
65 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
66 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
67 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
68 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
69 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
70 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
71 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 };
73 private byte[] Padding (int nLength)
76 byte[] padding = new byte [nLength];
77 for (int i = 0; i < padding.Length; i++)
78 padding[i] = (byte) nLength;
84 //--- constructor -----------------------------------------------------------
86 public MD2Managed () : base ()
88 // we allocate the context memory
89 state = new byte [16];
90 checksum = new byte [16];
91 buffer = new byte [16];
93 // the initialize our context
97 public override void Initialize ()
100 Array.Clear (state, 0, 16);
101 Array.Clear (checksum, 0, 16);
102 Array.Clear (buffer, 0, 16);
103 // Zeroize sensitive information
104 Array.Clear (x, 0, 48);
107 protected override void HashCore (byte[] array, int ibStart, int cbSize)
111 /* Update number of bytes mod 16 */
113 count = (int) (index + cbSize) & 0xf;
115 int partLen = 16 - index;
117 /* Transform as many times as possible. */
118 if (cbSize >= partLen) {
119 // MD2_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
120 Buffer.BlockCopy (array, ibStart, buffer, index, partLen);
121 // MD2Transform (context->state, context->checksum, context->buffer);
122 MD2Transform (state, checksum, buffer, 0);
124 for (i = partLen; i + 15 < cbSize; i += 16) {
125 // MD2Transform (context->state, context->checksum, &input[i]);
126 MD2Transform (state, checksum, array, ibStart + i);
134 /* Buffer remaining input */
135 // MD2_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
136 Buffer.BlockCopy (array, ibStart + i, buffer, index, (cbSize - i));
139 protected override byte[] HashFinal ()
141 // Pad out to multiple of 16.
143 int padLen = 16 - index;
145 // is padding needed ? required if length not a multiple of 16.
147 HashCore (Padding (padLen), 0, padLen);
149 // Extend with checksum
150 HashCore (checksum, 0, 16);
152 // Store state in digest
153 byte[] digest = (byte[]) state.Clone ();
155 // Zeroize sensitive information.
161 //--- private methods ---------------------------------------------------
164 /// MD2 basic transformation. Transforms state and updates checksum
167 private void MD2Transform (byte[] state, byte[] checksum, byte[] block, int index)
169 /* Form encryption block from state, block, state ^ block. */
170 // MD2_memcpy ((POINTER)x, (POINTER)state, 16);
171 Buffer.BlockCopy (state, 0, x, 0, 16);
172 // MD2_memcpy ((POINTER)x+16, (POINTER)block, 16);
173 Buffer.BlockCopy (block, index, x, 16, 16);
175 // for (i = 0; i < 16; i++) x[i+32] = state[i] ^ block[i];
176 for (int i = 0; i < 16; i++)
177 x [i+32] = (byte) ((byte) state [i] ^ (byte) block [index + i]);
179 /* Encrypt block (18 rounds). */
181 for (int i = 0; i < 18; i++) {
182 for (int j = 0; j < 48; j++ )
183 t = x [j] ^= PI_SUBST [t];
188 // MD2_memcpy ((POINTER)state, (POINTER)x, 16);
189 Buffer.BlockCopy (x, 0, state, 0, 16);
191 /* Update checksum. */
193 for (int i = 0; i < 16; i++)
194 t = checksum [i] ^= PI_SUBST [block [index + i] ^ t];