From 80fcb13e8b545447f6a4da2adc4714d5a5badbff Mon Sep 17 00:00:00 2001 From: surtur Date: Sun, 30 Jan 2022 22:56:08 +0100 Subject: [PATCH] generator: use least-significant-byte-first ctr --- generator.cpp | 25 +++++++++++-------------- generator.h | 29 ++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/generator.cpp b/generator.cpp index a9d774c..997349a 100644 --- a/generator.cpp +++ b/generator.cpp @@ -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 ul(crypt_mtx); - // in case we need to convert counter to string - const std::string str_ctr{reinterpret_cast(&G.ctr)}; - // 16 bytes --> 128bit - static constexpr const std::size_t ctr_length{16}; - CryptoPP::FixedSizeSecBlock ctr; - std::memmove(ctr, str_ctr.c_str(), ctr_length); + CryptoPP::FixedSizeSecBlock 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 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 i{0}; while (true) { - this->G.counter.at(i) = static_cast( - static_cast(this->G.counter.at(i)) + 0x01); + this->G.ctr.at(i) = static_cast( + static_cast(this->G.ctr.at(i)) + 0x01); - if (this->G.counter.at(i) == static_cast(0x00) && - ++i < this->G.counter.size()) { + if (this->G.ctr.at(i) == static_cast(0x00) && + ++i < this->G.ctr.size()) { continue; } break; diff --git a/generator.h b/generator.h index 545f754..81b3bd9 100644 --- a/generator.h +++ b/generator.h @@ -40,23 +40,38 @@ public: auto is_seeded() const -> bool { std::lock_guard 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 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::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 k; -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" - unsigned __int128 ctr; -#pragma GCC diagnostic pop - std::array counter; + std::array ctr; }; G_state G;