From f64295f052150df4e8827c6747a37bec1e6b73f1 Mon Sep 17 00:00:00 2001 From: surtur Date: Wed, 3 Nov 2021 02:57:23 +0100 Subject: [PATCH] move to oop paradigm squashed the following: commit a28354d585790a1811c66117cf49439354836ee5 Author: surtur Date: Wed Nov 3 02:14:35 2021 +0100 chore: make G_state a private structure * make G_state private * move protected section behind private * also do cleanup a little -> k and ctr won't exist as part of the generator outside of the G_state struct commit 926216fad389291757408e7c782006cbfd8ddf19 Author: surtur Date: Wed Nov 3 01:37:28 2021 +0100 chore: make methods aware of objects as a consequence we stopped returning the generator state from every function and equally stopped using it as a parameter to every generator method that handles the state in any way commit 01eadae32ff7a071fa28312093e5792be5c6c129 Author: surtur Date: Tue Nov 2 05:10:58 2021 +0100 feat: move from structural to object paradigm * declare and implement Generator class and member methods * create an instance of Generator in main * call initialize_generator() from main --- generator.cpp | 58 ++++++++++++++++++++++++++++----------------------- generator.h | 29 ++++++++++++++++---------- main.cpp | 3 +++ 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/generator.cpp b/generator.cpp index 2df1ea4..2f0b27a 100644 --- a/generator.cpp +++ b/generator.cpp @@ -2,61 +2,69 @@ #include #include #include -#include #include "generator.h" +#ifndef FORTUNA_GENERATOR_CPP +#define FORTUNA_GENERATOR_CPP namespace fortuna { namespace generator { using namespace std; -struct G_state{ +struct Generator::G_state{ int64_t k; unsigned __int128 ctr; }; -auto reseed(G_state G, const string& s) -> G_state { +void Generator::initialize_generator(){ + // Generator::G_state *G = new G_state; // -> was told to use auto by + // clang-tidy... + auto G = new Generator::G_state{}; + G->k = 0; + G->ctr = 0; + printf("Generator initialized\n"); + delete(G); +}; + +auto Generator::get_state() -> G_state { + return G_state{}; +} + +auto Generator::reseed(const string& s) -> void { + G_state G = get_state(); string to_be_hashed = std::to_string(G.k) + s; G.k = do_sha(stoul(to_be_hashed)); G.ctr++; - return G; } -auto do_sha(int64_t key_with_seed) -> int64_t { +auto Generator::do_sha(int64_t key_with_seed) -> int64_t { /* do sha256 */ int64_t shastring = key_with_seed + 1; return shastring; } -G_state *initialize_generator(){ - auto G = new G_state; - G->k = 0; - G->ctr = 0; - return G; -}; - -auto do_crypto(int64_t k, unsigned __int128 ctr) -> string { +auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string { /* this function calls the block cipher * returns a string of k*(16 bytes); - do whatever atm */ + * do whatever atm */ k = 0; ctr = 0; return ""; } -/* lacking objects, we have to return both the state and the string */ -auto generate_blocks(G_state G, int k_blocks) -> tuple { +auto Generator::generate_blocks(int k_blocks) -> string { + G_state G = get_state(); assert (G.ctr!=0); string r = ""; for (int i = 0; i < k_blocks; ++i) { r += do_crypto(G.k, G.ctr); G.ctr++; } - return {r, G}; + return r; } -/* n is number of random bytes to generate */ -auto generate_random_data(G_state G, uint n) -> tuple { +auto Generator::generate_random_data(uint n) -> string { + G_state G = get_state(); string r = ""; if (n < 0){ /* this should not be possible */ @@ -68,18 +76,16 @@ auto generate_random_data(G_state G, uint n) -> tuple { } /* do magic to compute r * r ← first-n-bytes(GenerateBlocks(G, ceil(n/16) )) */ - string rr = std::get<0>(generate_blocks(G,ceil(n/16))); + string rr = generate_blocks(ceil(n/16)); r = rr.substr(0,n); /* re-key */ // TODO: check conversions - G.k = stoul(std::get<0>(generate_blocks(G, 2))); - // returning just r throws away our state, this should be reworked - // using OOP - // return r; - return {r, G}; + G.k = stoul(generate_blocks(2)); + return r; }; + } // namespace generator } // namespace fortuna - +#endif diff --git a/generator.h b/generator.h index 51142b7..0d6ede6 100644 --- a/generator.h +++ b/generator.h @@ -2,27 +2,34 @@ #define FORTUNA_GENERATOR_H #include -#include -#include namespace fortuna { namespace generator { -struct G_state; +using namespace std; -/* initializes generator */ -G_state *initialize_generator(); +class Generator { +public: + void initialize_generator(); -auto do_crypto(int64_t k, unsigned __int128 ctr) -> std::string; +private: + struct G_state; -auto reseed(G_state G, const std::string& s) -> G_state; + auto reseed(const string& s) -> void; -auto do_sha(int64_t key_with_seed) -> int64_t; + auto do_sha(int64_t key_with_seed) -> int64_t; -auto generate_blocks(G_state G, int k_blocks) -> std::tuple; + auto do_crypto(int64_t k, unsigned __int128 ctr) -> string; -/* returns output of 0 <= n <= 2^20 bytes */ -auto generate_random_data(G_state G, int n) -> std::tuple; + auto generate_blocks(int k_blocks) -> string; + + /* n is the number of random bytes to generate */ + auto generate_random_data(uint n) -> string; + +protected: + auto get_state() -> Generator::G_state; + +}; // class generator } //namespace generator } //namespace fortuna diff --git a/main.cpp b/main.cpp index 88dd5d5..862c048 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,11 @@ #include +#include "generator.h" int main() { using std::cout; using std::endl; cout << "[*] doing evil stuff" << endl; + fortuna::generator::Generator g{}; + g.initialize_generator(); return 0; }