generator: use least-significant-byte-first ctr
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-01-30 22:56:08 +01:00
parent c6bf1c7f52
commit 80fcb13e8b
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 33 additions and 21 deletions

@ -38,7 +38,7 @@ void Generator::initialize_generator() {
try {
std::memset(G.k, 0x00, G.k.size());
G.ctr = 0;
std::memset(G.ctr.begin(), 0x00, this->ctr_len);
fmt::print("Generator initialized\n");
}
catch (CryptoPP::Exception& e) {
@ -74,7 +74,7 @@ auto Generator::reseed(const std::string& s) -> void {
try {
std::string a{fortuna::Util::do_sha(da_key + s)};
std::memmove(G.k, fortuna::Util::de_hex(a).c_str(), G.k_length);
++G.ctr;
Generator::ctr_inc();
fmt::print("[i] generator: reseeded\n");
}
catch (std::exception& e) {
@ -97,12 +97,8 @@ auto Generator::do_crypto() -> std::string {
const std::string plain{"Oh, I am fortune's fool!"};
std::string cipher, encoded_c;
std::unique_lock<std::mutex> ul(crypt_mtx);
// in case we need to convert counter to string
const std::string str_ctr{reinterpret_cast<const char*>(&G.ctr)};
// 16 bytes --> 128bit
static constexpr const std::size_t ctr_length{16};
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, ctr_length> ctr;
std::memmove(ctr, str_ctr.c_str(), ctr_length);
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, Generator::ctr_len> ctr;
std::memmove(ctr, G.ctr.data(), Generator::ctr_len);
try {
this->enc.SetKeyWithIV(G.k, G.k.size(), ctr);
@ -139,7 +135,8 @@ auto Generator::do_crypto() -> std::string {
auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
std::lock_guard<std::recursive_mutex> lg(mtx);
assert((G.ctr != 0) && "Counter is not 0, generator has been seeded");
assert((G.ctr != this->null_blk) &&
"Counter is not 0, generator has been seeded");
if (!this->is_seeded()) {
throw std::logic_error("G.ctr == 0, generator has not been seeded!");
}
@ -147,7 +144,7 @@ auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
std::string r{""};
while (k_blocks--) {
r += Generator::do_crypto();
++G.ctr;
Generator::ctr_inc();
}
return r;
}
@ -215,11 +212,11 @@ auto Generator::ctr_inc() -> void {
std::atomic<uint8_t> i{0};
while (true) {
this->G.counter.at(i) = static_cast<std::byte>(
static_cast<uint8_t>(this->G.counter.at(i)) + 0x01);
this->G.ctr.at(i) = static_cast<std::byte>(
static_cast<uint8_t>(this->G.ctr.at(i)) + 0x01);
if (this->G.counter.at(i) == static_cast<std::byte>(0x00) &&
++i < this->G.counter.size()) {
if (this->G.ctr.at(i) == static_cast<std::byte>(0x00) &&
++i < this->G.ctr.size()) {
continue;
}
break;

@ -40,23 +40,38 @@ public:
auto is_seeded() const -> bool {
std::lock_guard<std::recursive_mutex> lg(mtx);
return !(this->G.ctr == 0x00);
return !(this->G.ctr == this->null_blk);
}
private:
static constexpr const std::size_t ctr_len{16};
static constexpr const std::array<std::byte, ctr_len> null_blk{
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
std::byte{0x00},
}; // used for comparison with G.ctr in is_seeded() and generate_blocks()
CryptoPP::CTR_Mode<CryptoPP::Serpent>::Encryption enc;
struct G_state {
// 32*8
static constexpr const std::size_t k_length{32};
static constexpr const std::size_t ctr_len{16};
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, k_length> k;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
unsigned __int128 ctr;
#pragma GCC diagnostic pop
std::array<std::byte, ctr_len> counter;
std::array<std::byte, ctr_len> ctr;
};
G_state G;