X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/blobdiff_plain/aab4f66d038fc5892e7ff9fb280f8c6d0c858b66..149268e31dcc62cf18eddd1b8ffa37cf81acbbca:/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp diff --git a/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp b/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp index b34f756..cefdb77 100644 --- a/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp +++ b/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp @@ -38,7 +38,7 @@ int ecbprng=0; - +typedef __uint64_t mylong; typedef unsigned char uchar; @@ -74,12 +74,12 @@ uint xorshift32(const uint t) } -ulong xorseed; +mylong xorseed; -ulong xorshift64() +mylong xorshift64() { /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ - ulong x = xorseed; + mylong x = xorseed; x ^= x >> 12; // a x ^= x << 25; // b x ^= x >> 27; // c @@ -191,17 +191,17 @@ void prga(uchar *sc, int ldata, uchar *r) { template<int h> -void encrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, ulong myrand, int debug) { +void encrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar* IV,mylong myrand, int debug) { uchar X[h]; uchar Y[h]; uchar fX[h]; uchar gY[h]; - uchar *RM1=&RM[0]; - uchar *RM2=&RM[h]; + uchar RM1[h]; + uchar RM2[h]; uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; + mylong *rm1=(mylong*)RM1; + mylong *rm2=(mylong*)RM2; @@ -319,7 +319,7 @@ void encrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbo template<int h> -void decrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, ulong myrand, int debug) { +void decrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar* IV,mylong myrand, int debug) { uchar invfX[h]; uchar invgY[h]; @@ -328,8 +328,8 @@ void decrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pb uchar RM1[h]; uchar RM2[h]; uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; + mylong *rm1=(mylong*)RM1; + mylong *rm2=(mylong*)RM2; for(int it=0;it<len/2;it++) { @@ -446,30 +446,49 @@ void decrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pb template<int h> -void encrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, ulong myrand, int debug) { +void encrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *IV, int debug) { uchar X[h]; uchar Y[h]; uchar fX[h]; uchar gY[h]; - uchar RM1[h]; - uchar RM2[h]; + uchar IV1[h]; + uchar IV2[h]; + uchar *RM1; + uchar *RM2; uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; + + + + for(int a=0;a<h;a+=4) { + IV1[a]=IV[a]; + IV1[a+1]=IV[a+1]; + IV1[a+2]=IV[a+2]; + IV1[a+3]=IV[a+3]; + } + + + for(int a=0;a<h;a+=4) { + IV2[a]=IV[h+a]; + IV2[a+1]=IV[h+a+1]; + IV2[a+2]=IV[h+a+2]; + IV2[a+3]=IV[h+a+3]; + + } + + + + + for(int it=0;it<len/2;it++) { int ind1=Pbox[it]*h; int ind2=Pbox[it+len/2]*h; - - for(int a=0;a<(h>>3);a++) { - myrand=xorshift64(); - rm1[a]=myrand; - myrand=xorshift64(); - rm2[a]=myrand; - } + RM1=&RM[PboxSRM[it]*h]; + RM2=&RM[h*h+PboxSRM[len/2-it-1]*h]; + for(int a=0;a<h;a+=4) { @@ -572,29 +591,45 @@ void encrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, template<int h> -void decrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, ulong myrand, int debug) { +void decrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *IV, int debug) { uchar invfX[h]; uchar invgY[h]; uchar fX[h]; uchar gY[h]; - uchar RM1[h]; - uchar RM2[h]; + uchar IV1[h]; + uchar IV2[h]; + uchar *RM1; + uchar *RM2; uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; + + + + for(int a=0;a<h;a+=4) { + IV1[a]=IV[a]; + IV1[a+1]=IV[a+1]; + IV1[a+2]=IV[a+2]; + IV1[a+3]=IV[a+3]; + } + + + for(int a=0;a<h;a+=4) { + IV2[a]=IV[h+a]; + IV2[a+1]=IV[h+a+1]; + IV2[a+2]=IV[h+a+2]; + IV2[a+3]=IV[h+a+3]; + + } + + for(int it=0;it<len/2;it++) { int ind1=Pbox[it]*h; int ind2=Pbox[it+len/2]*h; - for(int a=0;a<(h>>3);a++) { - myrand=xorshift64(); - rm1[a]=myrand; - myrand=xorshift64(); - rm2[a]=myrand; - } + RM1=&RM[PboxSRM[it]*h]; + RM2=&RM[h*h+PboxSRM[len/2-it-1]*h]; @@ -694,170 +729,14 @@ void decrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox -/* - -template<int h> -void encrypt_ecb(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) { - - uchar X[h]; - uchar Y[h]; - uchar fX[h]; - uchar gY[h]; - uchar *RM1; - uchar *RM2; - - for(int it=0;it<len/2;it++) { - int ind1=Pbox[it]*h; - int ind2=Pbox[it+len/2]*h; - - - - RM1=&RM[PboxSRM[it]*h]; - RM2=&RM[h*h+PboxSRM[it]*h]; - - - for(int a=0;a<h;a+=4) { - X[a]=seq_in[ind2+a]; - X[a+1]=seq_in[ind2+a+1]; - X[a+2]=seq_in[ind2+a+2]; - X[a+3]=seq_in[ind2+a+3]; - } - - for(int a=0;a<h;a+=4) { - Y[a]=seq_in[ind1+a]; - Y[a+1]=seq_in[ind1+a+1]; - Y[a+2]=seq_in[ind1+a+2]; - Y[a+3]=seq_in[ind1+a+3]; - } - - - for(int a=0;a<h;a+=4) { - fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]]; - fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]]; - fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]]; - fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]]; - } - - for(int a=0;a<h;a+=4) { - gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]]; - gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]]; - gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]]; - gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]]; - - } - - for(int a=0;a<h;a+=4) { - seq_out[ind2+a]=gY[a]; - seq_out[ind2+a+1]=gY[a+1]; - seq_out[ind2+a+2]=gY[a+2]; - seq_out[ind2+a+3]=gY[a+3]; - } - - for(int a=0;a<h;a+=4) { - seq_out[ind1+a]=fX[a]; - seq_out[ind1+a+1]=fX[a+1]; - seq_out[ind1+a+2]=fX[a+2]; - seq_out[ind1+a+3]=fX[a+3]; - } - - - - } - - - - -} - - - - - - - - -template<int h> -void decrypt_ecb(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) { - - uchar invfX[h]; - uchar invgY[h]; - uchar fX[h]; - uchar gY[h]; - uchar *RM1; - uchar *RM2; - - for(int it=0;it<len/2;it++) { - int ind1=Pbox[it]*h; - int ind2=Pbox[it+len/2]*h; - - - RM1=&RM[PboxSRM[it]*h]; - RM2=&RM[h*h+PboxSRM[it]*h]; - - - for(int a=0;a<h;a+=4) { - gY[a]=seq_in[ind2+a]; - gY[a+1]=seq_in[ind2+a+1]; - gY[a+2]=seq_in[ind2+a+2]; - gY[a+3]=seq_in[ind2+a+3]; - } - - for(int a=0;a<h;a+=4) { - fX[a]=seq_in[ind1+a]; - fX[a+1]=seq_in[ind1+a+1]; - fX[a+2]=seq_in[ind1+a+2]; - fX[a+3]=seq_in[ind1+a+3]; - } - - for(int a=0;a<h;a+=4) { - invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a]; - invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1]; - invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2]; - invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3]; - } - - - - for(int a=0;a<h;a+=4) { - invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a]; - invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1]; - invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2]; - invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3]; - - } - - - for(int a=0;a<h;a+=4) { - seq_out[ind2+a]=invfX[a]; - seq_out[ind2+a+1]=invfX[a+1]; - seq_out[ind2+a+2]=invfX[a+2]; - seq_out[ind2+a+3]=invfX[a+3]; - } - - for(int a=0;a<h;a+=4) { - seq_out[ind1+a]=invgY[a]; - seq_out[ind1+a+1]=invgY[a+1]; - seq_out[ind1+a+2]=invgY[a+2]; - seq_out[ind1+a+3]=invgY[a+3]; - } - - - - } - - - - -} -*/ template<int h> -void encrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) { +void encrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *IV,uint myrand, int debug) { uchar X[h]; uchar Y[h]; @@ -865,324 +744,14 @@ void encrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbo uchar gY[h]; uchar IV1[h]; uchar IV2[h]; - uchar *RM1=&RM[0]; - uchar *RM2=&RM[h]; - uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; - - - - - for(int it=0;it<len/2;it++) { - int ind1=Pbox[it]*h; - int ind2=Pbox[it+len/2]*h; - - - for(int a=0;a<(h>>3);a++) { - myrand=xorshift64(); - rm1[a]=myrand; - myrand=xorshift64(); - rm2[a]=myrand; - } - - - - for(int a=0;a<h;a+=4) { - X[a]=seq_in[ind2+a]; - X[a+1]=seq_in[ind2+a+1]; - X[a+2]=seq_in[ind2+a+2]; - X[a+3]=seq_in[ind2+a+3]; - } - - for(int a=0;a<h;a+=4) { - Y[a]=seq_in[ind1+a]; - Y[a+1]=seq_in[ind1+a+1]; - Y[a+2]=seq_in[ind1+a+2]; - Y[a+3]=seq_in[ind1+a+3]; - } - - - for(int a=0;a<h;a+=4) { - tmp[a]=X[a]^RM1[a]^IV1[a]; - tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1]; - tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2]; - tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3]; - } - - for(int a=0;a<h;a+=4) { - tmp[a]=Sbox1[tmp[a]]; - tmp[a+1]=Sbox1[tmp[a+1]]; - tmp[a+2]=Sbox1[tmp[a+2]]; - tmp[a+3]=Sbox1[tmp[a+3]]; - } - - - /*for(int a=0;a<h;a+=4) { - tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]]; - tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]; - tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]; - tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]; - }*/ - - for(int a=0;a<h;a+=4) { - fX[a]=Sbox2[tmp[a]^Y[a]]; - fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]]; - fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]]; - fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]]; - } - - /* - for(int a=0;a<h;a+=4) { - fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]]; - fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]]; - fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]]; - fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]]; - }*/ - - - for(int a=0;a<h;a+=4) { - tmp[a]=fX[a]^Y[a]^IV2[a]; - tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1]; - tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2]; - tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3]; - - } - - for(int a=0;a<h;a+=4) { - tmp[a]=Sbox2[tmp[a]]; - tmp[a+1]=Sbox2[tmp[a+1]]; - tmp[a+2]=Sbox2[tmp[a+2]]; - tmp[a+3]=Sbox2[tmp[a+3]]; - - } - - /* - for(int a=0;a<h;a+=4) { - tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]]; - tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]; - tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]; - tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]; - - } - */ - - for(int a=0;a<h;a+=4) { - gY[a]=Sbox1[tmp[a]^RM2[a]]; - gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]]; - gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]]; - gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]]; - - } - - - - /* - for(int a=0;a<h;a+=4) { - gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]]; - gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]]; - gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]]; - gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]]; - - } - */ - - for(int a=0;a<h;a+=4) { - seq_out[ind2+a]=gY[a]; - seq_out[ind2+a+1]=gY[a+1]; - seq_out[ind2+a+2]=gY[a+2]; - seq_out[ind2+a+3]=gY[a+3]; - } - - for(int a=0;a<h;a+=4) { - seq_out[ind1+a]=fX[a]; - seq_out[ind1+a+1]=fX[a+1]; - seq_out[ind1+a+2]=fX[a+2]; - seq_out[ind1+a+3]=fX[a+3]; - } - for(int a=0;a<h;a+=4) { - IV1[a]=fX[a]; - IV1[a+1]=fX[a+1]; - IV1[a+2]=fX[a+2]; - IV1[a+3]=fX[a+3]; - } - - for(int a=0;a<h;a+=4) { - IV2[a]=gY[a]; - IV2[a+1]=gY[a+1]; - IV2[a+2]=gY[a+2]; - IV2[a+3]=gY[a+3]; - } - - } - - - - -} - - - - - - - - -template<int h> -void decrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) { - - uchar invfX[h]; - uchar invgY[h]; - uchar fX[h]; - uchar gY[h]; - uchar IV1[h]; - uchar IV2[h]; - uchar *RM1=&RM[0]; - uchar *RM2=&RM[h]; -// uchar RM1[h]; -// uchar RM2[h]; - uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; - - for(int it=0;it<len/2;it++) { - int ind1=Pbox[it]*h; - int ind2=Pbox[it+len/2]*h; - - - for(int a=0;a<(h>>3);a++) { - myrand=xorshift64(); - rm1[a]=myrand; - myrand=xorshift64(); - rm2[a]=myrand; - } - - for(int a=0;a<h;a+=4) { - gY[a]=seq_in[ind2+a]; - gY[a+1]=seq_in[ind2+a+1]; - gY[a+2]=seq_in[ind2+a+2]; - gY[a+3]=seq_in[ind2+a+3]; - } - - for(int a=0;a<h;a+=4) { - fX[a]=seq_in[ind1+a]; - fX[a+1]=seq_in[ind1+a+1]; - fX[a+2]=seq_in[ind1+a+2]; - fX[a+3]=seq_in[ind1+a+3]; - } - - - for(int a=0;a<h;a+=4) { - tmp[a]=Inv_Sbox1[gY[a]]^RM2[a]; - tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1]; - tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2]; - tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3]; - } - - - for(int a=0;a<h;a+=4) { - tmp[a]=Inv_Sbox2[tmp[a]]; - tmp[a+1]=Inv_Sbox2[tmp[a+1]]; - tmp[a+2]=Inv_Sbox2[tmp[a+2]]; - tmp[a+3]=Inv_Sbox2[tmp[a+3]]; - } - - - - for(int a=0;a<h;a+=4) { - invgY[a]=tmp[a]^fX[a]^IV2[a]; - invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1]; - invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2]; - invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3]; - } - - - for(int a=0;a<h;a+=4) { - tmp[a]=Inv_Sbox2[fX[a]]^invgY[a]; - tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1]; - tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2]; - tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3]; - - } - - - for(int a=0;a<h;a+=4) { - tmp[a]=Inv_Sbox1[tmp[a]]; - tmp[a+1]=Inv_Sbox1[tmp[a+1]]; - tmp[a+2]=Inv_Sbox1[tmp[a+2]]; - tmp[a+3]=Inv_Sbox1[tmp[a+3]]; - - } - - - - - for(int a=0;a<h;a+=4) { - invfX[a]=tmp[a]^RM1[a]^IV1[a]; - invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1]; - invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2]; - invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3]; - - } - - - for(int a=0;a<h;a+=4) { - seq_out[ind2+a]=invfX[a]; - seq_out[ind2+a+1]=invfX[a+1]; - seq_out[ind2+a+2]=invfX[a+2]; - seq_out[ind2+a+3]=invfX[a+3]; - } - - for(int a=0;a<h;a+=4) { - seq_out[ind1+a]=invgY[a]; - seq_out[ind1+a+1]=invgY[a+1]; - seq_out[ind1+a+2]=invgY[a+2]; - seq_out[ind1+a+3]=invgY[a+3]; - } - for(int a=0;a<h;a+=4) { - IV1[a]=fX[a]; - IV1[a+1]=fX[a+1]; - IV1[a+2]=fX[a+2]; - IV1[a+3]=fX[a+3]; - } - - for(int a=0;a<h;a+=4) { - IV2[a]=gY[a]; - IV2[a+1]=gY[a+1]; - IV2[a+2]=gY[a+2]; - IV2[a+3]=gY[a+3]; - } - - - } - - - - -} - - - - - - - - -template<int h> -void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *IV, int debug) { - - uchar X[h]; - uchar Y[h]; - uchar fX[h]; - uchar gY[h]; - uchar IV1[h]; - uchar IV2[h]; - uchar *RM1; - uchar *RM2; + uchar RM1[h]; + uchar RM2[h]; uchar tmp[h]; + mylong *rm1=(mylong*)RM1; + mylong *rm2=(mylong*)RM2; - + for(int a=0;a<h;a+=4) { IV1[a]=IV[a]; IV1[a+1]=IV[a+1]; @@ -1199,14 +768,21 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, } + for(int it=0;it<len/2;it++) { int ind1=Pbox[it]*h; int ind2=Pbox[it+len/2]*h; - RM1=&RM[PboxSRM[it]*h]; - RM2=&RM[h*h+PboxSRM[len/2-it]*h]; - + for(int a=0;a<(h>>3);a++) { + myrand=xorshift64(); + rm1[a]=myrand; + myrand=xorshift64(); + rm2[a]=myrand; + } + + + for(int a=0;a<h;a+=4) { X[a]=seq_in[ind2+a]; X[a+1]=seq_in[ind2+a+1]; @@ -1237,7 +813,7 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, } - /*for(int a=0;a<h;a+=4) { + /* for(int a=0;a<h;a+=4) { tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]]; tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]; tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]; @@ -1249,16 +825,16 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]]; fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]]; fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]]; - } + } - /* - for(int a=0;a<h;a+=4) { + + /*for(int a=0;a<h;a+=4) { fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]]; fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]]; fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]]; fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]]; - }*/ - + } + */ for(int a=0;a<h;a+=4) { tmp[a]=fX[a]^Y[a]^IV2[a]; @@ -1276,7 +852,7 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, } - /* + /* for(int a=0;a<h;a+=4) { tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]]; tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]; @@ -1284,7 +860,7 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]; } - */ + */ for(int a=0;a<h;a+=4) { gY[a]=Sbox1[tmp[a]^RM2[a]]; @@ -1296,7 +872,7 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, - /* +/* for(int a=0;a<h;a+=4) { gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]]; gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]]; @@ -1304,7 +880,7 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]]; } - */ +*/ for(int a=0;a<h;a+=4) { seq_out[ind2+a]=gY[a]; @@ -1348,19 +924,22 @@ void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, template<int h> -void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *IV, int debug) { +void decrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar* IV, uint myrand, int debug) { uchar invfX[h]; uchar invgY[h]; uchar fX[h]; uchar gY[h]; + uchar RM1[h]; + uchar RM2[h]; uchar IV1[h]; uchar IV2[h]; - uchar *RM1; - uchar *RM2; uchar tmp[h]; + mylong *rm1=(mylong*)RM1; + mylong *rm2=(mylong*)RM2; + for(int a=0;a<h;a+=4) { IV1[a]=IV[a]; IV1[a+1]=IV[a+1]; @@ -1376,17 +955,19 @@ void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox IV2[a+3]=IV[h+a+3]; } + for(int it=0;it<len/2;it++) { int ind1=Pbox[it]*h; int ind2=Pbox[it+len/2]*h; - - RM1=&RM[PboxSRM[it]*h]; - RM2=&RM[h*h+PboxSRM[len/2-it]*h]; - - - + + for(int a=0;a<(h>>3);a++) { + myrand=xorshift64(); + rm1[a]=myrand; + myrand=xorshift64(); + rm2[a]=myrand; + } for(int a=0;a<h;a+=4) { gY[a]=seq_in[ind2+a]; @@ -1428,6 +1009,16 @@ void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox } +/* + for(int a=0;a<h;a+=4) { + invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a]^IV2[a]; + invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1]^IV2[a+1]; + invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2]^IV2[a+2]; + invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3]^IV2[a+3]; + } +*/ + + for(int a=0;a<h;a+=4) { tmp[a]=Inv_Sbox2[fX[a]]^invgY[a]; tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1]; @@ -1456,6 +1047,18 @@ void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox } +/* + for(int a=0;a<h;a+=4) { + invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a]^IV1[a]; + invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1]^IV1[a+1]; + invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2]^IV1[a+2]; + invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3]^IV1[a+3]; + + } + +*/ + + for(int a=0;a<h;a+=4) { seq_out[ind2+a]=invfX[a]; @@ -1496,10 +1099,11 @@ void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox -/* + + template<int h> -void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) { +void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *IV, int debug) { uchar X[h]; uchar Y[h]; @@ -1509,49 +1113,39 @@ void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, in uchar IV2[h]; uchar *RM1; uchar *RM2; - - int h2=h*h; + uchar tmp[h]; - for(int a=0;a<h;a+=4) { - myrand=xorshift32(myrand); - uint mm=myrand; - IV1[a]=(mm&255); - mm>>=8; - IV1[a+1]=(mm&255); - mm>>=8; - IV1[a+2]=(mm&255); - mm>>=8; - IV1[a+3]=(mm&255); + IV1[a]=IV[a]; + IV1[a+1]=IV[a+1]; + IV1[a+2]=IV[a+2]; + IV1[a+3]=IV[a+3]; } + for(int a=0;a<h;a+=4) { - myrand=xorshift32(myrand); - uint mm=myrand; - IV2[a]=(mm&255); - mm>>=8; - IV2[a+1]=(mm&255); - mm>>=8; - IV2[a+2]=(mm&255); - mm>>=8; - IV2[a+3]=(mm&255); + IV2[a]=IV[h+a]; + IV2[a+1]=IV[h+a+1]; + IV2[a+2]=IV[h+a+2]; + IV2[a+3]=IV[h+a+3]; } - + for(int it=0;it<len/2;it++) { int ind1=Pbox[it]*h; int ind2=Pbox[it+len/2]*h; + RM1=&RM[PboxSRM[it]*h]; + RM2=&RM[h*h+PboxSRM[len/2-it-1]*h]; - RM1=&RM[PboxSRM[it]*h]; - RM2=&RM[h*h+PboxSRM[it]*h]; + for(int a=0;a<h;a+=4) { X[a]=seq_in[ind2+a]; X[a+1]=seq_in[ind2+a+1]; @@ -1567,21 +1161,92 @@ void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, in } + for(int a=0;a<h;a+=4) { + tmp[a]=X[a]^RM1[a]^IV1[a]; + tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1]; + tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2]; + tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3]; + } + + for(int a=0;a<h;a+=4) { + tmp[a]=Sbox1[tmp[a]]; + tmp[a+1]=Sbox1[tmp[a+1]]; + tmp[a+2]=Sbox1[tmp[a+2]]; + tmp[a+3]=Sbox1[tmp[a+3]]; + } + + + /*for(int a=0;a<h;a+=4) { + tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]]; + tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]; + tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]; + tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]; + }*/ + + for(int a=0;a<h;a+=4) { + fX[a]=Sbox2[tmp[a]^Y[a]]; + fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]]; + fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]]; + fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]]; + } + + /* for(int a=0;a<h;a+=4) { fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]]; fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]]; fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]]; fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]]; - } + }*/ + + + for(int a=0;a<h;a+=4) { + tmp[a]=fX[a]^Y[a]^IV2[a]; + tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1]; + tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2]; + tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3]; + + } + + for(int a=0;a<h;a+=4) { + tmp[a]=Sbox2[tmp[a]]; + tmp[a+1]=Sbox2[tmp[a+1]]; + tmp[a+2]=Sbox2[tmp[a+2]]; + tmp[a+3]=Sbox2[tmp[a+3]]; + + } + + /* + for(int a=0;a<h;a+=4) { + tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]]; + tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]; + tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]; + tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]; + + } + */ + for(int a=0;a<h;a+=4) { + gY[a]=Sbox1[tmp[a]^RM2[a]]; + gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]]; + gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]]; + gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]]; + + } + + + + /* for(int a=0;a<h;a+=4) { gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]]; gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]]; gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]]; gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]]; - } + } + */ + + for(int a=0;a<h;a+=4) { seq_out[ind2+a]=gY[a]; seq_out[ind2+a+1]=gY[a+1]; @@ -1595,6 +1260,7 @@ void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, in seq_out[ind1+a+2]=fX[a+2]; seq_out[ind1+a+3]=fX[a+3]; } + for(int a=0;a<h;a+=4) { IV1[a]=fX[a]; IV1[a+1]=fX[a+1]; @@ -1624,7 +1290,7 @@ void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, in template<int h> -void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) { +void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *IV, int debug) { uchar invfX[h]; uchar invgY[h]; @@ -1634,36 +1300,24 @@ void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, i uchar IV2[h]; uchar *RM1; uchar *RM2; + uchar tmp[h]; - for(int a=0;a<h;a+=4) { - myrand=xorshift32(myrand); - uint mm=myrand; - IV1[a]=(mm&255); - mm>>=8; - IV1[a+1]=(mm&255); - mm>>=8; - IV1[a+2]=(mm&255); - mm>>=8; - IV1[a+3]=(mm&255); - } for(int a=0;a<h;a+=4) { - myrand=xorshift32(myrand); - uint mm=myrand; - IV2[a]=(mm&255); - mm>>=8; - IV2[a+1]=(mm&255); - mm>>=8; - IV2[a+2]=(mm&255); - mm>>=8; - IV2[a+3]=(mm&255); - + IV1[a]=IV[a]; + IV1[a+1]=IV[a+1]; + IV1[a+2]=IV[a+2]; + IV1[a+3]=IV[a+3]; } - - + for(int a=0;a<h;a+=4) { + IV2[a]=IV[h+a]; + IV2[a+1]=IV[h+a+1]; + IV2[a+2]=IV[h+a+2]; + IV2[a+3]=IV[h+a+3]; + } for(int it=0;it<len/2;it++) { int ind1=Pbox[it]*h; @@ -1671,7 +1325,9 @@ void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, i RM1=&RM[PboxSRM[it]*h]; - RM2=&RM[h*h+PboxSRM[it]*h]; + RM2=&RM[h*h+PboxSRM[len/2-it-1]*h]; + + for(int a=0;a<h;a+=4) { @@ -1690,34 +1346,55 @@ void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, i for(int a=0;a<h;a+=4) { - invgY[a]=Inv_Sbox1[gY[a]]^RM2[a]; - invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1]; - invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2]; - invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3]; + tmp[a]=Inv_Sbox1[gY[a]]^RM2[a]; + tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1]; + tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2]; + tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3]; + } + + + for(int a=0;a<h;a+=4) { + tmp[a]=Inv_Sbox2[tmp[a]]; + tmp[a+1]=Inv_Sbox2[tmp[a+1]]; + tmp[a+2]=Inv_Sbox2[tmp[a+2]]; + tmp[a+3]=Inv_Sbox2[tmp[a+3]]; } + for(int a=0;a<h;a+=4) { - invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a]; - invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1]; - invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2]; - invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3]; + invgY[a]=tmp[a]^fX[a]^IV2[a]; + invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1]; + invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2]; + invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3]; } for(int a=0;a<h;a+=4) { - invfX[a]=Inv_Sbox2[fX[a]]^invgY[a]; - invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1]; - invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2]; - invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3]; + tmp[a]=Inv_Sbox2[fX[a]]^invgY[a]; + tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1]; + tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2]; + tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3]; + + } + + + for(int a=0;a<h;a+=4) { + tmp[a]=Inv_Sbox1[tmp[a]]; + tmp[a+1]=Inv_Sbox1[tmp[a+1]]; + tmp[a+2]=Inv_Sbox1[tmp[a+2]]; + tmp[a+3]=Inv_Sbox1[tmp[a+3]]; } + + + for(int a=0;a<h;a+=4) { - invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a]; - invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1]; - invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2]; - invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3]; + invfX[a]=tmp[a]^RM1[a]^IV1[a]; + invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1]; + invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2]; + invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3]; } @@ -1756,7 +1433,9 @@ void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, i } -*/ + + + @@ -1780,7 +1459,7 @@ int main(int argc, char** argv) { if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer } - printf("nb times %d\n",nb_test); +/* printf("nb times %d\n",nb_test); printf("cbcrm %d\n",cbcrm); printf("cbcprng %d\n",cbcprng); printf("ecbrm %d\n",ecbrm); @@ -1788,7 +1467,7 @@ int main(int argc, char** argv) { printf("h %d\n",h); printf("lena %d\n",lena); printf("size_buf %d\n",size_buf); - +*/ @@ -1831,7 +1510,8 @@ int main(int argc, char** argv) { } else { width=height=size_buf; - imsize=width*height; + imsize=width*height*3; + //cout<<"imsize "<<imsize<<endl; buffer=new uchar[imsize]; for(int i=0;i<imsize;i++) { buffer[i]=lrand48(); @@ -1840,7 +1520,7 @@ int main(int argc, char** argv) { - + cout<<"imsize "<<imsize<<endl; uchar* seq= new uchar[imsize]; uchar* seq2= new uchar[imsize]; @@ -1854,14 +1534,14 @@ int main(int argc, char** argv) { } } else { - for(int i=0;i<oneD;i++) { + for(int i=0;i<oneD*3;i++) { seq[i]=buffer[i]; } } - + int total_len=imsize; int rp=1; @@ -1910,35 +1590,38 @@ int main(int argc, char** argv) { uchar RM[h*h*2+256]; uchar IV[2*h]; - ulong myrand=0; - + mylong myrand=0; + double time_encrypt=0; double time_decrypt=0; double t=TimeStart(); - rc4key(DK, Sbox1, 8); - - - rc4key(&DK[8], Sbox2, 8); - - rc4key(&DK[16], sc, 16); - prga(sc, h*h*2+256, RM); - + for(int i=0;i<nb_test;i++) { - - - rc4keyperm(&DK[72], len, rp, Pbox, 16); + rc4key(DK, Sbox1, 8); - rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16); + rc4key(&DK[8], Sbox2, 8); + + rc4key(&DK[16], sc, 16); + prga(sc, h*h*2+256, RM); + + - for(int i=0;i<len/2;i++) { - PboxSRM[i]=PboxSRM2[i]&(h-1); - } + + rc4keyperm(&DK[72], len, rp, Pbox, 16); + + + rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16); + + for(int i=0;i<len/2;i++) { + PboxSRM[i]=PboxSRM2[i]&(h-1); + } + /* for(int i=0;i<h*2;i++) { for(int j=0;j<h;j++) @@ -1946,11 +1629,11 @@ int main(int argc, char** argv) { cout<<endl; } */ + } - - - //time+=TimeStop(t); - //cout<<"Time initializaton "<<time<<endl; + double time_init=0; + time_init+=TimeStop(t); + cout<<"Time initializaton nb times "<<nb_test<<" = "<<time_init<<endl; @@ -1981,65 +1664,65 @@ int main(int argc, char** argv) { for(i=0;i<nb_test;i++) { if(cbcprng) - encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); if(cbcrm) encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbrm) - encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbprng) - encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); } break; case 8: for(i=0;i<nb_test;i++) { if(cbcprng) - encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); if(cbcrm) encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbrm) - encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbprng) - encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); } break; case 16: for(i=0;i<nb_test;i++) { if(cbcprng) - encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); if(cbcrm) encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbrm) - encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbprng) - encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); } break; case 32: for(i=0;i<nb_test;i++) { if(cbcprng) - encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); if(cbcrm) encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbrm) - encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbprng) - encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); } break; case 64: for(i=0;i<nb_test;i++) { if(cbcprng) - encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); if(cbcrm) encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbrm) - encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbprng) - encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); } break; @@ -2047,33 +1730,36 @@ int main(int argc, char** argv) { for(i=0;i<nb_test;i++) { if(cbcprng) - encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); if(cbcrm) encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbrm) - encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbprng) - encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); } break; - case 256: + case 256: + for(i=0;i<nb_test;i++) { if(cbcprng) - encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); if(cbcrm) encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbrm) - encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0); if(ecbprng) - encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0); + encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0); } break; } + + time_encrypt+=TimeStop(t); - //cout<<"Time encrypt "<< + cout<<"Time encrypt "<<time_encrypt<<endl; cout<<(double)imsize*nb_test/time_encrypt<<"\t"; @@ -2095,91 +1781,93 @@ int main(int argc, char** argv) { case 4: for(i=0;i<nb_test;i++) { if(cbcprng) - decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); if(cbcrm) decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbrm) - decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbprng) - decrypt_ecb_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); } break; case 8: for(i=0;i<nb_test;i++) { if(cbcprng) - decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); if(cbcrm) decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbrm) - decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbprng) - decrypt_ecb_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); } break; case 16: for(i=0;i<nb_test;i++) { if(cbcprng) - decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); if(cbcrm) decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbrm) - decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbprng) - decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); } break; case 32: for(i=0;i<nb_test;i++) { if(cbcprng) - decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); if(cbcrm) decrypt_cbc_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbrm) - decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbprng) - decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); } break; case 64: for(i=0;i<nb_test;i++) { if(cbcprng) - decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); if(cbcrm) decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbrm) - decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbprng) - decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); } break; case 128: for(i=0;i<nb_test;i++) { if(cbcprng) - decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); if(cbcrm) decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbrm) - decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbprng) - decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); } break; case 256: for(i=0;i<nb_test;i++) { if(cbcprng) - decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); if(cbcrm) decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbrm) - decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0); if(ecbprng) - decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0); + decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0); } break; } + + time_decrypt+=TimeStop(t); - //cout<<"Time decrypt " +// cout<<"Time decrypt "<<time_decrypt<<endl; cout<<(double)imsize*nb_test/time_decrypt<<"\t"; if(lena) { @@ -2198,10 +1886,9 @@ int main(int argc, char** argv) { equal=false; } } -// cout<<"RESULT CORRECT: "<<equal<<endl; + //cout<<"RESULT CORRECT: "<<equal<<endl; } - return 0; }