2 // Mono.Security.Cryptography SHA224 class implementation
3 // based on SHA256Managed class implementation (mscorlib.dll)
6 // Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
7 // Sebastien Pouliot <sebastien@ximian.com>
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #if !MONOTOUCH && !XAMMAC
34 using System.Security.Cryptography;
36 namespace Mono.Security.Cryptography {
38 public class SHA224Managed : SHA224 {
40 private const int BLOCK_SIZE_BYTES = 64;
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 SHA224Managed ()
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[28];
130 ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
132 for (i=0; i<7; 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;
162 uint[] K1 = SHAConstants.K1;
163 uint[] buff = this.buff;
165 count += BLOCK_SIZE_BYTES;
167 for (i=0; i<16; i++) {
168 buff[i] = (uint)(((inputBuffer[inputOffset+4*i]) << 24)
169 | ((inputBuffer[inputOffset+4*i+1]) << 16)
170 | ((inputBuffer[inputOffset+4*i+2]) << 8)
171 | ((inputBuffer[inputOffset+4*i+3])));
175 for (i=16; i<64; i++) {
177 t1 = (((t1 >> 7) | (t1 << 25)) ^ ((t1 >> 18) | (t1 << 14)) ^ (t1 >> 3));
180 t2 = (((t2 >> 17) | (t2 << 15)) ^ ((t2 >> 19) | (t2 << 13)) ^ (t2 >> 10));
181 buff[i] = t2 + buff[i - 7] + t1 + buff[i - 16];
193 for (i=0; i<64; i++) {
194 t1 = h + (((e >> 6) | (e << 26)) ^ ((e >> 11) | (e << 21)) ^ ((e >> 25) | (e << 7))) + ((e & f) ^ (~e & g)) + K1[i] + buff[i];
196 t2 = (((a >> 2) | (a << 30)) ^ ((a >> 13) | (a << 19)) ^ ((a >> 22) | (a << 10)));
197 t2 = t2 + ((a & b) ^ (a & c) ^ (b & c));
218 private void ProcessFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
220 ulong total = count + (ulong)inputCount;
221 int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES));
224 paddingSize += BLOCK_SIZE_BYTES;
226 byte[] fooBuffer = new byte[inputCount+paddingSize+8];
228 for (int i=0; i<inputCount; i++) {
229 fooBuffer[i] = inputBuffer[i+inputOffset];
232 fooBuffer[inputCount] = 0x80;
233 for (int i=inputCount+1; i<inputCount+paddingSize; i++) {
237 // I deal in bytes. The algorithm deals in bits.
238 ulong size = total << 3;
239 AddLength (size, fooBuffer, inputCount+paddingSize);
240 ProcessBlock (fooBuffer, 0);
242 if (inputCount+paddingSize+8 == 128) {
243 ProcessBlock(fooBuffer, 64);
247 internal void AddLength (ulong length, byte[] buffer, int position)
249 buffer [position++] = (byte)(length >> 56);
250 buffer [position++] = (byte)(length >> 48);
251 buffer [position++] = (byte)(length >> 40);
252 buffer [position++] = (byte)(length >> 32);
253 buffer [position++] = (byte)(length >> 24);
254 buffer [position++] = (byte)(length >> 16);
255 buffer [position++] = (byte)(length >> 8);
256 buffer [position] = (byte)(length);