2 // System.Security.Cryptography.SHA512Managed.cs
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot (sebastien@ximian.com)
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)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
33 using System.Runtime.InteropServices;
35 namespace System.Security.Cryptography {
40 public class SHA512Managed : SHA512 {
45 private ulong byteCount1;
46 private ulong byteCount2;
48 private ulong H1, H2, H3, H4, H5, H6, H7, H8;
53 public SHA512Managed ()
57 Initialize (false); // limited initialization
60 private void Initialize (bool reuse)
62 // SHA-512 initial hash value
63 // The first 64 bits of the fractional parts of the square roots
64 // of the first eight prime numbers
65 H1 = 0x6a09e667f3bcc908L;
66 H2 = 0xbb67ae8584caa73bL;
67 H3 = 0x3c6ef372fe94f82bL;
68 H4 = 0xa54ff53a5f1d36f1L;
69 H5 = 0x510e527fade682d1L;
70 H6 = 0x9b05688c2b3e6c1fL;
71 H7 = 0x1f83d9abfb41bd6bL;
72 H8 = 0x5be0cd19137e2179L;
79 for (int i = 0; i < xBuf.Length; i++)
83 for (int i = 0; i != W.Length; i++)
88 public override void Initialize ()
90 Initialize (true); // reuse instance
95 protected override void HashCore (byte[] rgb, int start, int count)
97 // fill the current word
98 while ((xBufOff != 0) && (count > 0)) {
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;
112 // load in the remainder.
114 update (rgb [start]);
120 protected override byte[] HashFinal ()
124 ulong lowBitLength = byteCount1 << 3;
125 ulong hiBitLength = byteCount2;
127 // add the pad bytes.
132 processLength (lowBitLength, hiBitLength);
135 byte[] output = new byte [64];
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 unpackWord(H7, output, 48);
143 unpackWord(H8, output, 56);
149 private void update (byte input)
151 xBuf [xBufOff++] = input;
152 if (xBufOff == xBuf.Length) {
153 processWord(xBuf, 0);
159 private void processWord (byte[] input, int inOff)
161 W [wOff++] = ( (ulong) input [inOff] << 56)
162 | ( (ulong) input [inOff + 1] << 48)
163 | ( (ulong) input [inOff + 2] << 40)
164 | ( (ulong) input [inOff + 3] << 32)
165 | ( (ulong) input [inOff + 4] << 24)
166 | ( (ulong) input [inOff + 5] << 16)
167 | ( (ulong) input [inOff + 6] << 8)
168 | ( (ulong) input [inOff + 7]);
173 private void unpackWord (ulong word, byte[] output, int outOff)
175 output[outOff] = (byte) (word >> 56);
176 output[outOff + 1] = (byte) (word >> 48);
177 output[outOff + 2] = (byte) (word >> 40);
178 output[outOff + 3] = (byte) (word >> 32);
179 output[outOff + 4] = (byte) (word >> 24);
180 output[outOff + 5] = (byte) (word >> 16);
181 output[outOff + 6] = (byte) (word >> 8);
182 output[outOff + 7] = (byte) word;
185 // adjust the byte counts so that byteCount2 represents the
186 // upper long (less 3 bits) word of the byte count.
187 private void adjustByteCounts ()
189 if (byteCount1 > 0x1fffffffffffffffL) {
190 byteCount2 += (byteCount1 >> 61);
191 byteCount1 &= 0x1fffffffffffffffL;
195 private void processLength (ulong lowW, ulong hiW)
203 private void processBlock ()
206 // expand 16 word block into 80 word blocks.
207 for (int t = 16; t <= 79; t++)
208 W[t] = Sigma1 (W [t - 2]) + W [t - 7] + Sigma0 (W [t - 15]) + W [t - 16];
210 // set up working variables.
220 for (int t = 0; t <= 79; t++) {
221 ulong T1 = h + Sum1 (e) + Ch (e, f, g) + SHAConstants.K2 [t] + W [t];
222 ulong T2 = Sum0 (a) + Maj (a, b, c);
241 // reset the offset and clean out the word buffer.
243 for (int i = 0; i != W.Length; i++)
247 private ulong rotateRight (ulong x, int n)
249 return (x >> n) | (x << (64 - n));
252 /* SHA-512 and SHA-512 functions (as for SHA-256 but for longs) */
253 private ulong Ch (ulong x, ulong y, ulong z)
255 return ((x & y) ^ ((~x) & z));
258 private ulong Maj (ulong x, ulong y, ulong z)
260 return ((x & y) ^ (x & z) ^ (y & z));
263 private ulong Sum0 (ulong x)
265 return rotateRight (x, 28) ^ rotateRight (x, 34) ^ rotateRight (x, 39);
268 private ulong Sum1 (ulong x)
270 return rotateRight (x, 14) ^ rotateRight (x, 18) ^ rotateRight (x, 41);
273 private ulong Sigma0 (ulong x)
275 return rotateRight (x, 1) ^ rotateRight(x, 8) ^ (x >> 7);
278 private ulong Sigma1 (ulong x)
280 return rotateRight (x, 19) ^ rotateRight (x, 61) ^ (x >> 6);