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 f64295f052
All checks were successful
continuous-integration/drone/push Build is passing
move to oop paradigm
squashed the following:

commit a28354d585
Author: surtur <a_mirre@utb.cz>
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 926216fad3
Author: surtur <a_mirre@utb.cz>
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 01eadae32f
Author: surtur <a_mirre@utb.cz>
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
2021-11-03 02:57:23 +01:00

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