diff --git a/generator.cpp b/generator.cpp index 2df1ea4..232144b 100644 --- a/generator.cpp +++ b/generator.cpp @@ -5,47 +5,57 @@ #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; + k = 0; + ctr = 0; + printf("Generator initialized\n"); + delete(G); +}; + +auto Generator::get_state() -> G_state { + return G_state{}; +} + +auto Generator::reseed(G_state G, const string& s) -> G_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(G_state G, int k_blocks) -> tuple { assert (G.ctr!=0); string r = ""; for (int i = 0; i < k_blocks; ++i) { @@ -55,8 +65,7 @@ auto generate_blocks(G_state G, int k_blocks) -> tuple { return {r, G}; } -/* n is number of random bytes to generate */ -auto generate_random_data(G_state G, uint n) -> tuple { +auto Generator::generate_random_data(G_state G, uint n) -> tuple { string r = ""; if (n < 0){ /* this should not be possible */ @@ -74,12 +83,10 @@ auto generate_random_data(G_state G, uint n) -> tuple { /* 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}; }; + } // namespace generator } // namespace fortuna - +#endif diff --git a/generator.h b/generator.h index 51142b7..6288ca2 100644 --- a/generator.h +++ b/generator.h @@ -2,27 +2,38 @@ #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; +protected: + struct G_state; + auto get_state() -> Generator::G_state; -auto reseed(G_state G, const std::string& s) -> G_state; +private: + int64_t k; + unsigned __int128 ctr; -auto do_sha(int64_t key_with_seed) -> int64_t; + auto reseed(Generator::G_state G, const string& s) -> Generator::G_state; -auto generate_blocks(G_state G, int k_blocks) -> std::tuple; + auto do_sha(int64_t key_with_seed) -> int64_t; -/* returns output of 0 <= n <= 2^20 bytes */ -auto generate_random_data(G_state G, int n) -> std::tuple; + auto do_crypto(int64_t k, unsigned __int128 ctr) -> string; + + /* TODO(me): lacking objects (no more!), we have to return both the state and the string */ + auto generate_blocks(Generator::G_state G, int k_blocks) -> tuple; + + /* n is the number of random bytes to generate */ + auto generate_random_data(Generator::G_state G, uint n) -> tuple; + +}; // 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; }