* BindingSourceTest.cs: New Filter/RemoveFilter tests.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / SHA256Managed.cs
1 //
2 // System.Security.Cryptography SHA256Managed Class implementation
3 //
4 // Author:
5 //   Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
6 //
7 // (C) 2001 
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.Runtime.InteropServices;
31
32 namespace System.Security.Cryptography {
33         
34 #if NET_2_0
35         [ComVisible (true)]
36 #endif
37         public class SHA256Managed : SHA256 {
38
39                 private const int BLOCK_SIZE_BYTES =  64;
40                 private const int HASH_SIZE_BYTES  =  32;
41                 private uint[] _H;
42                 private uint[] K;
43                 private ulong count;
44                 private byte[] _ProcessingBuffer;   // Used to start data when passed less than a block worth.
45                 private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
46                 private uint[] buff;
47         
48                 public SHA256Managed () 
49                 {
50                         _H = new uint [8];
51                         _ProcessingBuffer = new byte [BLOCK_SIZE_BYTES];
52                         buff = new uint[64];
53                         Initialize ();
54                 }
55
56                 private uint Ch (uint u, uint v, uint w) 
57                 {
58                         return (u&v) ^ (~u&w);
59                 }
60
61                 private uint Maj (uint u, uint v, uint w) 
62                 {
63                         return (u&v) ^ (u&w) ^ (v&w);
64                 }
65
66                 private uint Ro0 (uint x) 
67                 {
68                         return ((x >> 7) | (x << 25))
69                                 ^ ((x >> 18) | (x << 14))
70                                 ^ (x >> 3);
71                 }
72
73                 private uint Ro1 (uint x) 
74                 {
75                         return ((x >> 17) | (x << 15))
76                                 ^ ((x >> 19) | (x << 13))
77                                 ^ (x >> 10);
78                 }
79
80                 private uint Sig0 (uint x) 
81                 {
82                         return ((x >> 2) | (x << 30))
83                                 ^ ((x >> 13) | (x << 19))
84                                 ^ ((x >> 22) | (x << 10));
85                 }
86
87                 private uint Sig1 (uint x) 
88                 {
89                         return ((x >> 6) | (x << 26))
90                                 ^ ((x >> 11) | (x << 21))
91                                 ^ ((x >> 25) | (x << 7));
92                 }
93
94                 protected override void HashCore (byte[] rgb, int start, int size) 
95                 {
96                         int i;
97                         State = 1;
98
99                         if (_ProcessingBufferCount != 0) {
100                                 if (size < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
101                                         System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, size);
102                                         _ProcessingBufferCount += size;
103                                         return;
104                                 }
105                                 else {
106                                         i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
107                                         System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, i);
108                                         ProcessBlock (_ProcessingBuffer, 0);
109                                         _ProcessingBufferCount = 0;
110                                         start += i;
111                                         size -= i;
112                                 }
113                         }
114
115                         for (i=0; i<size-size%BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
116                                 ProcessBlock (rgb, start+i);
117                         }
118
119                         if (size%BLOCK_SIZE_BYTES != 0) {
120                                 System.Buffer.BlockCopy (rgb, size-size%BLOCK_SIZE_BYTES+start, _ProcessingBuffer, 0, size%BLOCK_SIZE_BYTES);
121                                 _ProcessingBufferCount = size%BLOCK_SIZE_BYTES;
122                         }
123                 }
124         
125                 protected override byte[] HashFinal () 
126                 {
127                         byte[] hash = new byte[32];
128                         int i, j;
129
130                         ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
131
132                         for (i=0; i<8; i++) {
133                                 for (j=0; j<4; j++) {
134                                         hash[i*4+j] = (byte)(_H[i] >> (24-j*8));
135                                 }
136                         }
137
138                         State = 0;
139                         return hash;
140                 }
141
142                 public override void Initialize () 
143                 {
144                         count = 0;
145                         _ProcessingBufferCount = 0;
146                 
147                         _H[0] = 0x6A09E667;
148                         _H[1] = 0xBB67AE85;
149                         _H[2] = 0x3C6EF372;
150                         _H[3] = 0xA54FF53A;
151                         _H[4] = 0x510E527F;
152                         _H[5] = 0x9B05688C;
153                         _H[6] = 0x1F83D9AB;
154                         _H[7] = 0x5BE0CD19;
155                 }
156
157                 private void ProcessBlock (byte[] inputBuffer, int inputOffset) 
158                 {
159                         uint a, b, c, d, e, f, g, h;
160                         uint t1, t2;
161                         int i;
162                 
163                         count += BLOCK_SIZE_BYTES;
164
165                         for (i=0; i<16; i++) {
166                                 buff[i] = ((uint)(inputBuffer[inputOffset+4*i]) << 24)
167                                         | ((uint)(inputBuffer[inputOffset+4*i+1]) << 16)
168                                         | ((uint)(inputBuffer[inputOffset+4*i+2]) <<  8)
169                                         | ((uint)(inputBuffer[inputOffset+4*i+3]));
170                         }
171
172                 
173                         for (i=16; i<64; i++) {
174                                 buff[i] = Ro1(buff[i-2]) + buff[i-7] + Ro0(buff[i-15]) + buff[i-16];
175                         }
176
177                         a = _H[0];
178                         b = _H[1];
179                         c = _H[2];
180                         d = _H[3];
181                         e = _H[4];
182                         f = _H[5];
183                         g = _H[6];
184                         h = _H[7];
185
186                         for (i=0; i<64; i++) {
187                                 t1 = h + Sig1(e) + Ch(e,f,g) + SHAConstants.K1 [i] + buff[i];
188                                 t2 = Sig0(a) + Maj(a,b,c);
189                                 h = g;
190                                 g = f;
191                                 f = e;
192                                 e = d + t1;
193                                 d = c;
194                                 c = b;
195                                 b = a;
196                                 a = t1 + t2;
197                         }
198
199                         _H[0] += a;
200                         _H[1] += b;
201                         _H[2] += c;
202                         _H[3] += d;
203                         _H[4] += e;
204                         _H[5] += f;
205                         _H[6] += g;
206                         _H[7] += h;
207                 }
208         
209                 private void ProcessFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
210                 {
211                         ulong total = count + (ulong)inputCount;
212                         int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES));
213
214                         if (paddingSize < 1)
215                                 paddingSize += BLOCK_SIZE_BYTES;
216
217                         byte[] fooBuffer = new byte[inputCount+paddingSize+8];
218
219                         for (int i=0; i<inputCount; i++) {
220                                 fooBuffer[i] = inputBuffer[i+inputOffset];
221                         }
222
223                         fooBuffer[inputCount] = 0x80;
224                         for (int i=inputCount+1; i<inputCount+paddingSize; i++) {
225                                 fooBuffer[i] = 0x00;
226                         }
227
228                         // I deal in bytes. The algorithm deals in bits.
229                         ulong size = total << 3;
230                         AddLength (size, fooBuffer, inputCount+paddingSize);
231                         ProcessBlock (fooBuffer, 0);
232
233                         if (inputCount+paddingSize+8 == 128) {
234                                 ProcessBlock(fooBuffer, 64);
235                         }
236                 }
237
238                 internal void AddLength (ulong length, byte[] buffer, int position)
239                 {
240                         buffer [position++] = (byte)(length >> 56);
241                         buffer [position++] = (byte)(length >> 48);
242                         buffer [position++] = (byte)(length >> 40);
243                         buffer [position++] = (byte)(length >> 32);
244                         buffer [position++] = (byte)(length >> 24);
245                         buffer [position++] = (byte)(length >> 16);
246                         buffer [position++] = (byte)(length >>  8);
247                         buffer [position]   = (byte)(length);
248                 }
249         }
250 }
251