This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
fortuna/generator.cpp
surtur 01eadae32f
All checks were successful
continuous-integration/drone/push Build is passing
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

93 lines
2.0 KiB
C++

#include <cmath>
#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 Generator::G_state{
int64_t k;
unsigned __int128 ctr;
};
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 Generator::do_sha(int64_t key_with_seed) -> int64_t {
/* do sha256 */
int64_t shastring = key_with_seed + 1;
return shastring;
}
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 */
k = 0;
ctr = 0;
return "";
}
auto Generator::generate_blocks(G_state G, int k_blocks) -> tuple<string, G_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};
}
auto Generator::generate_random_data(G_state G, uint n) -> tuple<string, G_state> {
string r = "";
if (n < 0){
/* this should not be possible */
printf("[*] error: n cannot be < 0\n");
throw invalid_argument("n cannot be < 0");
} else if (n > pow(2,20)){
printf("[*] error: n cannot be > 2^20\n");
throw invalid_argument("n cannot be > 2^20");
}
/* 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)));
r = rr.substr(0,n);
/* re-key */
// TODO: check conversions
G.k = stoul(std::get<0>(generate_blocks(G, 2)));
return {r, G};
};
} // namespace generator
} // namespace fortuna
#endif