2 // System.Security.Cryptography.SHA384Managed.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.
35 using System
.Runtime
.InteropServices
;
37 namespace System
.Security
.Cryptography
{
40 public class SHA384Managed
: SHA384
{
45 private ulong byteCount1
;
46 private ulong byteCount2
;
48 private ulong H1
, H2
, H3
, H4
, H5
, H6
, H7
, H8
;
52 public SHA384Managed ()
56 Initialize (false); // limited initialization
59 private void Initialize (bool reuse
)
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
;
78 for (int i
= 0; i
< xBuf
.Length
; i
++)
82 for (int i
= 0; i
!= W
.Length
; i
++)
87 public override void Initialize ()
89 Initialize (true); // reuse instance
94 protected override void HashCore (byte[] rgb
, int ibStart
, int cbSize
)
96 // fill the current word
97 while ((xBufOff
!= 0) && (cbSize
> 0)) {
98 update (rgb
[ibStart
]);
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
;
111 // load in the remainder.
113 update (rgb
[ibStart
]);
119 protected override byte[] HashFinal ()
123 ulong lowBitLength
= byteCount1
<< 3;
124 ulong hiBitLength
= byteCount2
;
126 // add the pad bytes.
127 update ( (byte) 128);
131 processLength (lowBitLength
, hiBitLength
);
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);
146 private void update (byte input
)
148 xBuf
[xBufOff
++] = input
;
149 if (xBufOff
== xBuf
.Length
) {
150 processWord(xBuf
, 0);
156 private void processWord (byte[] input
, int inOff
)
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]);
170 private void unpackWord (ulong word
, byte[] output
, int outOff
)
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
;
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()
186 if (byteCount1
> 0x1fffffffffffffffL
) {
187 byteCount2
+= (byteCount1
>> 61);
188 byteCount1
&= 0x1fffffffffffffffL
;
192 private void processLength (ulong lowW
, ulong hiW
)
200 private void processBlock ()
202 ulong a
, b
, c
, d
, e
, f
, g
, h
;
204 // abcrem doesn't work on fields
206 ulong[] K2
= SHAConstants
.K2
;
209 // expand 16 word block into 80 word blocks.
210 for (int t
= 16; t
<= 79; t
++)
213 a
= ((a
>> 1) | (a
<< 63)) ^
((a
>> 8) | (a
<< 56)) ^
(a
>> 7);
215 b
= ((b
>> 19) | (b
<< 45)) ^
((b
>> 61) | (b
<< 3)) ^
(b
>> 6);
216 W
[t
] = b
+ W
[t
- 7] + a
+ W
[t
- 16];
218 // set up working variables.
228 for (int t
= 0; t
<= 79; t
++)
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
];
233 ulong T2
= ((a
>> 28) | (a
<< 36)) ^
((a
>> 34) | (a
<< 30)) ^
((a
>> 39) | (a
<< 25));
234 T2
+= ((a
& b
) ^
(a
& c
) ^
(b
& c
));
254 // reset the offset and clean out the word buffer.
256 for (int i
= 0; i
!= W
.Length
; i
++)