generator: stop hex-encoding bytes in do_crypto()
All checks were successful
continuous-integration/drone/push Build is passing

in fortuna, print the random bytes as they come, without a newline, to
stdout. the idea is it can be directly piped to, say, dieharder, for,
e.g. testing purposes. also print info msg to stderr as is now customary
throughout the program.
This commit is contained in:
surtur 2022-02-03 02:36:55 +01:00
parent 3fe5c06c49
commit c4dcab3046
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 9 additions and 23 deletions

View File

@ -101,11 +101,8 @@ auto Fortuna::random_data(const uint64_t n_bytes) -> void {
} }
else { else {
const std::string n{R.Gen.generate_random_data(n_bytes)}; const std::string n{R.Gen.generate_random_data(n_bytes)};
fmt::print( fmt::print("{}", n); // intentionally without a newline
stderr, fmt::print(stderr, "[i] fortuna: delivered {} bytes\n", n_bytes);
"got you {} proper bytes from generate_random_data -> {}\n",
n_bytes,
n);
} }
} }
catch (std::exception& e) { catch (std::exception& e) {

View File

@ -96,7 +96,7 @@ auto Generator::do_crypto() -> std::string {
// William Shakespeare, Romeo and Juliet // William Shakespeare, Romeo and Juliet
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;
std::unique_lock<std::mutex> ul(crypt_mtx); std::unique_lock<std::mutex> ul(crypt_mtx);
CryptoPP::FixedSizeSecBlock<CryptoPP::byte, Generator::ctr_len> ctr; CryptoPP::FixedSizeSecBlock<CryptoPP::byte, Generator::ctr_len> ctr;
std::memmove(ctr, G.ctr.data(), Generator::ctr_len); std::memmove(ctr, G.ctr.data(), Generator::ctr_len);
@ -122,15 +122,9 @@ auto Generator::do_crypto() -> std::string {
throw; throw;
} }
// Pretty print cipher text // just return the bytes, hex-encode can be done at a later point or by the
CryptoPP::StringSource str_src2( // final user/in console/perhaps not at all
cipher, return cipher;
true,
new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded_c)) // HexEncoder
); // StringSource
return encoded_c;
} }
auto Generator::generate_blocks(unsigned int k_blocks) -> std::string { auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
@ -177,10 +171,8 @@ auto Generator::generate_random_data(const uint64_t& n) -> std::string {
static_cast<unsigned int>(std::ceil(static_cast<double>(n) / 16))); static_cast<unsigned int>(std::ceil(static_cast<double>(n) / 16)));
std::string rr{generate_blocks(how_many)}; std::string rr{generate_blocks(how_many)};
fmt::print(stderr, "rr (output from generate_blocks): {}\n", rr);
// since we're truncating hex, we need to get twice more characters r = rr.substr(0, n); // not hex-encoded anymore, just ask for n chars
r = rr.substr(0, n * 0x02ul);
rr.clear(); rr.clear();
} }
catch (std::exception& e) { catch (std::exception& e) {
@ -190,14 +182,11 @@ auto Generator::generate_random_data(const uint64_t& n) -> std::string {
/* re-key */ /* re-key */
try { try {
std::string nu_G_k{generate_blocks(2)}; std::string nu_G_k{generate_blocks(2)};
// fmt::print("nu_G_k: {}\n", nu_G_k); // debugging
std::string dst{fortuna::Util::de_hex(nu_G_k)};
nu_G_k.clear();
/* clear out the old key and set a new one */ /* clear out the old key and set a new one */
std::memset(G.k, 0x00, G.k_length); std::memset(G.k, 0x00, G.k_length);
std::memmove(G.k, dst.c_str(), G.k_length); std::memmove(G.k, nu_G_k.c_str(), G.k_length);
nu_G_k.clear();
} }
catch (std::exception& e) { catch (std::exception& e) {
fmt::print(stderr, "{}", e.what()); fmt::print(stderr, "{}", e.what());