Example #1
0
        public void InvalidNonceSize(int nonceSize)
        {
            int dataLength = 30;

            byte[] plaintext  = Enumerable.Range(1, dataLength).Select((x) => (byte)x).ToArray();
            byte[] ciphertext = new byte[dataLength];
            byte[] key        = AesGcmTestHelpers.GetRandomBuffer(16);
            byte[] nonce      = AesGcmTestHelpers.GetRandomBuffer(nonceSize);
            byte[] tag        = new byte[AesGcmWindows.TagByteSizes.MinSize];

            using (var aesGcm = new AesGcmWindows(key))
            {
                Assert.Throws <ArgumentException>(() => aesGcm.Encrypt(nonce, plaintext, ciphertext, tag), "nonce");
            }
        }
Example #2
0
        public static void InplaceEncryptDecrypt()
        {
            byte[] key               = "d5a194ed90cfe08abecd4691997ceb2c".HexToByteArray();
            byte[] nonce             = AesGcmTestHelpers.GetRandomBuffer(12);
            byte[] originalPlaintext = new byte[] { 1, 2, 8, 12, 16, 99, 0 };
            byte[] data              = (byte[])originalPlaintext.Clone();
            byte[] tag               = AesGcmTestHelpers.GetRandomBuffer(16);

            using (var aesGcm = new AesGcmWindows(key))
            {
                aesGcm.Encrypt(nonce, data, data, tag);
                Assert.AreNotEqual(originalPlaintext, data);

                aesGcm.Decrypt(nonce, data, tag, data);
                Assert.AreEqual(originalPlaintext, data);
            }
        }
Example #3
0
        public static void ValidTagSize(int tagSize)
        {
            const int dataLength = 35;

            byte[] plaintext  = Enumerable.Range(1, dataLength).Select((x) => (byte)x).ToArray();
            byte[] ciphertext = new byte[dataLength];
            byte[] key        = AesGcmTestHelpers.GetRandomBuffer(16);
            byte[] nonce      = AesGcmTestHelpers.GetRandomBuffer(12);
            byte[] tag        = new byte[tagSize];

            using (var aesGcm = new AesGcmWindows(key))
            {
                aesGcm.Encrypt(nonce, plaintext, ciphertext, tag);

                byte[] decrypted = new byte[dataLength];
                aesGcm.Decrypt(nonce, ciphertext, tag, decrypted);
                Assert.AreEqual(plaintext, decrypted);
            }
        }
Example #4
0
        public static void InplaceEncryptTamperTagDecrypt()
        {
            byte[] key               = "d5a194ed90cfe08abecd4691997ceb2c".HexToByteArray();
            byte[] nonce             = AesGcmTestHelpers.GetRandomBuffer(12);
            byte[] originalPlaintext = new byte[] { 1, 2, 8, 12, 16, 99, 0 };
            byte[] data              = (byte[])originalPlaintext.Clone();
            byte[] tag               = new byte[16];

            using (var aesGcm = new AesGcmWindows(key))
            {
                aesGcm.Encrypt(nonce, data, data, tag);
                Assert.AreNotEqual(originalPlaintext, data);

                tag[0] ^= 1;

                Assert.Throws <CryptographicException>(
                    () => aesGcm.Decrypt(nonce, data, tag, data));
                Assert.AreEqual(new byte[data.Length], data);
            }
        }
Example #5
0
        public static void AesGcmNistTestsTamperTagVectorSelected()
        {
            foreach (AeadTest test in s_nistGcmTestVectorsSelectedCases)
            {
                using (var aesGcm = new AesGcmWindows(test.Key))
                {
                    byte[] ciphertext = new byte[test.Plaintext.Length];
                    byte[] tag        = new byte[test.Tag.Length];
                    aesGcm.Encrypt(test.Nonce, test.Plaintext, ciphertext, tag, test.AssociatedData);
                    Assert.AreEqual(test.Ciphertext, ciphertext);
                    Assert.AreEqual(test.Tag, tag);

                    tag[0] ^= 1;

                    byte[] plaintext = AesGcmTestHelpers.GetRandomBuffer(test.Plaintext.Length);
                    Assert.Throws <CryptographicException>(
                        () => aesGcm.Decrypt(test.Nonce, ciphertext, tag, plaintext, test.AssociatedData));
                    Assert.AreEqual(new byte[plaintext.Length], plaintext);
                }
            }
        }
Example #6
0
        public void EncryptTamperAADDecrypt(int dataLength, int additionalDataLength)
        {
            byte[] additionalData = AesGcmTestHelpers.GetRandomBuffer(additionalDataLength);

            byte[] plaintext  = Enumerable.Range(1, dataLength).Select((x) => (byte)x).ToArray();
            byte[] ciphertext = new byte[dataLength];
            byte[] key        = AesGcmTestHelpers.GetRandomBuffer(16);
            byte[] nonce      = AesGcmTestHelpers.GetRandomBuffer(AesGcmWindows.NonceByteSizes.MinSize);
            byte[] tag        = new byte[AesGcmWindows.TagByteSizes.MinSize];

            using (var aesGcm = new AesGcmWindows(key))
            {
                aesGcm.Encrypt(nonce, plaintext, ciphertext, tag, additionalData);

                additionalData[0] ^= 1;

                byte[] decrypted = new byte[dataLength];
                Assert.Throws <CryptographicException>(
                    () => aesGcm.Decrypt(nonce, ciphertext, tag, decrypted, additionalData));
            }
        }