bkcrack 1.7.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 "Crc32Tab.hpp"
5#include "KeystreamTab.hpp"
6#include "MultTab.hpp"
7
9class 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
static auto crc32inv(std::uint32_t crc, std::uint8_t b) -> std::uint32_t
Definition Crc32Tab.hpp:17
static auto crc32(std::uint32_t pval, std::uint8_t b) -> std::uint32_t
Definition Crc32Tab.hpp:11
auto getK() const -> std::uint8_t
Definition Keys.hpp:70
void updateBackwardPlaintext(std::uint8_t p)
Update the state backward with a plaintext byte.
Definition Keys.hpp:41
auto getZ() const -> std::uint32_t
Definition Keys.hpp:64
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(const std::string &password)
Construct keys associated to the given password.
auto getX() const -> std::uint32_t
Definition Keys.hpp:52
void updateBackward(std::uint8_t c)
Update the state backward with a ciphertext byte.
Definition Keys.hpp:33
void update(std::uint8_t p)
Update the state with a plaintext byte.
Definition Keys.hpp:22
auto getY() const -> std::uint32_t
Definition Keys.hpp:58
Keys(std::uint32_t x, std::uint32_t y, std::uint32_t z)
Construct keys from given components.
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()=default
Construct default state.
static auto getByte(std::uint32_t zi) -> std::uint8_t
Definition KeystreamTab.hpp:14
static constexpr std::uint32_t mult
Multiplicative constant used in traditional PKWARE encryption.
Definition MultTab.hpp:25
static constexpr std::uint32_t multInv
Multiplicative inverse of mult modulo 2^32.
Definition MultTab.hpp:28
constexpr auto lsb(std::uint32_t x) -> std::uint8_t
Definition types.hpp:24
constexpr auto msb(std::uint32_t x) -> std::uint8_t
Definition types.hpp:30