[System.Security.SecurityCritical]  // auto-generated
        private static unsafe void SHATransform (UInt64* expandedBuffer, UInt64* state, byte* block)
        {
            UInt64 a, b, c, d, e, f, g, h;
            UInt64 aa, bb, cc, dd, ee, ff, hh, gg;
            UInt64 T1;

            a = state[0];
            b = state[1];
            c = state[2];
            d = state[3];
            e = state[4];
            f = state[5];
            g = state[6];
            h = state[7];

            // fill in the first 16 blocks of W.
            Utils.QuadWordFromBigEndian (expandedBuffer, 16, block);
            SHA512Expand (expandedBuffer);

            /* Apply the SHA512 compression function */
            // We are trying to be smart here and avoid as many copies as we can
            // The perf gain with this method over the straightforward modify and shift 
            // forward is >= 20%, so it's worth the pain
            for (int j=0; j<80; ) {
                T1 = h + Sigma_1(e) + Ch(e,f,g) + _K[j] + expandedBuffer[j];
                ee = d + T1;
                aa = T1 + Sigma_0(a) + Maj(a,b,c);
                j++;

                T1 = g + Sigma_1(ee) + Ch(ee,e,f) + _K[j] + expandedBuffer[j];
                ff = c + T1;
                bb = T1 + Sigma_0(aa) + Maj(aa,a,b);
                j++;

                T1 = f + Sigma_1(ff) + Ch(ff,ee,e) + _K[j] + expandedBuffer[j];
                gg = b + T1;
                cc = T1 + Sigma_0(bb) + Maj(bb,aa,a);
                j++;

                T1 = e + Sigma_1(gg) + Ch(gg,ff,ee) + _K[j] + expandedBuffer[j];
                hh = a + T1;
                dd = T1 + Sigma_0(cc) + Maj(cc,bb,aa);
                j++;

                T1 = ee + Sigma_1(hh) + Ch(hh,gg,ff) + _K[j] + expandedBuffer[j];
                h = aa + T1;
                d = T1 + Sigma_0(dd) + Maj(dd,cc,bb);
                j++;

                T1 = ff + Sigma_1(h) + Ch(h,hh,gg) + _K[j] + expandedBuffer[j];
                g = bb + T1;
                c = T1 + Sigma_0(d) + Maj(d,dd,cc);
                j++;

                T1 = gg + Sigma_1(g) + Ch(g,h,hh) + _K[j] + expandedBuffer[j];
                f = cc + T1;
                b = T1 + Sigma_0(c) + Maj(c,d,dd);
                j++;

                T1 = hh + Sigma_1(f) + Ch(f,g,h) + _K[j] + expandedBuffer[j];
                e = dd + T1;
                a = T1 + Sigma_0(b) + Maj(b,c,d);
                j++;
            }

            state[0] += a;
            state[1] += b;
            state[2] += c;
            state[3] += d;
            state[4] += e;
            state[5] += f;
            state[6] += g;
            state[7] += h;
        }
Exemplo n.º 2
0
        // Import/export functions

        // We can provide a default implementation of FromXmlString because we require
        // every RSA implementation to implement ImportParameters
        // All we have to do here is parse the XML.

        public override void FromXmlString(String xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            Contract.EndContractBlock();
            RSAParameters   rsaParams  = new RSAParameters();
            Parser          p          = new Parser(xmlString);
            SecurityElement topElement = p.GetTopElement();

            // Modulus is always present
            String modulusString = topElement.SearchForTextOfLocalName("Modulus");

            if (modulusString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "RSA", "Modulus"));
            }
            rsaParams.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(modulusString));

            // Exponent is always present
            String exponentString = topElement.SearchForTextOfLocalName("Exponent");

            if (exponentString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "RSA", "Exponent"));
            }
            rsaParams.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(exponentString));

            // P is optional
            String pString = topElement.SearchForTextOfLocalName("P");

            if (pString != null)
            {
                rsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
            }

            // Q is optional
            String qString = topElement.SearchForTextOfLocalName("Q");

            if (qString != null)
            {
                rsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
            }

            // DP is optional
            String dpString = topElement.SearchForTextOfLocalName("DP");

            if (dpString != null)
            {
                rsaParams.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dpString));
            }

            // DQ is optional
            String dqString = topElement.SearchForTextOfLocalName("DQ");

            if (dqString != null)
            {
                rsaParams.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dqString));
            }

            // InverseQ is optional
            String inverseQString = topElement.SearchForTextOfLocalName("InverseQ");

            if (inverseQString != null)
            {
                rsaParams.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inverseQString));
            }

            // D is optional
            String dString = topElement.SearchForTextOfLocalName("D");

            if (dString != null)
            {
                rsaParams.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dString));
            }

            ImportParameters(rsaParams);
        }
 public bool VerifyData(byte[] buffer, object halg, byte[] signature)
 {
     string str = Utils.ObjToOidValue(halg);
     byte[] rgbHash = Utils.ObjToHashAlgorithm(halg).ComputeHash(buffer);
     return this.VerifyHash(rgbHash, str, signature);
 }
 public byte[] SignData(byte[] buffer, int offset, int count, object halg)
 {
     string str = Utils.ObjToOidValue(halg);
     byte[] rgbHash = Utils.ObjToHashAlgorithm(halg).ComputeHash(buffer, offset, count);
     return this.SignHash(rgbHash, str);
 }
 public byte[] SignData(Stream inputStream, object halg)
 {
     string str = Utils.ObjToOidValue(halg);
     byte[] rgbHash = Utils.ObjToHashAlgorithm(halg).ComputeHash(inputStream);
     return this.SignHash(rgbHash, str);
 }
 public void ImportCspBlob(byte[] keyBlob)
 {
     Utils.ImportCspBlobHelper(CspAlgorithmType.Rsa, keyBlob, IsPublic(keyBlob), ref this._parameters, this._randomKeyContainer, ref this._safeProvHandle, ref this._safeKeyHandle);
 }
 public byte[] ExportCspBlob(bool includePrivateParameters)
 {
     this.GetKeyPair();
     return Utils.ExportCspBlobHelper(includePrivateParameters, this._parameters, this._safeKeyHandle);
 }