81 lines
1.9 KiB
C++
81 lines
1.9 KiB
C++
#include <cmath>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
#include <tuple>
|
|
#include "generator.h"
|
|
|
|
using namespace std;
|
|
|
|
struct G_state{
|
|
int64_t k;
|
|
unsigned __int128 ctr;
|
|
};
|
|
|
|
auto reseed(G_state G, const string& s) -> std::tuple<G_state, unsigned __int128> {
|
|
unsigned __int128 ctr;
|
|
// TODO(me): conctatenate the key with seed
|
|
G.k = do_sha(G.ctr);
|
|
// return G, will need to get just k from that
|
|
return {G, ctr};
|
|
}
|
|
|
|
auto 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 {
|
|
/* this function calls the block cipher
|
|
* returns a string of k*(16 bytes);
|
|
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> {
|
|
assert (G.ctr!=0);
|
|
string r = "";
|
|
for (int i = 0; i < k_blocks; ++i) {
|
|
r += do_crypto(G.k, G.ctr);
|
|
G.ctr += 1;
|
|
}
|
|
return {r, G};
|
|
}
|
|
|
|
/* n is number of random bytes to generate */
|
|
auto 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)));
|
|
// returning just r throws away our state, this should be reworked
|
|
// using OOP
|
|
// return r;
|
|
return {r, G};
|
|
};
|
|
|