X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/blobdiff_plain/3935cf9ac06b7bc328ee3d942edb5f60130d5b74..6c6f5dc1f56346adeb48cc669b13c49b508fbd9d:/Arduino/sketch_One_Round/sketch_One_Round.ino diff --git a/Arduino/sketch_One_Round/sketch_One_Round.ino b/Arduino/sketch_One_Round/sketch_One_Round.ino index 014b85b..69ec9c2 100644 --- a/Arduino/sketch_One_Round/sketch_One_Round.ino +++ b/Arduino/sketch_One_Round/sketch_One_Round.ino @@ -1,11 +1,11 @@ -#include <AES.h> + #include <AES.h> //#include "./printf.h" //#include <ESP8266WiFi.h> -const int size_mesg=256; +const int size_mesg=16; @@ -13,7 +13,7 @@ const int size_mesg=256; const int sleepTimeS = 10; -const int h=4; +const int h=2; const int h2=h*h; byte DK[64]; int rp=1; @@ -23,6 +23,7 @@ const int len=size_mesg/h2; typedef byte uchar; +typedef unsigned int uint; int Pbox[len]; int PboxRM[h2]; @@ -30,13 +31,42 @@ int Pbox[len]; uchar Sbox2[256]; uchar RM1_cpy[h2]; + +struct ulong2{ + unsigned long int x, y; +}; + +typedef struct ulong2 ulong2; +typedef unsigned long int ulong; +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; +} +uint pcg32_random_r(ulong2* rng) +{ + // pcg32_random_t *rng=(pcg32_random_t*)rng2; + ulong oldstate = rng->x; + // Advance internal state + rng->x = oldstate * 6364136223846793005ULL + (rng->y|1); + // Calculate output function (XSH RR), uses old state for max ILP + uint xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u; + uint rot = oldstate >> 59u; + return (xorshifted >> rot) | (xorshifted << ((-rot) & 31)); +} + + void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) { @@ -122,7 +152,7 @@ void encrypt_ctr(const uchar* seq_in, uchar *seq_out, int len,uchar* RM1,const i for(int a=0;a<h2;a++) { - X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a] + X[a]=Sbox1[a]; //Warning according to the size of h2, we can be outsize of Sbox1[a] } @@ -239,12 +269,16 @@ void setup () printf("\n===testng mode\n") ; + + + + uchar sc[256]; uchar RM1[h2]; uchar RM2[h2]; - randomSeed(134); + randomSeed(14); for(byte i=0;i<64;i++) { DK[i]=random(255); @@ -294,6 +328,23 @@ void loop () void prekey (int bits) { +unsigned int res=21; + unsigned long msms = micros (); + ulong2 pcg; + pcg.x=190812; + pcg.y=20911; + for(int i=0;i<100;i++) { + res=random(255); + //res=xorshift32(res); + //res=pcg32_random_r(&pcg); + + } + unsigned long res2=micros() - msms; + Serial.print("rand"); + Serial.println(res2); + Serial.println(res); + + byte plain[size_mesg]; byte cipher [size_mesg] ; @@ -313,7 +364,11 @@ void prekey (int bits) } - + printArray(RM1,16); + printArray(Sbox1,16); + printArray(Sbox2,16); + //printArray(Pbox,16); + delay(100); unsigned long ms1 = micros (); encrypt_ctr(plain, cipher,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1); @@ -326,11 +381,11 @@ void prekey (int bits) Serial.println(micros() - ms1); printf("\n\nPLAIN :"); - printArray(plain,16*2); + printArray(plain,16); printf("\nCIPHER:"); - printArray(cipher,16*2); + printArray(cipher,16); printf("\nCHECK :"); - printArray(check,16*2); + printArray(check,16); bool equal=true; for(int i=0;i<size_mesg;i++) { @@ -349,3 +404,4 @@ void prekey_test () prekey (128) ; } +