From 1a0cdd1fa39d85ac808a83034b8132d999d62513 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Couturier?= Date: Tue, 20 Aug 2019 21:59:50 +0200 Subject: [PATCH] new scprng --- OneRoundIoT/NEW/scprng.cpp | 41 +++- OneRoundIoT/NEW/scprng_old.cpp | 414 +++++++++++++++++++++++++++++++++ 2 files changed, 446 insertions(+), 9 deletions(-) create mode 100644 OneRoundIoT/NEW/scprng_old.cpp diff --git a/OneRoundIoT/NEW/scprng.cpp b/OneRoundIoT/NEW/scprng.cpp index 3b2eb6b..e0cc4c4 100644 --- a/OneRoundIoT/NEW/scprng.cpp +++ b/OneRoundIoT/NEW/scprng.cpp @@ -193,7 +193,7 @@ void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) { } -void scprng(uint64_t *plain, uint64_t* cipher, int bufsize, int nb_bloc, uint64_t *Val, uchar *Sbox1, uchar *Sbox2, int *Pbox, int *Pbox2, uchar *DK, int delta) { +void scprng(uint64_t *plain, uint64_t* cipher, int bufsize, int nb_bloc, uint64_t *Val, uchar *Sbox1, uchar *Sbox2, uchar * Sbox3, uchar *Sbox4, int *Pbox, int *Pbox2, int *Pbox3, int *Pbox4, uchar *DK, int delta) { int update=0; @@ -226,13 +226,28 @@ void scprng(uint64_t *plain, uint64_t* cipher, int bufsize, int nb_bloc, uint64_ uchar *ptr=(uchar*)Val; for(int j=0;j +#include +#include +#include +#include + +typedef unsigned char uchar; + + + + +typedef uint64_t u8; + +extern "C" { + int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data); + void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height); +} +using namespace std; + + + +typedef struct sfcctx { u8 a; u8 b; u8 c; u8 count; } sfcctx; +#define rot64(x,k) (((x)<<(k))|((x)>>(64-(k)))) + +#define rotation 24 +#define right_shift 11 +#define left_shift 3 + + +static inline u8 sfc( sfcctx *x ) { + u8 tmp = x->a + x->b + x->count++; + x->a = x->b ^ (x->b >> right_shift); + x->b = x->c + (x->c << left_shift); + x->c = rot64(x->c, rotation) + tmp; + return tmp; +} + +static inline void sfcinit(sfcctx *x, uint64_t seed) { + x->a = seed; + x->b = seed; + x->c = seed; + x->count = 1; + for(int i=0;i<12;i++) + sfc(x); +} + + + +typedef struct ranctx { u8 a; u8 b; u8 c; u8 d; } ranctx; + + + +static inline u8 jsf( ranctx *x ) { + u8 e = x->a - rot64(x->b, 7); + x->a = x->b ^ rot64(x->c, 13); + x->b = x->c + rot64(x->d, 37); + x->c = x->d + e; + x->d = e + x->a; + return x->d; +} + +static inline void jsfinit(ranctx *x, uint64_t seed) { + x->a = 0xf1ea5eed, x->b = x->c = x->d = seed; +} + + + + + + + + + +inline uint64_t xorshift64( const uint64_t state) +{ + uint64_t x = state; + x^= x << 13; + x^= x >> 7; + x^= x << 17; + return x; +} + +static inline uint64_t splitmix64_stateless(uint64_t index) { + uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15)); + z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); + z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); + return z ^ (z >> 31); +} + + +struct sulong2 { + uint64_t x; + uint64_t y; +}; + +typedef struct sulong2 ulong2; + +static inline uint64_t rotl(const uint64_t x, int k) { + return (x << k) | (x >> (64 - k)); +} + +// call this one before calling xoroshiro128plus +static inline void xoroshiro128plus_seed(ulong2 *xoro,uint64_t seed) { + xoro->x = splitmix64_stateless(seed); + xoro->y = splitmix64_stateless(seed + 1); +} + +// returns random number, modifies xoroshiro128plus_s +static inline uint64_t xoroshiro128plus(ulong2 *xoro) { + const uint64_t s0 = xoro->x; + uint64_t s1 = xoro->y; + const uint64_t result = s0 + s1; + + s1 ^= s0; + xoro->x = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b + xoro->y = rotl(s1, 36); // c + + return result; +} + + + + + + +double TimeStart() +{ + struct timeval tstart; + gettimeofday(&tstart,0); + return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) ); +} + +double TimeStop(double t) +{ + struct timeval tend; + + gettimeofday(&tend,0); + t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t; + return (t); +} + + +uint xorshift32(const uint t) +{ + /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ + uint x = t; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return x; +} + + + + + +void rc4key(uchar *key, uchar *sc, int size_DK) { + + for(int i=0;i<256;i++) { + sc[i]=i; + } + + + uchar j0 = 0; + for(int i0=0; i0<256; i0++) { + j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF; + uchar tmp = sc[i0]; + sc[i0] = sc[j0 ]; + sc[j0] = tmp; + } +} + + +void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) { + + //sc=1:len; + + + + for (int i=0;i