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