public void EncryptDecryptLongMessagesWithNonceTest() { var rnd = new Random(); var dataSize = 16; while (dataSize <= (1 << 24)) { var plaintext = new byte[dataSize]; rnd.NextBytes(plaintext); var key = new byte[Snuffle.KEY_SIZE_IN_BYTES]; rnd.NextBytes(key); var cipher = new XChaCha20(key, 0); var nonce = new byte[cipher.NonceSizeInBytes()]; rnd.NextBytes(nonce); var ciphertext = cipher.Encrypt(plaintext, nonce); var decrypted = cipher.Decrypt(ciphertext, nonce); //Assert.AreEqual(plaintext, decrypted); Assert.IsTrue(CryptoBytes.ConstantTimeEquals(plaintext, decrypted)); dataSize += 5 * dataSize / 11; } }
public void XChaCha20BlockWhenLengthIsInvalidFails() { // Arrange var key = new byte[Snuffle.KEY_SIZE_IN_BYTES]; var cipher = new XChaCha20(key, 0); var nonce = new byte[cipher.NonceSizeInBytes() + TestHelpers.ReturnRandomPositiveNegative()]; var block = new byte[0]; // Act & Assert Assert.Throws <CryptographicException>(() => cipher.ProcessKeyStreamBlock(nonce, 0, block)); }
public void DecryptWhenNonceLengthIsInvalidFails() { // Arrange var cipher = new XChaCha20(new byte[Snuffle.KEY_SIZE_IN_BYTES], 0); // Act & Assert Assert.Throws <CryptographicException>(() => cipher.Decrypt(new byte[0], new byte[cipher.NonceSizeInBytes() + TestHelpers.ReturnRandomPositiveNegative()]), EXCEPTION_MESSAGE_NONCE_LENGTH); }