465adc8f35bf358b68c97223b7235b18d9e69c80
[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 uint[] _H;  // these are my chaining variables
46                 private ulong count;
47                 private byte[] _ProcessingBuffer;   // Used to start data when passed less than a block worth.
48                 private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
49                 private uint[] buff;
50
51                 public SHA1Internal () 
52                 {
53                         _H = new uint[5];
54                         _ProcessingBuffer = new byte[BLOCK_SIZE_BYTES];
55                         buff = new uint[80];
56                         
57                         Initialize();
58                 }
59
60                 public void HashCore (byte[] rgb, int ibStart, int cbSize) 
61                 {
62                         int i;
63
64                         if (_ProcessingBufferCount != 0) {
65                                 if (cbSize < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
66                                         System.Buffer.BlockCopy (rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, cbSize);
67                                         _ProcessingBufferCount += cbSize;
68                                         return;
69                                 }
70                                 else {
71                                         i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
72                                         System.Buffer.BlockCopy (rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, i);
73                                         ProcessBlock (_ProcessingBuffer, 0);
74                                         _ProcessingBufferCount = 0;
75                                         ibStart += i;
76                                         cbSize -= i;
77                                 }
78                         }
79
80                         for (i = 0; i < cbSize - cbSize % BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
81                                 ProcessBlock (rgb, (uint)(ibStart + i));
82                         }
83
84                         if (cbSize % BLOCK_SIZE_BYTES != 0) {
85                                 System.Buffer.BlockCopy (rgb, cbSize - cbSize % BLOCK_SIZE_BYTES + ibStart, _ProcessingBuffer, 0, cbSize % BLOCK_SIZE_BYTES);
86                                 _ProcessingBufferCount = cbSize % BLOCK_SIZE_BYTES;
87                         }
88                 }
89         
90                 public byte[] HashFinal () 
91                 {
92                         byte[] hash = new byte[20];
93
94                         ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
95
96                         for (int i=0; i<5; i++) {
97                                 for (int j=0; j<4; j++) {
98                                         hash [i*4+j] = (byte)(_H[i] >> (8*(3-j)));
99                                 }
100                         }
101
102                         return hash;
103                 }
104
105                 public void Initialize () 
106                 {
107                         count = 0;
108                         _ProcessingBufferCount = 0;
109
110                         _H[0] = 0x67452301;
111                         _H[1] = 0xefcdab89;
112                         _H[2] = 0x98badcfe;
113                         _H[3] = 0x10325476;
114                         _H[4] = 0xC3D2E1F0;
115                 }
116
117                 private void ProcessBlock(byte[] inputBuffer, uint inputOffset) 
118                 {
119                         uint a, b, c, d, e;
120
121                         count += BLOCK_SIZE_BYTES;
122
123                         // abc removal would not work on the fields
124                         uint[] _H = this._H;
125                         uint[] buff = this.buff;
126                         InitialiseBuff(buff, inputBuffer, inputOffset);
127                         FillBuff(buff);
128
129                         a = _H[0];
130                         b = _H[1];
131                         c = _H[2];
132                         d = _H[3];
133                         e = _H[4];
134
135                         // This function was unrolled because it seems to be doubling our performance with current compiler/VM.
136                         // Possibly roll up if this changes.
137
138                         // ---- Round 1 --------
139                         int i=0;
140                         while (i < 20)
141                         {
142                                 e += ((a << 5) | (a >> 27)) + (((c ^ d) & b) ^ d) + 0x5A827999 + buff[i];
143                                 b = (b << 30) | (b >> 2);
144
145                                 d += ((e << 5) | (e >> 27)) + (((b ^ c) & a) ^ c) + 0x5A827999 + buff[i+1];
146                                 a = (a << 30) | (a >> 2);
147
148                                 c += ((d << 5) | (d >> 27)) + (((a ^ b) & e) ^ b) + 0x5A827999 + buff[i+2];
149                                 e = (e << 30) | (e >> 2);
150
151                                 b += ((c << 5) | (c >> 27)) + (((e ^ a) & d) ^ a) + 0x5A827999 + buff[i+3];
152                                 d = (d << 30) | (d >> 2);
153
154                                 a += ((b << 5) | (b >> 27)) + (((d ^ e) & c) ^ e) + 0x5A827999 + buff[i+4];
155                                 c = (c << 30) | (c >> 2);
156                                 i += 5;
157                         }
158
159                         // ---- Round 2 --------
160                         while (i < 40)
161                         {
162                                 e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0x6ED9EBA1 + buff[i];
163                                 b = (b << 30) | (b >> 2);
164
165                                 d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0x6ED9EBA1 + buff[i + 1];
166                                 a = (a << 30) | (a >> 2);
167
168                                 c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0x6ED9EBA1 + buff[i + 2];
169                                 e = (e << 30) | (e >> 2);
170
171                                 b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0x6ED9EBA1 + buff[i + 3];
172                                 d = (d << 30) | (d >> 2);
173
174                                 a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0x6ED9EBA1 + buff[i + 4];
175                                 c = (c << 30) | (c >> 2);
176                                 i += 5;
177                         }
178                    
179                         // ---- Round 3 --------
180                         while (i < 60)
181                         {
182                                 e += ((a << 5) | (a >> 27)) + ((b & c) | (b & d) | (c & d)) + 0x8F1BBCDC + buff[i];
183                                 b = (b << 30) | (b >> 2);
184
185                                 d += ((e << 5) | (e >> 27)) + ((a & b) | (a & c) | (b & c)) + 0x8F1BBCDC + buff[i + 1];
186                                 a = (a << 30) | (a >> 2);
187
188                                 c += ((d << 5) | (d >> 27)) + ((e & a) | (e & b) | (a & b)) + 0x8F1BBCDC + buff[i + 2];
189                                 e = (e << 30) | (e >> 2);
190
191                                 b += ((c << 5) | (c >> 27)) + ((d & e) | (d & a) | (e & a)) + 0x8F1BBCDC + buff[i + 3];
192                                 d = (d << 30) | (d >> 2);
193
194                                 a += ((b << 5) | (b >> 27)) + ((c & d) | (c & e) | (d & e)) + 0x8F1BBCDC + buff[i + 4];
195                                 c = (c << 30) | (c >> 2);
196                                 i += 5;
197                         }
198
199                         // ---- Round 4 --------
200                         while (i < 80)
201                         {
202                                 e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0xCA62C1D6 + buff[i];
203                                 b = (b << 30) | (b >> 2);
204
205                                 d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0xCA62C1D6 + buff[i + 1];
206                                 a = (a << 30) | (a >> 2);
207
208                                 c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0xCA62C1D6 + buff[i + 2];
209                                 e = (e << 30) | (e >> 2);
210
211                                 b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0xCA62C1D6 + buff[i + 3];
212                                 d = (d << 30) | (d >> 2);
213
214                                 a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0xCA62C1D6 + buff[i + 4];
215                                 c = (c << 30) | (c >> 2);
216                                 i += 5;
217                         }
218
219                         _H[0] += a;
220                         _H[1] += b;
221                         _H[2] += c;
222                         _H[3] += d;
223                         _H[4] += e;
224                 }
225
226                 private static void InitialiseBuff(uint[] buff, byte[] input, uint inputOffset)
227                 {
228                         buff[0] = (uint)((input[inputOffset + 0] << 24) | (input[inputOffset + 1] << 16) | (input[inputOffset + 2] << 8) | (input[inputOffset + 3]));
229                         buff[1] = (uint)((input[inputOffset + 4] << 24) | (input[inputOffset + 5] << 16) | (input[inputOffset + 6] << 8) | (input[inputOffset + 7]));
230                         buff[2] = (uint)((input[inputOffset + 8] << 24) | (input[inputOffset + 9] << 16) | (input[inputOffset + 10] << 8) | (input[inputOffset + 11]));
231                         buff[3] = (uint)((input[inputOffset + 12] << 24) | (input[inputOffset + 13] << 16) | (input[inputOffset + 14] << 8) | (input[inputOffset + 15]));
232                         buff[4] = (uint)((input[inputOffset + 16] << 24) | (input[inputOffset + 17] << 16) | (input[inputOffset + 18] << 8) | (input[inputOffset + 19]));
233                         buff[5] = (uint)((input[inputOffset + 20] << 24) | (input[inputOffset + 21] << 16) | (input[inputOffset + 22] << 8) | (input[inputOffset + 23]));
234                         buff[6] = (uint)((input[inputOffset + 24] << 24) | (input[inputOffset + 25] << 16) | (input[inputOffset + 26] << 8) | (input[inputOffset + 27]));
235                         buff[7] = (uint)((input[inputOffset + 28] << 24) | (input[inputOffset + 29] << 16) | (input[inputOffset + 30] << 8) | (input[inputOffset + 31]));
236                         buff[8] = (uint)((input[inputOffset + 32] << 24) | (input[inputOffset + 33] << 16) | (input[inputOffset + 34] << 8) | (input[inputOffset + 35]));
237                         buff[9] = (uint)((input[inputOffset + 36] << 24) | (input[inputOffset + 37] << 16) | (input[inputOffset + 38] << 8) | (input[inputOffset + 39]));
238                         buff[10] = (uint)((input[inputOffset + 40] << 24) | (input[inputOffset + 41] << 16) | (input[inputOffset + 42] << 8) | (input[inputOffset + 43]));
239                         buff[11] = (uint)((input[inputOffset + 44] << 24) | (input[inputOffset + 45] << 16) | (input[inputOffset + 46] << 8) | (input[inputOffset + 47]));
240                         buff[12] = (uint)((input[inputOffset + 48] << 24) | (input[inputOffset + 49] << 16) | (input[inputOffset + 50] << 8) | (input[inputOffset + 51]));
241                         buff[13] = (uint)((input[inputOffset + 52] << 24) | (input[inputOffset + 53] << 16) | (input[inputOffset + 54] << 8) | (input[inputOffset + 55]));
242                         buff[14] = (uint)((input[inputOffset + 56] << 24) | (input[inputOffset + 57] << 16) | (input[inputOffset + 58] << 8) | (input[inputOffset + 59]));
243                         buff[15] = (uint)((input[inputOffset + 60] << 24) | (input[inputOffset + 61] << 16) | (input[inputOffset + 62] << 8) | (input[inputOffset + 63]));
244                 }
245
246                 private static void FillBuff(uint[] buff)
247                 {
248                         uint val;
249                         for (int i = 16; i < 80; i += 8)
250                         {
251                                 val = buff[i - 3] ^ buff[i - 8] ^ buff[i - 14] ^ buff[i - 16];
252                                 buff[i] = (val << 1) | (val >> 31);
253
254                                 val = buff[i - 2] ^ buff[i - 7] ^ buff[i - 13] ^ buff[i - 15];
255                                 buff[i + 1] = (val << 1) | (val >> 31);
256
257                                 val = buff[i - 1] ^ buff[i - 6] ^ buff[i - 12] ^ buff[i - 14];
258                                 buff[i + 2] = (val << 1) | (val >> 31);
259
260                                 val = buff[i + 0] ^ buff[i - 5] ^ buff[i - 11] ^ buff[i - 13];
261                                 buff[i + 3] = (val << 1) | (val >> 31);
262
263                                 val = buff[i + 1] ^ buff[i - 4] ^ buff[i - 10] ^ buff[i - 12];
264                                 buff[i + 4] = (val << 1) | (val >> 31);
265
266                                 val = buff[i + 2] ^ buff[i - 3] ^ buff[i - 9] ^ buff[i - 11];
267                                 buff[i + 5] = (val << 1) | (val >> 31);
268
269                                 val = buff[i + 3] ^ buff[i - 2] ^ buff[i - 8] ^ buff[i - 10];
270                                 buff[i + 6] = (val << 1) | (val >> 31);
271
272                                 val = buff[i + 4] ^ buff[i - 1] ^ buff[i - 7] ^ buff[i - 9];
273                                 buff[i + 7] = (val << 1) | (val >> 31);
274                         }
275                 }
276         
277                 private void ProcessFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
278                 {
279                         ulong total = count + (ulong)inputCount;
280                         int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES));
281
282                         if (paddingSize < 1)
283                                 paddingSize += BLOCK_SIZE_BYTES;
284
285                         int length = inputCount+paddingSize+8;
286                         byte[] fooBuffer = (length == 64) ? _ProcessingBuffer : new byte[length];
287
288                         for (int i=0; i<inputCount; i++) {
289                                 fooBuffer[i] = inputBuffer[i+inputOffset];
290                         }
291
292                         fooBuffer[inputCount] = 0x80;
293                         for (int i=inputCount+1; i<inputCount+paddingSize; i++) {
294                                 fooBuffer[i] = 0x00;
295                         }
296
297                         // I deal in bytes. The algorithm deals in bits.
298                         ulong size = total << 3;
299                         AddLength (size, fooBuffer, inputCount+paddingSize);
300                         ProcessBlock (fooBuffer, 0);
301
302                         if (length == 128)
303                                 ProcessBlock (fooBuffer, 64);
304                 }
305
306                 internal void AddLength (ulong length, byte[] buffer, int position)
307                 {
308                         buffer [position++] = (byte)(length >> 56);
309                         buffer [position++] = (byte)(length >> 48);
310                         buffer [position++] = (byte)(length >> 40);
311                         buffer [position++] = (byte)(length >> 32);
312                         buffer [position++] = (byte)(length >> 24);
313                         buffer [position++] = (byte)(length >> 16);
314                         buffer [position++] = (byte)(length >>  8);
315                         buffer [position]   = (byte)(length);
316                 }
317         }
318
319 #if !MOONLIGHT
320
321         [ComVisible (true)]
322         public sealed class SHA1CryptoServiceProvider : SHA1 {
323
324                 private SHA1Internal sha;
325
326                 public SHA1CryptoServiceProvider () 
327                 {
328                         sha = new SHA1Internal ();
329                 }
330
331                 ~SHA1CryptoServiceProvider () 
332                 {
333                         Dispose (false);
334                 }
335
336                 protected override void Dispose (bool disposing) 
337                 {
338                         // nothing new to do (managed implementation)
339                         base.Dispose (disposing);
340                 }
341
342                 protected override void HashCore (byte[] rgb, int ibStart, int cbSize) 
343                 {
344                         State = 1;
345                         sha.HashCore (rgb, ibStart, cbSize);
346                 }
347
348                 protected override byte[] HashFinal () 
349                 {
350                         State = 0;
351                         return sha.HashFinal ();
352                 }
353
354                 public override void Initialize () 
355                 {
356                         sha.Initialize ();
357                 }
358         }
359 #endif // NET_2_1
360 }