switch to using {tuples,128bit int for ctr}
All checks were successful
continuous-integration/drone/push Build is passing

multiple changes combined in a single commit, I know...

also, a move towards the use of OOP paradigm is imminent as we now have
to do rewrites at multiple places in the code for any minor change
This commit is contained in:
surtur 2021-10-26 15:59:22 +02:00
parent b0d4cb2468
commit c0efa75c1a
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 21 additions and 11 deletions

View File

@ -1,14 +1,14 @@
#include <cmath>
#include <cassert>
#include <stdexcept>
#include <tuple>
#include "generator.h"
using namespace std;
struct G_state{
long k;
// TODO: use __int128 for ctr eventually;
unsigned long ctr;
unsigned __int128 ctr;
};
G_state *initialize_generator(){
@ -18,7 +18,7 @@ G_state *initialize_generator(){
return G;
};
string do_crypto(long k, unsigned long ctr){
string do_crypto(long k, unsigned __int128 ctr){
/* this function calls the block cipher
do whatever atm */
k = 0;
@ -26,17 +26,19 @@ string do_crypto(long k, unsigned long ctr){
return "";
}
G_state generate_blocks(G_state G, int k_blocks){
/* lacking objects, we have to return both the state and the string */
tuple<string, G_state> generate_blocks(G_state G, int k_blocks){
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 G;
return {r, G};
}
string generate_random_data(G_state G, uint n){
/* n is number of random bytes to generate */
tuple<string, G_state> generate_random_data(G_state G, uint n){
string r = "";
if (n < 0){
/* this should not be possible */
@ -48,8 +50,15 @@ string generate_random_data(G_state G, uint n){
}
/* do magic to compute r
* r rst-n-bytes(GenerateBlocks(G, ceil(n/16) )) */
string rr = to_string(generate_blocks(G,ceil(n/16)).ctr);
string rr = std::get<0>(generate_blocks(G,ceil(n/16)));
r = rr.substr(0,n);
return r;
/* 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};
};

View File

@ -3,17 +3,18 @@
#include <string>
#include <cstring>
#include <tuple>
struct G_state;
/* initializes generator */
G_state *initialize_generator();
std::string do_crypto(long k, unsigned long ctr);
std::string do_crypto(long k, unsigned __int128 ctr);
G_state generate_blocks(G_state G, int k_blocks);
std::tuple<std::string, G_state> generate_blocks(G_state G, int k_blocks);
/* returns output of 0 <= n <= 2^20 bytes */
std::string generate_random_data(G_state G, int n);
std::tuple<std::string, G_state> generate_random_data(G_state G, int n);
#endif//FORTUNA_GENERATOR_H