public static void EncryptDecryptNullTag() { byte[] key = "d5a194ed90cfe08abecd4691997ceb2c".HexToByteArray(); byte[] nonce = new byte[12]; byte[] plaintext = Array.Empty <byte>(); byte[] ciphertext = Array.Empty <byte>(); using (var aesGcm = new AesGcmWindows(key)) { Assert.Throws <ArgumentNullException>(() => aesGcm.Encrypt(nonce, plaintext, ciphertext, (byte[])null)); Assert.Throws <ArgumentNullException>(() => aesGcm.Decrypt(nonce, ciphertext, (byte[])null, plaintext)); } }
public static void PlaintextAndCiphertextSizeDiffer(int ptLen, int ctLen) { byte[] key = new byte[16]; byte[] nonce = new byte[12]; byte[] plaintext = new byte[ptLen]; byte[] ciphertext = new byte[ctLen]; byte[] tag = new byte[16]; using (var aesGcm = new AesGcmWindows(key)) { Assert.Throws <ArgumentException>(() => aesGcm.Encrypt(nonce, plaintext, ciphertext, tag)); Assert.Throws <ArgumentException>(() => aesGcm.Decrypt(nonce, ciphertext, tag, plaintext)); } }
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"); } }
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); } }
public static void AesGcmNistTestsVectorSelected() { 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); byte[] plaintext = new byte[test.Plaintext.Length]; aesGcm.Decrypt(test.Nonce, ciphertext, tag, plaintext, test.AssociatedData); Assert.AreEqual(test.Plaintext, plaintext); } } }
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); } }
public static void TwoEncryptionsAndDecryptionsUsingOneInstance() { byte[] key = "d5a194ed90cfe08abecd4691997ceb2c".HexToByteArray(); byte[] originalData1 = Enumerable.Range(1, 15).Select((x) => (byte)x).ToArray(); byte[] originalData2 = Enumerable.Range(14, 97).Select((x) => (byte)x).ToArray(); byte[] associatedData2 = Enumerable.Range(100, 109).Select((x) => (byte)x).ToArray(); byte[] nonce1 = "b41329dd64af2c3036661b46".HexToByteArray(); byte[] nonce2 = "8ba10892e8b87d031196bf99".HexToByteArray(); byte[] expectedCiphertext1 = "f1af1fb2d4485cc536d618475d52ff".HexToByteArray(); byte[] expectedTag1 = "5ab65624c46b8160f34e81f5".HexToByteArray(); byte[] expectedCiphertext2 = ( "217bed01446d731a372a2b30ac7fcd73aed7c946d9171ae9c00b1c589ca73ba2" + "1c1bac79235d9ac0d0c899184dd8596b866fd96a6c1a28083557b43a5cbb5315" + "00e8cfbad8247c6d1deb51a7c5dfe45801a8d8d519b3fa982f546aa2d02db978" + "da").HexToByteArray(); byte[] expectedTag2 = "9c75d006640ff4fb68c60c9548a45cf8".HexToByteArray(); using (var aesGcm = new AesGcmWindows(key)) { byte[] ciphertext1 = new byte[originalData1.Length]; byte[] tag1 = new byte[expectedTag1.Length]; aesGcm.Encrypt(nonce1, originalData1, ciphertext1, tag1); Assert.AreEqual(expectedCiphertext1, ciphertext1); Assert.AreEqual(expectedTag1, tag1); byte[] ciphertext2 = new byte[originalData2.Length]; byte[] tag2 = new byte[expectedTag2.Length]; aesGcm.Encrypt(nonce2, originalData2, ciphertext2, tag2, associatedData2); Assert.AreEqual(expectedCiphertext2, ciphertext2); Assert.AreEqual(expectedTag2, tag2); byte[] plaintext1 = new byte[originalData1.Length]; aesGcm.Decrypt(nonce1, ciphertext1, tag1, plaintext1); Assert.AreEqual(originalData1, plaintext1); byte[] plaintext2 = new byte[originalData2.Length]; aesGcm.Decrypt(nonce2, ciphertext2, tag2, plaintext2, associatedData2); Assert.AreEqual(originalData2, plaintext2); } }
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); } }
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); } } }
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)); } }