From c82d3912f53befccfeb5a6b21a5736a95808fa4a Mon Sep 17 00:00:00 2001 From: surtur Date: Mon, 15 Nov 2021 23:27:17 +0100 Subject: [PATCH] generator: add CTR-mode Serpent to random_data() operate on a fixed string with randomly generated key and iv block on each run. this is neither finished nor correct implementation, it currently also calls the Decryption routine to prove that it's working properly with the input given ("Oh, I am fortune's fool!" seemed fitting for fortuna). --- fortuna.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/fortuna.cpp b/fortuna.cpp index 83f64ff..8501348 100644 --- a/fortuna.cpp +++ b/fortuna.cpp @@ -4,6 +4,16 @@ #include "fortuna.h" #include "generator.h" +#include +#include +#include +#include +#include + +#include +#include +#include + namespace fortuna { Fortuna::Fortuna(){ @@ -13,9 +23,78 @@ namespace fortuna { Fortuna::~Fortuna() = default; auto Fortuna::random_data(unsigned int n_blocks) -> void { - // TODO(me) - // will one day return random data to the user - } + // for the moment loosely based on + // https://www.cryptopp.com/wiki/CTR_Mode + using CryptoPP::StringSource; + using CryptoPP::StringSink; + using CryptoPP::HexEncoder; + using CryptoPP::StreamTransformationFilter; + using CryptoPP::Serpent; + using CryptoPP::CTR_Mode; + + CryptoPP::AutoSeededRandomPool prng; + // use 256bit key + CryptoPP::SecByteBlock key(CryptoPP::Serpent::MAX_KEYLENGTH); + + prng.GenerateBlock(key,key.size()); + + CryptoPP::byte ctr[Serpent::BLOCKSIZE]; + prng.GenerateBlock(ctr,sizeof(ctr)); + + // William Shakespeare, Romeo and Juliet + std::string plain = "Oh, I am fortune's fool!"; + std::string cipher, encoded_c, decrypted; + + try { + std::cout << "plain text: " << plain << std::endl; + + CTR_Mode::Encryption e; + e.SetKeyWithIV(key,key.size(),ctr); + + // The StreamTransformationFilter adds padding as required. ECB and + // CBC Mode must be padded to the block size of the cipher. CTR + // mode not. + // the "true" param - pump all of the data immediately to its + // attached transformation + StringSource str_src1(plain,true, + new StreamTransformationFilter(e, + new StringSink(cipher) + ) // StreamTransformationFilter + ); // StringSource + } + catch(CryptoPP::Exception& e) { + std::cerr << e.what() << std::endl; + exit(1); + } + + // Pretty print cipher text + StringSource str_src2(cipher,true, + new HexEncoder( + new StringSink(encoded_c) + ) // HexEncoder + ); // StringSource + std::cout << "cipher text: " << encoded_c << std::endl; + + try { + CTR_Mode::Decryption d; + d.SetKeyWithIV(key,key.size(),ctr); + + // The StreamTransformationFilter removes padding as required. + StringSource str_src3(cipher, true, + new StreamTransformationFilter(d, + new StringSink(decrypted) + ) // StreamTransformationFilter + ); // StringSource + + std::cout << "decrypted text: " << decrypted << std::endl; + } + catch(CryptoPP::Exception& e) { + std::cerr << e.what() << std::endl; + exit(1); + } + + + } //random_data } // namespace fortuna