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