Example #1
0
        public void EncryptDecryptNBlocksWithDestinationBufferTest()
        {
            // Arrange
            var rnd = new Random();
            var key = new byte[Snuffle.KEY_SIZE_IN_BYTES];

            for (var i = 0; i < 64; i++)
            {
                RandomNumberGenerator.Fill(key);

                var nonce = new byte[XChaCha20.NONCE_SIZE_IN_BYTES];
                RandomNumberGenerator.Fill(nonce);

                var cipher = new XChaCha20(key, 0);

                for (var j = 0; j < 64; j++)
                {
                    var expected = new byte[rnd.Next(300)];
                    rnd.NextBytes(expected);

                    // Act
                    var ciphertext = new byte[expected.Length];
                    cipher.Encrypt(expected, nonce, ciphertext);

                    var plaintext = new byte[expected.Length];
                    cipher.Decrypt(ciphertext, nonce, plaintext);

                    // Assert
                    plaintext.Should().Equal(expected);
                }
            }
        }
Example #2
0
        public void Decrypt(Tests.Vectors.XChaCha20TestVector test)
        {
            var plaintext = new byte[test.CipherText.Length];
            var cipher    = new XChaCha20(test.Key, 0);

            cipher.Decrypt(test.CipherText, test.Nonce, plaintext);
        }
Example #3
0
        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;
            }
        }
Example #4
0
        public void EncryptDecryptLongMessagesWithDestinationBufferTest()
        {
            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];
                RandomNumberGenerator.Fill(key);

                var nonce = new byte[XChaCha20.NONCE_SIZE_IN_BYTES];
                RandomNumberGenerator.Fill(nonce);

                var cipher = new XChaCha20(key, 0);

                var ciphertext = new byte[plaintext.Length];
                cipher.Encrypt(plaintext, nonce, ciphertext);

                var decrypted = new byte[plaintext.Length];
                cipher.Decrypt(ciphertext, nonce, decrypted);

                decrypted.Should().Equal(plaintext);
                dataSize += 5 * dataSize / 11;
            }
        }
Example #5
0
        public void EncryptDecryptNBlocksTest()
        {
            // Arrange
            var rnd = new Random();
            var key = new byte[Snuffle.KEY_SIZE_IN_BYTES];

            for (var i = 0; i < 64; i++)
            {
                rnd.NextBytes(key);

                var cipher = new XChaCha20(key, 0);

                for (var j = 0; j < 64; j++)
                {
                    var expectedInput = new byte[rnd.Next(300)];
                    rnd.NextBytes(expectedInput);

                    // Act
                    var output      = cipher.Encrypt(expectedInput);
                    var actualInput = cipher.Decrypt(output);

                    // Assert
                    //Assert.AreEqual(expectedInput, actualInput);
                    Assert.IsTrue(CryptoBytes.ConstantTimeEquals(expectedInput, actualInput));
                }
            }
        }
Example #6
0
        public void EncryptDecryptNBlocksTest()
        {
            // Arrange
            var rnd = new Random();
            var key = new byte[Snuffle.KEY_SIZE_IN_BYTES];

            for (var i = 0; i < 64; i++)
            {
                RandomNumberGenerator.Fill(key);

                var cipher = new XChaCha20(key, 0);

                for (var j = 0; j < 64; j++)
                {
                    var expected = new byte[rnd.Next(300)];
                    rnd.NextBytes(expected);

                    // Act
                    var output    = cipher.Encrypt(expected);
                    var plaintext = cipher.Decrypt(output);

                    // Assert
                    plaintext.Should().Equal(expected);
                }
            }
        }
Example #7
0
        public void DecryptWhenNonceIsEmptyFails()
        {
            // 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[0]), EXCEPTION_MESSAGE_NONCE_LENGTH);
        }
Example #8
0
        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);
        }
Example #9
0
        public void DecryptWhenCiphertextIsTooShortFails()
        {
            // Arrange
            var rnd = new Random();
            var key = new byte[Snuffle.KEY_SIZE_IN_BYTES];

            // Act
            var cipher = new XChaCha20(key, 0);

            // Assert
            Assert.Throws <CryptographicException>(() => cipher.Decrypt(new byte[2]));
        }
Example #10
0
        public void XChaCha20TestVectors()
        {
            // From libsodium's test/default/xchacha20.c (tv_stream_xchacha20) and https://tools.ietf.org/html/draft-arciszewski-xchacha-00.

            // Arrange
            foreach (var test in XChaCha20TestVector.XChaCha20TestVectors)
            {
                // Act
                var cipher = new XChaCha20(test.Key, 0);

                // We do test all method overloads
                var output1 = cipher.Decrypt(CryptoBytes.Combine(test.Nonce, test.CipherText));
                var output2 = cipher.Decrypt(test.CipherText, test.Nonce);
                var output3 = new byte[test.CipherText.Length];
                cipher.Decrypt(test.CipherText, test.Nonce, output3);

                // Assert
                output1.Should().Equal(test.PlainText);
                output2.Should().Equal(test.PlainText);
                output3.Should().Equal(test.PlainText);
            }
        }
Example #11
0
        public void DecryptWhenCiphertextIsNotEqualToPlaintextFails(int ciphertextLen, int plaintextLen)
        {
            // Arrange
            var key    = new byte[Snuffle.KEY_SIZE_IN_BYTES];
            var cipher = new XChaCha20(key, 0);
            var nonce  = new byte[cipher.NonceSizeInBytes];

            // Act
            var act = () => cipher.Decrypt(new byte[ciphertextLen], nonce, new byte[plaintextLen]);

            // Assert
            act.Should().Throw <ArgumentException>().WithMessage("The ciphertext parameter and the plaintext do not have the same length.");
        }
Example #12
0
        public void DecryptWhenCiphertextIsTooShortFails()
        {
            // Arrange
            var key = new byte[Snuffle.KEY_SIZE_IN_BYTES];

            RandomNumberGenerator.Fill(key);

            // Act
            var    cipher = new XChaCha20(key, 0);
            Action act    = () => cipher.Decrypt(new byte[2]);

            // Assert
            act.Should().Throw <ArgumentException>();
        }
Example #13
0
        public void DecryptWhenNonceIsEmptyFails()
        {
            // Arrange
            var nonce      = new byte[0];
            var plaintext  = new byte[0];
            var ciphertext = new byte[0];

            var cipher = new XChaCha20(new byte[Snuffle.KEY_SIZE_IN_BYTES], 0);

            // Act & Assert
            Action act = () => cipher.Decrypt(ciphertext, nonce, plaintext);

            act.Should().Throw <ArgumentException>().WithMessage(EXCEPTION_MESSAGE_NONCE_LENGTH);
        }
Example #14
0
        public void DecryptWhenNonceLengthIsInvalidFails()
        {
            // Arrange
            var nonce      = new byte[XChaCha20.NONCE_SIZE_IN_BYTES + TestHelpers.ReturnRandomPositiveNegative()];
            var plaintext  = new byte[0];
            var ciphertext = new byte[0];

            var cipher = new XChaCha20(new byte[Snuffle.KEY_SIZE_IN_BYTES], 0);

            // Act & Assert
            Action act = () => cipher.Decrypt(ciphertext, nonce, plaintext);

            act.Should().Throw <ArgumentException>().WithMessage(EXCEPTION_MESSAGE_NONCE_LENGTH);
        }
Example #15
0
        public void XChaCha20TestVectors()
        {
            // From libsodium's test/default/xchacha20.c (tv_stream_xchacha20) and https://tools.ietf.org/html/draft-arciszewski-xchacha-00.

            // Arrange
            foreach (var test in XChaCha20TestVector.XChaCha20TestVectors)
            {
                // Act
                var cipher = new XChaCha20(test.Key, 0);
                var output = cipher.Decrypt(CryptoBytes.Combine(test.Nonce, test.CipherText));

                // Assert
                //Assert.That(output, Is.EqualTo(test.Output));
                Assert.IsTrue(CryptoBytes.ConstantTimeEquals(test.PlainText, output));
            }
        }
Example #16
0
        public void XChaCha20TestVectors()
        {
            // From libsodium's test/default/xchacha20.c (tv_stream_xchacha20) and https://tools.ietf.org/html/draft-arciszewski-xchacha-00.

            // Arrange
            foreach (var test in XChaCha20TestVector.XChaCha20TestVectors)
            {
                // Act
                var cipher = new XChaCha20(test.Key, 0);

                var output = new byte[test.CipherText.Length];
                cipher.Decrypt(test.CipherText, test.Nonce, output);

                // Assert
                output.Should().Equal(test.PlainText);
            }
        }
Example #17
0
        public void EncryptDecryptLongMessagesTest()
        {
            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];
                RandomNumberGenerator.Fill(key);

                var cipher = new XChaCha20(key, 0);

                var ciphertext = cipher.Encrypt(plaintext);
                var decrypted  = cipher.Decrypt(ciphertext);

                decrypted.Should().Equal(plaintext);
                dataSize += 5 * dataSize / 11;
            }
        }
Example #18
0
        public byte[] Decrypt(Tests.Vectors.XChaCha20TestVector test)
        {
            var cipher = new XChaCha20(test.Key, 0);

            return(cipher.Decrypt(CryptoBytes.Combine(test.Nonce, test.CipherText)));
        }