This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 namespace System.Security.Cryptography {
35         
36         public class SHA256Managed : SHA256 {
37
38                 private const int BLOCK_SIZE_BYTES =  64;
39                 private const int HASH_SIZE_BYTES  =  32;
40                 private uint[] _H;
41                 private uint[] K;
42                 private uint count;
43                 private byte[] _ProcessingBuffer;   // Used to start data when passed less than a block worth.
44                 private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
45         
46                 public SHA256Managed () 
47                 {
48                         _H = new uint [8];
49                         _ProcessingBuffer = new byte [BLOCK_SIZE_BYTES];
50                         Initialize ();
51                 }
52
53                 private uint Ch (uint u, uint v, uint w) 
54                 {
55                         return (u&v) ^ (~u&w);
56                 }
57
58                 private uint Maj (uint u, uint v, uint w) 
59                 {
60                         return (u&v) ^ (u&w) ^ (v&w);
61                 }
62
63                 private uint Ro0 (uint x) 
64                 {
65                         return ((x >> 7) | (x << 25))
66                                 ^ ((x >> 18) | (x << 14))
67                                 ^ (x >> 3);
68                 }
69
70                 private uint Ro1 (uint x) 
71                 {
72                         return ((x >> 17) | (x << 15))
73                                 ^ ((x >> 19) | (x << 13))
74                                 ^ (x >> 10);
75                 }
76
77                 private uint Sig0 (uint x) 
78                 {
79                         return ((x >> 2) | (x << 30))
80                                 ^ ((x >> 13) | (x << 19))
81                                 ^ ((x >> 22) | (x << 10));
82                 }
83
84                 private uint Sig1 (uint x) 
85                 {
86                         return ((x >> 6) | (x << 26))
87                                 ^ ((x >> 11) | (x << 21))
88                                 ^ ((x >> 25) | (x << 7));
89                 }
90
91                 protected override void HashCore (byte[] rgb, int start, int size) 
92                 {
93                         int i;
94                         State = 1;
95
96                         if (_ProcessingBufferCount != 0) {
97                                 if (size < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
98                                         System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, size);
99                                         _ProcessingBufferCount += size;
100                                         return;
101                                 }
102                                 else {
103                                         i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
104                                         System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, i);
105                                         ProcessBlock (_ProcessingBuffer, 0);
106                                         _ProcessingBufferCount = 0;
107                                         start += i;
108                                         size -= i;
109                                 }
110                         }
111
112                         for (i=0; i<size-size%BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
113                                 ProcessBlock (rgb, start+i);
114                         }
115
116                         if (size%BLOCK_SIZE_BYTES != 0) {
117                                 System.Buffer.BlockCopy (rgb, size-size%BLOCK_SIZE_BYTES+start, _ProcessingBuffer, 0, size%BLOCK_SIZE_BYTES);
118                                 _ProcessingBufferCount = size%BLOCK_SIZE_BYTES;
119                         }
120                 }
121         
122                 protected override byte[] HashFinal () 
123                 {
124                         byte[] hash = new byte[32];
125                         int i, j;
126
127                         ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
128
129                         for (i=0; i<8; i++) {
130                                 for (j=0; j<4; j++) {
131                                         hash[i*4+j] = (byte)(_H[i] >> (24-j*8));
132                                 }
133                         }
134
135                         State = 0;
136                         return hash;
137                 }
138
139                 public override void Initialize () 
140                 {
141                         count = 0;
142                         _ProcessingBufferCount = 0;
143                 
144                         _H[0] = 0x6A09E667;
145                         _H[1] = 0xBB67AE85;
146                         _H[2] = 0x3C6EF372;
147                         _H[3] = 0xA54FF53A;
148                         _H[4] = 0x510E527F;
149                         _H[5] = 0x9B05688C;
150                         _H[6] = 0x1F83D9AB;
151                         _H[7] = 0x5BE0CD19;
152                 }
153
154                 private void ProcessBlock (byte[] inputBuffer, int inputOffset) 
155                 {
156                         uint a, b, c, d, e, f, g, h;
157                         uint t1, t2;
158                         int i;
159                         uint[] buff;
160                 
161                         count += BLOCK_SIZE_BYTES;
162                 
163                         buff = new uint[64];
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                         byte[] fooBuffer;
212                         int paddingSize;
213                         int i;
214                         uint size;
215
216                         paddingSize = (int)(56 - (inputCount + count) % BLOCK_SIZE_BYTES);
217
218                         if (paddingSize < 1)
219                                 paddingSize += BLOCK_SIZE_BYTES;
220
221                         fooBuffer = new byte[inputCount+paddingSize+8];
222
223                         for (i=0; i<inputCount; i++) {
224                                 fooBuffer[i] = inputBuffer[i+inputOffset];
225                         }
226
227                         fooBuffer[inputCount] = 0x80;
228                         for (i=inputCount+1; i<inputCount+paddingSize; i++) {
229                                 fooBuffer[i] = 0x00;
230                         }
231
232                         size = (uint)(count+inputCount);
233                         size *= 8;
234
235                         fooBuffer[inputCount+paddingSize]   = 0x00;
236                         fooBuffer[inputCount+paddingSize+1] = 0x00;
237                         fooBuffer[inputCount+paddingSize+2] = 0x00;
238                         fooBuffer[inputCount+paddingSize+3] = 0x00;
239
240                         fooBuffer[inputCount+paddingSize+4] = (byte)((size) >> 24);
241                         fooBuffer[inputCount+paddingSize+5] = (byte)((size) >> 16);
242                         fooBuffer[inputCount+paddingSize+6] = (byte)((size) >>  8);
243                         fooBuffer[inputCount+paddingSize+7] = (byte)((size) >>  0);
244
245                         ProcessBlock(fooBuffer, 0);
246
247                         if (inputCount+paddingSize+8 == 128) {
248                                 ProcessBlock(fooBuffer, 64);
249                         }
250                 }
251         }
252 }
253