generator: add CTR-mode Serpent to random_data()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
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).
This commit is contained in:
parent
b934b11aa9
commit
c82d3912f5
85
fortuna.cpp
85
fortuna.cpp
@ -4,6 +4,16 @@
|
||||
#include "fortuna.h"
|
||||
#include "generator.h"
|
||||
|
||||
#include <cryptopp/osrng.h>
|
||||
#include <cryptopp/hex.h>
|
||||
#include <cryptopp/filters.h>
|
||||
#include <cryptopp/serpent.h>
|
||||
#include <cryptopp/ccm.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
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<Serpent>::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<Serpent>::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
|
||||
|
||||
|
Reference in New Issue
Block a user