New tests.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / SHA384Managed.cs
index 705d158cb91827512c3a024dafbad50ceb1f7656..43495bef6b11ed4e70869b0dce3bc1f9305e4eab 100644 (file)
@@ -3,42 +3,60 @@
 //
 // Authors:
 //     Dan Lewis (dihlewis@yahoo.co.uk)
-//     Sébastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot (sebastien@ximian.com)
 //
 // (C) 2002
 // Implementation translated from Bouncy Castle JCE (http://www.bouncycastle.org/)
 // See bouncycastle.txt for license.
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#if !MOONLIGHT
 
-using System;
+using System.Runtime.InteropServices;
 
 namespace System.Security.Cryptography {
        
+[ComVisible (true)]
 public class SHA384Managed : SHA384 {
 
        private byte[] xBuf;
        private int xBufOff;
 
-       [CLSCompliant(false)]
        private ulong byteCount1;
-       [CLSCompliant(false)]
        private ulong byteCount2;
 
-       [CLSCompliant(false)]
-       protected ulong H1, H2, H3, H4, H5, H6, H7, H8;
-
-       [CLSCompliant(false)]
-       private ulong[] W = new ulong [80];
+       private ulong H1, H2, H3, H4, H5, H6, H7, H8;
+       private ulong[] W;
        private int wOff;
 
        public SHA384Managed () 
        {
                xBuf = new byte [8];
-               xBufOff = 0;
-               Initialize ();
+               W = new ulong [80];
+               Initialize (false); // limited initialization
        }
 
-       public override void Initialize (
+       private void Initialize (bool reuse
        {
                // SHA-384 initial hash value
                // The first 64 bits of the fractional parts of the square roots
@@ -52,48 +70,55 @@ public class SHA384Managed : SHA384 {
                H7 = 0xdb0c2e0d64f98fa7L;
                H8 = 0x47b5481dbefa4fa4L;
 
-               byteCount1 = 0;
-               byteCount2 = 0;
+               if (reuse) {
+                       byteCount1 = 0;
+                       byteCount2 = 0;
 
-               xBufOff = 0;
-               for (int i = 0; i < xBuf.Length; i++) 
-                       xBuf [i] = 0;
+                       xBufOff = 0;
+                       for (int i = 0; i < xBuf.Length; i++) 
+                               xBuf [i] = 0;
 
-               wOff = 0;
-               for (int i = 0; i != W.Length; i++)
-                       W [i] = 0;
+                       wOff = 0;
+                       for (int i = 0; i != W.Length; i++)
+                               W [i] = 0;
+               }
+       }
+
+       public override void Initialize () 
+       {
+               Initialize (true); // reuse instance
        }
 
        // protected
 
-       protected override void HashCore (byte[] rgb, int start, int count
+       protected override void HashCore (byte[] rgb, int ibStart, int cbSize
        {
                // fill the current word
-               while ((xBufOff != 0) && (count > 0)) {
-                       update( rgb [start]);
-                       start++;
-                       count--;
+               while ((xBufOff != 0) && (cbSize > 0)) {
+                       update (rgb [ibStart]);
+                       ibStart++;
+                       cbSize--;
                }
 
                // process whole words.
-               while (count > xBuf.Length) {
-                       processWord(rgb, start);
-                       start += xBuf.Length;
-                       count -= xBuf.Length;
+               while (cbSize > xBuf.Length) {
+                       processWord (rgb, ibStart);
+                       ibStart += xBuf.Length;
+                       cbSize -= xBuf.Length;
                        byteCount1 += (ulong) xBuf.Length;
                }
 
                // load in the remainder.
-               while (count > 0) {
-                       update( rgb [start]);
-                       start++;
-                       count--;
+               while (cbSize > 0) {
+                       update (rgb [ibStart]);
+                       ibStart++;
+                       cbSize--;
                }
        }
 
        protected override byte[] HashFinal () 
        {
-               adjustByteCounts();
+               adjustByteCounts ();
 
                ulong lowBitLength = byteCount1 << 3;
                ulong hiBitLength = byteCount2;
@@ -130,14 +155,14 @@ public class SHA384Managed : SHA384 {
 
        private void processWord (byte[] input, int inOff)
        {
-               W [wOff++] = ( (ulong) (input [inOff] & 0xff) << 56)
-                       | ( (ulong) (input [inOff + 1] & 0xff) << 48)
-                       | ( (ulong) (input [inOff + 2] & 0xff) << 40)
-                       | ( (ulong) (input [inOff + 3] & 0xff) << 32)
-                       | ( (ulong) (input [inOff + 4] & 0xff) << 24)
-                       | ( (ulong) (input [inOff + 5] & 0xff) << 16)
-                       | ( (ulong) (input [inOff + 6] & 0xff) << 8)
-                       | ( (ulong) (input [inOff + 7] & 0xff)); 
+               W [wOff++] = ( (ulong) input [inOff] << 56)
+                       | ( (ulong) input [inOff + 1] << 48)
+                       | ( (ulong) input [inOff + 2] << 40)
+                       | ( (ulong) input [inOff + 3] << 32)
+                       | ( (ulong) input [inOff + 4] << 24)
+                       | ( (ulong) input [inOff + 5] << 16)
+                       | ( (ulong) input [inOff + 6] << 8)
+                       | ( (ulong) input [inOff + 7]); 
                if (wOff == 16)
                        processBlock ();
        }
@@ -167,31 +192,47 @@ public class SHA384Managed : SHA384 {
        private void processLength (ulong lowW, ulong hiW)
        {
                if (wOff > 14)
-                       processBlock();
+                       processBlock ();
                W[14] = hiW;
                W[15] = lowW;
        }
 
        private void processBlock ()
        {
+               ulong a, b, c, d, e, f, g, h;
+
+               // abcrem doesn't work on fields
+               ulong[] W = this.W;
+               ulong[] K2 = SHAConstants.K2;
+
                adjustByteCounts ();
                // expand 16 word block into 80 word blocks.
                for (int t = 16; t <= 79; t++)
-                       W[t] = Sigma1 (W [t - 2]) + W [t - 7] + Sigma0 (W [t - 15]) + W [t - 16];
-
+               {
+                       a = W[t-15];
+                       a = ((a >> 1) | (a << 63)) ^ ((a >> 8) | (a << 56)) ^ (a >> 7);
+                       b = W[t - 2];
+                       b = ((b >> 19) | (b << 45)) ^ ((b >> 61) | (b << 3)) ^ (b >> 6);
+                       W[t] = b + W[t - 7] + a + W[t - 16];
+               }
                // set up working variables.
-               ulong a = H1;
-               ulong b = H2;
-               ulong c = H3;
-               ulong d = H4;
-               ulong e = H5;
-               ulong f = H6;
-               ulong g = H7;
-               ulong h = H8;
-
-               for (int t = 0; t <= 79; t++) {
-                       ulong T1 = h + Sum1 (e) + Ch (e, f, g) + K [t] + W [t];
-                       ulong T2 = Sum0 (a) + Maj (a, b, c);
+               a = H1;
+               b = H2;
+               c = H3;
+               d = H4;
+               e = H5;
+               f = H6;
+               g = H7;
+               h = H8;
+
+               for (int t = 0; t <= 79; t++)
+               {
+                       ulong T1 = ((e >> 14) | (e << 50)) ^ ((e >> 18) | (e << 46)) ^ ((e >> 41) | (e << 23));
+                       T1 += h + ((e & f) ^ ((~e) & g)) + K2[t] + W[t];
+
+                       ulong T2 = ((a >> 28) | (a << 36)) ^ ((a >> 34) | (a << 30)) ^ ((a >> 39) | (a << 25));
+                       T2 += ((a & b) ^ (a & c) ^ (b & c));
+
                        h = g;
                        g = f;
                        f = e;
@@ -215,69 +256,9 @@ public class SHA384Managed : SHA384 {
                for (int i = 0; i != W.Length; i++)
                        W[i] = 0;
        }
-
-       private ulong rotateRight (ulong x, int n)
-       {
-               return (x >> n) | (x << (64 - n));
-       }
-
-       /* SHA-384 and SHA-512 functions (as for SHA-256 but for longs) */
-       private ulong Ch (ulong x, ulong y, ulong z)
-       {
-               return ((x & y) ^ ((~x) & z));
-       }
-
-       private ulong Maj (ulong x, ulong y, ulong z)
-       {
-               return ((x & y) ^ (x & z) ^ (y & z));
-       }
-
-       private ulong Sum0 (ulong x)
-       {
-               return rotateRight (x, 28) ^ rotateRight (x, 34) ^ rotateRight (x, 39);
-       }
-
-       private ulong Sum1 (ulong x)
-       {
-               return rotateRight (x, 14) ^ rotateRight (x, 18) ^ rotateRight (x, 41);
-       }
-
-       private ulong Sigma0 (ulong x)
-       {
-               return rotateRight (x, 1) ^ rotateRight(x, 8) ^ (x >> 7);
-       }
-
-       private ulong Sigma1 (ulong x)
-       {
-               return rotateRight (x, 19) ^ rotateRight (x, 61) ^ (x >> 6);
-       }
-
-       // SHA-384 and SHA-512 Constants
-       // Rrepresent the first 64 bits of the fractional parts of the
-       // cube roots of the first sixty-four prime numbers
-       static ulong[] K = {
-               0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL,
-               0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L,
-               0xd807aa98a3030242L, 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
-               0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L, 0xc19bf174cf692694L,
-               0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L,
-               0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
-               0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L,
-               0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL, 0x142929670a0e6e70L,
-               0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
-               0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L, 0x92722c851482353bL,
-               0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L,
-               0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L,
-               0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L,
-               0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L,
-               0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
-               0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL,
-               0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L,
-               0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL,
-               0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
-               0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L
-       };
-
 }
 
 }
+
+#endif
+