New test.
[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 using System.Runtime.InteropServices;
34
35 namespace System.Security.Cryptography {
36         
37 #if NET_2_0
38 [ComVisible (true)]
39 #endif
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 start, int count) 
95         {
96                 // fill the current word
97                 while ((xBufOff != 0) && (count > 0)) {
98                         update (rgb [start]);
99                         start++;
100                         count--;
101                 }
102
103                 // process whole words.
104                 while (count > xBuf.Length) {
105                         processWord (rgb, start);
106                         start += xBuf.Length;
107                         count -= xBuf.Length;
108                         byteCount1 += (ulong) xBuf.Length;
109                 }
110
111                 // load in the remainder.
112                 while (count > 0) {
113                         update (rgb [start]);
114                         start++;
115                         count--;
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                 adjustByteCounts ();
203                 // expand 16 word block into 80 word blocks.
204                 for (int t = 16; t <= 79; t++)
205                         W[t] = Sigma1 (W [t - 2]) + W [t - 7] + Sigma0 (W [t - 15]) + W [t - 16];
206
207                 // set up working variables.
208                 ulong a = H1;
209                 ulong b = H2;
210                 ulong c = H3;
211                 ulong d = H4;
212                 ulong e = H5;
213                 ulong f = H6;
214                 ulong g = H7;
215                 ulong h = H8;
216
217                 for (int t = 0; t <= 79; t++) {
218                         ulong T1 = h + Sum1 (e) + Ch (e, f, g) + SHAConstants.K2 [t] + W [t];
219                         ulong T2 = Sum0 (a) + Maj (a, b, c);
220                         h = g;
221                         g = f;
222                         f = e;
223                         e = d + T1;
224                         d = c;
225                         c = b;
226                         b = a;
227                         a = T1 + T2;
228                 }
229
230                 H1 += a;
231                 H2 += b;
232                 H3 += c;
233                 H4 += d;
234                 H5 += e;
235                 H6 += f;
236                 H7 += g;
237                 H8 += h;
238                 // reset the offset and clean out the word buffer.
239                 wOff = 0;
240                 for (int i = 0; i != W.Length; i++)
241                         W[i] = 0;
242         }
243
244         private ulong rotateRight (ulong x, int n)
245         {
246                 return (x >> n) | (x << (64 - n));
247         }
248
249         /* SHA-384 and SHA-512 functions (as for SHA-256 but for longs) */
250         private ulong Ch (ulong x, ulong y, ulong z)
251         {
252                 return ((x & y) ^ ((~x) & z));
253         }
254
255         private ulong Maj (ulong x, ulong y, ulong z)
256         {
257                 return ((x & y) ^ (x & z) ^ (y & z));
258         }
259
260         private ulong Sum0 (ulong x)
261         {
262                 return rotateRight (x, 28) ^ rotateRight (x, 34) ^ rotateRight (x, 39);
263         }
264
265         private ulong Sum1 (ulong x)
266         {
267                 return rotateRight (x, 14) ^ rotateRight (x, 18) ^ rotateRight (x, 41);
268         }
269
270         private ulong Sigma0 (ulong x)
271         {
272                 return rotateRight (x, 1) ^ rotateRight(x, 8) ^ (x >> 7);
273         }
274
275         private ulong Sigma1 (ulong x)
276         {
277                 return rotateRight (x, 19) ^ rotateRight (x, 61) ^ (x >> 6);
278         }
279 }
280
281 }