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

55 lines
1.0 KiB
C++

#include <cmath>
#include <cassert>
#include <stdexcept>
#include "generator.h"
using namespace std;
struct G_state{
long k;
long c;
};
G_state *initialize_generator(){
auto G = new G_state;
G->k = 0;
G->c = 0;
return G;
};
string do_crypto(int k, int c){
/* this function calls the block cipher
do whatever atm */
k = 0;
c = 0;
return "";
}
G_state generate_blocks(G_state G, int k_blocks){
assert (G.c!=0);
string r = "";
for (int i = 0; i < k_blocks; ++i) {
r += do_crypto(G.k, G.c);
G.c += 1;
}
return G;
}
string generate_random_data(G_state G, uint n){
string r = "";
if (n < 0){
/* this should not be possible */
printf("[*] error: n cannot be < 0");
throw invalid_argument("n cannot be < 0");
} else if (n > pow(2,20)){
printf("[*] error: n cannot be > 2^20");
throw invalid_argument("n cannot be > 2^20");
}
/* do magic to compute r
* r ← first-n-bytes(GenerateBlocks(G, ceil(n/16) )) */
string rr = to_string(generate_blocks(G,ceil(n/16)).c);
r = rr.substr(0,n);
return r;
};