Пример #1
0
        public bool ECDHTest_KeyExchange(string svcProvider)
        {
            try
            {
                string txt = "This is a string to encode using ECDH";
                string res = "";
                byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(txt);
                byte[] encData = null;

                using (ECDiffieHellmanCryptoServiceProvider csp = new ECDiffieHellmanCryptoServiceProvider(svcProvider))
                {
                    csp.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                    csp.HashAlgorithm = MechanismType.SHA256;

                    using (ECDiffieHellmanCryptoServiceProvider csp2 = new ECDiffieHellmanCryptoServiceProvider(svcProvider))
                    {
                        csp2.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                        csp.HashAlgorithm = MechanismType.SHA256;

                        CryptoKey secret = csp.DeriveKeyMaterial(csp2.PublicKey);

                        CryptoKey secret2 = csp2.DeriveKeyMaterial(csp.PublicKey);

                        byte[] IV;

                        using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider(secret))
                        {
                            ICryptoTransform encr = aes.CreateEncryptor();

                            IV = aes.IV;

                            encData = encr.TransformFinalBlock(data, 0, data.Length);
                        }

                        using (AesCryptoServiceProvider aes2 = new AesCryptoServiceProvider(secret2))
                        {
                            aes2.IV = IV;

                            ICryptoTransform decr = aes2.CreateDecryptor();

                            byte[] decrData = decr.TransformFinalBlock(encData, 0, encData.Length);

                            res = new string(System.Text.UTF8Encoding.UTF8.GetChars(decrData));
                        }
                    }
                }

                return string.Compare(txt, res) == 0;
            }
            catch
            {
                return false;
            }
        }
        private static bool TestKeyExchangeHmac()
        {
            bool passed = true;

            int[] keySizes = new int[] { 256, 384, 521 };
            MechanismType[] hashAlgorithms = new MechanismType[]
            {
                MechanismType.MD5,
                MechanismType.SHA_1,
                MechanismType.SHA256,
                MechanismType.SHA384,
                MechanismType.SHA512
            };

            foreach (int keySize in keySizes)
            {
                WriteLine("  Using key size " + keySize);

                foreach (MechanismType hashAlgorithm in hashAlgorithms)
                {
                    WriteLine("    Hash algorithm " + hashAlgorithm.ToString());

                    using (ECDiffieHellmanCryptoServiceProvider dh = new ECDiffieHellmanCryptoServiceProvider())
                    using (ECDiffieHellmanCryptoServiceProvider dh2 = new ECDiffieHellmanCryptoServiceProvider())
                    {
                        dh.KeySize = keySize;
                        dh2.KeySize = keySize;

                        dh.HashAlgorithm = hashAlgorithm;
                        dh2.HashAlgorithm = hashAlgorithm;

                        dh.SecretAppend = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
                        dh2.SecretAppend = dh.SecretAppend;

                        dh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hmac;
                        dh2.KeyDerivationFunction = dh.KeyDerivationFunction;

                        CryptoKey k1 = dh.DeriveKeyMaterial(dh2.PublicKey);
                        CryptoKey k2 = dh2.DeriveKeyMaterial(dh.PublicKey);

                        byte[] key1 = k1.ExportKey(true);
                        byte[] key2 = k2.ExportKey(true);

                        WriteLine("      " + key1.Length * 8 + " bit key generated");

                        if (key1 == null || key2 == null)
                        {
                            WriteLine("Fail -- null key");
                            passed = false;
                        }

                        if (!CompareBytes(key1, key2))
                        {
                            WriteLine("Fail");
                            passed = false;
                        }
                    }
                }
            }

            return passed;
        }
Пример #3
0
        public MFTestResults ECDHTest_KeyExchangeDifferentTokens()
        {
            if (!m_isEmulator) return MFTestResults.Skip;

            try
            {
                string txt = "This is a string to encode using ECDH";
                string res = "";
                byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(txt);
                byte[] encData = null;

                using (ECDiffieHellmanCryptoServiceProvider csp = new ECDiffieHellmanCryptoServiceProvider(""))
                {
                    csp.KeySize = 521;
                    csp.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                    csp.HashAlgorithm = MechanismType.SHA256;

                    using (ECDiffieHellmanCryptoServiceProvider csp2 = new ECDiffieHellmanCryptoServiceProvider("Emulator_Crypto"))
                    {
                        csp2.KeySize = 521;
                        csp2.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                        csp2.HashAlgorithm = MechanismType.SHA256;

                        CryptoKey secret = csp.DeriveKeyMaterial(csp2.PublicKey);

                        CryptoKey secret2 = csp2.DeriveKeyMaterial(csp.PublicKey);

                        byte[] IV = new byte[16];

                        using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider(secret))
                        {
                            ICryptoTransform encr = aes.CreateEncryptor(secret, IV);

                            encData = encr.TransformFinalBlock(data, 0, data.Length);
                        }

                        using (AesCryptoServiceProvider aes2 = new AesCryptoServiceProvider(secret2))
                        {
                            ICryptoTransform decr = aes2.CreateDecryptor(secret2, IV);

                            ICryptoTransform encr = aes2.CreateEncryptor(secret2, IV);

                            byte[] encData2 = encr.TransformFinalBlock(data, 0, data.Length);

                            byte[] decrData2 = decr.TransformFinalBlock(encData2, 0, encData2.Length);

                            res = new string(System.Text.UTF8Encoding.UTF8.GetChars(decrData2));

                            byte[] decrData = decr.TransformFinalBlock(encData, 0, encData.Length);

                            res = new string(System.Text.UTF8Encoding.UTF8.GetChars(decrData));
                        }
                    }
                }

                return string.Compare(txt, res) == 0 ? MFTestResults.Pass : MFTestResults.Fail;
            }
            catch
            {
                // There is a discrpency in the derived key from openssl and .Net
                return MFTestResults.Skip;
            }

        }
        private static bool TestCreate()
        {
            bool passed = true;

            using (ECDiffieHellmanCryptoServiceProvider dh = new ECDiffieHellmanCryptoServiceProvider())
                passed &= dh != null && dh is ECDiffieHellmanCryptoServiceProvider;

            if (ECDiffieHellmanTest.m_isEmulator)
            {
                using (ECDiffieHellmanCryptoServiceProvider dh = new ECDiffieHellmanCryptoServiceProvider("Emulator_Crypto"))
                    passed &= dh != null && dh is ECDiffieHellmanCryptoServiceProvider;
            }

            return passed;
        }