generator: use least-significant-byte-first ctr
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
c6bf1c7f52
commit
80fcb13e8b
|
@ -38,7 +38,7 @@ void Generator::initialize_generator() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::memset(G.k, 0x00, G.k.size());
|
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");
|
fmt::print("Generator initialized\n");
|
||||||
}
|
}
|
||||||
catch (CryptoPP::Exception& e) {
|
catch (CryptoPP::Exception& e) {
|
||||||
|
@ -74,7 +74,7 @@ auto Generator::reseed(const std::string& s) -> void {
|
||||||
try {
|
try {
|
||||||
std::string a{fortuna::Util::do_sha(da_key + s)};
|
std::string a{fortuna::Util::do_sha(da_key + s)};
|
||||||
std::memmove(G.k, fortuna::Util::de_hex(a).c_str(), G.k_length);
|
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");
|
fmt::print("[i] generator: reseeded\n");
|
||||||
}
|
}
|
||||||
catch (std::exception& e) {
|
catch (std::exception& e) {
|
||||||
|
@ -97,12 +97,8 @@ auto Generator::do_crypto() -> std::string {
|
||||||
const std::string plain{"Oh, I am fortune's fool!"};
|
const std::string plain{"Oh, I am fortune's fool!"};
|
||||||
std::string cipher, encoded_c;
|
std::string cipher, encoded_c;
|
||||||
std::unique_lock<std::mutex> ul(crypt_mtx);
|
std::unique_lock<std::mutex> ul(crypt_mtx);
|
||||||
// in case we need to convert counter to string
|
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, Generator::ctr_len> ctr;
|
||||||
const std::string str_ctr{reinterpret_cast<const char*>(&G.ctr)};
|
std::memmove(ctr, G.ctr.data(), Generator::ctr_len);
|
||||||
// 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);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this->enc.SetKeyWithIV(G.k, G.k.size(), ctr);
|
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 {
|
auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
|
||||||
std::lock_guard<std::recursive_mutex> lg(mtx);
|
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()) {
|
if (!this->is_seeded()) {
|
||||||
throw std::logic_error("G.ctr == 0, generator has not been 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{""};
|
std::string r{""};
|
||||||
while (k_blocks--) {
|
while (k_blocks--) {
|
||||||
r += Generator::do_crypto();
|
r += Generator::do_crypto();
|
||||||
++G.ctr;
|
Generator::ctr_inc();
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
@ -215,11 +212,11 @@ auto Generator::ctr_inc() -> void {
|
||||||
|
|
||||||
std::atomic<uint8_t> i{0};
|
std::atomic<uint8_t> i{0};
|
||||||
while (true) {
|
while (true) {
|
||||||
this->G.counter.at(i) = static_cast<std::byte>(
|
this->G.ctr.at(i) = static_cast<std::byte>(
|
||||||
static_cast<uint8_t>(this->G.counter.at(i)) + 0x01);
|
static_cast<uint8_t>(this->G.ctr.at(i)) + 0x01);
|
||||||
|
|
||||||
if (this->G.counter.at(i) == static_cast<std::byte>(0x00) &&
|
if (this->G.ctr.at(i) == static_cast<std::byte>(0x00) &&
|
||||||
++i < this->G.counter.size()) {
|
++i < this->G.ctr.size()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
29
generator.h
29
generator.h
|
@ -40,23 +40,38 @@ public:
|
||||||
|
|
||||||
auto is_seeded() const -> bool {
|
auto is_seeded() const -> bool {
|
||||||
std::lock_guard<std::recursive_mutex> lg(mtx);
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
||||||
return !(this->G.ctr == 0x00);
|
return !(this->G.ctr == this->null_blk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
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;
|
CryptoPP::CTR_Mode<CryptoPP::Serpent>::Encryption enc;
|
||||||
|
|
||||||
struct G_state {
|
struct G_state {
|
||||||
// 32*8
|
// 32*8
|
||||||
static constexpr const std::size_t k_length{32};
|
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;
|
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, k_length> k;
|
||||||
#pragma GCC diagnostic push
|
std::array<std::byte, ctr_len> ctr;
|
||||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
|
||||||
unsigned __int128 ctr;
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
std::array<std::byte, ctr_len> counter;
|
|
||||||
};
|
};
|
||||||
G_state G;
|
G_state G;
|
||||||
|
|
||||||
|
|
Reference in New Issue