Compare commits

...

3 Commits

Author SHA1 Message Date
surtur a28354d585
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
2021-11-03 02:14:35 +01:00
surtur 926216fad3
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
2021-11-03 01:48:18 +01:00
surtur 01eadae32f
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
2021-11-02 05:10:58 +01:00
3 changed files with 53 additions and 37 deletions

View File

@ -2,61 +2,69 @@
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <tuple>
#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<string, G_state> {
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<string, G_state> {
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<string, G_state> {
}
/* do magic to compute r
* r rst-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

View File

@ -2,27 +2,34 @@
#define FORTUNA_GENERATOR_H
#include <string>
#include <cstring>
#include <tuple>
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<std::string, G_state>;
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<std::string, G_state>;
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

View File

@ -1,8 +1,11 @@
#include <iostream>
#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;
}