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 using System.Security.Cryptography;
34 namespace Mono.Security.Cryptography {
36 public class SHA224Managed : SHA224 {
38 private const int BLOCK_SIZE_BYTES = 64;
42 private byte[] _ProcessingBuffer; // Used to start data when passed less than a block worth.
43 private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
46 public SHA224Managed ()
49 _ProcessingBuffer = new byte [BLOCK_SIZE_BYTES];
54 private uint Ch (uint u, uint v, uint w)
56 return (u&v) ^ (~u&w);
59 private uint Maj (uint u, uint v, uint w)
61 return (u&v) ^ (u&w) ^ (v&w);
64 private uint Ro0 (uint x)
66 return ((x >> 7) | (x << 25))
67 ^ ((x >> 18) | (x << 14))
71 private uint Ro1 (uint x)
73 return ((x >> 17) | (x << 15))
74 ^ ((x >> 19) | (x << 13))
78 private uint Sig0 (uint x)
80 return ((x >> 2) | (x << 30))
81 ^ ((x >> 13) | (x << 19))
82 ^ ((x >> 22) | (x << 10));
85 private uint Sig1 (uint x)
87 return ((x >> 6) | (x << 26))
88 ^ ((x >> 11) | (x << 21))
89 ^ ((x >> 25) | (x << 7));
92 protected override void HashCore (byte[] rgb, int start, int size)
97 if (_ProcessingBufferCount != 0) {
98 if (size < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
99 System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, size);
100 _ProcessingBufferCount += size;
104 i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
105 System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, i);
106 ProcessBlock (_ProcessingBuffer, 0);
107 _ProcessingBufferCount = 0;
113 for (i=0; i<size-size%BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
114 ProcessBlock (rgb, start+i);
117 if (size%BLOCK_SIZE_BYTES != 0) {
118 System.Buffer.BlockCopy (rgb, size-size%BLOCK_SIZE_BYTES+start, _ProcessingBuffer, 0, size%BLOCK_SIZE_BYTES);
119 _ProcessingBufferCount = size%BLOCK_SIZE_BYTES;
123 protected override byte[] HashFinal ()
125 byte[] hash = new byte[28];
128 ProcessFinalBlock (_ProcessingBuffer, 0, _ProcessingBufferCount);
130 for (i=0; i<7; i++) {
131 for (j=0; j<4; j++) {
132 hash[i*4+j] = (byte)(_H[i] >> (24-j*8));
140 public override void Initialize ()
143 _ProcessingBufferCount = 0;
155 private void ProcessBlock (byte[] inputBuffer, int inputOffset)
157 uint a, b, c, d, e, f, g, h;
160 uint[] K1 = SHAConstants.K1;
161 uint[] buff = this.buff;
163 count += BLOCK_SIZE_BYTES;
165 for (i=0; i<16; i++) {
166 buff[i] = (uint)(((inputBuffer[inputOffset+4*i]) << 24)
167 | ((inputBuffer[inputOffset+4*i+1]) << 16)
168 | ((inputBuffer[inputOffset+4*i+2]) << 8)
169 | ((inputBuffer[inputOffset+4*i+3])));
173 for (i=16; i<64; i++) {
175 t1 = (((t1 >> 7) | (t1 << 25)) ^ ((t1 >> 18) | (t1 << 14)) ^ (t1 >> 3));
178 t2 = (((t2 >> 17) | (t2 << 15)) ^ ((t2 >> 19) | (t2 << 13)) ^ (t2 >> 10));
179 buff[i] = t2 + buff[i - 7] + t1 + buff[i - 16];
191 for (i=0; i<64; i++) {
192 t1 = h + (((e >> 6) | (e << 26)) ^ ((e >> 11) | (e << 21)) ^ ((e >> 25) | (e << 7))) + ((e & f) ^ (~e & g)) + K1[i] + buff[i];
194 t2 = (((a >> 2) | (a << 30)) ^ ((a >> 13) | (a << 19)) ^ ((a >> 22) | (a << 10)));
195 t2 = t2 + ((a & b) ^ (a & c) ^ (b & c));
216 private void ProcessFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
218 ulong total = count + (ulong)inputCount;
219 int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES));
222 paddingSize += BLOCK_SIZE_BYTES;
224 byte[] fooBuffer = new byte[inputCount+paddingSize+8];
226 for (int i=0; i<inputCount; i++) {
227 fooBuffer[i] = inputBuffer[i+inputOffset];
230 fooBuffer[inputCount] = 0x80;
231 for (int i=inputCount+1; i<inputCount+paddingSize; i++) {
235 // I deal in bytes. The algorithm deals in bits.
236 ulong size = total << 3;
237 AddLength (size, fooBuffer, inputCount+paddingSize);
238 ProcessBlock (fooBuffer, 0);
240 if (inputCount+paddingSize+8 == 128) {
241 ProcessBlock(fooBuffer, 64);
245 internal void AddLength (ulong length, byte[] buffer, int position)
247 buffer [position++] = (byte)(length >> 56);
248 buffer [position++] = (byte)(length >> 48);
249 buffer [position++] = (byte)(length >> 40);
250 buffer [position++] = (byte)(length >> 32);
251 buffer [position++] = (byte)(length >> 24);
252 buffer [position++] = (byte)(length >> 16);
253 buffer [position++] = (byte)(length >> 8);
254 buffer [position] = (byte)(length);