2004-05-05 Sebastien Pouliot <sebastien@ximian.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 using System;
15
16 namespace System.Security.Cryptography {
17         
18 public class SHA384Managed : SHA384 {
19
20         private byte[] xBuf;
21         private int xBufOff;
22
23         private ulong byteCount1;
24         private ulong byteCount2;
25
26         private ulong H1, H2, H3, H4, H5, H6, H7, H8;
27         private ulong[] W;
28         private int wOff;
29
30         public SHA384Managed () 
31         {
32                 xBuf = new byte [8];
33                 W = new ulong [80];
34                 Initialize (false); // limited initialization
35         }
36
37         private void Initialize (bool reuse) 
38         {
39                 // SHA-384 initial hash value
40                 // The first 64 bits of the fractional parts of the square roots
41                 // of the 9th through 16th prime numbers
42                 H1 = 0xcbbb9d5dc1059ed8L;
43                 H2 = 0x629a292a367cd507L;
44                 H3 = 0x9159015a3070dd17L;
45                 H4 = 0x152fecd8f70e5939L;
46                 H5 = 0x67332667ffc00b31L;
47                 H6 = 0x8eb44a8768581511L;
48                 H7 = 0xdb0c2e0d64f98fa7L;
49                 H8 = 0x47b5481dbefa4fa4L;
50
51                 if (reuse) {
52                         byteCount1 = 0;
53                         byteCount2 = 0;
54
55                         xBufOff = 0;
56                         for (int i = 0; i < xBuf.Length; i++) 
57                                 xBuf [i] = 0;
58
59                         wOff = 0;
60                         for (int i = 0; i != W.Length; i++)
61                                 W [i] = 0;
62                 }
63         }
64
65         public override void Initialize () 
66         {
67                 Initialize (true); // reuse instance
68         }
69
70         // protected
71
72         protected override void HashCore (byte[] rgb, int start, int count) 
73         {
74                 // fill the current word
75                 while ((xBufOff != 0) && (count > 0)) {
76                         update (rgb [start]);
77                         start++;
78                         count--;
79                 }
80
81                 // process whole words.
82                 while (count > xBuf.Length) {
83                         processWord (rgb, start);
84                         start += xBuf.Length;
85                         count -= xBuf.Length;
86                         byteCount1 += (ulong) xBuf.Length;
87                 }
88
89                 // load in the remainder.
90                 while (count > 0) {
91                         update (rgb [start]);
92                         start++;
93                         count--;
94                 }
95         }
96
97         protected override byte[] HashFinal () 
98         {
99                 adjustByteCounts ();
100
101                 ulong lowBitLength = byteCount1 << 3;
102                 ulong hiBitLength = byteCount2;
103
104                 // add the pad bytes.
105                 update ( (byte) 128);
106                 while (xBufOff != 0)
107                         update ( (byte)0);
108
109                 processLength (lowBitLength, hiBitLength);
110                 processBlock ();
111                 
112                 byte[] output = new byte [48];
113                 unpackWord(H1, output, 0);
114                 unpackWord(H2, output, 8);
115                 unpackWord(H3, output, 16);
116                 unpackWord(H4, output, 24);
117                 unpackWord(H5, output, 32);
118                 unpackWord(H6, output, 40);
119
120                 Initialize ();
121                 return output;
122         }
123
124         private void update (byte input)
125         {
126                 xBuf [xBufOff++] = input;
127                 if (xBufOff == xBuf.Length) {
128                         processWord(xBuf, 0);
129                         xBufOff = 0;
130                 }
131                 byteCount1++;
132         }
133
134         private void processWord (byte[] input, int inOff)
135         {
136                 W [wOff++] = ( (ulong) input [inOff] << 56)
137                         | ( (ulong) input [inOff + 1] << 48)
138                         | ( (ulong) input [inOff + 2] << 40)
139                         | ( (ulong) input [inOff + 3] << 32)
140                         | ( (ulong) input [inOff + 4] << 24)
141                         | ( (ulong) input [inOff + 5] << 16)
142                         | ( (ulong) input [inOff + 6] << 8)
143                         | ( (ulong) input [inOff + 7]); 
144                 if (wOff == 16)
145                         processBlock ();
146         }
147
148         private void unpackWord (ulong word, byte[] output, int outOff)
149         {
150                 output[outOff]     = (byte) (word >> 56);
151                 output[outOff + 1] = (byte) (word >> 48);
152                 output[outOff + 2] = (byte) (word >> 40);
153                 output[outOff + 3] = (byte) (word >> 32);
154                 output[outOff + 4] = (byte) (word >> 24);
155                 output[outOff + 5] = (byte) (word >> 16);
156                 output[outOff + 6] = (byte) (word >> 8);
157                 output[outOff + 7] = (byte) word;
158         }
159
160         // adjust the byte counts so that byteCount2 represents the
161         // upper long (less 3 bits) word of the byte count.
162         private void adjustByteCounts()
163         {
164                 if (byteCount1 > 0x1fffffffffffffffL) {
165                         byteCount2 += (byteCount1 >> 61);
166                         byteCount1 &= 0x1fffffffffffffffL;
167                 }
168         }
169
170         private void processLength (ulong lowW, ulong hiW)
171         {
172                 if (wOff > 14)
173                         processBlock ();
174                 W[14] = hiW;
175                 W[15] = lowW;
176         }
177
178         private void processBlock ()
179         {
180                 adjustByteCounts ();
181                 // expand 16 word block into 80 word blocks.
182                 for (int t = 16; t <= 79; t++)
183                         W[t] = Sigma1 (W [t - 2]) + W [t - 7] + Sigma0 (W [t - 15]) + W [t - 16];
184
185                 // set up working variables.
186                 ulong a = H1;
187                 ulong b = H2;
188                 ulong c = H3;
189                 ulong d = H4;
190                 ulong e = H5;
191                 ulong f = H6;
192                 ulong g = H7;
193                 ulong h = H8;
194
195                 for (int t = 0; t <= 79; t++) {
196                         ulong T1 = h + Sum1 (e) + Ch (e, f, g) + SHAConstants.K2 [t] + W [t];
197                         ulong T2 = Sum0 (a) + Maj (a, b, c);
198                         h = g;
199                         g = f;
200                         f = e;
201                         e = d + T1;
202                         d = c;
203                         c = b;
204                         b = a;
205                         a = T1 + T2;
206                 }
207
208                 H1 += a;
209                 H2 += b;
210                 H3 += c;
211                 H4 += d;
212                 H5 += e;
213                 H6 += f;
214                 H7 += g;
215                 H8 += h;
216                 // reset the offset and clean out the word buffer.
217                 wOff = 0;
218                 for (int i = 0; i != W.Length; i++)
219                         W[i] = 0;
220         }
221
222         private ulong rotateRight (ulong x, int n)
223         {
224                 return (x >> n) | (x << (64 - n));
225         }
226
227         /* SHA-384 and SHA-512 functions (as for SHA-256 but for longs) */
228         private ulong Ch (ulong x, ulong y, ulong z)
229         {
230                 return ((x & y) ^ ((~x) & z));
231         }
232
233         private ulong Maj (ulong x, ulong y, ulong z)
234         {
235                 return ((x & y) ^ (x & z) ^ (y & z));
236         }
237
238         private ulong Sum0 (ulong x)
239         {
240                 return rotateRight (x, 28) ^ rotateRight (x, 34) ^ rotateRight (x, 39);
241         }
242
243         private ulong Sum1 (ulong x)
244         {
245                 return rotateRight (x, 14) ^ rotateRight (x, 18) ^ rotateRight (x, 41);
246         }
247
248         private ulong Sigma0 (ulong x)
249         {
250                 return rotateRight (x, 1) ^ rotateRight(x, 8) ^ (x >> 7);
251         }
252
253         private ulong Sigma1 (ulong x)
254         {
255                 return rotateRight (x, 19) ^ rotateRight (x, 61) ^ (x >> 6);
256         }
257 }
258
259 }