Skip to content

Commit

Permalink
PR #460: Account for AES overhead in compressed entry size (#460)
Browse files Browse the repository at this point in the history
* Unit test for writing encrypted 0 byte files to ZipOutputStream
* In ZipOutputStream.PutNextEntry, account for AES overhead when calculating compressed entry size
  • Loading branch information
Numpsy authored Aug 7, 2020
1 parent 3835f0c commit bf4372a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
22 changes: 21 additions & 1 deletion src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public bool LocalHeaderRequiresZip64

if ((versionToExtract == 0) && IsCrypted)
{
trueCompressedSize += ZipConstants.CryptoHeaderSize;
trueCompressedSize += (ulong)this.EncryptionOverheadSize;
}

// TODO: A better estimation of the true limit based on compression overhead should be used
Expand Down Expand Up @@ -1013,6 +1013,26 @@ internal int AESOverheadSize
}
}

/// <summary>
/// Number of extra bytes required to hold the encryption header fields.
/// </summary>
internal int EncryptionOverheadSize
{
get
{
// Entry is not encrypted - no overhead
if (!this.IsCrypted)
return 0;

// Entry is encrypted using ZipCrypto
if (_aesEncryptionStrength == 0)
return ZipConstants.CryptoHeaderSize;

// Entry is encrypted using AES
return this.AESOverheadSize;
}
}

/// <summary>
/// Process extra data fields updating the entry based on the contents.
/// </summary>
Expand Down
13 changes: 3 additions & 10 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public void PutNextEntry(ZipEntry entry)
}
else
{
WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
WriteLeInt((int)entry.CompressedSize + entry.EncryptionOverheadSize);
WriteLeInt((int)entry.Size);
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ public void PutNextEntry(ZipEntry entry)
if (headerInfoAvailable)
{
ed.AddLeLong(entry.Size);
ed.AddLeLong(entry.CompressedSize);
ed.AddLeLong(entry.CompressedSize + entry.EncryptionOverheadSize);
}
else
{
Expand Down Expand Up @@ -540,14 +540,7 @@ public void CloseEntry()

if (curEntry.IsCrypted)
{
if (curEntry.AESKeySize > 0)
{
curEntry.CompressedSize += curEntry.AESOverheadSize;
}
else
{
curEntry.CompressedSize += ZipConstants.CryptoHeaderSize;
}
curEntry.CompressedSize += curEntry.EncryptionOverheadSize;
}

// Patch the header if possible
Expand Down
36 changes: 36 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ public void ZipCryptoEncryption(CompressionMethod compressionMethod)
CreateZipWithEncryptedEntries("foo", 0, compressionMethod);
}

/// <summary>
/// Test Known zero length encrypted entries with ZipOutputStream.
/// These are entries where the entry size is set to 0 ahead of time, so that PutNextEntry will fill in the header and there will be no patching.
/// Test with Zip64 on and off, as the logic is different for the two.
/// </summary>
[Test]
public void ZipOutputStreamEncryptEmptyEntries(
[Values] UseZip64 useZip64,
[Values(0, 128, 256)] int keySize,
[Values(CompressionMethod.Stored, CompressionMethod.Deflated)] CompressionMethod compressionMethod)
{
using (var ms = new MemoryStream())
{
using (var zipOutputStream = new ZipOutputStream(ms))
{
zipOutputStream.IsStreamOwner = false;
zipOutputStream.Password = "password";
zipOutputStream.UseZip64 = useZip64;

ZipEntry zipEntry = new ZipEntry("emptyEntry")
{
AESKeySize = keySize,
CompressionMethod = compressionMethod,
CompressedSize = 0,
Crc = 0,
Size = 0,
};

zipOutputStream.PutNextEntry(zipEntry);
zipOutputStream.CloseEntry();
}

VerifyZipWith7Zip(ms, "password");
}
}

[Test]
[Category("Encryption")]
[Category("Zip")]
Expand Down

0 comments on commit bf4372a

Please sign in to comment.