diff --git a/src/Geralt.Tests/ChaCha20Poly1305Tests.cs b/src/Geralt.Tests/ChaCha20Poly1305Tests.cs index c16300b..bf36a7d 100644 --- a/src/Geralt.Tests/ChaCha20Poly1305Tests.cs +++ b/src/Geralt.Tests/ChaCha20Poly1305Tests.cs @@ -155,6 +155,14 @@ public void Decrypt_InvalidPlaintext() plaintext = new byte[Ciphertext.Length - ChaCha20Poly1305.TagSize - 1]; Assert.ThrowsException(() => ChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key)); } + + [TestMethod] + public void Decrypt_InvalidCiphertext() + { + var plaintext = new byte[Ciphertext.Length - ChaCha20Poly1305.TagSize]; + var ciphertext = new byte[ChaCha20Poly1305.TagSize - 1]; + Assert.ThrowsException(() => ChaCha20Poly1305.Decrypt(plaintext, ciphertext, Nonce, Key)); + } [TestMethod] public void Decrypt_InvalidNonce() diff --git a/src/Geralt.Tests/XChaCha20Poly1305Tests.cs b/src/Geralt.Tests/XChaCha20Poly1305Tests.cs index 7381f31..6b0d1da 100644 --- a/src/Geralt.Tests/XChaCha20Poly1305Tests.cs +++ b/src/Geralt.Tests/XChaCha20Poly1305Tests.cs @@ -73,15 +73,7 @@ public void Encrypt_InvalidCiphertext() ciphertext = new byte[Plaintext.Length + XChaCha20Poly1305.TagSize - 1]; Assert.ThrowsException(() => XChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key)); } - - [TestMethod] - public void Encrypt_InvalidPlaintext() - { - var ciphertext = new byte[Plaintext.Length + XChaCha20Poly1305.TagSize]; - var plaintext = Array.Empty(); - Assert.ThrowsException(() => XChaCha20Poly1305.Encrypt(ciphertext, plaintext, Nonce, Key)); - } - + [TestMethod] public void Encrypt_InvalidNonce() { @@ -164,6 +156,14 @@ public void Decrypt_InvalidPlaintext() Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key)); } + [TestMethod] + public void Decrypt_InvalidCiphertext() + { + var plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize]; + var ciphertext = new byte[XChaCha20Poly1305.TagSize - 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, ciphertext, Nonce, Key)); + } + [TestMethod] public void Decrypt_InvalidNonce() { diff --git a/src/Geralt/Crypto/ChaCha20Poly1305.cs b/src/Geralt/Crypto/ChaCha20Poly1305.cs index 1f46f28..3c223ff 100644 --- a/src/Geralt/Crypto/ChaCha20Poly1305.cs +++ b/src/Geralt/Crypto/ChaCha20Poly1305.cs @@ -24,8 +24,8 @@ public static unsafe void Encrypt(Span ciphertext, ReadOnlySpan plai public static unsafe void Decrypt(Span plaintext, ReadOnlySpan ciphertext, ReadOnlySpan nonce, ReadOnlySpan key, ReadOnlySpan associatedData = default) { - Validation.EqualToSize(nameof(plaintext), plaintext.Length, ciphertext.Length - TagSize); Validation.NotLessThanMin(nameof(ciphertext), ciphertext.Length, TagSize); + Validation.EqualToSize(nameof(plaintext), plaintext.Length, ciphertext.Length - TagSize); Validation.EqualToSize(nameof(nonce), nonce.Length, NonceSize); Validation.EqualToSize(nameof(key), key.Length, KeySize); Sodium.Initialise(); diff --git a/src/Geralt/Crypto/XChaCha20Poly1305.cs b/src/Geralt/Crypto/XChaCha20Poly1305.cs index 8f819ac..e02a87e 100644 --- a/src/Geralt/Crypto/XChaCha20Poly1305.cs +++ b/src/Geralt/Crypto/XChaCha20Poly1305.cs @@ -24,8 +24,8 @@ public static unsafe void Encrypt(Span ciphertext, ReadOnlySpan plai public static unsafe void Decrypt(Span plaintext, ReadOnlySpan ciphertext, ReadOnlySpan nonce, ReadOnlySpan key, ReadOnlySpan associatedData = default) { - Validation.EqualToSize(nameof(plaintext), plaintext.Length, ciphertext.Length - TagSize); Validation.NotLessThanMin(nameof(ciphertext), ciphertext.Length, TagSize); + Validation.EqualToSize(nameof(plaintext), plaintext.Length, ciphertext.Length - TagSize); Validation.EqualToSize(nameof(nonce), nonce.Length, NonceSize); Validation.EqualToSize(nameof(key), key.Length, KeySize); Sodium.Initialise();