Пример #1
0
        public void AuthenticatedAesCngCcmRoundTripTest()
        {
            var testData = new RoundTripTestData
            {
                ChainingMode       = CngChainingMode.Ccm,
                Plaintext          = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                Key                = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                IV                 = new byte[] { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
                ExpectedCiphertext = new byte[] { 0x83, 0x57, 0x54, 0x84, 0x0e, 0x3d, 0x4a, 0x81, 0x42, 0x33, 0x6a, 0xd3, 0x99, 0x9a, 0x3e, 0x03 },
                ExpectedTag        = new byte[] { 0xfa, 0xf7, 0xab, 0x3c, 0x9b, 0xbf, 0x1e, 0x10, 0xc8, 0xa3, 0xc9, 0xd8, 0x66, 0x39, 0xa6, 0x77 }
            };

            AuthenticatedAesCngRoundTripTest(testData);
        }
Пример #2
0
        public void AuthenticatedAesCngGcmTwoBlocksRoundTripTest()
        {
            var testData = new RoundTripTestData
            {
                ChainingMode       = CngChainingMode.Gcm,
                Plaintext          = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                Key                = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                IV                 = new byte[] { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
                ExpectedCiphertext = new byte[] { 0x54, 0x2d, 0x26, 0x15, 0x9c, 0xb3, 0x6e, 0x21, 0xd2, 0x58, 0xcf, 0x9c, 0x6e, 0xce, 0xfb, 0x5f, 0x8c, 0x2a, 0xb8, 0x22, 0x4d, 0x6d, 0xd0, 0x02, 0x76, 0xd2, 0xab, 0x22, 0xa2, 0xd6, 0xee, 0x5b },
                ExpectedTag        = new byte[] { 0xc1, 0x34, 0x38, 0x0b, 0xc3, 0x87, 0x7c, 0xf5, 0x2f, 0x3b, 0xa9, 0xfe, 0x3c, 0x69, 0x4b, 0x9f }
            };

            AuthenticatedAesCngRoundTripTest(testData);
        }
Пример #3
0
        public void AuthenticatedAesCngGcmRoundTripTest()
        {
            var testData = new RoundTripTestData
            {
                ChainingMode       = CngChainingMode.Gcm,
                Plaintext          = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                Key                = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                IV                 = new byte[] { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
                ExpectedCiphertext = new byte[] { 0x54, 0x2d, 0x26, 0x15, 0x9c, 0xb3, 0x6e, 0x21, 0xd2, 0x58, 0xcf, 0x9c, 0x6e, 0xce, 0xfb, 0x5f },
                ExpectedTag        = new byte[] { 0xd8, 0x80, 0xc3, 0x7a, 0x5a, 0x93, 0xc4, 0x7c, 0xd2, 0x44, 0x2d, 0x7d, 0x6b, 0xfa, 0x5c, 0x02 }
            };

            AuthenticatedAesCngRoundTripTest(testData);
        }
Пример #4
0
        public void AuthenticatedAesCngCcmAuthDataRoundTripTest()
        {
            var testData = new RoundTripTestData
            {
                ChainingMode       = CngChainingMode.Ccm,
                Plaintext          = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                Key                = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                IV                 = new byte[] { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
                AuthenticationData = new byte[] { 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 },
                ExpectedCiphertext = new byte[] { 0x83, 0x57, 0x54, 0x84, 0x0e, 0x3d, 0x4a, 0x81, 0x42, 0x33, 0x6a, 0xd3, 0x99, 0x9a, 0x3e, 0x03 },
                ExpectedTag        = new byte[] { 0x1b, 0x74, 0x18, 0xfd, 0xca, 0x76, 0x3c, 0x61, 0x03, 0x5c, 0x46, 0xa8, 0xe1, 0x77, 0xac, 0x96 }
            };

            AuthenticatedAesCngRoundTripTest(testData);
        }
Пример #5
0
        public void AuthenticatedAesCngGcmAuthDataRoundTripTest()
        {
            var testData = new RoundTripTestData
            {
                ChainingMode       = CngChainingMode.Gcm,
                Plaintext          = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                Key                = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 },
                IV                 = new byte[] { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
                AuthenticationData = new byte[] { 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 },
                ExpectedCiphertext = new byte[] { 0x54, 0x2d, 0x26, 0x15, 0x9c, 0xb3, 0x6e, 0x21, 0xd2, 0x58, 0xcf, 0x9c, 0x6e, 0xce, 0xfb, 0x5f },
                ExpectedTag        = new byte[] { 0xb4, 0xb9, 0x6b, 0xea, 0x33, 0x41, 0xeb, 0x4f, 0x19, 0xc8, 0x25, 0x92, 0xfa, 0x1b, 0x2b, 0xf1 }
            };

            AuthenticatedAesCngRoundTripTest(testData);
        }
Пример #6
0
        public void AuthenticatedAesCngDefaultIVTest()
        {
            var ccmTestData = new RoundTripTestData
            {
                ChainingMode = CngChainingMode.Ccm,
                Plaintext    = Encoding.UTF8.GetBytes("Secret")
            };

            AuthenticatedAesCngRoundTripTest(ccmTestData);

            var gcmTestData = new RoundTripTestData
            {
                ChainingMode = CngChainingMode.Gcm,
                Plaintext    = Encoding.UTF8.GetBytes("Secret")
            };

            AuthenticatedAesCngRoundTripTest(gcmTestData);
        }
Пример #7
0
        /// <summary>
        ///     Perform a round trip test given input and the expected output
        /// </summary>
        private void AuthenticatedAesCngRoundTripTest(RoundTripTestData testData)
        {
            using (AuthenticatedAesCng aes = new AuthenticatedAesCng())
            {
                aes.CngMode = testData.ChainingMode;

                if (testData.Key != null)
                {
                    aes.Key = testData.Key;
                }

                if (testData.IV != null)
                {
                    aes.IV = testData.IV;
                }

                if (testData.AuthenticationData != null)
                {
                    aes.AuthenticatedData = testData.AuthenticationData;
                }

                if (testData.ExpectedTag != null)
                {
                    aes.Tag = testData.ExpectedTag;
                }

                // Encrypt
                byte[] ciphertext = null;
                byte[] tag        = null;
                using (MemoryStream ms = new MemoryStream())
                    using (IAuthenticatedCryptoTransform encryptor = aes.CreateAuthenticatedEncryptor())
                        using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                        {
                            cs.Write(testData.Plaintext, 0, testData.Plaintext.Length);
                            cs.FlushFinalBlock();

                            tag        = encryptor.GetTag();
                            ciphertext = ms.ToArray();

                            // Check if the ciphertext and tag are what are expected.
                            if (testData.ExpectedCiphertext != null)
                            {
                                Assert.IsTrue(Util.CompareBytes(testData.ExpectedCiphertext, ciphertext));
                            }

                            if (testData.ExpectedTag != null)
                            {
                                Assert.IsTrue(Util.CompareBytes(testData.ExpectedTag, tag));
                            }
                        }

                if (testData.ExpectedTag == null)
                {
                    aes.Tag = tag;
                }

                // Decrypt
                using (MemoryStream ms = new MemoryStream())
                    using (ICryptoTransform decryptor = aes.CreateDecryptor())
                        using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
                        {
                            cs.Write(ciphertext, 0, ciphertext.Length);
                            cs.FlushFinalBlock();

                            // Compare the decrypted text to the initial ciphertext.
                            byte[] decrypted = ms.ToArray();
                            Assert.IsTrue(Util.CompareBytes(testData.Plaintext, decrypted));
                        }
            }
        }