From c0efa75c1adce9f4bfe6e36d50e57935a0d13f16 Mon Sep 17 00:00:00 2001 From: surtur Date: Tue, 26 Oct 2021 15:59:22 +0200 Subject: [PATCH] switch to using {tuples,128bit int for ctr} 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 --- generator.cpp | 25 +++++++++++++++++-------- generator.h | 7 ++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/generator.cpp b/generator.cpp index bb2d234..3ee3c3b 100644 --- a/generator.cpp +++ b/generator.cpp @@ -1,14 +1,14 @@ #include #include #include +#include #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 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 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 ← first-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}; }; diff --git a/generator.h b/generator.h index 470af3b..53304e7 100644 --- a/generator.h +++ b/generator.h @@ -3,17 +3,18 @@ #include #include +#include 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 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 generate_random_data(G_state G, int n); #endif//FORTUNA_GENERATOR_H