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