Пример #1
0
 public void RNGCngNegativeTest()
 {
     using (RNGCng rng = new RNGCng())
     {
         rng.GetBytes(null);
     }
 }
Пример #2
0
        public void PBKDF2ZeroLengthTest()
        {
            // Test with zero-length salt/password
            RNGCng rng = new RNGCng();

            byte[] password;
            byte[] salt;

            // Test with zero-length salt
            password = new byte[10];
            rng.GetBytes(password);
            salt = new byte[0];
            foreach (string hash in hashes)
            {
                byte[] derivedKey = BCryptPBKDF2.ComputeHash(hash, password, salt, ITERATION_COUNT);
                ValidateDerivedKey(derivedKey, hash);
            }

            // Test with zero-length password
            salt = new byte[16];
            rng.GetBytes(salt);
            password = new byte[0];
            foreach (string hash in hashes)
            {
                byte[] derivedKey = BCryptPBKDF2.ComputeHash(hash, password, salt, ITERATION_COUNT);
                ValidateDerivedKey(derivedKey, hash);
            }
        }
Пример #3
0
 public void RNGCngPropertiesTest()
 {
     using (RNGCng rng = new RNGCng())
     {
         Assert.AreEqual(CngProvider2.MicrosoftPrimitiveAlgorithmProvider, rng.Provider);
     }
 }
Пример #4
0
        public void TripleDESCngPaddingRoundTripTests()
        {
            int blockSize = 0;

            using (TripleDESCng tdes = new TripleDESCng())
            {
                blockSize = tdes.BlockSize / 8;
            }

            using (RNGCng rng = new RNGCng())
            {
                byte[] zeroByte = new byte[0];

                byte[] oneByte = new byte[1];
                rng.GetBytes(oneByte);

                byte[] blockMinusOne = new byte[blockSize - 1];
                rng.GetBytes(blockMinusOne);

                byte[] block = new byte[blockSize];
                rng.GetBytes(block);

                byte[] blockPlusOne = new byte[blockSize + 1];
                rng.GetBytes(blockPlusOne);

                foreach (var paddingMode in new PaddingMode[] { PaddingMode.ANSIX923, PaddingMode.ISO10126, PaddingMode.PKCS7 })
                {
                    Assert.IsTrue(RoundTripHelper(zeroByte, typeof(TripleDESCng), typeof(TripleDESCng), (tdes) => { tdes.Padding = paddingMode; }), paddingMode.ToString() + " - zeroByte");
                    Assert.IsTrue(RoundTripHelper(oneByte, typeof(TripleDESCng), typeof(TripleDESCng), (tdes) => { tdes.Padding = paddingMode; }), paddingMode.ToString() + " - oneByte");
                    Assert.IsTrue(RoundTripHelper(blockMinusOne, typeof(TripleDESCng), typeof(TripleDESCng), (tdes) => { tdes.Padding = paddingMode; }), paddingMode.ToString() + " - blockMinusOne");
                    Assert.IsTrue(RoundTripHelper(block, typeof(TripleDESCng), typeof(TripleDESCng), (tdes) => { tdes.Padding = paddingMode; }), paddingMode.ToString() + " - block");
                    Assert.IsTrue(RoundTripHelper(blockPlusOne, typeof(TripleDESCng), typeof(TripleDESCng), (tdes) => { tdes.Padding = paddingMode; }), paddingMode.ToString() + " - blockPlusOne");
                }
            }
        }
Пример #5
0
        public void TripleDESCngModeRoundTripTests()
        {
            int blockSize = 0;

            using (TripleDESCng tdes = new TripleDESCng())
            {
                blockSize = tdes.BlockSize / 8;
            }

            using (RNGCng rng = new RNGCng())
            {
                for (int i = 1; i <= 10; ++i)
                {
                    byte[] data = new byte[i * blockSize];
                    rng.GetBytes(data);

                    foreach (var cipherMode in new CipherMode[] { CipherMode.CBC, CipherMode.ECB, CipherMode.CFB })
                    {
                        Assert.IsTrue(RoundTripHelper(data, typeof(TripleDESCng), typeof(TripleDESCng), (tdes) => { tdes.Mode = cipherMode; }), i.ToString() + " blocks - " + cipherMode.ToString());
                    }

                    foreach (var chainingMode in new CngChainingMode[] { CngChainingMode.Cbc, CngChainingMode.Ecb, CngChainingMode.Cfb })
                    {
                        Assert.IsTrue(RoundTripHelper(data, typeof(TripleDESCng), typeof(TripleDESCng), (tdes) => { (tdes as ICngSymmetricAlgorithm).CngMode = chainingMode; }), i.ToString() + " blocks - " + chainingMode.ToString());
                    }
                }
            }
        }
Пример #6
0
 public void RNGCngNonZeroNegativeTest()
 {
     using (RNGCng rng = new RNGCng())
     {
         byte[] randomBytes = new byte[128];
         rng.GetNonZeroBytes(randomBytes);
     }
 }
Пример #7
0
 public void RNGCngZeroTest()
 {
     using (RNGCng rng = new RNGCng())
     {
         byte[] randomBytes = new byte[128];
         rng.GetBytes(randomBytes);
         Assert.IsTrue(true);
     }
 }
Пример #8
0
 public void RNGCngLargeTest()
 {
     using (RNGCng rng = new RNGCng())
     {
         byte[] randomBytes = new byte[10485760];        // 10 MB
         rng.GetBytes(randomBytes);
         Assert.IsTrue(AreRandomBytes(randomBytes));
     }
 }
Пример #9
0
 public void RNGCngPositiveTest()
 {
     using (RNGCng rng = new RNGCng())
     {
         byte[] randomBytes = new byte[128];
         rng.GetBytes(randomBytes);
         Assert.IsTrue(AreRandomBytes(randomBytes));
     }
 }
Пример #10
0
 /// <summary>
 ///     Utility method to help write AesCng round-trip tests
 /// </summary>
 private static bool RoundTripHelper(int bytes)
 {
     using (RNGCng rng = new RNGCng())
     {
         byte[] data = new byte[bytes];
         rng.GetBytes(data);
         return(RoundTripHelper(data));
     }
 }
Пример #11
0
        public void TripleDESCngInteropTests()
        {
            using (RNGCng rng = new RNGCng())
            {
                byte[] data = new byte[1001];
                rng.GetBytes(data);

                Assert.IsTrue(RoundTripHelper(data, typeof(TripleDESCng), typeof(TripleDESCryptoServiceProvider), (tdes) => { }), "CNG/CSP interop failed");
                Assert.IsTrue(RoundTripHelper(data, typeof(TripleDESCryptoServiceProvider), typeof(TripleDESCng), (tdes) => { }), "CSP/CNG interop failed");
            }
        }
Пример #12
0
        public void RSACngSignaturePssRoundTripTest()
        {
            using (RSACng rsa = new RSACng())
                using (RNGCng rng = new RNGCng())
                {
                    rsa.SignaturePaddingMode = AsymmetricPaddingMode.Pss;

                    byte[] data = new byte[2000];
                    rng.GetBytes(data);

                    byte[] signature = rsa.SignData(data);
                    Assert.IsTrue(rsa.VerifyData(data, signature));
                }
        }
Пример #13
0
        public void PBKDF2SmallInputTest()
        {
            // Test with a large password and large salt
            RNGCng rng = new RNGCng();

            byte[] password = new byte[1];
            byte[] salt     = new byte[1];
            rng.GetBytes(salt);
            rng.GetBytes(password);

            foreach (string hash in hashes)
            {
                byte[] derivedKey = BCryptPBKDF2.ComputeHash(hash, password, salt, ITERATION_COUNT);
                ValidateDerivedKey(derivedKey, hash);
            }
        }
Пример #14
0
        public void PBKDF2OneIterationTest()
        {
            // Test with one iteration
            RNGCng rng = new RNGCng();

            byte[] password = new byte[12];
            byte[] salt     = new byte[16];
            rng.GetBytes(salt);
            rng.GetBytes(password);

            foreach (string hash in hashes)
            {
                byte[] derivedKey = BCryptPBKDF2.ComputeHash(hash, password, salt, 1);
                ValidateDerivedKey(derivedKey, hash);
            }
        }
Пример #15
0
        public void RSACngXmlRoundTripTest()
        {
            using (RSACng rsa = new RSACng())
                using (RSACng rsaRT = new RSACng())
                    using (RNGCng rng = new RNGCng())
                    {
                        string keyXml = rsa.ToXmlString(false); // The default KSP does not support importing full RSA key blobs
                        rsaRT.FromXmlString(keyXml);

                        rsa.SignaturePaddingMode   = AsymmetricPaddingMode.Pkcs1;
                        rsaRT.SignaturePaddingMode = AsymmetricPaddingMode.Pkcs1;

                        byte[] data = new byte[2000];
                        rng.GetBytes(data);

                        byte[] signature = rsa.SignData(data);
                        Assert.IsTrue(rsaRT.VerifyData(data, signature));
                    }
        }
Пример #16
0
        public void HMACSHA384CngTest()
        {
            using (RNGCng rng = new RNGCng())
            {
                byte[] key = new byte[128];
                rng.GetBytes(key);

                using (HMACSHA384 bclHmac = new HMACSHA384(key))
                    using (HMACSHA384Cng cngHmac = new HMACSHA384Cng(key))
                    {
                        for (int i = 0; i < 10; ++i)
                        {
                            byte[] data = new byte[2048];
                            rng.GetBytes(data);

                            byte[] bcl = bclHmac.ComputeHash(data);
                            byte[] cng = cngHmac.ComputeHash(data);

                            Assert.IsTrue(Util.CompareBytes(bcl, cng));
                        }
                    }
            }
        }
Пример #17
0
        public void AuthenticatedAesCngChainingTest()
        {
            byte[] plaintext         = new byte[20 * 1024];
            byte[] iv                = new byte[12];
            byte[] authenticatedData = new byte[1024];

            using (RNGCng rng = new RNGCng())
            {
                rng.GetBytes(plaintext);
                rng.GetBytes(iv);
                rng.GetBytes(authenticatedData);
            }

            foreach (CngChainingMode chainingMode in new CngChainingMode[] { CngChainingMode.Ccm, CngChainingMode.Gcm })
            {
                using (AuthenticatedAesCng aes = new AuthenticatedAesCng())
                {
                    aes.AuthenticatedData = authenticatedData;
                    aes.CngMode           = chainingMode;
                    aes.IV = iv;

                    // Encrypt the whole block of data at once
                    byte[] wholeCiphertext = null;
                    byte[] wholeTag        = null;
                    using (IAuthenticatedCryptoTransform encryptor = aes.CreateAuthenticatedEncryptor())
                    {
                        wholeCiphertext = encryptor.TransformFinalBlock(plaintext, 0, plaintext.Length);
                        wholeTag        = encryptor.GetTag();
                    }

                    // Encrypt it in chunks
                    byte[] blockCiphertext = null;
                    byte[] blockTag        = null;
                    using (MemoryStream ms = new MemoryStream())
                        using (IAuthenticatedCryptoTransform encryptor = aes.CreateAuthenticatedEncryptor())
                            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                            {
                                int chunkSize = 128;
                                for (int offset = 0; offset < plaintext.Length; offset += chunkSize)
                                {
                                    cs.Write(plaintext, offset, chunkSize);
                                }
                                cs.FlushFinalBlock();

                                blockCiphertext = ms.ToArray();
                                blockTag        = encryptor.GetTag();
                            }

                    // Make sure we got the same results in both cases
                    Assert.IsTrue(Util.CompareBytes(wholeCiphertext, blockCiphertext));
                    Assert.IsTrue(Util.CompareBytes(wholeTag, blockTag));

                    aes.Tag = wholeTag;

                    // Decrypt the whole block of data at once
                    using (ICryptoTransform decryptor = aes.CreateDecryptor())
                    {
                        byte[] wholePlaintext = decryptor.TransformFinalBlock(wholeCiphertext, 0, wholeCiphertext.Length);
                        Assert.IsTrue(Util.CompareBytes(plaintext, wholePlaintext));
                    }

                    // Decrypt the data in chunks
                    using (MemoryStream ms = new MemoryStream())
                        using (ICryptoTransform decryptor = aes.CreateDecryptor())
                            using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
                            {
                                int chunkSize = 128;
                                for (int offset = 0; offset < blockCiphertext.Length; offset += chunkSize)
                                {
                                    cs.Write(blockCiphertext, offset, chunkSize);
                                }
                                cs.FlushFinalBlock();

                                byte[] blockPlaintext = ms.ToArray();
                                Assert.IsTrue(Util.CompareBytes(plaintext, blockPlaintext));
                            }
                }
            }
        }