public void Test_Decrypt_Fails_ThrowsCryptographicException(Type cipherType)
        {
            using (var key = XChaChaKey.Generate())
            {
                var cipher = (XChaChaSecretKeyCipher)Activator.CreateInstance(cipherType);
                var nonce  = XChaChaNonce.Generate();

                const int messageLength = 1024 * 1024;
                var       message       = RandomBytesGenerator.NextBytes(messageLength);
                var       ciphertext    = new byte[cipher.GetCipherTextLength(message.Length)];

                cipher.Encrypt(message, ciphertext, key, nonce);

                Action action = () =>
                {
                    var wrongNonce = XChaChaNonce.Generate();
                    cipher.Decrypt(ciphertext, ciphertext, key, wrongNonce);
                };
                var exception = Assert.Throws <CryptographicException>(action);
                Assert.Equal("decryption failed", exception.Message);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="stream">When encrypting, the stream to write the ciphertext to. When decrypting, the stream to read the ciphertext from.</param>
 /// <param name="key">The encryption key.</param>
 /// <param name="encryptionMode">Whether the stream will be used for encryption or decryption.</param>
 /// <param name="leaveOpen">Whether to leave the <paramref name="stream"/> open.</param>
 public XChaChaStream(Stream stream, XChaChaKey key, EncryptionMode encryptionMode, bool leaveOpen = false)
     : base(stream, key, encryptionMode, leaveOpen)
 {
 }
Esempio n. 3
0
 /// <summary>
 /// Encrypts the <paramref name="message"/> and writes computed ciphertext to <paramref name="ciphertext"/>.
 /// </summary>
 /// <param name="message">The message to encrypt.</param>
 /// <param name="ciphertext">The buffer in which to write the ciphertext.</param>
 /// <param name="key">The encryption key.</param>
 /// <param name="nonce">The nonce to use when encrypting the <paramref name="message"/>.</param>
 public void Encrypt(ReadOnlySpan <byte> message, Span <byte> ciphertext, XChaChaKey key, XChaChaNonce nonce)
 {
     this.InternalEncrypt(message, ciphertext, key, nonce);
 }
Esempio n. 4
0
 /// <summary>
 /// Decrypts the <paramref name="ciphertext"/>, verifies the authenticaion tag, and if successful,
 /// writes the output to <paramref name="message"/>.
 /// </summary>
 /// <param name="ciphertext">The ciphertext to decrypt.</param>
 /// <param name="message">The buffer in which to write the decrypted message.</param>
 /// <param name="key">The encryption key.</param>
 /// <param name="nonce">The nonce to use when decrypting the <paramref name="ciphertext"/>.</param>
 public void Decrypt(ReadOnlySpan <byte> ciphertext, Span <byte> message, XChaChaKey key, XChaChaNonce nonce)
 {
     this.InternalDecrypt(ciphertext, message, key, nonce);
 }
Esempio n. 5
0
 private protected abstract void InternalDecrypt(
     ReadOnlySpan <byte> ciphertext,
     Span <byte> message,
     XChaChaKey key,
     XChaChaNonce nonce);