diff --git a/src/Geralt.Tests/Argon2idTests.cs b/src/Geralt.Tests/Argon2idTests.cs index 166f176..dfdb6b5 100644 --- a/src/Geralt.Tests/Argon2idTests.cs +++ b/src/Geralt.Tests/Argon2idTests.cs @@ -27,12 +27,12 @@ public void DeriveKey_ValidInputs() } [TestMethod] - public void DeriveKey_DifferentPassword() + public void DeriveKey_EmptyPassword() { Span key = stackalloc byte[Argon2id.KeySize]; - Span password = Password.ToArray(); - password[0]++; + Span password = Span.Empty; Argon2id.DeriveKey(key, password, Salt, Iterations, MemorySize); + Assert.IsFalse(key.SequenceEqual(new byte[key.Length])); Assert.IsFalse(key.SequenceEqual(Argon2idKey)); } @@ -68,15 +68,7 @@ public void DeriveKey_InvalidKey() var key = new byte[Argon2id.MinKeySize - 1]; Assert.ThrowsException(() => Argon2id.DeriveKey(key, Password, Salt, Iterations, MemorySize)); } - - [TestMethod] - public void DeriveKey_InvalidPassword() - { - var key = new byte[Argon2id.KeySize]; - var password = Array.Empty(); - Assert.ThrowsException(() => Argon2id.DeriveKey(key, password, Salt, Iterations, MemorySize)); - } - + [TestMethod] public void DeriveKey_InvalidSalt() { @@ -111,6 +103,16 @@ public void ComputeHash_ValidInputs() bool rehash = Argon2id.NeedsRehash(hash, Iterations, MemorySize); Assert.IsFalse(rehash); } + + [TestMethod] + public void ComputeHash_EmptyPassword() + { + Span hash = stackalloc byte[Argon2id.MaxHashSize]; + Span password = Span.Empty; + Argon2id.ComputeHash(hash, password, Iterations, MemorySize); + bool valid = Argon2id.VerifyHash(hash, password); + Assert.IsTrue(valid); + } [TestMethod] public void ComputeHash_InvalidHash() @@ -121,14 +123,6 @@ public void ComputeHash_InvalidHash() Assert.ThrowsException(() => Argon2id.ComputeHash(hash, Password, Iterations, MemorySize)); } - [TestMethod] - public void ComputeHash_InvalidPassword() - { - var hash = new byte[Argon2id.MaxHashSize]; - var password = Array.Empty(); - Assert.ThrowsException(() => Argon2id.ComputeHash(hash, password, Iterations, MemorySize)); - } - [TestMethod] public void ComputeHash_InvalidIterations() { @@ -177,15 +171,7 @@ public void VerifyHash_InvalidHash() hash = new byte[Argon2id.MaxHashSize + 1]; Assert.ThrowsException(() => Argon2id.VerifyHash(hash, Password)); } - - [TestMethod] - public void VerifyHash_InvalidPassword() - { - var hash = Encoding.UTF8.GetBytes(Argon2idHash); - var password = Array.Empty(); - Assert.ThrowsException(() => Argon2id.VerifyHash(hash, password)); - } - + [TestMethod] public void NeedsRehash_CorrectParameters() { diff --git a/src/Geralt.Tests/BLAKE2bTests.cs b/src/Geralt.Tests/BLAKE2bTests.cs index af29b28..9936990 100644 --- a/src/Geralt.Tests/BLAKE2bTests.cs +++ b/src/Geralt.Tests/BLAKE2bTests.cs @@ -31,13 +31,15 @@ public void ComputeHash_ValidInputs() } [TestMethod] - public void ComputeHash_DifferentMessage() + public void ComputeHash_EmptyMessage() { Span hash = stackalloc byte[BLAKE2b.MaxHashSize]; - BLAKE2b.ComputeHash(hash, Hash); + Span message = Span.Empty; + BLAKE2b.ComputeHash(hash, message); + Assert.IsFalse(hash.SequenceEqual(new byte[hash.Length])); Assert.IsFalse(hash.SequenceEqual(Hash)); } - + [TestMethod] public void ComputeHash_InvalidHash() { @@ -47,14 +49,6 @@ public void ComputeHash_InvalidHash() Assert.ThrowsException(() => BLAKE2b.ComputeHash(hash, Message)); } - [TestMethod] - public void ComputeHash_InvalidMessage() - { - var hash = new byte[BLAKE2b.MaxHashSize]; - var message = Array.Empty(); - Assert.ThrowsException(() => BLAKE2b.ComputeHash(hash, message)); - } - [TestMethod] public void ComputeHashStream_ValidInputs() { @@ -66,12 +60,13 @@ public void ComputeHashStream_ValidInputs() } [TestMethod] - public void ComputeHashStream_DifferentMessage() + public void ComputeHashStream_EmptyMessage() { var hash = new byte[BLAKE2b.MaxHashSize]; using var blake2b = new BLAKE2bHashAlgorithm(hash.Length); - using var memoryStream = new MemoryStream(Hash, writable: false); + using var memoryStream = new MemoryStream(Array.Empty(), writable: false); hash = blake2b.ComputeHash(memoryStream); + Assert.IsFalse(hash.SequenceEqual(new byte[hash.Length])); Assert.IsFalse(hash.SequenceEqual(Hash)); } @@ -85,96 +80,93 @@ public void ComputeHashStream_InvalidHash() } [TestMethod] - public void ComputeTagStream_ValidInputs() + public void ComputeTag_ValidInputs() { - var tag = new byte[BLAKE2b.TagSize]; - using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key); - using var memoryStream = new MemoryStream(Message, writable: false); - tag = blake2b.ComputeHash(memoryStream); + Span tag = stackalloc byte[BLAKE2b.TagSize]; + BLAKE2b.ComputeTag(tag, Message, Key); Assert.IsTrue(tag.SequenceEqual(Tag)); } [TestMethod] - public void ComputeTagStream_DifferentKey() + public void ComputeTag_EmptyMessage() { - var tag = new byte[BLAKE2b.TagSize]; - using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, DifferentKey); - using var memoryStream = new MemoryStream(Message, writable: false); - tag = blake2b.ComputeHash(memoryStream); + Span tag = stackalloc byte[BLAKE2b.TagSize]; + Span message = Span.Empty; + BLAKE2b.ComputeTag(tag, message, Key); + Assert.IsFalse(tag.SequenceEqual(new byte[tag.Length])); Assert.IsFalse(tag.SequenceEqual(Tag)); + bool valid = BLAKE2b.VerifyTag(tag, message, Key); + Assert.IsTrue(valid); } [TestMethod] - public void ComputeTagStream_DifferentMessage() + public void ComputeTag_DifferentKey() { - var tag = new byte[BLAKE2b.TagSize]; - using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key); - using var memoryStream = new MemoryStream(Hash, writable: false); - tag = blake2b.ComputeHash(memoryStream); + Span tag = stackalloc byte[BLAKE2b.TagSize]; + BLAKE2b.ComputeTag(tag, Message, DifferentKey); Assert.IsFalse(tag.SequenceEqual(Tag)); } [TestMethod] - public void ComputeTagStream_InvalidKey() - { - var tag = new byte[BLAKE2b.TagSize]; - var key = new byte[BLAKE2b.MinKeySize - 1]; - Assert.ThrowsException(() => new BLAKE2bHashAlgorithm(tag.Length, key)); - key = new byte[BLAKE2b.MaxKeySize + 1]; - Assert.ThrowsException(() => new BLAKE2bHashAlgorithm(tag.Length, key)); - } - - [TestMethod] - public void ComputeTag_ValidInputs() + public void ComputeTag_InvalidTag() { - Span tag = stackalloc byte[BLAKE2b.TagSize]; - BLAKE2b.ComputeTag(tag, Message, Key); - Assert.IsTrue(tag.SequenceEqual(Tag)); + var tag = new byte[BLAKE2b.MinTagSize - 1]; + Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, Key)); + tag = new byte[BLAKE2b.MaxTagSize + 1]; + Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, Key)); } [TestMethod] - public void ComputeTag_DifferentMessage() + public void ComputeTag_InvalidKey() { - Span tag = stackalloc byte[BLAKE2b.TagSize]; - BLAKE2b.ComputeTag(tag, Hash, Key); - Assert.IsFalse(tag.SequenceEqual(Tag)); + var tag = new byte[BLAKE2b.TagSize]; + var key = new byte[BLAKE2b.MinKeySize - 1]; + Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, key)); + key = new byte[BLAKE2b.MaxKeySize + 1]; + Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, key)); } [TestMethod] - public void ComputeTag_DifferentKey() + public void ComputeTagStream_ValidInputs() { - Span tag = stackalloc byte[BLAKE2b.TagSize]; - BLAKE2b.ComputeTag(tag, Hash, DifferentKey); - Assert.IsFalse(tag.SequenceEqual(Tag)); + var tag = new byte[BLAKE2b.TagSize]; + using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key); + using var memoryStream = new MemoryStream(Message, writable: false); + tag = blake2b.ComputeHash(memoryStream); + Assert.IsTrue(tag.SequenceEqual(Tag)); } [TestMethod] - public void ComputeTag_InvalidTag() + public void ComputeTagStream_DifferentKey() { - var tag = new byte[BLAKE2b.MinTagSize - 1]; - Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, Key)); - tag = new byte[BLAKE2b.MaxTagSize + 1]; - Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, Key)); + var tag = new byte[BLAKE2b.TagSize]; + using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, DifferentKey); + using var memoryStream = new MemoryStream(Message, writable: false); + tag = blake2b.ComputeHash(memoryStream); + Assert.IsFalse(tag.SequenceEqual(Tag)); } [TestMethod] - public void ComputeTag_InvalidMessage() + public void ComputeTagStream_EmptyMessage() { var tag = new byte[BLAKE2b.TagSize]; - var message = Array.Empty(); - Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, message, Key)); + using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key); + using var memoryStream = new MemoryStream(Array.Empty(), writable: false); + tag = blake2b.ComputeHash(memoryStream); + Assert.IsFalse(tag.SequenceEqual(new byte[tag.Length])); + Assert.IsFalse(tag.SequenceEqual(Tag)); } [TestMethod] - public void ComputeTag_InvalidKey() + public void ComputeTagStream_InvalidKey() { var tag = new byte[BLAKE2b.TagSize]; var key = new byte[BLAKE2b.MinKeySize - 1]; - Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, key)); + Assert.ThrowsException(() => new BLAKE2bHashAlgorithm(tag.Length, key)); key = new byte[BLAKE2b.MaxKeySize + 1]; - Assert.ThrowsException(() => BLAKE2b.ComputeTag(tag, Message, key)); + Assert.ThrowsException(() => new BLAKE2bHashAlgorithm(tag.Length, key)); } - + [TestMethod] public void VerifyTag_ValidInputs() { @@ -211,15 +203,7 @@ public void VerifyTag_InvalidTag() tag = new byte[BLAKE2b.MaxTagSize + 1]; Assert.ThrowsException(() => BLAKE2b.VerifyTag(tag, Message, Key)); } - - [TestMethod] - public void VerifyTag_InvalidMessage() - { - var tag = new byte[BLAKE2b.TagSize]; - var message = Array.Empty(); - Assert.ThrowsException(() => BLAKE2b.VerifyTag(tag, message, Key)); - } - + [TestMethod] public void VerifyTag_InvalidKey() { diff --git a/src/Geralt.Tests/ChaCha20Poly1305Tests.cs b/src/Geralt.Tests/ChaCha20Poly1305Tests.cs index 8aaf059..c16300b 100644 --- a/src/Geralt.Tests/ChaCha20Poly1305Tests.cs +++ b/src/Geralt.Tests/ChaCha20Poly1305Tests.cs @@ -24,13 +24,15 @@ public void Encrypt_ValidInputs() } [TestMethod] - public void Encrypt_DifferentPlaintext() + public void Encrypt_EmptyPlaintext() { - Span ciphertext = stackalloc byte[Plaintext.Length + ChaCha20Poly1305.TagSize]; - Span plaintext = Plaintext.ToArray(); - plaintext[0]++; + Span plaintext = Span.Empty; + Span ciphertext = stackalloc byte[plaintext.Length + ChaCha20Poly1305.TagSize]; ChaCha20Poly1305.Encrypt(ciphertext, plaintext, Nonce, Key); - Assert.IsFalse(ciphertext.SequenceEqual(Ciphertext)); + Assert.IsFalse(ciphertext.SequenceEqual(new byte[ciphertext.Length])); + Span decrypted = stackalloc byte[plaintext.Length]; + ChaCha20Poly1305.Decrypt(decrypted, ciphertext, Nonce, Key); + Assert.IsTrue(plaintext.SequenceEqual(decrypted)); } [TestMethod] @@ -66,18 +68,12 @@ public void Encrypt_DifferentAssociatedData() [TestMethod] public void Encrypt_InvalidCiphertext() { - var ciphertext = Array.Empty(); + var ciphertext = new byte[Plaintext.Length + ChaCha20Poly1305.TagSize + 1]; + Assert.ThrowsException(() => ChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key)); + ciphertext = new byte[Plaintext.Length + ChaCha20Poly1305.TagSize - 1]; Assert.ThrowsException(() => ChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key)); } - - [TestMethod] - public void Encrypt_InvalidPlaintext() - { - var ciphertext = new byte[Plaintext.Length + ChaCha20Poly1305.TagSize]; - var plaintext = Array.Empty(); - Assert.ThrowsException(() => ChaCha20Poly1305.Encrypt(ciphertext, plaintext, Nonce, Key)); - } - + [TestMethod] public void Encrypt_InvalidNonce() { @@ -105,7 +101,7 @@ public void Decrypt_ValidInputs() ChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key, AssociatedData); Assert.IsTrue(plaintext.SequenceEqual(Plaintext)); } - + [TestMethod] public void Decrypt_WrongCiphertext() { @@ -154,18 +150,12 @@ public void Decrypt_WrongAssociatedData() [TestMethod] public void Decrypt_InvalidPlaintext() { - var plaintext = Array.Empty(); + var plaintext = new byte[Ciphertext.Length - ChaCha20Poly1305.TagSize + 1]; + Assert.ThrowsException(() => ChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key)); + 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 = Array.Empty(); - Assert.ThrowsException(() => ChaCha20Poly1305.Decrypt(plaintext, ciphertext, Nonce, Key)); - } - + [TestMethod] public void Decrypt_InvalidNonce() { diff --git a/src/Geralt.Tests/ChaCha20Tests.cs b/src/Geralt.Tests/ChaCha20Tests.cs index 494c81c..bbcbd19 100644 --- a/src/Geralt.Tests/ChaCha20Tests.cs +++ b/src/Geralt.Tests/ChaCha20Tests.cs @@ -21,6 +21,18 @@ public void Encrypt_ValidInputs() Assert.IsTrue(ciphertext.SequenceEqual(Ciphertext)); } + [TestMethod] + public void Encrypt_EmptyPlaintext() + { + Span plaintext = Span.Empty; + Span ciphertext = stackalloc byte[plaintext.Length]; + ChaCha20.Encrypt(ciphertext, plaintext, Nonce, Key); + Assert.IsTrue(plaintext.SequenceEqual(ciphertext)); + Span decrypted = stackalloc byte[plaintext.Length]; + ChaCha20.Decrypt(decrypted, ciphertext, Nonce, Key); + Assert.IsTrue(plaintext.SequenceEqual(decrypted)); + } + [TestMethod] public void Encrypt_DifferentPlaintext() { @@ -54,7 +66,9 @@ public void Encrypt_DifferentKey() [TestMethod] public void Encrypt_InvalidCiphertext() { - var ciphertext = Array.Empty(); + var ciphertext = new byte[Plaintext.Length - 1]; + Assert.ThrowsException(() => ChaCha20.Encrypt(ciphertext, Plaintext, Nonce, Key)); + ciphertext = new byte[Plaintext.Length + 1]; Assert.ThrowsException(() => ChaCha20.Encrypt(ciphertext, Plaintext, Nonce, Key)); } @@ -136,7 +150,9 @@ public void Decrypt_InvalidPlaintext() public void Decrypt_InvalidCiphertext() { var plaintext = new byte[Ciphertext.Length]; - var ciphertext = Array.Empty(); + var ciphertext = new byte[plaintext.Length - 1]; + Assert.ThrowsException(() => ChaCha20.Decrypt(plaintext, ciphertext, Nonce, Key)); + ciphertext = new byte[plaintext.Length + 1]; Assert.ThrowsException(() => ChaCha20.Decrypt(plaintext, ciphertext, Nonce, Key)); } diff --git a/src/Geralt.Tests/Ed25519Tests.cs b/src/Geralt.Tests/Ed25519Tests.cs index 1161d00..f8e8d56 100644 --- a/src/Geralt.Tests/Ed25519Tests.cs +++ b/src/Geralt.Tests/Ed25519Tests.cs @@ -188,13 +188,14 @@ public void Sign_ValidInputs() } [TestMethod] - public void Sign_DifferentMessage() + public void Sign_EmptyMessage() { Span signature = stackalloc byte[Ed25519.SignatureSize]; - Span message = Message.ToArray(); - message[0]++; + Span message = Span.Empty; Ed25519.Sign(signature, message, AlicePrivateKey); Assert.IsFalse(signature.SequenceEqual(Signature)); + bool valid = Ed25519.Verify(signature, message, AlicePublicKey); + Assert.IsTrue(valid); } [TestMethod] @@ -213,15 +214,7 @@ public void Sign_InvalidSignature() signature = new byte[Ed25519.SignatureSize + 1]; Assert.ThrowsException(() => Ed25519.Sign(signature, Message, AlicePrivateKey)); } - - [TestMethod] - public void Sign_InvalidMessage() - { - var signature = new byte[Ed25519.SignatureSize]; - var message = Array.Empty(); - Assert.ThrowsException(() => Ed25519.Sign(signature, message, AlicePrivateKey)); - } - + [TestMethod] public void Sign_InvalidPrivateKey() { @@ -268,15 +261,7 @@ public void Verify_InvalidSignature() signature = new byte[Ed25519.SignatureSize + 1]; Assert.ThrowsException(() => Ed25519.Verify(signature, Message, AlicePublicKey)); } - - [TestMethod] - public void Verify_InvalidMessage() - { - var signature = new byte[Ed25519.SignatureSize]; - var message = Array.Empty(); - Assert.ThrowsException(() => Ed25519.Verify(signature, message, AlicePublicKey)); - } - + [TestMethod] public void Verify_InvalidPublicKey() { diff --git a/src/Geralt.Tests/Poly1305Tests.cs b/src/Geralt.Tests/Poly1305Tests.cs index bcab8f3..9e7e714 100644 --- a/src/Geralt.Tests/Poly1305Tests.cs +++ b/src/Geralt.Tests/Poly1305Tests.cs @@ -11,6 +11,9 @@ public class Poly1305Tests private static readonly byte[] Message = Convert.FromHexString("43727970746f6772617068696320466f72756d2052657365617263682047726f7570"); private static readonly byte[] Tag = Convert.FromHexString("a8061dc1305136c6c22b8baf0c0127a9"); private static readonly byte[] Key = Convert.FromHexString("85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b"); + // Generated using libsodium-core + private static readonly byte[] DifferentTag = Convert.FromHexString("bca21536da522787b9267be10c1b7499"); + private static readonly byte[] DifferentKey = Convert.FromHexString("593d4b15f6fb98a16835f9ef6b67ed241678ab31756c2191dad397064b5e5849"); [TestMethod] public void ComputeTag_ValidInputs() @@ -20,6 +23,17 @@ public void ComputeTag_ValidInputs() Assert.IsTrue(tag.SequenceEqual(Tag)); } + [TestMethod] + public void ComputeTag_EmptyMessage() + { + Span tag = stackalloc byte[Poly1305.TagSize]; + Span message = Span.Empty; + Poly1305.ComputeTag(tag, message, Key); + Assert.IsFalse(tag.SequenceEqual(Tag)); + bool valid = Poly1305.VerifyTag(tag, message, Key); + Assert.IsTrue(valid); + } + [TestMethod] public void ComputeTag_DifferentMessage() { @@ -41,27 +55,66 @@ public void ComputeTag_DifferentKey() } [TestMethod] - public void DeriveKey_InvalidTag() + public void ComputeTag_InvalidTag() { var tag = Array.Empty(); Assert.ThrowsException(() => Poly1305.ComputeTag(tag, Message, Key)); } [TestMethod] - public void DeriveKey_InvalidMessage() + public void ComputeTag_InvalidKey() { var tag = new byte[Poly1305.TagSize]; - var message = Array.Empty(); - Assert.ThrowsException(() => Poly1305.ComputeTag(tag, message, Key)); + var key = new byte[Poly1305.KeySize - 1]; + Assert.ThrowsException(() => Poly1305.ComputeTag(tag, Message, key)); + key = new byte[Poly1305.KeySize + 1]; + Assert.ThrowsException(() => Poly1305.ComputeTag(tag, Message, key)); + } + + [TestMethod] + public void VerifyTag_ValidInputs() + { + bool valid = Poly1305.VerifyTag(Tag, Message, Key); + Assert.IsTrue(valid); } [TestMethod] - public void DeriveKey_InvalidKey() + public void VerifyTag_DifferentTag() + { + bool valid = Poly1305.VerifyTag(DifferentTag, Message, Key); + Assert.IsFalse(valid); + } + + [TestMethod] + public void VerifyTag_DifferentMessage() + { + bool valid = Poly1305.VerifyTag(Tag, DifferentTag, Key); + Assert.IsFalse(valid); + } + + [TestMethod] + public void VerifyTag_DifferentKey() + { + bool valid = Poly1305.VerifyTag(Tag, Message, DifferentKey); + Assert.IsFalse(valid); + } + + [TestMethod] + public void VerifyTag_InvalidTag() + { + var tag = new byte[Poly1305.TagSize - 1]; + Assert.ThrowsException(() => Poly1305.VerifyTag(tag, Message, Key)); + tag = new byte[Poly1305.TagSize + 1]; + Assert.ThrowsException(() => Poly1305.VerifyTag(tag, Message, Key)); + } + + [TestMethod] + public void VerifyTag_InvalidKey() { var tag = new byte[Poly1305.TagSize]; var key = new byte[Poly1305.KeySize - 1]; - Assert.ThrowsException(() => Poly1305.ComputeTag(tag, Message, key)); + Assert.ThrowsException(() => Poly1305.VerifyTag(tag, Message, key)); key = new byte[Poly1305.KeySize + 1]; - Assert.ThrowsException(() => Poly1305.ComputeTag(tag, Message, key)); + Assert.ThrowsException(() => Poly1305.VerifyTag(tag, Message, key)); } } \ No newline at end of file diff --git a/src/Geralt.Tests/XChaCha20Poly1305Tests.cs b/src/Geralt.Tests/XChaCha20Poly1305Tests.cs index 718cf98..7381f31 100644 --- a/src/Geralt.Tests/XChaCha20Poly1305Tests.cs +++ b/src/Geralt.Tests/XChaCha20Poly1305Tests.cs @@ -24,13 +24,15 @@ public void Encrypt_ValidInputs() } [TestMethod] - public void Encrypt_DifferentPlaintext() + public void Encrypt_EmptyPlaintext() { - Span ciphertext = stackalloc byte[Plaintext.Length + XChaCha20Poly1305.TagSize]; - Span plaintext = Plaintext.ToArray(); - plaintext[0]++; + Span plaintext = Span.Empty; + Span ciphertext = stackalloc byte[plaintext.Length + XChaCha20Poly1305.TagSize]; XChaCha20Poly1305.Encrypt(ciphertext, plaintext, Nonce, Key); - Assert.IsFalse(ciphertext.SequenceEqual(Ciphertext)); + Assert.IsFalse(ciphertext.SequenceEqual(new byte[ciphertext.Length])); + Span decrypted = stackalloc byte[plaintext.Length]; + XChaCha20Poly1305.Decrypt(decrypted, ciphertext, Nonce, Key); + Assert.IsTrue(plaintext.SequenceEqual(decrypted)); } [TestMethod] @@ -66,7 +68,9 @@ public void Encrypt_DifferentAssociatedData() [TestMethod] public void Encrypt_InvalidCiphertext() { - var ciphertext = Array.Empty(); + var ciphertext = new byte[Plaintext.Length + XChaCha20Poly1305.TagSize + 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key)); + ciphertext = new byte[Plaintext.Length + XChaCha20Poly1305.TagSize - 1]; Assert.ThrowsException(() => XChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key)); } @@ -106,41 +110,6 @@ public void Decrypt_ValidInputs() Assert.IsTrue(plaintext.SequenceEqual(Plaintext)); } - [TestMethod] - public void Decrypt_InvalidPlaintext() - { - var plaintext = Array.Empty(); - Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key)); - } - - [TestMethod] - public void Decrypt_InvalidCiphertext() - { - var plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize]; - var ciphertext = Array.Empty(); - Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, ciphertext, Nonce, Key)); - } - - [TestMethod] - public void Decrypt_InvalidNonce() - { - var plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize]; - var nonce = new byte[XChaCha20Poly1305.NonceSize - 1]; - Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, nonce, Key)); - nonce = new byte[XChaCha20Poly1305.NonceSize + 1]; - Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, nonce, Key)); - } - - [TestMethod] - public void Decrypt_InvalidKey() - { - var plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize]; - var key = new byte[XChaCha20Poly1305.KeySize - 1]; - Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, key)); - key = new byte[XChaCha20Poly1305.KeySize + 1]; - Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, key)); - } - [TestMethod] public void Decrypt_WrongCiphertext() { @@ -185,4 +154,33 @@ public void Decrypt_WrongAssociatedData() associatedData[0]++; Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key, associatedData)); } + + [TestMethod] + public void Decrypt_InvalidPlaintext() + { + var plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize + 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key)); + plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize - 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key)); + } + + [TestMethod] + public void Decrypt_InvalidNonce() + { + var plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize]; + var nonce = new byte[XChaCha20Poly1305.NonceSize - 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, nonce, Key)); + nonce = new byte[XChaCha20Poly1305.NonceSize + 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, nonce, Key)); + } + + [TestMethod] + public void Decrypt_InvalidKey() + { + var plaintext = new byte[Ciphertext.Length - XChaCha20Poly1305.TagSize]; + var key = new byte[XChaCha20Poly1305.KeySize - 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, key)); + key = new byte[XChaCha20Poly1305.KeySize + 1]; + Assert.ThrowsException(() => XChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, key)); + } } \ No newline at end of file diff --git a/src/Geralt.Tests/XChaCha20Tests.cs b/src/Geralt.Tests/XChaCha20Tests.cs index b5cc1c3..94ca933 100644 --- a/src/Geralt.Tests/XChaCha20Tests.cs +++ b/src/Geralt.Tests/XChaCha20Tests.cs @@ -21,6 +21,18 @@ public void Encrypt_ValidInputs() Assert.IsTrue(ciphertext.SequenceEqual(Ciphertext)); } + [TestMethod] + public void Encrypt_EmptyPlaintext() + { + Span plaintext = Span.Empty; + Span ciphertext = stackalloc byte[plaintext.Length]; + XChaCha20.Encrypt(ciphertext, plaintext, Nonce, Key); + Assert.IsTrue(plaintext.SequenceEqual(ciphertext)); + Span decrypted = stackalloc byte[plaintext.Length]; + XChaCha20.Decrypt(decrypted, ciphertext, Nonce, Key); + Assert.IsTrue(plaintext.SequenceEqual(decrypted)); + } + [TestMethod] public void Encrypt_DifferentPlaintext() { @@ -54,7 +66,9 @@ public void Encrypt_DifferentKey() [TestMethod] public void Encrypt_InvalidCiphertext() { - var ciphertext = Array.Empty(); + var ciphertext = new byte[Plaintext.Length - 1]; + Assert.ThrowsException(() => XChaCha20.Encrypt(ciphertext, Plaintext, Nonce, Key)); + ciphertext = new byte[Plaintext.Length + 1]; Assert.ThrowsException(() => XChaCha20.Encrypt(ciphertext, Plaintext, Nonce, Key)); } @@ -135,7 +149,9 @@ public void Decrypt_InvalidPlaintext() public void Decrypt_InvalidCiphertext() { var plaintext = new byte[Ciphertext.Length]; - var ciphertext = Array.Empty(); + var ciphertext = new byte[plaintext.Length - 1]; + Assert.ThrowsException(() => XChaCha20.Decrypt(plaintext, ciphertext, Nonce, Key)); + ciphertext = new byte[plaintext.Length + 1]; Assert.ThrowsException(() => XChaCha20.Decrypt(plaintext, ciphertext, Nonce, Key)); }