Exemplo n.º 1
0
        public void Test_Decrypt_VerifyEndOfMessage_FalseWhenPartiallyDecrypted()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext1 = RandomBytesGenerator.NextBytes(1024);
                    var plaintext2 = RandomBytesGenerator.NextBytes(1024);

                    using (var encryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext1);
                        encryptionStream.WriteFinal(plaintext2);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var decryptedPlainText = new byte[plaintext1.Length];

                        decryptionStream.Read(decryptedPlainText);
                        Assert.False(decryptionStream.VerifyEndOfMessage());
                    }
                }
        }
Exemplo n.º 2
0
        public void Test_Encrypt_WriteDifferentAmountsToStream()
        {
            var plaintext1 = RandomBytesGenerator.NextBytes(157 * 1024);
            var plaintext2 = RandomBytesGenerator.NextBytes(314 * 1024);
            var plaintext3 = RandomBytesGenerator.NextBytes(273 * 1024);

            var       totalPlainTextLength = plaintext1.Length + plaintext2.Length + plaintext3.Length;
            const int numberOfWrites       = 3;
            var       expectedOutputLength = StreamHeaderLength + totalPlainTextLength + (numberOfWrites * StreamABytes);

            using (var outputStream = new MemoryStream())
            {
                using (var key = XChaChaKey.Generate())
                    using (var encryptionStream = new XChaChaStream(outputStream, key, EncryptionMode.Encrypt))
                    {
                        encryptionStream.Write(plaintext1);
                        encryptionStream.Write(plaintext2);
                        encryptionStream.WriteFinal(plaintext3);
                    }

                var ciphertext = outputStream.ToArray();

                Assert.Equal(expectedOutputLength, ciphertext.Length);
            }
        }
Exemplo n.º 3
0
        public void Test_InitializeNonce_ExistingNonce_InitializesSuccessfully()
        {
            var nonceBytes = RandomBytesGenerator.NextBytes(NonceLength);
            var nonce      = new XChaChaNonce(nonceBytes);

            Assert.Equal(nonce.ToArray(), nonceBytes);
        }
Exemplo n.º 4
0
        public void Test_Decrypt_DecryptsTwoBlocks()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    const int blockLength = 128 * 1024;
                    var       block1      = RandomBytesGenerator.NextBytes(blockLength);
                    var       block2      = RandomBytesGenerator.NextBytes(blockLength);

                    using (var encryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(block1);
                        encryptionStream.WriteFinal(block2);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var decryptedBlock1      = new byte[blockLength];
                        var decryptedBlock2      = new byte[blockLength];
                        var numberOfBytesOutput1 = decryptionStream.Read(decryptedBlock1);
                        var numberOfBytesOutput2 = decryptionStream.Read(decryptedBlock2);

                        Assert.Equal(blockLength, numberOfBytesOutput1);
                        Assert.Equal(block1, decryptedBlock1);
                        Assert.Equal(blockLength, numberOfBytesOutput2);
                        Assert.Equal(block2, decryptedBlock2);
                    }
                }
        }
        public void Test_Decrypt_DecryptsLargeBlock(int bufferLength)
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext = RandomBytesGenerator.NextBytes(1024 * 1024);

                    using (var encryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Encrypt, bufferLength, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt, bufferLength))
                    {
                        var decryptedPlainText  = new byte[plaintext.Length];
                        var numberOfBytesOutput = decryptionStream.Read(decryptedPlainText);

                        Assert.Equal(plaintext.Length, numberOfBytesOutput);
                        Assert.Equal(plaintext.ToArray(), decryptedPlainText);
                    }
                }
        }
        public void Test_Decrypt_MultipleSmallBlocks()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext1 = RandomBytesGenerator.NextBytes(17);
                    var plaintext2 = RandomBytesGenerator.NextBytes(23);

                    using (var encryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext1);
                        encryptionStream.Write(plaintext2);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var plaintextLength     = plaintext1.Length + plaintext2.Length;
                        var decryptedPlainText  = new byte[plaintextLength];
                        var numberOfBytesOutput = decryptionStream.Read(decryptedPlainText);

                        Assert.Equal(plaintextLength, numberOfBytesOutput);
                        Assert.Equal(plaintext1.Concat(plaintext2), decryptedPlainText);
                    }
                }
        }
Exemplo n.º 7
0
        public void Test_InitializeNonce_LengthIncorrect_ThrowsArgumentException(int length)
        {
            var nonceBytes = RandomBytesGenerator.NextBytes(length);

            Action action = () => new XChaChaNonce(nonceBytes);

            var exception = Assert.Throws <ArgumentException>(action);

            Assert.Equal("nonceBytes has incorrect length", exception.Message);
        }
        public void Test_Encrypt_WithLargeData_ProducesCorrectOutputLength(int bufferLength)
        {
            var plaintext = RandomBytesGenerator.NextBytes(1024 * 1024);

            using (var outputStream = new MemoryStream())
            {
                using (var key = XChaChaKey.Generate())
                    using (var encryptionStream = new XChaChaBufferedStream(outputStream, key, EncryptionMode.Encrypt, bufferLength))
                    {
                        encryptionStream.Write(plaintext);
                    }

                var ciphertext = outputStream.ToArray();

                var numberOfBlocks           = Math.Ceiling((decimal)plaintext.Length / bufferLength);
                var expectedCipherTextLength = plaintext.Length + StreamHeaderLength + (StreamABytes * numberOfBlocks);
                Assert.Equal(expectedCipherTextLength, ciphertext.Length);
            }
        }
Exemplo n.º 9
0
        public void Test_Encrypt_EncryptsTwoBlocks()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    const int blockLength = 128 * 1024;
                    var       block1      = RandomBytesGenerator.NextBytes(blockLength);
                    var       block2      = RandomBytesGenerator.NextBytes(blockLength);

                    using (var encryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Encrypt))
                    {
                        encryptionStream.Write(block1);
                        encryptionStream.WriteFinal(block2);
                    }

                    var ciphertext = ciphertextStream.ToArray();

                    const int numberOfWrites           = 2;
                    const int expectedCiphertextLength = StreamHeaderLength + 2 * blockLength + (numberOfWrites * StreamABytes);

                    Assert.Equal(expectedCiphertextLength, ciphertext.Length);
                }
        }
        public void Test_Decrypt_VerifyEndOfMessage_TrueWhenFullyDecrypted()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext = RandomBytesGenerator.NextBytes(1024 * 1024);

                    using (var encryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var decryptedPlainText = new byte[plaintext.Length];

                        decryptionStream.Read(decryptedPlainText);
                        Assert.True(decryptionStream.VerifyEndOfMessage());
                    }
                }
        }