All checks were successful
continuous-integration/drone/push Build is passing
* 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
92 lines
1.9 KiB
C++
92 lines
1.9 KiB
C++
#include <cmath>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
#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;
|
|
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++;
|
|
}
|
|
|
|
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(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;
|
|
}
|
|
|
|
auto Generator::generate_random_data(uint n) -> string {
|
|
G_state G = get_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 = generate_blocks(ceil(n/16));
|
|
r = rr.substr(0,n);
|
|
|
|
/* re-key */
|
|
// TODO: check conversions
|
|
G.k = stoul(generate_blocks(2));
|
|
return r;
|
|
};
|
|
|
|
|
|
} // namespace generator
|
|
} // namespace fortuna
|
|
#endif
|