[Mono.Security] Fixes build
[mono.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / SHA224Managed.cs
1 //
2 // Mono.Security.Cryptography SHA224 class implementation
3 //      based on SHA256Managed class implementation (mscorlib.dll)
4 //
5 // Authors:
6 //      Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
7 //      Sebastien Pouliot <sebastien@ximian.com>
8 //
9 // (C) 2001 
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if !MONOTOUCH && !XAMMAC
33
34 using System.Security.Cryptography;
35
36 namespace Mono.Security.Cryptography {
37         
38         public class SHA224Managed : SHA224 {
39
40                 private const int BLOCK_SIZE_BYTES =  64;
41
42                 private uint[] _H;
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 SHA224Managed ()
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[28];
128                         int i, j;
129
130                         ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
131
132                         for (i=0; i<7; 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] = 0xC1059ED8;
148                         _H[1] = 0x367CD507;
149                         _H[2] = 0x3070DD17;
150                         _H[3] = 0xF70E5939;
151                         _H[4] = 0xFFC00B31;
152                         _H[5] = 0x68581511;
153                         _H[6] = 0x64F98FA7;
154                         _H[7] = 0xBEFA4FA4;
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                         uint[] K1 = SHAConstants.K1;
163                         uint[] buff = this.buff;
164                 
165                         count += BLOCK_SIZE_BYTES;
166                 
167                         for (i=0; i<16; i++) {
168                                 buff[i] = (uint)(((inputBuffer[inputOffset+4*i]) << 24)
169                                         | ((inputBuffer[inputOffset+4*i+1]) << 16)
170                                         | ((inputBuffer[inputOffset+4*i+2]) <<  8)
171                                         | ((inputBuffer[inputOffset+4*i+3])));
172                         }
173
174                 
175                         for (i=16; i<64; i++) {
176                                 t1 = buff[i - 15];
177                                 t1 = (((t1 >> 7) | (t1 << 25)) ^ ((t1 >> 18) | (t1 << 14)) ^ (t1 >> 3));
178
179                                 t2 = buff[i - 2];
180                                 t2 = (((t2 >> 17) | (t2 << 15)) ^ ((t2 >> 19) | (t2 << 13)) ^ (t2 >> 10));
181                                 buff[i] = t2 + buff[i - 7] + t1 + buff[i - 16];
182                         }
183
184                         a = _H[0];
185                         b = _H[1];
186                         c = _H[2];
187                         d = _H[3];
188                         e = _H[4];
189                         f = _H[5];
190                         g = _H[6];
191                         h = _H[7];
192
193                         for (i=0; i<64; i++) {
194                                 t1 = h + (((e >> 6) | (e << 26)) ^ ((e >> 11) | (e << 21)) ^ ((e >> 25) | (e << 7))) + ((e & f) ^ (~e & g)) + K1[i] + buff[i];
195
196                                 t2 = (((a >> 2) | (a << 30)) ^ ((a >> 13) | (a << 19)) ^ ((a >> 22) | (a << 10)));
197                                 t2 = t2 + ((a & b) ^ (a & c) ^ (b & c));
198                                 h = g;
199                                 g = f;
200                                 f = e;
201                                 e = d + t1;
202                                 d = c;
203                                 c = b;
204                                 b = a;
205                                 a = t1 + t2;
206                         }
207
208                         _H[0] += a;
209                         _H[1] += b;
210                         _H[2] += c;
211                         _H[3] += d;
212                         _H[4] += e;
213                         _H[5] += f;
214                         _H[6] += g;
215                         _H[7] += h;
216                 }
217         
218                 private void ProcessFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
219                 {
220                         ulong total = count + (ulong)inputCount;
221                         int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES));
222
223                         if (paddingSize < 1)
224                                 paddingSize += BLOCK_SIZE_BYTES;
225
226                         byte[] fooBuffer = new byte[inputCount+paddingSize+8];
227
228                         for (int i=0; i<inputCount; i++) {
229                                 fooBuffer[i] = inputBuffer[i+inputOffset];
230                         }
231
232                         fooBuffer[inputCount] = 0x80;
233                         for (int i=inputCount+1; i<inputCount+paddingSize; i++) {
234                                 fooBuffer[i] = 0x00;
235                         }
236
237                         // I deal in bytes. The algorithm deals in bits.
238                         ulong size = total << 3;
239                         AddLength (size, fooBuffer, inputCount+paddingSize);
240                         ProcessBlock (fooBuffer, 0);
241
242                         if (inputCount+paddingSize+8 == 128) {
243                                 ProcessBlock(fooBuffer, 64);
244                         }
245                 }
246
247                 internal void AddLength (ulong length, byte[] buffer, int position)
248                 {
249                         buffer [position++] = (byte)(length >> 56);
250                         buffer [position++] = (byte)(length >> 48);
251                         buffer [position++] = (byte)(length >> 40);
252                         buffer [position++] = (byte)(length >> 32);
253                         buffer [position++] = (byte)(length >> 24);
254                         buffer [position++] = (byte)(length >> 16);
255                         buffer [position++] = (byte)(length >>  8);
256                         buffer [position]   = (byte)(length);
257                 }
258         }
259 }
260
261 #endif