New tests.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / SHA384Managed.cs
1 //
2 // System.Security.Cryptography.SHA384Managed.cs
3 //
4 // Authors:
5 //      Dan Lewis (dihlewis@yahoo.co.uk)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // (C) 2002
9 // Implementation translated from Bouncy Castle JCE (http://www.bouncycastle.org/)
10 // See bouncycastle.txt for license.
11 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if !MOONLIGHT
34
35 using System.Runtime.InteropServices;
36
37 namespace System.Security.Cryptography {
38         
39 [ComVisible (true)]
40 public class SHA384Managed : SHA384 {
41
42         private byte[] xBuf;
43         private int xBufOff;
44
45         private ulong byteCount1;
46         private ulong byteCount2;
47
48         private ulong H1, H2, H3, H4, H5, H6, H7, H8;
49         private ulong[] W;
50         private int wOff;
51
52         public SHA384Managed () 
53         {
54                 xBuf = new byte [8];
55                 W = new ulong [80];
56                 Initialize (false); // limited initialization
57         }
58
59         private void Initialize (bool reuse) 
60         {
61                 // SHA-384 initial hash value
62                 // The first 64 bits of the fractional parts of the square roots
63                 // of the 9th through 16th prime numbers
64                 H1 = 0xcbbb9d5dc1059ed8L;
65                 H2 = 0x629a292a367cd507L;
66                 H3 = 0x9159015a3070dd17L;
67                 H4 = 0x152fecd8f70e5939L;
68                 H5 = 0x67332667ffc00b31L;
69                 H6 = 0x8eb44a8768581511L;
70                 H7 = 0xdb0c2e0d64f98fa7L;
71                 H8 = 0x47b5481dbefa4fa4L;
72
73                 if (reuse) {
74                         byteCount1 = 0;
75                         byteCount2 = 0;
76
77                         xBufOff = 0;
78                         for (int i = 0; i < xBuf.Length; i++) 
79                                 xBuf [i] = 0;
80
81                         wOff = 0;
82                         for (int i = 0; i != W.Length; i++)
83                                 W [i] = 0;
84                 }
85         }
86
87         public override void Initialize () 
88         {
89                 Initialize (true); // reuse instance
90         }
91
92         // protected
93
94         protected override void HashCore (byte[] rgb, int ibStart, int cbSize) 
95         {
96                 // fill the current word
97                 while ((xBufOff != 0) && (cbSize > 0)) {
98                         update (rgb [ibStart]);
99                         ibStart++;
100                         cbSize--;
101                 }
102
103                 // process whole words.
104                 while (cbSize > xBuf.Length) {
105                         processWord (rgb, ibStart);
106                         ibStart += xBuf.Length;
107                         cbSize -= xBuf.Length;
108                         byteCount1 += (ulong) xBuf.Length;
109                 }
110
111                 // load in the remainder.
112                 while (cbSize > 0) {
113                         update (rgb [ibStart]);
114                         ibStart++;
115                         cbSize--;
116                 }
117         }
118
119         protected override byte[] HashFinal () 
120         {
121                 adjustByteCounts ();
122
123                 ulong lowBitLength = byteCount1 << 3;
124                 ulong hiBitLength = byteCount2;
125
126                 // add the pad bytes.
127                 update ( (byte) 128);
128                 while (xBufOff != 0)
129                         update ( (byte)0);
130
131                 processLength (lowBitLength, hiBitLength);
132                 processBlock ();
133                 
134                 byte[] output = new byte [48];
135                 unpackWord(H1, output, 0);
136                 unpackWord(H2, output, 8);
137                 unpackWord(H3, output, 16);
138                 unpackWord(H4, output, 24);
139                 unpackWord(H5, output, 32);
140                 unpackWord(H6, output, 40);
141
142                 Initialize ();
143                 return output;
144         }
145
146         private void update (byte input)
147         {
148                 xBuf [xBufOff++] = input;
149                 if (xBufOff == xBuf.Length) {
150                         processWord(xBuf, 0);
151                         xBufOff = 0;
152                 }
153                 byteCount1++;
154         }
155
156         private void processWord (byte[] input, int inOff)
157         {
158                 W [wOff++] = ( (ulong) input [inOff] << 56)
159                         | ( (ulong) input [inOff + 1] << 48)
160                         | ( (ulong) input [inOff + 2] << 40)
161                         | ( (ulong) input [inOff + 3] << 32)
162                         | ( (ulong) input [inOff + 4] << 24)
163                         | ( (ulong) input [inOff + 5] << 16)
164                         | ( (ulong) input [inOff + 6] << 8)
165                         | ( (ulong) input [inOff + 7]); 
166                 if (wOff == 16)
167                         processBlock ();
168         }
169
170         private void unpackWord (ulong word, byte[] output, int outOff)
171         {
172                 output[outOff]     = (byte) (word >> 56);
173                 output[outOff + 1] = (byte) (word >> 48);
174                 output[outOff + 2] = (byte) (word >> 40);
175                 output[outOff + 3] = (byte) (word >> 32);
176                 output[outOff + 4] = (byte) (word >> 24);
177                 output[outOff + 5] = (byte) (word >> 16);
178                 output[outOff + 6] = (byte) (word >> 8);
179                 output[outOff + 7] = (byte) word;
180         }
181
182         // adjust the byte counts so that byteCount2 represents the
183         // upper long (less 3 bits) word of the byte count.
184         private void adjustByteCounts()
185         {
186                 if (byteCount1 > 0x1fffffffffffffffL) {
187                         byteCount2 += (byteCount1 >> 61);
188                         byteCount1 &= 0x1fffffffffffffffL;
189                 }
190         }
191
192         private void processLength (ulong lowW, ulong hiW)
193         {
194                 if (wOff > 14)
195                         processBlock ();
196                 W[14] = hiW;
197                 W[15] = lowW;
198         }
199
200         private void processBlock ()
201         {
202                 ulong a, b, c, d, e, f, g, h;
203
204                 // abcrem doesn't work on fields
205                 ulong[] W = this.W;
206                 ulong[] K2 = SHAConstants.K2;
207
208                 adjustByteCounts ();
209                 // expand 16 word block into 80 word blocks.
210                 for (int t = 16; t <= 79; t++)
211                 {
212                         a = W[t-15];
213                         a = ((a >> 1) | (a << 63)) ^ ((a >> 8) | (a << 56)) ^ (a >> 7);
214                         b = W[t - 2];
215                         b = ((b >> 19) | (b << 45)) ^ ((b >> 61) | (b << 3)) ^ (b >> 6);
216                         W[t] = b + W[t - 7] + a + W[t - 16];
217                 }
218                 // set up working variables.
219                 a = H1;
220                 b = H2;
221                 c = H3;
222                 d = H4;
223                 e = H5;
224                 f = H6;
225                 g = H7;
226                 h = H8;
227
228                 for (int t = 0; t <= 79; t++)
229                 {
230                         ulong T1 = ((e >> 14) | (e << 50)) ^ ((e >> 18) | (e << 46)) ^ ((e >> 41) | (e << 23));
231                         T1 += h + ((e & f) ^ ((~e) & g)) + K2[t] + W[t];
232
233                         ulong T2 = ((a >> 28) | (a << 36)) ^ ((a >> 34) | (a << 30)) ^ ((a >> 39) | (a << 25));
234                         T2 += ((a & b) ^ (a & c) ^ (b & c));
235
236                         h = g;
237                         g = f;
238                         f = e;
239                         e = d + T1;
240                         d = c;
241                         c = b;
242                         b = a;
243                         a = T1 + T2;
244                 }
245
246                 H1 += a;
247                 H2 += b;
248                 H3 += c;
249                 H4 += d;
250                 H5 += e;
251                 H6 += f;
252                 H7 += g;
253                 H8 += h;
254                 // reset the offset and clean out the word buffer.
255                 wOff = 0;
256                 for (int i = 0; i != W.Length; i++)
257                         W[i] = 0;
258         }
259 }
260
261 }
262
263 #endif
264