Example #1
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);
            }
        }
Example #2
0
        public void Test_Decrypt_WithInvalidAdditionalData_Fails()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext      = TestConstants.MessageBytes;
                    var additionalData = Encoding.UTF8.GetBytes("apple");

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

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var decryptedPlainText    = new byte[plaintext.Length];
                        var invalidAdditionalData = Encoding.UTF8.GetBytes("pear");

                        Action action    = () => decryptionStream.Read(decryptedPlainText, invalidAdditionalData);
                        var    exception = Assert.Throws <CryptographicException>(action);

                        Assert.Equal("block is invalid or corrupt", exception.Message);
                        Assert.True(decryptedPlainText.All((b) => b == 0));
                    }
                }
        }
Example #3
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());
                    }
                }
        }
Example #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);
                    }
                }
        }
Example #5
0
        public void Test_Decrypt_DecryptsBlock_WithAdditionalData()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext      = TestConstants.MessageBytes;
                    var additionalData = Encoding.UTF8.GetBytes("apple");

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

                    ciphertextStream.Position = 0;

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

                        Assert.Equal(plaintext.Length, numberOfBytesOutput);
                        Assert.Equal(plaintext, decryptedPlainText);
                    }
                }
        }
 public void StandardStream()
 {
     using (var encryptionStream = new XChaChaStream(Stream.Null, this.key, EncryptionMode.Encrypt))
     {
         encryptionStream.WriteFinal(data);
     }
 }
Example #7
0
 public void StandardStream()
 {
     this.nonBufferedCiphertextStream.Position = 0;
     using (var decryptionStream = new XChaChaStream(
                nonBufferedCiphertextStream, this.key, EncryptionMode.Decrypt, leaveOpen: true))
     {
         decryptionStream.Read(this.nonBufferedOutput);
     }
 }
Example #8
0
        public void Test_Flush_FlushesHeader()
        {
            using (var outputStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                    using (var encryptionStream = new XChaChaStream(outputStream, key, EncryptionMode.Encrypt))
                    {
                        var plaintext = Array.Empty <byte>();

                        encryptionStream.WriteFinal(plaintext);
                        encryptionStream.Flush();

                        var ciphertext = outputStream.ToArray();
                        Assert.Equal(StreamHeaderLength, ciphertext.Length);
                    }
        }
Example #9
0
        public void Test_Encrypt_ProducesCorrectOutputLength()
        {
            var plaintext = TestConstants.MessageBytes;

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

                var ciphertext = outputStream.ToArray();

                var expectedCipherTextLength = StreamHeaderLength + plaintext.Length + StreamABytes;
                Assert.Equal(expectedCipherTextLength, ciphertext.Length);
            }
        }
Example #10
0
        public void Test_Encrypt_WithAdditionalData()
        {
            var plaintext      = TestConstants.MessageBytes;
            var additionalData = Encoding.UTF8.GetBytes("apple");

            using (var outputStream = new MemoryStream())
            {
                using (var key = XChaChaKey.Generate())
                    using (var encryptionStream = new XChaChaStream(outputStream, key, EncryptionMode.Encrypt))
                    {
                        encryptionStream.WriteFinal(plaintext, additionalData);
                    }

                var ciphertext = outputStream.ToArray();

                var expectedCipherTextLength = StreamHeaderLength + plaintext.Length + StreamABytes;
                Assert.Equal(expectedCipherTextLength, ciphertext.Length);
            }
        }
Example #11
0
        public void GlobalSetup()
        {
            var data = new byte[this.DataLengthKb * 1024];

            new Random(31).NextBytes(data);
            this.nonBufferedCiphertextStream = new MemoryStream();
            this.bufferedCiphertextStream    = new MemoryStream();
            this.key = XChaChaKey.Generate();

            using (var bufferedEncryptionStream = new XChaChaBufferedStream(
                       this.bufferedCiphertextStream, this.key, EncryptionMode.Encrypt, leaveOpen: true))
            {
                bufferedEncryptionStream.Write(data);
            }

            using (var encryptionStream = new XChaChaStream(
                       this.nonBufferedCiphertextStream, this.key, EncryptionMode.Encrypt, leaveOpen: true))
            {
                encryptionStream.WriteFinal(data);
            }
        }
Example #12
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);
                }
        }
Example #13
0
        public void Test_Decrypt_OverReadDecryptionStream_OutputsCorrectNumberOfBytes()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext = TestConstants.MessageBytes;

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

                    ciphertextStream.Position = 0;

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

                        Assert.Equal(plaintext.Length, numberOfBytesOutput);
                        Assert.Equal(plaintext, decryptedPlainText.Take(plaintext.Length));
                    }
                }
        }