Merge pull request #216 from ilkerde/master
[mono.git] / mcs / class / corlib / System.Security.Cryptography / SHA256Managed.cs
index 82cc6672a527248214bd831b4264479d1618818b..9f4c2b85ad8dfd1d9c3557ce1e95bda4fb1abc8c 100644 (file)
@@ -1,5 +1,5 @@
 //
-// System.Security.Cryptography SHA256Managed Class implementation
+// System.Security.Cryptography.SHA256Managed.cs
 //
 // Author:
 //   Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+using System.Runtime.InteropServices;
+
 namespace System.Security.Cryptography {
        
+       [ComVisible (true)]
        public class SHA256Managed : SHA256 {
 
                private const int BLOCK_SIZE_BYTES =  64;
-               private const int HASH_SIZE_BYTES  =  32;
                private uint[] _H;
-               private uint[] K;
                private ulong count;
                private byte[] _ProcessingBuffer;   // Used to start data when passed less than a block worth.
                private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed.
@@ -48,72 +49,34 @@ namespace System.Security.Cryptography {
                        Initialize ();
                }
 
-               private uint Ch (uint u, uint v, uint w) 
-               {
-                       return (u&v) ^ (~u&w);
-               }
-
-               private uint Maj (uint u, uint v, uint w) 
-               {
-                       return (u&v) ^ (u&w) ^ (v&w);
-               }
-
-               private uint Ro0 (uint x) 
-               {
-                       return ((x >> 7) | (x << 25))
-                               ^ ((x >> 18) | (x << 14))
-                               ^ (x >> 3);
-               }
-
-               private uint Ro1 (uint x) 
-               {
-                       return ((x >> 17) | (x << 15))
-                               ^ ((x >> 19) | (x << 13))
-                               ^ (x >> 10);
-               }
-
-               private uint Sig0 (uint x) 
-               {
-                       return ((x >> 2) | (x << 30))
-                               ^ ((x >> 13) | (x << 19))
-                               ^ ((x >> 22) | (x << 10));
-               }
-
-               private uint Sig1 (uint x) 
-               {
-                       return ((x >> 6) | (x << 26))
-                               ^ ((x >> 11) | (x << 21))
-                               ^ ((x >> 25) | (x << 7));
-               }
-
-               protected override void HashCore (byte[] rgb, int start, int size) 
+               protected override void HashCore (byte[] rgb, int ibStart, int cbSize) 
                {
                        int i;
                        State = 1;
 
                        if (_ProcessingBufferCount != 0) {
-                               if (size < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
-                                       System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, size);
-                                       _ProcessingBufferCount += size;
+                               if (cbSize < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) {
+                                       System.Buffer.BlockCopy (rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, cbSize);
+                                       _ProcessingBufferCount += cbSize;
                                        return;
                                }
                                else {
                                        i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount);
-                                       System.Buffer.BlockCopy (rgb, start, _ProcessingBuffer, _ProcessingBufferCount, i);
+                                       System.Buffer.BlockCopy (rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, i);
                                        ProcessBlock (_ProcessingBuffer, 0);
                                        _ProcessingBufferCount = 0;
-                                       start += i;
-                                       size -= i;
+                                       ibStart += i;
+                                       cbSize -= i;
                                }
                        }
 
-                       for (i=0; i<size-size%BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
-                               ProcessBlock (rgb, start+i);
+                       for (i = 0; i < cbSize - cbSize % BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) {
+                               ProcessBlock (rgb, ibStart + i);
                        }
 
-                       if (size%BLOCK_SIZE_BYTES != 0) {
-                               System.Buffer.BlockCopy (rgb, size-size%BLOCK_SIZE_BYTES+start, _ProcessingBuffer, 0, size%BLOCK_SIZE_BYTES);
-                               _ProcessingBufferCount = size%BLOCK_SIZE_BYTES;
+                       if (cbSize % BLOCK_SIZE_BYTES != 0) {
+                               System.Buffer.BlockCopy (rgb, cbSize - cbSize % BLOCK_SIZE_BYTES + ibStart, _ProcessingBuffer, 0, cbSize % BLOCK_SIZE_BYTES);
+                               _ProcessingBufferCount = cbSize % BLOCK_SIZE_BYTES;
                        }
                }
        
@@ -154,19 +117,26 @@ namespace System.Security.Cryptography {
                        uint a, b, c, d, e, f, g, h;
                        uint t1, t2;
                        int i;
+                       uint[] K1 = SHAConstants.K1;
+                       uint[] buff = this.buff;
                
                        count += BLOCK_SIZE_BYTES;
 
                        for (i=0; i<16; i++) {
-                               buff[i] = ((uint)(inputBuffer[inputOffset+4*i]) << 24)
-                                       | ((uint)(inputBuffer[inputOffset+4*i+1]) << 16)
-                                       | ((uint)(inputBuffer[inputOffset+4*i+2]) <<  8)
-                                       | ((uint)(inputBuffer[inputOffset+4*i+3]));
+                               buff[i] = (uint)(((inputBuffer[inputOffset + 4 * i]) << 24)
+                                       | ((inputBuffer[inputOffset + 4 * i + 1]) << 16)
+                                       | ((inputBuffer[inputOffset + 4 * i + 2]) << 8)
+                                       | ((inputBuffer[inputOffset + 4 * i + 3])));
                        }
 
                
                        for (i=16; i<64; i++) {
-                               buff[i] = Ro1(buff[i-2]) + buff[i-7] + Ro0(buff[i-15]) + buff[i-16];
+                               t1 = buff[i - 15];
+                               t1 = (((t1 >> 7) | (t1 << 25)) ^ ((t1 >> 18) | (t1 << 14)) ^ (t1 >> 3));
+
+                               t2 = buff[i - 2];
+                               t2 = (((t2 >> 17) | (t2 << 15)) ^ ((t2 >> 19) | (t2 << 13)) ^ (t2 >> 10));
+                               buff[i] = t2 + buff[i - 7] + t1 + buff[i - 16];
                        }
 
                        a = _H[0];
@@ -179,8 +149,10 @@ namespace System.Security.Cryptography {
                        h = _H[7];
 
                        for (i=0; i<64; i++) {
-                               t1 = h + Sig1(e) + Ch(e,f,g) + SHAConstants.K1 [i] + buff[i];
-                               t2 = Sig0(a) + Maj(a,b,c);
+                               t1 = h + (((e >> 6) | (e << 26)) ^ ((e >> 11) | (e << 21)) ^ ((e >> 25) | (e << 7))) + ((e & f) ^ (~e & g)) + K1[i] + buff[i];
+
+                               t2 = (((a >> 2) | (a << 30)) ^ ((a >> 13) | (a << 19)) ^ ((a >> 22) | (a << 10)));
+                               t2 = t2 + ((a & b) ^ (a & c) ^ (b & c));
                                h = g;
                                g = f;
                                f = e;