bkcrack: include/bkcrack/Zip.hpp Source File

bkcrack 1.8.1 Crack legacy zip encryption with Biham and Kocher's known plaintext attack.

Zip.hpp
1#ifndef BKCRACK_ZIP_HPP
2#define BKCRACK_ZIP_HPP
3
4#include <bkcrack/Keys.hpp>
5#include <bkcrack/Progress.hpp>
6
7#include <istream>
8#include <optional>
9
20 class Zip
21{
22public:
24 class Error : public BaseError
25 {
26 public:
28 explicit Error(const std::string& description);
29 };
30
32 enum class Encryption
33 {
34 None,
35 Traditional,
36 Unsupported
37 };
38
40 enum class Compression
41 {
42 Store = 0,
43 Shrink = 1,
44 Implode = 6,
45 Deflate = 8,
46 Deflate64 = 9,
47 BZip2 = 12,
48 LZMA = 14,
49 Zstandard = 93,
50 MP3 = 94,
51 XZ = 95,
52 JPEG = 96,
53 WavPack = 97,
54 PPMd = 98,
55 };
56
58 struct Entry
59 {
60 std::string name;
61 Encryption encryption;
62 Compression compression;
63 std::uint32_t crc32;
64 std::uint64_t offset;
65 std::uint64_t packedSize;
66 std::uint64_t uncompressedSize;
67 std::uint8_t checkByte;
68 };
69
71 class Iterator
72 {
73 public:
76 using difference_type = std::ptrdiff_t;
77 using value_type = const Entry;
78 using pointer = const Entry*;
79 using reference = const Entry&;
80 using iterator_category = std::input_iterator_tag;
82
84 constexpr Iterator() noexcept = default;
85
88 explicit Iterator(const Zip& archive);
89
92 auto operator*() const -> const Entry&
93 {
94 return *m_entry;
95 }
96
99 auto operator->() const -> const Entry*
100 {
101 return &(*m_entry);
102 }
103
106 auto operator++() -> Iterator&;
107
109 auto operator++(int) -> Iterator;
110
112 auto operator==(const Zip::Iterator& other) const -> bool
113 {
114 return (m_is == nullptr) == (other.m_is == nullptr);
115 }
116
117 private:
118 std::istream* m_is = nullptr;
119 std::optional<Entry> m_entry; // optional type allows the end-of-stream iterator to be empty
120 };
121
124 explicit Zip(std::istream& stream);
125
127 auto begin() const -> Iterator
128 {
129 return Iterator{*this};
130 }
131
133 auto end() const -> Iterator
134 {
135 return Iterator{};
136 }
137
140 auto operator[](const std::string& name) const -> Entry;
141
144 auto operator[](std::size_t index) const -> Entry;
145
148 static void checkEncryption(const Entry& entry, Encryption expected);
149
152 auto seek(const Entry& entry) const -> std::istream&;
153
156 auto load(const Entry& entry, std::size_t count = std::numeric_limits<std::size_t>::max()) const
157 -> std::vector<std::uint8_t>;
158
161 void changeKeys(std::ostream& os, const Keys& oldKeys, const Keys& newKeys, Progress& progress) const;
162
165 void decrypt(std::ostream& os, const Keys& keys, Progress& progress) const;
166
167private:
168 std::istream& m_is;
169 const std::uint64_t m_centralDirectoryOffset;
170};
171
174void decipher(std::istream& is, std::size_t size, std::size_t discard, std::ostream& os, Keys keys);
175
176#endif // BKCRACK_ZIP_HPP
BaseError::BaseError
BaseError(const std::string &type, const std::string &description)
Constructor.
Keys
Keys defining the cipher state.
Definition Keys.hpp:10
Progress
Structure to report the progress of a long operation or to cancel it.
Definition Progress.hpp:11
Zip::Error::Error
Error(const std::string &description)
Constructor.
Zip::Iterator
Single-pass input iterator that reads successive Entry objects.
Definition Zip.hpp:72
Zip::Iterator::operator->
auto operator->() const -> const Entry *
Access a member of the current entry.
Definition Zip.hpp:99
Zip::Iterator::difference_type
std::ptrdiff_t difference_type
Required types for iterators.
Definition Zip.hpp:76
Zip::Iterator::value_type
const Entry value_type
Required types for iterators.
Definition Zip.hpp:77
Zip::Iterator::iterator_category
std::input_iterator_tag iterator_category
Required types for iterators.
Definition Zip.hpp:80
Zip::Iterator::operator++
auto operator++() -> Iterator &
Read the next central directory record if any or assign end-of-stream iterator.
Zip::Iterator::reference
const Entry & reference
Required types for iterators.
Definition Zip.hpp:79
Zip::Iterator::Iterator
constexpr Iterator() noexcept=default
Construct end-of-stream iterator.
Zip::Iterator::operator++
auto operator++(int) -> Iterator
Read the next central directory record if any or assign end-of-stream iterator.
Zip::Iterator::operator==
auto operator==(const Zip::Iterator &other) const -> bool
Test if iterators are equivalent, i.e. both are end-of-stream or both are valid.
Definition Zip.hpp:112
Zip::Iterator::pointer
const Entry * pointer
Required types for iterators.
Definition Zip.hpp:78
Zip::begin
auto begin() const -> Iterator
Get an iterator pointing to the first entry.
Definition Zip.hpp:127
Zip::operator[]
auto operator[](const std::string &name) const -> Entry
Get the first entry having the given name.
Zip::Compression
Compression
Compression algorithm.
Definition Zip.hpp:41
Zip::checkEncryption
static void checkEncryption(const Entry &entry, Encryption expected)
Check that the given entry uses the expected encryption algorithm.
Zip::end
auto end() const -> Iterator
Get an end-of-stream iterator.
Definition Zip.hpp:133
Zip::load
auto load(const Entry &entry, std::size_t count=std::numeric_limits< std::size_t >::max()) const -> std::vector< std::uint8_t >
Load at most count bytes of the given entry's raw data.
Zip::changeKeys
void changeKeys(std::ostream &os, const Keys &oldKeys, const Keys &newKeys, Progress &progress) const
Copy the zip file into os changing the encrypted data using the given keys.
Zip::operator[]
auto operator[](std::size_t index) const -> Entry
Get the entry at the given index.
Zip::seek
auto seek(const Entry &entry) const -> std::istream &
Set the underlying stream's input position indicator at the beginning the given entry's raw data.
Zip::Zip
Zip(std::istream &stream)
Open a zip archive from an already opened input stream.
Zip::decrypt
void decrypt(std::ostream &os, const Keys &keys, Progress &progress) const
Copy the zip file into os removing encryption using the given keys.
Zip::Encryption
Encryption
Encryption algorithm.
Definition Zip.hpp:33
Zip::Encryption::Traditional
@ Traditional
Traditional PKWARE encryption (ZipCrypto), vulnerable to known plaintext attack.
Definition Zip.hpp:35
Zip::Encryption::None
@ None
No encryption.
Definition Zip.hpp:34
Zip::Encryption::Unsupported
@ Unsupported
Other encryption (DES, RC2, 3DES, AES, Blowfish, Twofish, RC4).
Definition Zip.hpp:36
Zip::Entry
Information about a zip entry.
Definition Zip.hpp:59
Zip::Entry::encryption
Encryption encryption
Encryption method.
Definition Zip.hpp:61
Zip::Entry::offset
std::uint64_t offset
Offset of local file header.
Definition Zip.hpp:64
Zip::Entry::packedSize
std::uint64_t packedSize
Packed data size.
Definition Zip.hpp:65
Zip::Entry::checkByte
std::uint8_t checkByte
Last byte of the encryption header after decryption.
Definition Zip.hpp:67
Zip::Entry::uncompressedSize
std::uint64_t uncompressedSize
Uncompressed data size.
Definition Zip.hpp:66
Zip::Entry::name
std::string name
File name.
Definition Zip.hpp:60
Zip::Entry::compression
Compression compression
Compression method.
Definition Zip.hpp:62
Zip::Entry::crc32
std::uint32_t crc32
CRC-32 checksum.
Definition Zip.hpp:63