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