Jumbo patch for NET_2_0, mscorlib is now clean
[mono.git] / mcs / class / corlib / System.Security.Cryptography / SHA1CryptoServiceProvider.cs
1 //
2 // System.Security.Cryptography.SHA1CryptoServiceProvider.cs
3 //
4 // Authors:
5 //      Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // Copyright 2001 by Matthew S. Ford.
9 // Copyright (C) 2004, 2005, 2008 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 // Note:
32 // The MS Framework includes two (almost) identical class for SHA1.
33 //      SHA1Managed is a 100% managed implementation.
34 //      SHA1CryptoServiceProvider (this file) is a wrapper on CryptoAPI.
35 // Mono must provide those two class for binary compatibility.
36 // In our case both class are wrappers around a managed internal class SHA1Internal.
37
38 using System.Runtime.InteropServices;
39
40 namespace System.Security.Cryptography {
41
42         internal class SHA1Internal {
43         
44                 private const int BLOCK_SIZE_BYTES =  64;
45                 private const int HASH_SIZE_BYTES  =  20;
46                 private uint[] _H;  // these are my chaining variables
47                 private ulong count;
48                 private byte[] _ProcessingBuffer;   // Used to start data when passed less than a block worth.
49                 private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
50                 private uint[] buff;
51
52                 public SHA1Internal () 
53                 {
54                         _H = new uint[5];
55                         _ProcessingBuffer = new byte[BLOCK_SIZE_BYTES];
56                         buff = new uint[80];
57                         
58                         Initialize();
59                 }
60
61                 public void HashCore (byte[] rgb, int ibStart, int cbSize) 
62                 {
63                         int i;
64
65                         if (_ProcessingBufferCount != 0) {
66                                 if (cbSize < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
67                                         System.Buffer.BlockCopy (rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, cbSize);
68                                         _ProcessingBufferCount += cbSize;
69                                         return;
70                                 }
71                                 else {
72                                         i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
73                                         System.Buffer.BlockCopy (rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, i);
74                                         ProcessBlock (_ProcessingBuffer, 0);
75                                         _ProcessingBufferCount = 0;
76                                         ibStart += i;
77                                         cbSize -= i;
78                                 }
79                         }
80
81                         for (i = 0; i < cbSize - cbSize % BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
82                                 ProcessBlock (rgb, (uint)(ibStart + i));
83                         }
84
85                         if (cbSize % BLOCK_SIZE_BYTES != 0) {
86                                 System.Buffer.BlockCopy (rgb, cbSize - cbSize % BLOCK_SIZE_BYTES + ibStart, _ProcessingBuffer, 0, cbSize % BLOCK_SIZE_BYTES);
87                                 _ProcessingBufferCount = cbSize % BLOCK_SIZE_BYTES;
88                         }
89                 }
90         
91                 public byte[] HashFinal () 
92                 {
93                         byte[] hash = new byte[20];
94
95                         ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
96
97                         for (int i=0; i<5; i++) {
98                                 for (int j=0; j<4; j++) {
99                                         hash [i*4+j] = (byte)(_H[i] >> (8*(3-j)));
100                                 }
101                         }
102
103                         return hash;
104                 }
105
106                 public void Initialize () 
107                 {
108                         count = 0;
109                         _ProcessingBufferCount = 0;
110
111                         _H[0] = 0x67452301;
112                         _H[1] = 0xefcdab89;
113                         _H[2] = 0x98badcfe;
114                         _H[3] = 0x10325476;
115                         _H[4] = 0xC3D2E1F0;
116                 }
117
118                 private void ProcessBlock(byte[] inputBuffer, uint inputOffset) 
119                 {
120                         uint a, b, c, d, e;
121
122                         count += BLOCK_SIZE_BYTES;
123
124                         // abc removal would not work on the fields
125                         uint[] _H = this._H;
126                         uint[] buff = this.buff;
127                         InitialiseBuff(buff, inputBuffer, inputOffset);
128                         FillBuff(buff);
129
130                         a = _H[0];
131                         b = _H[1];
132                         c = _H[2];
133                         d = _H[3];
134                         e = _H[4];
135
136                         // This function was unrolled because it seems to be doubling our performance with current compiler/VM.
137                         // Possibly roll up if this changes.
138
139                         // ---- Round 1 --------
140                         int i=0;
141                         while (i < 20)
142                         {
143                                 e += ((a << 5) | (a >> 27)) + (((c ^ d) & b) ^ d) + 0x5A827999 + buff[i];
144                                 b = (b << 30) | (b >> 2);
145
146                                 d += ((e << 5) | (e >> 27)) + (((b ^ c) & a) ^ c) + 0x5A827999 + buff[i+1];
147                                 a = (a << 30) | (a >> 2);
148
149                                 c += ((d << 5) | (d >> 27)) + (((a ^ b) & e) ^ b) + 0x5A827999 + buff[i+2];
150                                 e = (e << 30) | (e >> 2);
151
152                                 b += ((c << 5) | (c >> 27)) + (((e ^ a) & d) ^ a) + 0x5A827999 + buff[i+3];
153                                 d = (d << 30) | (d >> 2);
154
155                                 a += ((b << 5) | (b >> 27)) + (((d ^ e) & c) ^ e) + 0x5A827999 + buff[i+4];
156                                 c = (c << 30) | (c >> 2);
157                                 i += 5;
158                         }
159
160                         // ---- Round 2 --------
161                         while (i < 40)
162                         {
163                                 e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0x6ED9EBA1 + buff[i];
164                                 b = (b << 30) | (b >> 2);
165
166                                 d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0x6ED9EBA1 + buff[i + 1];
167                                 a = (a << 30) | (a >> 2);
168
169                                 c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0x6ED9EBA1 + buff[i + 2];
170                                 e = (e << 30) | (e >> 2);
171
172                                 b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0x6ED9EBA1 + buff[i + 3];
173                                 d = (d << 30) | (d >> 2);
174
175                                 a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0x6ED9EBA1 + buff[i + 4];
176                                 c = (c << 30) | (c >> 2);
177                                 i += 5;
178                         }
179                    
180                         // ---- Round 3 --------
181                         while (i < 60)
182                         {
183                                 e += ((a << 5) | (a >> 27)) + ((b & c) | (b & d) | (c & d)) + 0x8F1BBCDC + buff[i];
184                                 b = (b << 30) | (b >> 2);
185
186                                 d += ((e << 5) | (e >> 27)) + ((a & b) | (a & c) | (b & c)) + 0x8F1BBCDC + buff[i + 1];
187                                 a = (a << 30) | (a >> 2);
188
189                                 c += ((d << 5) | (d >> 27)) + ((e & a) | (e & b) | (a & b)) + 0x8F1BBCDC + buff[i + 2];
190                                 e = (e << 30) | (e >> 2);
191
192                                 b += ((c << 5) | (c >> 27)) + ((d & e) | (d & a) | (e & a)) + 0x8F1BBCDC + buff[i + 3];
193                                 d = (d << 30) | (d >> 2);
194
195                                 a += ((b << 5) | (b >> 27)) + ((c & d) | (c & e) | (d & e)) + 0x8F1BBCDC + buff[i + 4];
196                                 c = (c << 30) | (c >> 2);
197                                 i += 5;
198                         }
199
200                         // ---- Round 4 --------
201                         while (i < 80)
202                         {
203                                 e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0xCA62C1D6 + buff[i];
204                                 b = (b << 30) | (b >> 2);
205
206                                 d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0xCA62C1D6 + buff[i + 1];
207                                 a = (a << 30) | (a >> 2);
208
209                                 c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0xCA62C1D6 + buff[i + 2];
210                                 e = (e << 30) | (e >> 2);
211
212                                 b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0xCA62C1D6 + buff[i + 3];
213                                 d = (d << 30) | (d >> 2);
214
215                                 a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0xCA62C1D6 + buff[i + 4];
216                                 c = (c << 30) | (c >> 2);
217                                 i += 5;
218                         }
219
220                         _H[0] += a;
221                         _H[1] += b;
222                         _H[2] += c;
223                         _H[3] += d;
224                         _H[4] += e;
225                 }
226
227                 private static void InitialiseBuff(uint[] buff, byte[] input, uint inputOffset)
228                 {
229                         buff[0] = (uint)((input[inputOffset + 0] << 24) | (input[inputOffset + 1] << 16) | (input[inputOffset + 2] << 8) | (input[inputOffset + 3]));
230                         buff[1] = (uint)((input[inputOffset + 4] << 24) | (input[inputOffset + 5] << 16) | (input[inputOffset + 6] << 8) | (input[inputOffset + 7]));
231                         buff[2] = (uint)((input[inputOffset + 8] << 24) | (input[inputOffset + 9] << 16) | (input[inputOffset + 10] << 8) | (input[inputOffset + 11]));
232                         buff[3] = (uint)((input[inputOffset + 12] << 24) | (input[inputOffset + 13] << 16) | (input[inputOffset + 14] << 8) | (input[inputOffset + 15]));
233                         buff[4] = (uint)((input[inputOffset + 16] << 24) | (input[inputOffset + 17] << 16) | (input[inputOffset + 18] << 8) | (input[inputOffset + 19]));
234                         buff[5] = (uint)((input[inputOffset + 20] << 24) | (input[inputOffset + 21] << 16) | (input[inputOffset + 22] << 8) | (input[inputOffset + 23]));
235                         buff[6] = (uint)((input[inputOffset + 24] << 24) | (input[inputOffset + 25] << 16) | (input[inputOffset + 26] << 8) | (input[inputOffset + 27]));
236                         buff[7] = (uint)((input[inputOffset + 28] << 24) | (input[inputOffset + 29] << 16) | (input[inputOffset + 30] << 8) | (input[inputOffset + 31]));
237                         buff[8] = (uint)((input[inputOffset + 32] << 24) | (input[inputOffset + 33] << 16) | (input[inputOffset + 34] << 8) | (input[inputOffset + 35]));
238                         buff[9] = (uint)((input[inputOffset + 36] << 24) | (input[inputOffset + 37] << 16) | (input[inputOffset + 38] << 8) | (input[inputOffset + 39]));
239                         buff[10] = (uint)((input[inputOffset + 40] << 24) | (input[inputOffset + 41] << 16) | (input[inputOffset + 42] << 8) | (input[inputOffset + 43]));
240                         buff[11] = (uint)((input[inputOffset + 44] << 24) | (input[inputOffset + 45] << 16) | (input[inputOffset + 46] << 8) | (input[inputOffset + 47]));
241                         buff[12] = (uint)((input[inputOffset + 48] << 24) | (input[inputOffset + 49] << 16) | (input[inputOffset + 50] << 8) | (input[inputOffset + 51]));
242                         buff[13] = (uint)((input[inputOffset + 52] << 24) | (input[inputOffset + 53] << 16) | (input[inputOffset + 54] << 8) | (input[inputOffset + 55]));
243                         buff[14] = (uint)((input[inputOffset + 56] << 24) | (input[inputOffset + 57] << 16) | (input[inputOffset + 58] << 8) | (input[inputOffset + 59]));
244                         buff[15] = (uint)((input[inputOffset + 60] << 24) | (input[inputOffset + 61] << 16) | (input[inputOffset + 62] << 8) | (input[inputOffset + 63]));
245                 }
246
247                 private static void FillBuff(uint[] buff)
248                 {
249                         uint val;
250                         for (int i = 16; i < 80; i += 8)
251                         {
252                                 val = buff[i - 3] ^ buff[i - 8] ^ buff[i - 14] ^ buff[i - 16];
253                                 buff[i] = (val << 1) | (val >> 31);
254
255                                 val = buff[i - 2] ^ buff[i - 7] ^ buff[i - 13] ^ buff[i - 15];
256                                 buff[i + 1] = (val << 1) | (val >> 31);
257
258                                 val = buff[i - 1] ^ buff[i - 6] ^ buff[i - 12] ^ buff[i - 14];
259                                 buff[i + 2] = (val << 1) | (val >> 31);
260
261                                 val = buff[i + 0] ^ buff[i - 5] ^ buff[i - 11] ^ buff[i - 13];
262                                 buff[i + 3] = (val << 1) | (val >> 31);
263
264                                 val = buff[i + 1] ^ buff[i - 4] ^ buff[i - 10] ^ buff[i - 12];
265                                 buff[i + 4] = (val << 1) | (val >> 31);
266
267                                 val = buff[i + 2] ^ buff[i - 3] ^ buff[i - 9] ^ buff[i - 11];
268                                 buff[i + 5] = (val << 1) | (val >> 31);
269
270                                 val = buff[i + 3] ^ buff[i - 2] ^ buff[i - 8] ^ buff[i - 10];
271                                 buff[i + 6] = (val << 1) | (val >> 31);
272
273                                 val = buff[i + 4] ^ buff[i - 1] ^ buff[i - 7] ^ buff[i - 9];
274                                 buff[i + 7] = (val << 1) | (val >> 31);
275                         }
276                 }
277         
278                 private void ProcessFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
279                 {
280                         ulong total = count + (ulong)inputCount;
281                         int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES));
282
283                         if (paddingSize < 1)
284                                 paddingSize += BLOCK_SIZE_BYTES;
285
286                         int length = inputCount+paddingSize+8;
287                         byte[] fooBuffer = (length == 64) ? _ProcessingBuffer : new byte[length];
288
289                         for (int i=0; i<inputCount; i++) {
290                                 fooBuffer[i] = inputBuffer[i+inputOffset];
291                         }
292
293                         fooBuffer[inputCount] = 0x80;
294                         for (int i=inputCount+1; i<inputCount+paddingSize; i++) {
295                                 fooBuffer[i] = 0x00;
296                         }
297
298                         // I deal in bytes. The algorithm deals in bits.
299                         ulong size = total << 3;
300                         AddLength (size, fooBuffer, inputCount+paddingSize);
301                         ProcessBlock (fooBuffer, 0);
302
303                         if (length == 128)
304                                 ProcessBlock (fooBuffer, 64);
305                 }
306
307                 internal void AddLength (ulong length, byte[] buffer, int position)
308                 {
309                         buffer [position++] = (byte)(length >> 56);
310                         buffer [position++] = (byte)(length >> 48);
311                         buffer [position++] = (byte)(length >> 40);
312                         buffer [position++] = (byte)(length >> 32);
313                         buffer [position++] = (byte)(length >> 24);
314                         buffer [position++] = (byte)(length >> 16);
315                         buffer [position++] = (byte)(length >>  8);
316                         buffer [position]   = (byte)(length);
317                 }
318         }
319
320 #if !NET_2_1 || MONOTOUCH
321
322         [ComVisible (true)]
323         public sealed class SHA1CryptoServiceProvider : SHA1 {
324
325                 private SHA1Internal sha;
326
327                 public SHA1CryptoServiceProvider () 
328                 {
329                         sha = new SHA1Internal ();
330                 }
331
332                 ~SHA1CryptoServiceProvider () 
333                 {
334                         Dispose (false);
335                 }
336
337                 protected override void Dispose (bool disposing) 
338                 {
339                         // nothing new to do (managed implementation)
340                         base.Dispose (disposing);
341                 }
342
343                 protected override void HashCore (byte[] rgb, int ibStart, int cbSize) 
344                 {
345                         State = 1;
346                         sha.HashCore (rgb, ibStart, cbSize);
347                 }
348
349                 protected override byte[] HashFinal () 
350                 {
351                         State = 0;
352                         return sha.HashFinal ();
353                 }
354
355                 public override void Initialize () 
356                 {
357                         sha.Initialize ();
358                 }
359         }
360 #endif // NET_2_1
361 }