Merge pull request #799 from kebby/master
[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,2010 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 #if !INSIDE_CORLIB
39         public
40 #endif
41         class MD2Managed : MD2 {
42
43                 private byte[] state;
44                 private byte[] checksum;
45                 private byte[] buffer;
46                 private int count;
47                 private byte[] x;
48
49                 /// <summary>
50                 /// Permutation of 0..255 constructed from the digits of pi. It gives a
51                 /// "random" nonlinear byte substitution operation.
52                 /// </summary>
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 };
72
73                 private byte[] Padding (int nLength)
74                 {
75                         if (nLength > 0) {
76                                 byte[] padding = new byte [nLength];
77                                 for (int i = 0; i < padding.Length; i++)
78                                         padding[i] = (byte) nLength;
79                                 return padding;
80                         }
81                         return null;
82                 }
83
84                 //--- constructor -----------------------------------------------------------
85                 
86                 public MD2Managed () : base ()
87                 {
88                         // we allocate the context memory
89                         state = new byte [16];
90                         checksum = new byte [16];
91                         buffer = new byte [16];
92                         x = new byte [48];
93                         // the initialize our context
94                         Initialize ();
95                 }
96
97                 public override void Initialize ()
98                 {
99                         count = 0;
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);
105                 }
106
107                 protected override void HashCore (byte[] array, int ibStart, int cbSize)
108                 {
109                         int i;
110
111                         /* Update number of bytes mod 16 */
112                         int index = count;
113                         count = (int) (index + cbSize) & 0xf;
114
115                         int partLen = 16 - index;
116
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);
123
124                                 for (i = partLen; i + 15 < cbSize; i += 16) {
125                                         // MD2Transform (context->state, context->checksum, &input[i]);
126                                         MD2Transform (state, checksum, array, ibStart + i);
127                                 }
128
129                                 index = 0;
130                         }
131                         else
132                                 i = 0;
133
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));
137                 }
138
139                 protected override byte[] HashFinal ()
140                 {
141                         // Pad out to multiple of 16. 
142                         int index = count;
143                         int padLen = 16 - index;
144
145                         // is padding needed ? required if length not a multiple of 16.
146                         if (padLen > 0)
147                                 HashCore (Padding (padLen), 0, padLen);
148
149                         // Extend with checksum 
150                         HashCore (checksum, 0, 16);
151
152                         // Store state in digest
153                         byte[] digest = (byte[]) state.Clone ();
154
155                         // Zeroize sensitive information.
156                         Initialize ();
157
158                         return digest;
159                 }
160
161                 //--- private methods ---------------------------------------------------
162
163                 /// <summary>
164                 /// MD2 basic transformation. Transforms state and updates checksum
165                 /// based on block. 
166                 /// </summary>
167                 private void MD2Transform (byte[] state, byte[] checksum, byte[] block, int index)
168                 {
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);
174
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]);
178
179                         /* Encrypt block (18 rounds). */
180                         int t = 0;
181                         for (int i = 0; i < 18; i++) {
182                                 for (int j = 0; j < 48; j++ ) 
183                                         t = x [j] ^= PI_SUBST [t];
184                                 t = (t + i) & 0xff;
185                         }
186
187                         /* Save new state */
188                         // MD2_memcpy ((POINTER)state, (POINTER)x, 16);
189                         Buffer.BlockCopy (x, 0, state, 0, 16);
190
191                         /* Update checksum. */
192                         t = checksum [15];
193                         for (int i = 0; i < 16; i++)
194                                 t = checksum [i] ^= PI_SUBST [block [index + i] ^ t];
195                 }
196         }
197 }