bkcrack: include/bkcrack/Keys.hpp Source File

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

Keys.hpp
1#ifndef BKCRACK_KEYS_HPP
2#define BKCRACK_KEYS_HPP
3
4#include <bkcrack/Crc32Tab.hpp>
5#include <bkcrack/KeystreamTab.hpp>
6#include <bkcrack/MultTab.hpp>
7
9 class Keys
10{
11public:
13 Keys() = default;
14
16 Keys(std::uint32_t x, std::uint32_t y, std::uint32_t z);
17
19 explicit Keys(const std::string& password);
20
22 void update(std::uint8_t p)
23 {
24 x = Crc32Tab::crc32(x, p);
25 y = (y + lsb(x)) * MultTab::mult + 1;
26 z = Crc32Tab::crc32(z, msb(y));
27 }
28
30 void update(const std::vector<std::uint8_t>& ciphertext, std::size_t current, std::size_t target);
31
33 void updateBackward(std::uint8_t c)
34 {
35 z = Crc32Tab::crc32inv(z, msb(y));
36 y = (y - 1) * MultTab::multInv - lsb(x);
37 x = Crc32Tab::crc32inv(x, c ^ getK());
38 }
39
41 void updateBackwardPlaintext(std::uint8_t p)
42 {
43 z = Crc32Tab::crc32inv(z, msb(y));
44 y = (y - 1) * MultTab::multInv - lsb(x);
45 x = Crc32Tab::crc32inv(x, p);
46 }
47
49 void updateBackward(const std::vector<std::uint8_t>& ciphertext, std::size_t current, std::size_t target);
50
52 auto getX() const -> std::uint32_t
53 {
54 return x;
55 }
56
58 auto getY() const -> std::uint32_t
59 {
60 return y;
61 }
62
64 auto getZ() const -> std::uint32_t
65 {
66 return z;
67 }
68
70 auto getK() const -> std::uint8_t
71 {
72 return KeystreamTab::getByte(z);
73 }
74
75private:
76 std::uint32_t x = 0x12345678;
77 std::uint32_t y = 0x23456789;
78 std::uint32_t z = 0x34567890;
79};
80
81#endif // BKCRACK_KEYS_HPP
Crc32Tab::crc32inv
static auto crc32inv(std::uint32_t crc, std::uint8_t b) -> std::uint32_t
Definition Crc32Tab.hpp:17
Crc32Tab::crc32
static auto crc32(std::uint32_t pval, std::uint8_t b) -> std::uint32_t
Definition Crc32Tab.hpp:11
Keys::getK
auto getK() const -> std::uint8_t
Definition Keys.hpp:70
Keys::updateBackwardPlaintext
void updateBackwardPlaintext(std::uint8_t p)
Update the state backward with a plaintext byte.
Definition Keys.hpp:41
Keys::getZ
auto getZ() const -> std::uint32_t
Definition Keys.hpp:64
Keys::update
void update(const std::vector< std::uint8_t > &ciphertext, std::size_t current, std::size_t target)
Update the state forward to a target offset.
Keys::Keys
Keys(const std::string &password)
Construct keys associated to the given password.
Keys::getX
auto getX() const -> std::uint32_t
Definition Keys.hpp:52
Keys::updateBackward
void updateBackward(std::uint8_t c)
Update the state backward with a ciphertext byte.
Definition Keys.hpp:33
Keys::update
void update(std::uint8_t p)
Update the state with a plaintext byte.
Definition Keys.hpp:22
Keys::getY
auto getY() const -> std::uint32_t
Definition Keys.hpp:58
Keys::Keys
Keys(std::uint32_t x, std::uint32_t y, std::uint32_t z)
Construct keys from given components.
Keys::updateBackward
void updateBackward(const std::vector< std::uint8_t > &ciphertext, std::size_t current, std::size_t target)
Update the state backward to a target offset.
Keys::Keys
Keys()=default
Construct default state.
KeystreamTab::getByte
static auto getByte(std::uint32_t zi) -> std::uint8_t
Definition KeystreamTab.hpp:14
MultTab::mult
static constexpr std::uint32_t mult
Multiplicative constant used in traditional PKWARE encryption.
Definition MultTab.hpp:25
MultTab::multInv
static constexpr std::uint32_t multInv
Multiplicative inverse of mult modulo 2^32.
Definition MultTab.hpp:28
lsb
constexpr auto lsb(std::uint32_t x) -> std::uint8_t
Definition types.hpp:24
msb
constexpr auto msb(std::uint32_t x) -> std::uint8_t
Definition types.hpp:30