2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
15 /*#include <cryptopp/hex.h>
16 #include <cryptopp/sha.h>
17 #include <cryptopp/osrng.h>
18 #include <cryptopp/secblock.h>
23 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
24 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
28 //using namespace CryptoPP;
42 typedef unsigned char uchar;
47 struct timeval tstart;
48 gettimeofday(&tstart,0);
49 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
52 double TimeStop(double t)
56 gettimeofday(&tend,0);
57 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
64 uint xorshift32(const uint t)
66 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
76 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
78 for(int i=0;i<size_tab;i++) {
79 inv_perm_tabs[tab[i]] = i;
84 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
86 for(int i=0;i<size_tab;i++) {
87 inv_perm_tabs[tab[i]] = i;
94 void rc4key(uchar *key, uchar *sc, int size_DK) {
96 for(int i=0;i<256;i++) {
102 for(int i0=0; i0<256; i0++) {
103 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
112 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
118 for (int i=0;i<len;i++) {
121 for (int it = 0; it < rp; it++) {
123 for(int i0 = 0; i0<len; i0++) {
124 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
133 void prga(uchar *sc, int ldata, uchar *r) {
137 for (int it=0; it<ldata; it++) {
138 i0 = ((i0+1)&0xFE); //%255);
139 j0 = (j0 + sc[i0])&0xFF;
143 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
153 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) {
162 for(int it=0;it<len/2;it++) {
164 int ind2=Pbox[it+len/2]*h;
168 RM1=&RM[PboxSRM[it]*h];
169 RM2=&RM[h*h+PboxSRM[it]*h];
172 for(int a=0;a<h;a+=4) {
174 X[a+1]=seq_in[ind2+a+1];
175 X[a+2]=seq_in[ind2+a+2];
176 X[a+3]=seq_in[ind2+a+3];
179 for(int a=0;a<h;a+=4) {
181 Y[a+1]=seq_in[ind1+a+1];
182 Y[a+2]=seq_in[ind1+a+2];
183 Y[a+3]=seq_in[ind1+a+3];
187 for(int a=0;a<h;a+=4) {
188 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
189 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
190 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
191 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
194 for(int a=0;a<h;a+=4) {
195 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
196 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
197 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
198 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
202 for(int a=0;a<h;a+=4) {
203 seq_out[ind2+a]=gY[a];
204 seq_out[ind2+a+1]=gY[a+1];
205 seq_out[ind2+a+2]=gY[a+2];
206 seq_out[ind2+a+3]=gY[a+3];
209 for(int a=0;a<h;a+=4) {
210 seq_out[ind1+a]=fX[a];
211 seq_out[ind1+a+1]=fX[a+1];
212 seq_out[ind1+a+2]=fX[a+2];
213 seq_out[ind1+a+3]=fX[a+3];
233 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) {
242 for(int it=0;it<len/2;it++) {
244 int ind2=Pbox[it+len/2]*h;
247 RM1=&RM[PboxSRM[it]*h];
248 RM2=&RM[h*h+PboxSRM[it]*h];
251 for(int a=0;a<h;a+=4) {
252 gY[a]=seq_in[ind2+a];
253 gY[a+1]=seq_in[ind2+a+1];
254 gY[a+2]=seq_in[ind2+a+2];
255 gY[a+3]=seq_in[ind2+a+3];
258 for(int a=0;a<h;a+=4) {
259 fX[a]=seq_in[ind1+a];
260 fX[a+1]=seq_in[ind1+a+1];
261 fX[a+2]=seq_in[ind1+a+2];
262 fX[a+3]=seq_in[ind1+a+3];
265 for(int a=0;a<h;a+=4) {
266 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
267 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
268 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
269 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
274 for(int a=0;a<h;a+=4) {
275 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
276 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
277 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
278 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
283 for(int a=0;a<h;a+=4) {
284 seq_out[ind2+a]=invfX[a];
285 seq_out[ind2+a+1]=invfX[a+1];
286 seq_out[ind2+a+2]=invfX[a+2];
287 seq_out[ind2+a+3]=invfX[a+3];
290 for(int a=0;a<h;a+=4) {
291 seq_out[ind1+a]=invgY[a];
292 seq_out[ind1+a+1]=invgY[a+1];
293 seq_out[ind1+a+2]=invgY[a+2];
294 seq_out[ind1+a+3]=invgY[a+3];
311 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) {
323 for(int a=0;a<h;a+=4) {
325 IV1[a+1]=RM[3*h-a-1];
326 IV1[a+2]=RM[3*h-a-2];
327 IV1[a+3]=RM[3*h-a-3];
330 for(int a=0;a<h;a+=4) {
332 IV2[a+1]=RM[h2+2*h-a-1];
333 IV2[a+2]=RM[h2+2*h-a-2];
334 IV2[a+3]=RM[h2+2*h-a-3];
340 for(int it=0;it<len/2;it++) {
342 int ind2=Pbox[it+len/2]*h;
346 RM1=&RM[PboxSRM[it]*h];
347 RM2=&RM[h*h+PboxSRM[it]*h];
350 for(int a=0;a<h;a+=4) {
352 X[a+1]=seq_in[ind2+a+1];
353 X[a+2]=seq_in[ind2+a+2];
354 X[a+3]=seq_in[ind2+a+3];
357 for(int a=0;a<h;a+=4) {
359 Y[a+1]=seq_in[ind1+a+1];
360 Y[a+2]=seq_in[ind1+a+2];
361 Y[a+3]=seq_in[ind1+a+3];
365 for(int a=0;a<h;a+=4) {
366 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
367 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
368 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
369 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
372 for(int a=0;a<h;a+=4) {
373 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
374 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
375 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
376 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
380 for(int a=0;a<h;a+=4) {
381 seq_out[ind2+a]=gY[a];
382 seq_out[ind2+a+1]=gY[a+1];
383 seq_out[ind2+a+2]=gY[a+2];
384 seq_out[ind2+a+3]=gY[a+3];
387 for(int a=0;a<h;a+=4) {
388 seq_out[ind1+a]=fX[a];
389 seq_out[ind1+a+1]=fX[a+1];
390 seq_out[ind1+a+2]=fX[a+2];
391 seq_out[ind1+a+3]=fX[a+3];
393 for(int a=0;a<h;a+=4) {
400 for(int a=0;a<h;a+=4) {
422 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) {
435 for(int a=0;a<h;a+=4) {
437 IV1[a+1]=RM[3*h-a-1];
438 IV1[a+2]=RM[3*h-a-2];
439 IV1[a+3]=RM[3*h-a-3];
442 for(int a=0;a<h;a+=4) {
444 IV2[a+1]=RM[h2+2*h-a-1];
445 IV2[a+2]=RM[h2+2*h-a-2];
446 IV2[a+3]=RM[h2+2*h-a-3];
452 for(int it=0;it<len/2;it++) {
454 int ind2=Pbox[it+len/2]*h;
457 RM1=&RM[PboxSRM[it]*h];
458 RM2=&RM[h*h+PboxSRM[it]*h];
461 for(int a=0;a<h;a+=4) {
462 gY[a]=seq_in[ind2+a];
463 gY[a+1]=seq_in[ind2+a+1];
464 gY[a+2]=seq_in[ind2+a+2];
465 gY[a+3]=seq_in[ind2+a+3];
468 for(int a=0;a<h;a+=4) {
469 fX[a]=seq_in[ind1+a];
470 fX[a+1]=seq_in[ind1+a+1];
471 fX[a+2]=seq_in[ind1+a+2];
472 fX[a+3]=seq_in[ind1+a+3];
476 for(int a=0;a<h;a+=4) {
477 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
478 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
479 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
480 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
484 for(int a=0;a<h;a+=4) {
485 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
486 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
487 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
488 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
492 for(int a=0;a<h;a+=4) {
493 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
494 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
495 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
496 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
500 for(int a=0;a<h;a+=4) {
501 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
502 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
503 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
504 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
509 for(int a=0;a<h;a+=4) {
510 seq_out[ind2+a]=invfX[a];
511 seq_out[ind2+a+1]=invfX[a+1];
512 seq_out[ind2+a+2]=invfX[a+2];
513 seq_out[ind2+a+3]=invfX[a+3];
516 for(int a=0;a<h;a+=4) {
517 seq_out[ind1+a]=invgY[a];
518 seq_out[ind1+a+1]=invgY[a+1];
519 seq_out[ind1+a+2]=invgY[a+2];
520 seq_out[ind1+a+3]=invgY[a+3];
522 for(int a=0;a<h;a+=4) {
529 for(int a=0;a<h;a+=4) {
545 int main(int argc, char** argv) {
554 for(int i=1; i<argc; i++){
555 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
556 if(strncmp(argv[i],"cbc",3)==0) cbc = atoi(&(argv[i][3])); //CBC ? 1 otherwise CBC like
557 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
558 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
559 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
562 /* printf("nb times %d\n",nb_test);
563 printf("ctr %d\n",ctr);
565 printf("lena %d\n",lena);
566 printf("size_buf %d\n",size_buf);
576 uchar Secretkey[key_size];
578 uchar counter[key_size];
580 for(int i=0;i<key_size;i++) {
581 Secretkey[i]=lrand48()&0xFF;
582 counter[i]=lrand48()&0xFF;
595 uchar *data_R, *data_G, *data_B;
604 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
605 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
606 imsize=width*height*3;
607 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
610 width=height=size_buf;
612 buffer=new uchar[imsize];
613 for(int i=0;i<imsize;i++) {
622 uchar* seq= new uchar[imsize];
623 uchar* seq2= new uchar[imsize];
625 int oneD=width*height;
627 for(int i=0;i<oneD;i++) {
629 seq[oneD+i]=data_G[i];
630 seq[2*oneD+i]=data_B[i];
634 for(int i=0;i<oneD;i++) {
643 int total_len=imsize;
645 int len= total_len/h;
649 uchar *mix=new uchar[256];
654 for (int i = 0; i < 256 ; i++) {
655 mix[i]=Secretkey[i]^counter[i];
660 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
661 // g_print("%s\n", sha512);
671 // cout<<"hash "<<endl;
672 for (int i = 0; i < 128 ; i++) {
679 int *Pbox=new int[len];
680 int *PboxSRM=new int[len/2];
681 int *PboxSRM2=new int[len/2];
684 uchar Inv_Sbox1[256];
685 uchar Inv_Sbox2[256];
693 double time_encrypt=0;
694 double time_decrypt=0;
697 double t=TimeStart();
698 rc4key(DK, Sbox1, 8);
701 rc4key(&DK[8], Sbox2, 8);
703 rc4key(&DK[16], sc, 16);
710 rc4keyperm(&DK[72], len, rp, Pbox, 16);
713 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
715 for(int i=0;i<len/2;i++) {
716 PboxSRM[i]=PboxSRM2[i]&(h-1);
720 for(int i=0;i<h*2;i++) {
722 cout<<(int)RM[i*h+j]<<" ";
730 //cout<<"Time initializaton "<<time<<endl;
735 for(int i=0;i<32;i++) {
739 uint myrand_copy=myrand;
747 inverse_tables(Sbox1,256,Inv_Sbox1);
748 inverse_tables(Sbox2,256,Inv_Sbox2);
759 for(i=0;i<nb_test;i++)
762 encrypt_cbc<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
764 encrypt_ecb<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
768 for(i=0;i<nb_test;i++)
771 encrypt_cbc<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
773 encrypt_ecb<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
777 for(i=0;i<nb_test;i++)
780 encrypt_cbc<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
782 encrypt_ecb<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
786 for(i=0;i<nb_test;i++)
789 encrypt_cbc<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
791 encrypt_ecb<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
795 for(i=0;i<nb_test;i++)
798 encrypt_cbc<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
800 encrypt_ecb<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
805 for(i=0;i<nb_test;i++)
808 encrypt_cbc<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
810 encrypt_ecb<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
815 time_encrypt+=TimeStop(t);
816 //cout<<"Time encrypt "<<
817 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
821 for(int i=0;i<oneD;i++) {
823 data_G[i]=seq2[oneD+i];
824 data_B[i]=seq2[2*oneD+i];
826 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
834 for(i=0;i<nb_test;i++) {
836 decrypt_cbc<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
838 decrypt_ecb<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
842 for(i=0;i<nb_test;i++) {
844 decrypt_cbc<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
846 decrypt_ecb<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
850 for(i=0;i<nb_test;i++) {
852 decrypt_cbc<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
854 decrypt_ecb<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
858 for(i=0;i<nb_test;i++) {
860 decrypt_cbc<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
862 decrypt_ecb<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
866 for(i=0;i<nb_test;i++) {
868 decrypt_cbc<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
870 decrypt_ecb<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
874 for(i=0;i<nb_test;i++) {
876 decrypt_cbc<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
878 decrypt_ecb<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
883 time_decrypt+=TimeStop(t);
884 //cout<<"Time decrypt "
885 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
888 for(int i=0;i<oneD;i++) {
890 data_G[i]=seq[oneD+i];
891 data_B[i]=seq[2*oneD+i];
893 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
897 for(int i=0;i<imsize;i++) {
898 //cout<<(int)buffer[i]<<endl;
899 if(buffer[i]!=seq[i]) {
903 // cout<<"RESULT CORRECT: "<<equal<<endl;