Skip to content

zip: read AES-encrypted zip files (AE-1 and AE-2)#2966

Merged
lerno merged 17 commits intoc3lang:masterfrom
konimarti:add-aex
Mar 11, 2026
Merged

zip: read AES-encrypted zip files (AE-1 and AE-2)#2966
lerno merged 17 commits intoc3lang:masterfrom
konimarti:add-aex

Conversation

@konimarti
Copy link
Copy Markdown
Contributor

Read AES-encrypyted Zip files using the AE-1 or AE-2 format 1;

@konimarti konimarti marked this pull request as draft February 22, 2026 09:01
@lerno
Copy link
Copy Markdown
Collaborator

lerno commented Feb 22, 2026

Can the Zip password ever be an empty string?

@konimarti
Copy link
Copy Markdown
Contributor Author

konimarti commented Feb 22, 2026

The empty string for the password doesn't make sense but I just added it as the default function value when it's not encrypted. I need to reconsider that. Also, there's an issue with an encrypted deflate stream that I initially didn't see and haven't resolved yet. This needs more work.

@konimarti
Copy link
Copy Markdown
Contributor Author

I discovered that WinZip’s AES-CTR uses a little-endian counter, so I had to increment the IV starting at the lowest-order byte instead of the highest (which is the common way). After adding a little-endian counter to AES-CTR, the decrypted DEFLATE streams should be correct now.

@konimarti
Copy link
Copy Markdown
Contributor Author

The failed nix test is because of a stochastic element in std::math::distributions. I thought I removed most of them but apprently not. Will provide a fix for this soon, too.

@konimarti
Copy link
Copy Markdown
Contributor Author

Can the Zip password ever be an empty string?

I changed the password handling to a callback which provides for a cleaner api.

@konimarti
Copy link
Copy Markdown
Contributor Author

konimarti commented Feb 22, 2026

For testing purposes, AES-encrypted zip files can be created with 7z:

7z a -tzip -mem=AES256 -pYourPassword secure.zip file_to_encrypt.txt

@konimarti konimarti marked this pull request as ready for review February 22, 2026 20:17
@lerno
Copy link
Copy Markdown
Collaborator

lerno commented Feb 23, 2026

Are you imagining that the callback would pop some dialog or something? I think more reasonable would be to fail on PASSWORD_NEEDED if the password isn't provided and it's passworded, and a PASSWORD_MISMATCH if the password doesn't work.

@konimarti
Copy link
Copy Markdown
Contributor Author

It was meant for flexibility: you can have different passwords for different zip entry files in the same archive and an empty string as a password is technically valid. I'm fine with changing it back to have the password in the function signature, though.

konimarti and others added 2 commits February 23, 2026 21:01
- Return PASSWORD_NEEDED for empty password strings.
- Return PASSWORD_MISMATCH if password is wrong.
@lerno
Copy link
Copy Markdown
Collaborator

lerno commented Mar 5, 2026

I'm sorry but I think this needs to be merged now.

self.file.read(compressed)!;
data = deflate::decompress(allocator, compressed)!;
}
data = io::read_fully(allocator, &reader)!;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned about this change here. It looks like it's completely ignoring the old behaviour?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZipArchive.read_file_all duplicates the STORE/DEFLATE dispatch logic that ZipEntryReader.read already handle. The duplication can be removed by just using the ZipEntryReader. Unit tests pass. @ManuLinares can you confirm?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Could you resolve the conflicts so I can review this?

@ManuLinares
Copy link
Copy Markdown
Member

LGTM 🚀

@lerno lerno merged commit efa76e2 into c3lang:master Mar 11, 2026
22 checks passed
@lerno
Copy link
Copy Markdown
Collaborator

lerno commented Mar 11, 2026

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants