--- /dev/null
+//version to test the performance of our code with message of size 16 to 240
+//in order to make test with Duc
+
+//the size of the message can be changed
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include<string.h>
+
+#include <sys/time.h>
+
+const int h=2;
+const int h2=h*h;
+
+
+
+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);
+}
+
+
+
+
+typedef uint8_t uchar;
+typedef uint8_t byte;
+//typedef unsigned long uint;
+
+uint seed=123;
+
+#define SKEY 64
+
+
+#define STATIC_KEY
+
+//#define STATIC_KEY
+//#define STATIC_KEY_256q
+//#define STATIC_KEY_128
+//#define STATIC_KEY_64
+//#define STATIC_KEY_32
+#define STATIC_KEY_64
+
+//dynamic key
+uchar DK[SKEY];
+
+#ifdef STATIC_KEY
+
+#ifdef STATIC_KEY_256
+
+uchar Nonce[256] = {
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 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7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
+};
+
+#elif defined STATIC_KEY_32
+
+uchar Nonce[32] = {
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
+};
+
+#elif defined STATIC_KEY_16
+
+uchar Nonce[16] = {
+ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
+};
+
+#endif
+
+#else
+
+uchar Nonce[SKEY];
+
+#endif
+
+int rp=1;
+
+//number of blocks
+
+
+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+=4) {
+ sc[i]=i;
+ sc[i+1]=i+1;
+ sc[i+2]=i+2;
+ sc[i+3]=i+3;
+ }
+
+ uchar j0 = 0;
+ for(int i0=0; i0<256; i0++) {
+ j0 = (j0 + sc[i0] + key[i0&(size_DK-1)] );
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0 ];
+ sc[j0] = tmp;
+ }
+}
+
+void rc4keyperm(uchar *key,int len, int rp,uchar *sc, int size_DK) {
+
+ //sc=1:len;
+
+ for (int i=0;i<len;i++) {
+ sc[i]=i;
+ }
+
+ for (int it = 0; it < rp; it++) {
+ int j0 = 1;
+ for(int i0 = 0; i0<len; i0++) {
+ j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
+ int tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ }
+ }
+}
+
+void prga(uchar *sc, int ldata, uchar *r) {
+ uchar i0=0;
+ uchar j0=0;
+
+ for (int it=0; it<ldata; it++) {
+ i0 = ((i0+1)%255);
+ j0 = (j0 + sc[i0])&0xFF;
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ r[it]=sc[(sc[i0]+sc[j0])&0xFF];
+ }
+}
+
+void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1, uchar *PboxRM, uchar *Sbox1, uchar *Sbox2,uint myrand,int enc) {
+
+ uchar X[h2];
+
+
+ int ind=0;
+
+ for(int a=0;a<h2;a+=4) {
+
+ uint mm=myrand;
+
+ X[a]=Sbox1[RM1[a]^(mm&255)]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
+ mm>>=8;
+ X[a+1]=Sbox2[RM1[a+1]^(mm&255)];
+ mm>>=8;
+ X[a+2]=Sbox1[RM1[a+2]^(mm&255)];
+ mm>>=8;
+ X[a+3]=Sbox2[RM1[a+3]^(mm&255)];
+
+
+/* uint32_t val=*((uint32_t*)&RM1[a])^mm;
+ uchar *val2=(uchar*)&val;
+ X[a]=Sbox1[val2[0]];
+ X[a+1]=Sbox2[val2[1]];
+ X[a+2]=Sbox1[val2[2]];
+ X[a+3]=Sbox2[val2[3]];
+*/
+
+
+
+ }
+
+ for(int it=0;it<len;it++) {
+
+ for(int a=0;a<h2;a+=4) {
+ myrand=xorshift32(myrand);
+ uint mm=myrand;
+ X[a]=Sbox2[X[a]^RM1[a]^(mm&255)];
+ mm>>=8;
+ X[a+1]=Sbox1[X[a+1]^RM1[a+1]^(mm&255)];
+ mm>>=8;
+ X[a+2]=Sbox2[X[a+2]^RM1[a+2]^(mm&255)];
+ mm>>=8;
+ X[a+3]=Sbox1[X[a+3]^RM1[a+3]^(mm&255)];
+
+/* uint32_t val=*((uint32_t*)&X[a])^*((uint32_t*)&RM1[a])^mm;
+ uchar *val2=(uchar*)&val;
+ X[a]=Sbox2[val2[0]];
+ X[a+1]=Sbox1[val2[1]];
+ X[a+2]=Sbox2[val2[2]];
+ X[a+3]=Sbox1[val2[3]];
+*/
+ }
+
+
+
+ for(int a=0;a<h2;a+=4) {
+ seq_out[ind+a]=X[a]^seq_in[ind+a];
+ seq_out[ind+a+1]=X[a+1]^seq_in[ind+a+1];
+ seq_out[ind+a+2]=X[a+2]^seq_in[ind+a+2];
+ seq_out[ind+a+3]=X[a+3]^seq_in[ind+a+3];
+ }
+
+ for(int a=0;a<h2;a+=4) {
+/* RM1[a]=Sbox2[RM1[PboxRM[a]]];
+ RM1[a+1]=Sbox1[RM1[PboxRM[a+1]]];
+ RM1[a+2]=Sbox2[RM1[PboxRM[a+2]]];
+ RM1[a+3]=Sbox1[RM1[PboxRM[a+3]]];
+*/
+ uchar w0=Sbox2[RM1[PboxRM[a]]];
+ uchar w1=Sbox1[RM1[PboxRM[a+1]]];
+ uchar w2=Sbox2[RM1[PboxRM[a+2]]];
+ uchar w3=Sbox1[RM1[PboxRM[a+3]]];
+
+ RM1[a]=w0;
+ RM1[a+1]=w1;
+ RM1[a+2]=w2;
+ RM1[a+3]=w3;
+
+
+
+ }
+
+ ind+=h2;
+ }
+}
+
+void printArray(byte *mes, int n) {
+ for (byte i = 0; i < n; i++) {
+ printf("%d ",mes[i]);
+ }
+ printf("\n");
+}
+
+int main (int argc, char ** argv)
+{
+
+
+ // const int h=2;
+ //const int h2=h*h;
+ int size_buf=1;
+ int nb_test=1;
+
+ for(int i=1; i<argc; i++){
+ if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
+ if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
+ }
+
+
+ size_buf=size_buf*size_buf;
+
+ int size_mesg=size_buf;
+
+
+ int len=(size_mesg+h2-1)/h2;
+
+
+ uchar sc[256];
+ uchar PboxRM[h2];
+ uchar Sbox1[256];
+ uchar Sbox2[256];
+
+ uchar RM1[h2];
+ uchar RM2[h2];
+
+ uint myrand;
+
+ byte plain[len*h2];
+ byte cipher[len*h2];
+ byte check[len*h2];
+
+
+
+ int bits=128;
+
+// delay(2000);
+
+ //initialization of the key
+ for(int i=0;i<SKEY;i+=4) {
+ seed=xorshift32(seed);
+ uint val=seed;
+ DK[i]=val&0xFF;
+ val>>=8;
+ DK[i+1]=val&0xFF;
+ val>>=8;
+ DK[i+2]=val&0xFF;
+ val>>=8;
+ DK[i+3]=val&0xFF;
+ }
+
+ // printf("TOTO\n");
+
+#ifndef STATIC_KEY
+
+ for(int i=0;i<256;i+=4) {
+ seed=xorshift32(seed);
+ uint val=seed;
+ Nonce[i]=val&0xFF;
+ val>>=8;
+ Nonce[i+1]=val&0xFF;
+ val>>=8;
+ Nonce[i+2]=val&0xFF;
+ val>>=8;
+ Nonce[i+3]=val&0xFF;
+ }
+
+#endif
+
+ //printf("DK\n");
+ for(int i=0;i<SKEY;i++) {
+ DK[i]^=Nonce[i];
+ }
+
+
+
+
+ //computation of sbox and other dynamic tables
+ rc4key(&DK[0], sc, SKEY/4);
+ prga(sc, h2, RM1);
+ rc4keyperm(&DK[SKEY/4], h2, rp, PboxRM, SKEY/4);
+ rc4key(&DK[SKEY/2], Sbox1, SKEY/4);
+ rc4key(&DK[(SKEY/4)*3], Sbox2, SKEY/4);
+
+
+
+ myrand=0;
+ for(int i=0;i<32;i++) {
+ myrand|=DK[i]&1;
+ myrand<<=1;
+ }
+
+ /* plain = malloc(max_size_mesg*sizeof(byte));
+ cipher=malloc(max_size_mesg*sizeof(byte));
+ check=malloc(max_size_mesg*sizeof(byte));
+*/
+
+ // printf("len %d\n",len);
+
+ //we keep the same message or not
+ //randomSeed(334);
+ for(int i=0;i<size_mesg;i++) {
+ seed=xorshift32(seed);
+ plain[i]=seed&0xFF;
+ }
+
+ myrand=0;
+ for(int i=0;i<32;i++) {
+ myrand|=DK[i]&1;
+ myrand<<=1;
+ }
+
+
+ //RM1 is changed in the encryption so we make a copy for the decryption
+ for(int i=0;i<h2;i++){
+ RM2[i]=RM1[i];
+ }
+
+
+ // unsigned long ms1 = micros ();
+double t=TimeStart();
+ for(int i=0;i<nb_test;i++)
+ {
+ encrypt_ctr(plain, cipher,len,RM1,PboxRM,Sbox1,Sbox2,myrand,1);
+ }
+double time_encrypt=TimeStop(t);
+ printf("%e \t",(double)size_buf*nb_test/time_encrypt);
+
+t=TimeStart();
+ for(int i=0;i<nb_test;i++)
+ {
+ encrypt_ctr(cipher, check,len,RM2,PboxRM,Sbox1,Sbox2,myrand,0);
+ }
+double time_decrypt=TimeStop(t);
+ printf("%e \t",(double)size_buf*nb_test/time_decrypt);
+
+
+ /*
+ int equal=1;
+
+ for(int i=0;i<size_mesg;i++) {
+
+ if(check[i]!=plain[i]) {
+ equal=0;
+ }
+ }
+
+ printf("CHECK %d\n",equal);
+ */
+
+
+}
+
+