Skip to content

Commit

Permalink
src: Reorder (X)ChaCha20Poly1305 validation.
Browse files Browse the repository at this point in the history
Seems more logical.
  • Loading branch information
samuel-lucas6 committed Sep 1, 2022
1 parent 28b23fd commit 1590986
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/Geralt.Tests/ChaCha20Poly1305Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ public void Decrypt_InvalidPlaintext()
plaintext = new byte[Ciphertext.Length - ChaCha20Poly1305.TagSize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => 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<ArgumentOutOfRangeException>(() => ChaCha20Poly1305.Decrypt(plaintext, ciphertext, Nonce, Key));
}

[TestMethod]
public void Decrypt_InvalidNonce()
Expand Down
18 changes: 9 additions & 9 deletions src/Geralt.Tests/XChaCha20Poly1305Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ public void Encrypt_InvalidCiphertext()
ciphertext = new byte[Plaintext.Length + XChaCha20Poly1305.TagSize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => XChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key));
}

[TestMethod]
public void Encrypt_InvalidPlaintext()
{
var ciphertext = new byte[Plaintext.Length + XChaCha20Poly1305.TagSize];
var plaintext = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => XChaCha20Poly1305.Encrypt(ciphertext, plaintext, Nonce, Key));
}


[TestMethod]
public void Encrypt_InvalidNonce()
{
Expand Down Expand Up @@ -164,6 +156,14 @@ public void Decrypt_InvalidPlaintext()
Assert.ThrowsException<ArgumentOutOfRangeException>(() => 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<ArgumentOutOfRangeException>(() => XChaCha20Poly1305.Decrypt(plaintext, ciphertext, Nonce, Key));
}

[TestMethod]
public void Decrypt_InvalidNonce()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Geralt/Crypto/ChaCha20Poly1305.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static unsafe void Encrypt(Span<byte> ciphertext, ReadOnlySpan<byte> plai

public static unsafe void Decrypt(Span<byte> plaintext, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> key, ReadOnlySpan<byte> 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();
Expand Down
2 changes: 1 addition & 1 deletion src/Geralt/Crypto/XChaCha20Poly1305.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static unsafe void Encrypt(Span<byte> ciphertext, ReadOnlySpan<byte> plai

public static unsafe void Decrypt(Span<byte> plaintext, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> key, ReadOnlySpan<byte> 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();
Expand Down

0 comments on commit 1590986

Please sign in to comment.