X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/blobdiff_plain/f468314fbb129d22a901c316d2276d25923c9f47..ab7c86a2ba461b455a9045cb18a0d002a5db69cd:/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp?ds=sidebyside diff --git a/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp b/OneRoundIoT/EnhancedOneRound/enhanced_oneround.cpp index 22a7a90..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; @@ -73,18 +73,22 @@ uint xorshift32(const uint t) return x; } -ulong xorshift64(ulong t) + +mylong xorseed; + +mylong xorshift64() { /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ - ulong x = t; + mylong x = xorseed; x ^= x >> 12; // a x ^= x << 25; // b x ^= x >> 27; // c - return x; + return xorseed=x; } +/* __uint128_t g_lehmer64_state; inline uint64_t splitmix64_stateless(uint64_t index) { @@ -106,7 +110,7 @@ inline uint64_t lehmer64() { return g_lehmer64_state >> 64; } - +*/ @@ -187,7 +191,7 @@ void prga(uchar *sc, int ldata, uchar *r) { template -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]; @@ -196,8 +200,10 @@ void encrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbo 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>3);a++) { - myrand=lehmer64(); + myrand=xorshift64(); rm1[a]=myrand; - myrand=lehmer64(); + myrand=xorshift64(); rm2[a]=myrand; } @@ -313,7 +319,7 @@ void encrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbo template -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]; @@ -322,8 +328,9 @@ 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>3);a++) { - myrand=lehmer64(); + myrand=xorshift64(); rm1[a]=myrand; - myrand=lehmer64(); + myrand=xorshift64(); rm2[a]=myrand; } @@ -439,30 +446,49 @@ void decrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pb template -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>3);a++) { - myrand=lehmer64(); - rm1[a]=myrand; - myrand=lehmer64(); - rm2[a]=myrand; - } + RM1=&RM[PboxSRM[it]*h]; + RM2=&RM[h*h+PboxSRM[len/2-it-1]*h]; + for(int a=0;a -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>3);a++) { - myrand=lehmer64(); - rm1[a]=myrand; - myrand=lehmer64(); - rm2[a]=myrand; - } + RM1=&RM[PboxSRM[it]*h]; + RM2=&RM[h*h+PboxSRM[len/2-it-1]*h]; @@ -687,170 +729,14 @@ void decrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox -/* - -template -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 -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 -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]; @@ -859,318 +745,13 @@ void encrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbo uchar IV1[h]; uchar IV2[h]; uchar RM1[h]; - uchar RM2[h]; - uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; - - for(int it=0;it>3);a++) { - myrand=lehmer64(); - rm1[a]=myrand; - myrand=lehmer64(); - rm2[a]=myrand; - } - - - - for(int a=0;a -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[h]; - uchar RM2[h]; - uchar tmp[h]; - ulong *rm1=(ulong*)RM1; - ulong *rm2=(ulong*)RM2; - - for(int it=0;it>3);a++) { - myrand=lehmer64(); - rm1[a]=myrand; - myrand=lehmer64(); - rm2[a]=myrand; - } - - for(int a=0;a -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 RM2[h]; uchar tmp[h]; + mylong *rm1=(mylong*)RM1; + mylong *rm2=(mylong*)RM2; - + for(int a=0;a>3);a++) { + myrand=xorshift64(); + rm1[a]=myrand; + myrand=xorshift64(); + rm2[a]=myrand; + } + + + for(int a=0;a -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>3);a++) { + myrand=xorshift64(); + rm1[a]=myrand; + myrand=xorshift64(); + rm2[a]=myrand; + } for(int a=0;a -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]; @@ -1497,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>=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>=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 -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]; @@ -1622,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>=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>=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(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(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(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(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(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(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(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 "<