public void TestInvalidArguments() { using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Encoding.UTF8.GetBytes("AxCrypt is Great!"))) { using (Stream outputStream = new MemoryStream()) { using (V1AxCryptDocument document = new V1AxCryptDocument()) { Assert.Throws <ArgumentNullException>(() => { document.EncryptTo(inputStream, null, AxCryptOptions.EncryptWithCompression); }); Assert.Throws <ArgumentNullException>(() => { document.EncryptTo(null, outputStream, AxCryptOptions.EncryptWithCompression); }); Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, new NonSeekableStream(), AxCryptOptions.EncryptWithCompression); }); Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, outputStream, AxCryptOptions.EncryptWithCompression | AxCryptOptions.EncryptWithoutCompression); }); Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, outputStream, AxCryptOptions.None); }); Passphrase passphrase = new Passphrase("a"); V1DocumentHeaders headers = new V1DocumentHeaders(passphrase, 13); Assert.Throws <ArgumentNullException>(() => { document.CopyEncryptedTo(null, outputStream); }); Assert.Throws <ArgumentNullException>(() => { document.CopyEncryptedTo(headers, null); }); Assert.Throws <ArgumentException>(() => { document.CopyEncryptedTo(headers, new NonSeekableStream()); }); Assert.Throws <InternalErrorException>(() => { document.CopyEncryptedTo(headers, outputStream); }); } } } }
public void TestInvalidHmacInCopyEncryptedTo() { Passphrase passphrase = new Passphrase("a"); using (V1AxCryptDocument document = new V1AxCryptDocument()) { bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt)); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct and should work!"); Passphrase newPassphrase = new Passphrase("b"); using (Stream changedStream = new MemoryStream()) { V1DocumentHeaders outputDocumentHeaders = new V1DocumentHeaders(document.DocumentHeaders); outputDocumentHeaders.RewrapMasterKey(new V1DerivedKey(newPassphrase), 15); byte[] modifiedHmacBytes = document.DocumentHeaders.Headers.Hmac.GetBytes(); modifiedHmacBytes[0] += 1; document.DocumentHeaders.Headers.Hmac = new V1Hmac(modifiedHmacBytes); Assert.Throws <Axantum.AxCrypt.Core.Runtime.IncorrectDataException>(() => { document.CopyEncryptedTo(outputDocumentHeaders, changedStream); }); } } }
public void TestChangePassphraseForSimpleFile() { Passphrase passphrase = new Passphrase("a"); using (V1AxCryptDocument document = new V1AxCryptDocument()) { bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt)); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct and should work!"); Passphrase newPassphrase = new Passphrase("b"); using (Stream changedStream = new MemoryStream()) { V1DocumentHeaders outputDocumentHeaders = new V1DocumentHeaders(document.DocumentHeaders); outputDocumentHeaders.RewrapMasterKey(new V1DerivedKey(newPassphrase), 35); document.CopyEncryptedTo(outputDocumentHeaders, changedStream); changedStream.Position = 0; using (V1AxCryptDocument changedDocument = new V1AxCryptDocument()) { bool changedKeyIsOk = changedDocument.Load(newPassphrase, new V1Aes128CryptoFactory().CryptoId, changedStream); Assert.That(changedKeyIsOk, Is.True, "The changed passphrase provided is correct and should work!"); using (MemoryStream plaintextStream = new MemoryStream()) { changedDocument.DecryptTo(plaintextStream); Assert.That(Encoding.ASCII.GetString(plaintextStream.GetBuffer(), 0, (int)plaintextStream.Length), Is.EqualTo("HelloWorld"), "Unexpected result of decryption."); Assert.That(changedDocument.DocumentHeaders.PlaintextLength, Is.EqualTo(10), "'HelloWorld' should be 10 bytes uncompressed plaintext."); } } } } }