public void WriteEndWithHmac(V2HmacCalculator hmacCalculator, Stream hmacStream, long plaintextLength, long compressedPlaintextLength) { if (hmacCalculator == null) { throw new ArgumentNullException("hmacCalculator"); } if (hmacStream == null) { throw new ArgumentNullException("hmacStream"); } WriteGeneralHeaders(hmacStream); V2PlaintextLengthsEncryptedHeaderBlock lengths = new V2PlaintextLengthsEncryptedHeaderBlock(CreateKeyStreamCrypto(LENGTHSINFO_KEYSTREAM_INDEX)); lengths.PlaintextLength = plaintextLength; lengths.CompressedPlaintextLength = compressedPlaintextLength; lengths.Write(hmacStream); hmacStream.Flush(); V2HmacHeaderBlock hmac = new V2HmacHeaderBlock(); hmac.Hmac = hmacCalculator.Hmac; hmac.Write(hmacStream); }
public void TestWriteWithHmac() { V2DocumentHeaders headers = new V2DocumentHeaders(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("v2passzz")), 20); byte[] output; V2HmacCalculator hmacCalculator = new V2HmacCalculator(new SymmetricKey(headers.GetHmacKey())); using (V2HmacStream <MemoryStream> hmacStream = V2HmacStream.Create <MemoryStream>(hmacCalculator, new MemoryStream())) { headers.WriteStartWithHmac(hmacStream); headers.WriteEndWithHmac(hmacCalculator, hmacStream, 0, 0); hmacStream.Flush(); output = hmacStream.Chained.ToArray(); } byte[] hmacBytesFromHeaders = new byte[V2Hmac.RequiredLength]; Array.Copy(output, output.Length - V2Hmac.RequiredLength, hmacBytesFromHeaders, 0, V2Hmac.RequiredLength); V2Hmac hmacFromHeaders = new V2Hmac(hmacBytesFromHeaders); byte[] dataToHmac = new byte[output.Length - (V2Hmac.RequiredLength + 5)]; Array.Copy(output, 0, dataToHmac, 0, dataToHmac.Length); HMACSHA512 hmac = new HMACSHA512(headers.GetHmacKey()); hmac.TransformFinalBlock(dataToHmac, 0, dataToHmac.Length); V2Hmac hmacFromCalculation = new V2Hmac(hmac.Hash); Assert.That(hmacFromHeaders, Is.EqualTo(hmacFromCalculation)); }
public bool Load(Headers headers) { if (headers == null) { throw new ArgumentNullException("headers"); } headers.EnsureFileFormatVersion(4, 4); if (!IsMasterKeyKnown(headers)) { return(false); } HmacCalculator = new V2HmacCalculator(new SymmetricKey(GetHmacKey())); using (Stream hmacStream = V2HmacStream.Create(HmacCalculator)) { AxCrypt1Guid.Write(hmacStream); foreach (HeaderBlock header in headers.HeaderBlocks) { header.Write(hmacStream); } } SetDataEncryptingCryptoForEncryptedHeaderBlocks(headers.HeaderBlocks); _headers = headers; return(true); }
/// <summary> /// Encrypt a stream with a given set of headers and write to an output stream. The caller is responsible for consistency and completeness /// of the headers. Headers that are not known until encryption and compression are added here. /// </summary> /// <param name="outputDocumentHeaders"></param> /// <param name="inputStream"></param> /// <param name="outputStream"></param> public void EncryptTo(Stream inputStream, Stream outputStream, AxCryptOptions options) { if (inputStream == null) { throw new ArgumentNullException("inputStream"); } if (outputStream == null) { throw new ArgumentNullException("outputStream"); } if (options.HasMask(AxCryptOptions.EncryptWithCompression) && options.HasMask(AxCryptOptions.EncryptWithoutCompression)) { throw new ArgumentException("Invalid options, cannot specify both with and without compression."); } if (!options.HasMask(AxCryptOptions.EncryptWithCompression) && !options.HasMask(AxCryptOptions.EncryptWithoutCompression)) { throw new ArgumentException("Invalid options, must specify either with or without compression."); } DocumentHeaders.IsCompressed = options.HasMask(AxCryptOptions.EncryptWithCompression); V2HmacCalculator hmacCalculator = new V2HmacCalculator(new SymmetricKey(DocumentHeaders.GetHmacKey())); V2HmacStream <Stream> outputHmacStream = V2HmacStream.Create(hmacCalculator, outputStream); CryptoStreamBase encryptingStream = New <CryptoStreamBase>().Initialize(V2AxCryptDataStream.Create(outputHmacStream), DocumentHeaders.DataCrypto().EncryptingTransform(), CryptoStreamMode.Write); DocumentHeaders.WriteStartWithHmac(outputHmacStream); if (DocumentHeaders.IsCompressed) { using (ZOutputStream deflatingStream = new ZOutputStream(encryptingStream, -1)) { deflatingStream.FlushMode = JZlib.Z_SYNC_FLUSH; inputStream.CopyTo(deflatingStream); deflatingStream.FlushMode = JZlib.Z_FINISH; deflatingStream.Finish(); _plaintextLength = deflatingStream.TotalIn; _compressedPlaintextLength = deflatingStream.TotalOut; encryptingStream.FinalFlush(); DocumentHeaders.WriteEndWithHmac(hmacCalculator, outputHmacStream, _plaintextLength, _compressedPlaintextLength); } } else { try { _compressedPlaintextLength = _plaintextLength = StreamExtensions.CopyTo(inputStream, encryptingStream); encryptingStream.FinalFlush(); DocumentHeaders.WriteEndWithHmac(hmacCalculator, outputHmacStream, _plaintextLength, _compressedPlaintextLength); } finally { encryptingStream.Dispose(); } } }
public static void TestConstructorNullArgument() { V2HmacCalculator nullCalculator = null; Stream nullStream = null; Stream stream = null; Assert.Throws <ArgumentNullException>(() => stream = V2HmacStream.Create(nullCalculator)); Assert.That(stream, Is.Null); Assert.Throws <ArgumentNullException>(() => stream = V2HmacStream.Create(new V2HmacCalculator(SymmetricKey.Zero128), nullStream)); Assert.That(stream, Is.Null); }