Ejemplo n.º 1
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);
                    }
                }
        }
Ejemplo n.º 2
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());
                    }
                }
        }
Ejemplo n.º 3
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));
                    }
                }
        }
Ejemplo n.º 4
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);
                    }
                }
        }
Ejemplo n.º 5
0
 public void StandardStream()
 {
     this.nonBufferedCiphertextStream.Position = 0;
     using (var decryptionStream = new XChaChaStream(
                nonBufferedCiphertextStream, this.key, EncryptionMode.Decrypt, leaveOpen: true))
     {
         decryptionStream.Read(this.nonBufferedOutput);
     }
 }
Ejemplo n.º 6
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));
                    }
                }
        }