public static void TestPrematureEndOfFile(CryptoImplementation cryptoImplementation) { SetupAssembly.AssemblySetupCrypto(cryptoImplementation); V1DocumentHeaders headers = new V1DocumentHeaders(new Passphrase("passphrase"), 10); using (MemoryStream stream = new MemoryStream()) { headers.WriteWithoutHmac(stream); stream.Position = 0; using (V1AxCryptReader reader = new V1AxCryptReader(new LookAheadStream(stream))) { AxCryptItemType lastItemType = AxCryptItemType.Undefined; while (reader.Read()) { lastItemType = reader.CurrentItemType; } Assert.That(lastItemType, Is.EqualTo(AxCryptItemType.Data)); Assert.That(reader.CurrentItemType, Is.EqualTo(AxCryptItemType.Data)); reader.SetStartOfData(); Assert.That(reader.Read(), Is.False); Assert.That(reader.CurrentItemType, Is.EqualTo(AxCryptItemType.EndOfStream)); Assert.That(reader.Read(), Is.False); Assert.That(reader.CurrentItemType, Is.EqualTo(AxCryptItemType.EndOfStream)); } } }
public static void TestHeaderBlockFactory(CryptoImplementation cryptoImplementation) { SetupAssembly.AssemblySetupCrypto(cryptoImplementation); V1DocumentHeaders headers = new V1DocumentHeaders(new Passphrase("passphrase"), 10); using (MemoryStream stream = new MemoryStream()) { headers.WriteWithoutHmac(stream); stream.Position = 0; UnversionedAxCryptReader reader = new UnversionedAxCryptReader(new LookAheadStream(stream)); bool unexpectedHeaderTypeFound = false; while (reader.Read()) { if (reader.CurrentItemType != AxCryptItemType.HeaderBlock) { continue; } switch (reader.CurrentHeaderBlock.HeaderBlockType) { case HeaderBlockType.Preamble: case HeaderBlockType.Version: case HeaderBlockType.Data: case HeaderBlockType.Unrecognized: break; default: unexpectedHeaderTypeFound = !(reader.CurrentHeaderBlock is UnrecognizedHeaderBlock); break; } } Assert.That(unexpectedHeaderTypeFound, Is.False); } }
public void TestBadArguments() { V1DocumentHeaders documentHeaders = new V1DocumentHeaders(Passphrase.Empty, 37); Assert.Throws <ArgumentNullException>(() => { documentHeaders.WriteWithHmac(null); }); Assert.Throws <ArgumentNullException>(() => { documentHeaders.WriteWithoutHmac(null); }); Assert.Throws <ArgumentNullException>(() => { documentHeaders.Headers.Hmac = null; }); }
/// <summary> /// Write a copy of the current encrypted stream. Used to change meta-data /// and encryption key(s) etc. /// </summary> /// <param name="outputStream"></param> public void CopyEncryptedTo(V1DocumentHeaders outputDocumentHeaders, Stream cipherStream) { if (outputDocumentHeaders == null) { throw new ArgumentNullException("outputDocumentHeaders"); } if (cipherStream == null) { throw new ArgumentNullException("cipherStream"); } if (!cipherStream.CanSeek) { throw new ArgumentException("The output stream must support seek in order to back-track and write the HMAC."); } if (!PassphraseIsValid) { throw new InternalErrorException("Passphrase is not valid."); } using (V1HmacStream hmacStreamOutput = new V1HmacStream(outputDocumentHeaders.HmacSubkey.Key, cipherStream)) { outputDocumentHeaders.WriteWithHmac(hmacStreamOutput); using (V1AxCryptDataStream encryptedDataStream = CreateEncryptedDataStream(_reader.InputStream, DocumentHeaders.CipherTextLength)) { encryptedDataStream.CopyTo(hmacStreamOutput); if (Hmac != DocumentHeaders.Headers.Hmac) { throw new Axantum.AxCrypt.Core.Runtime.IncorrectDataException("HMAC validation error in the input stream.", ErrorStatus.HmacValidationError); } } outputDocumentHeaders.Headers.Hmac = hmacStreamOutput.HmacResult; // Rewind and rewrite the headers, now with the updated HMAC outputDocumentHeaders.WriteWithoutHmac(cipherStream); cipherStream.Position = cipherStream.Length; } }