2 // System.Security.Cryptography SHA256Managed Class implementation
5 // Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
8 // Copyright (C) 2004, 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
32 namespace System.Security.Cryptography {
37 public class SHA256Managed : SHA256 {
39 private const int BLOCK_SIZE_BYTES = 64;
40 private const int HASH_SIZE_BYTES = 32;
44 private byte[] _ProcessingBuffer; // Used to start data when passed less than a block worth.
45 private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
48 public SHA256Managed ()
51 _ProcessingBuffer = new byte [BLOCK_SIZE_BYTES];
56 private uint Ch (uint u, uint v, uint w)
58 return (u&v) ^ (~u&w);
61 private uint Maj (uint u, uint v, uint w)
63 return (u&v) ^ (u&w) ^ (v&w);
66 private uint Ro0 (uint x)
68 return ((x >> 7) | (x << 25))
69 ^ ((x >> 18) | (x << 14))
73 private uint Ro1 (uint x)
75 return ((x >> 17) | (x << 15))
76 ^ ((x >> 19) | (x << 13))
80 private uint Sig0 (uint x)
82 return ((x >> 2) | (x << 30))
83 ^ ((x >> 13) | (x << 19))
84 ^ ((x >> 22) | (x << 10));
87 private uint Sig1 (uint x)
89 return ((x >> 6) | (x << 26))
90 ^ ((x >> 11) | (x << 21))
91 ^ ((x >> 25) | (x << 7));
94 protected override void HashCore (byte[] rgb, int start, int size)
99 if (_ProcessingBufferCount != 0) {
100 if (size < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
101 System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, size);
102 _ProcessingBufferCount += size;
106 i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
107 System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, i);
108 ProcessBlock (_ProcessingBuffer, 0);
109 _ProcessingBufferCount = 0;
115 for (i=0; i<size-size%BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
116 ProcessBlock (rgb, start+i);
119 if (size%BLOCK_SIZE_BYTES != 0) {
120 System.Buffer.BlockCopy (rgb, size-size%BLOCK_SIZE_BYTES+start, _ProcessingBuffer, 0, size%BLOCK_SIZE_BYTES);
121 _ProcessingBufferCount = size%BLOCK_SIZE_BYTES;
125 protected override byte[] HashFinal ()
127 byte[] hash = new byte[32];
130 ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
132 for (i=0; i<8; i++) {
133 for (j=0; j<4; j++) {
134 hash[i*4+j] = (byte)(_H[i] >> (24-j*8));
142 public override void Initialize ()
145 _ProcessingBufferCount = 0;
157 private void ProcessBlock (byte[] inputBuffer, int inputOffset)
159 uint a, b, c, d, e, f, g, h;
163 count += BLOCK_SIZE_BYTES;
165 for (i=0; i<16; i++) {
166 buff[i] = ((uint)(inputBuffer[inputOffset+4*i]) << 24)
167 | ((uint)(inputBuffer[inputOffset+4*i+1]) << 16)
168 | ((uint)(inputBuffer[inputOffset+4*i+2]) << 8)
169 | ((uint)(inputBuffer[inputOffset+4*i+3]));
173 for (i=16; i<64; i++) {
174 buff[i] = Ro1(buff[i-2]) + buff[i-7] + Ro0(buff[i-15]) + buff[i-16];
186 for (i=0; i<64; i++) {
187 t1 = h + Sig1(e) + Ch(e,f,g) + SHAConstants.K1 [i] + buff[i];
188 t2 = Sig0(a) + Maj(a,b,c);
209 private void ProcessFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
211 ulong total = count + (ulong)inputCount;
212 int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES));
215 paddingSize += BLOCK_SIZE_BYTES;
217 byte[] fooBuffer = new byte[inputCount+paddingSize+8];
219 for (int i=0; i<inputCount; i++) {
220 fooBuffer[i] = inputBuffer[i+inputOffset];
223 fooBuffer[inputCount] = 0x80;
224 for (int i=inputCount+1; i<inputCount+paddingSize; i++) {
228 // I deal in bytes. The algorithm deals in bits.
229 ulong size = total << 3;
230 AddLength (size, fooBuffer, inputCount+paddingSize);
231 ProcessBlock (fooBuffer, 0);
233 if (inputCount+paddingSize+8 == 128) {
234 ProcessBlock(fooBuffer, 64);
238 internal void AddLength (ulong length, byte[] buffer, int position)
240 buffer [position++] = (byte)(length >> 56);
241 buffer [position++] = (byte)(length >> 48);
242 buffer [position++] = (byte)(length >> 40);
243 buffer [position++] = (byte)(length >> 32);
244 buffer [position++] = (byte)(length >> 24);
245 buffer [position++] = (byte)(length >> 16);
246 buffer [position++] = (byte)(length >> 8);
247 buffer [position] = (byte)(length);