2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
13 /*#include <cryptopp/hex.h>
14 #include <cryptopp/sha.h>
15 #include <cryptopp/osrng.h>
16 #include <cryptopp/secblock.h>
21 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
22 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
26 //using namespace CryptoPP;
40 typedef unsigned char uchar;
45 struct timeval tstart;
46 gettimeofday(&tstart,0);
47 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
50 double TimeStop(double t)
54 gettimeofday(&tend,0);
55 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
64 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
66 for(int i=0;i<size_tab;i++) {
67 inv_perm_tabs[tab[i]] = i;
72 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
74 for(int i=0;i<size_tab;i++) {
75 inv_perm_tabs[tab[i]] = i;
82 void rc4key(uchar *key, uchar *sc, int size_DK) {
84 for(int i=0;i<256;i++) {
90 for(int i0=0; i0<256; i0++) {
91 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
100 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
106 for (int i=0;i<len;i++) {
109 for (int it = 0; it < rp; it++) {
111 for(int i0 = 0; i0<len; i0++) {
112 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
121 void prga(uchar *sc, int ldata, uchar *r) {
125 for (int it=0; it<ldata; it++) {
126 i0 = ((i0+1)&0xFE); //%255);
127 j0 = (j0 + sc[i0])&0xFF;
131 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
138 template<int h2, int h>
139 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,uchar *RM2,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc) {
142 // uchar *X=new uchar[h2];
143 // uchar *fX=new uchar[h2];
153 for(int a=0;a<h2;a+=4) {
154 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
155 X[a+1]=Sbox1[(a+1)&0xFF];
156 X[a+2]=Sbox1[(a+2)&0xFF];
157 X[a+3]=Sbox1[(a+3)&0xFF];
161 for(int it=0;it<len;it++) {
172 for(int a=0;a<h2;a+=4) {
174 X[a+1]=Sbox1[X[a+1]];
175 X[a+2]=Sbox1[X[a+2]];
176 X[a+3]=Sbox1[X[a+3]];
179 for(int a=0;a<h2;a+=4) {
181 fX[a+1]=X[a+1]^RM1[a+1];
182 fX[a+2]=X[a+2]^RM1[a+2];
183 fX[a+3]=X[a+3]^RM1[a+3];
186 for(int a=0;a<h2;a+=4) {
187 fX[a]=fX[a]^seq_in[ind2+a];
188 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
189 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
190 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
195 for(int k=0;k<h;k++) {
198 for(int a=0;a<h;a+=4) {
199 X2[a]=RM2[a]^seq_in[ind2+k*h+a];
200 X2[a+1]=RM2[a+1]^seq_in[ind2+k*h+a+1];
201 X2[a+2]=RM2[a+2]^seq_in[ind2+k*h+a+2];
202 X2[a+3]=RM2[a+3]^seq_in[ind2+k*h+a+3];
206 for(int a=1;a<h;a++) {
210 for(int a=0;a<h;a+=4) {
212 Y[a+1]=Sbox2[Y[a+1]];
213 Y[a+2]=Sbox2[Y[a+2]];
214 Y[a+3]=Sbox2[Y[a+3]];
222 for(int a=h-1;a>0;a--) {
227 for(int a=0;a<h;a+=4) {
243 for(int a=0;a<h2;a+=4) {
244 seq_out[ind1+a]=fX[a];
245 seq_out[ind1+a+1]=fX[a+1];
246 seq_out[ind1+a+2]=fX[a+2];
247 seq_out[ind1+a+3]=fX[a+3];
252 for(int k=0;k<h;k++) {
255 for(int a=0;a<h;a+=4) {
256 X2[a]=RM2[a]^fX[k*h+a];
257 X2[a+1]=RM2[a+1]^fX[k*h+a+1];
258 X2[a+2]=RM2[a+2]^fX[k*h+a+2];
259 X2[a+3]=RM2[a+3]^fX[k*h+a+3];
263 for(int a=1;a<h;a++) {
267 for(int a=0;a<h;a+=4) {
269 Y[a+1]=Sbox2[Y[a+1]];
270 Y[a+2]=Sbox2[Y[a+2]];
271 Y[a+3]=Sbox2[Y[a+3]];
279 for(int a=h-1;a>0;a--) {
284 for(int a=0;a<h;a+=4) {
294 for(int a=0;a<h2;a+=4) {
295 RM1[a]=RM1[PboxRM[a]];
296 RM1[a+1]=RM1[PboxRM[a+1]];
297 RM1[a+2]=RM1[PboxRM[a+2]];
298 RM1[a+3]=RM1[PboxRM[a+3]];
305 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
308 /* uchar *X=new uchar[h2];
309 uchar *fX=new uchar[h2];
310 unsigned int *lX=(unsigned int*)X;
311 unsigned int *lseq_in=(unsigned int*)seq_in;
315 // unsigned int *lX=(unsigned int*)X;
316 // unsigned int *lseq_in=(unsigned int*)seq_in;
319 for(int it=0;it<len;it++) {
321 int ind2=Pbox[it]*h2;
323 for(int a=0;a<h2;a+=4) {
325 X[a+1]=seq_in[ind2+a+1];
326 X[a+2]=seq_in[ind2+a+2];
327 X[a+3]=seq_in[ind2+a+3];
330 for(int a=0;a<h2;a+=4){
332 fX[a+1]=Sbox1[X[a+1]];
333 fX[a+2]=Sbox1[X[a+2]];
334 fX[a+3]=Sbox1[X[a+3]];
338 for(int a=0;a<h2;a+=4) {
340 fX[a+1]=fX[a+1]^RM1[a+1];
341 fX[a+2]=fX[a+2]^RM1[a+2];
342 fX[a+3]=fX[a+3]^RM1[a+3];
346 for(int a=0;a<h2;a+=4) {
347 seq_out[ind1+a]=Sbox2[fX[a]];
348 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
349 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
350 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
353 for(int a=0;a<h2;a+=4) {
354 RM1[a]=RM1[PboxRM[a]];
355 RM1[a+1]=RM1[PboxRM[a+1]];
356 RM1[a+2]=RM1[PboxRM[a+2]];
357 RM1[a+3]=RM1[PboxRM[a+3]];
371 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug) {
374 /*uchar *fX=new uchar[h2];
375 uchar *Inv_Sbox1=new uchar[256];
376 uchar *Inv_Sbox2=new uchar[256];
382 for(int it=0;it<len;it++) {
385 int ind2=Pbox[it]*h2;
390 for(int a=0;a<h2;a+=4) {
391 fX[a]=seq_in[ind1+a];
392 fX[a+1]=seq_in[ind1+a+1];
393 fX[a+2]=seq_in[ind1+a+2];
394 fX[a+3]=seq_in[ind1+a+3];
397 for(int a=0;a<h2;a+=4) {
398 fX[a]=Inv_Sbox2[fX[a]];
399 fX[a+1]=Inv_Sbox2[fX[a+1]];
400 fX[a+2]=Inv_Sbox2[fX[a+2]];
401 fX[a+3]=Inv_Sbox2[fX[a+3]];
403 for(int a=0;a<h2;a+=4) {
405 fX[a+1]=fX[a+1]^RM1[a+1];
406 fX[a+2]=fX[a+2]^RM1[a+2];
407 fX[a+3]=fX[a+3]^RM1[a+3];
410 for(int a=0;a<h2;a+=4) {
411 RM1[a]=RM1[PboxRM[a]];
412 RM1[a+1]=RM1[PboxRM[a+1]];
413 RM1[a+2]=RM1[PboxRM[a+2]];
414 RM1[a+3]=RM1[PboxRM[a+3]];
417 for(int a=0;a<h2;a+=4) {
418 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
419 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
420 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
421 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
431 int main(int argc, char** argv) {
441 for(int i=1; i<argc; i++){
442 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
443 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
444 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
445 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
446 if(strncmp(argv[i],"impb",4)==0) impb = atoi(&(argv[i][4])); //Use Lena or buffer
447 if(strncmp(argv[i],"tgpb",4)==0) tgpb = atoi(&(argv[i][4])); //Use Lena or buffer
450 /* printf("nb times %d\n",nb_test);
451 printf("ctr %d\n",ctr);
453 printf("lena %d\n",lena);
454 printf("size_buf %d\n",size_buf);
464 uchar Secretkey[key_size];
466 uchar counter[key_size];
468 for(int i=0;i<key_size;i++) {
469 Secretkey[i]=lrand48()&0xFF;
470 counter[i]=lrand48()&0xFF;
483 uchar *data_R, *data_G, *data_B;
488 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
489 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
490 imsize=width*height*3;
491 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
494 width=height=size_buf;
496 buffer=new uchar[imsize];
497 for(int i=0;i<imsize;i++) {
506 uchar* seq= new uchar[imsize];
507 uchar* seq2= new uchar[imsize];
509 int oneD=width*height;
511 for(int i=0;i<oneD;i++) {
513 seq[oneD+i]=data_G[i];
514 seq[2*oneD+i]=data_B[i];
518 for(int i=0;i<oneD;i++) {
527 int total_len=imsize;
529 int len= total_len/h2;
533 uchar *mix=new uchar[256];
538 for (int i = 0; i < 256 ; i++) {
539 mix[i]=Secretkey[i]^counter[i];
543 // cout<<"hash "<<endl;
544 for (int i = 0; i < 64 ; i++) {
551 int *Pbox=new int[len];
552 int *PboxRM=new int[h2];
555 uchar Inv_Sbox1[256];
556 uchar Inv_Sbox2[256];
565 double time_encrypt=0;
566 double time_decrypt=0;
569 double t=TimeStart();
570 rc4key(DK, Sbox1, 8);
573 rc4key(&DK[8], Sbox2, 8);
575 rc4key(&DK[16], sc, 16);
581 rc4keyperm(&DK[32], len, rp, Pbox, 16);
584 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
587 rc4key(&DK[64], sc, 16);
593 //cout<<"Time initializaton "<<time<<endl;
605 for(int i=0;i<h2;i++){
609 for(int i=0;i<h;i++){
614 inverse_tables(Sbox1,256,Inv_Sbox1);
615 inverse_tables(Sbox2,256,Inv_Sbox2);
627 for(i=0;i<nb_test;i++)
630 encrypt_ctr<4*4,4>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
632 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
637 for(i=0;i<nb_test;i++)
640 encrypt_ctr<8*8,8>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
642 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
647 for(i=0;i<nb_test;i++)
650 encrypt_ctr<16*16,16>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
652 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
657 for(i=0;i<nb_test;i++)
660 encrypt_ctr<32*32,32>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
662 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
667 for(i=0;i<nb_test;i++)
670 encrypt_ctr<64*64,64>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
672 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
677 for(i=0;i<nb_test;i++)
680 encrypt_ctr<128*128,128>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
682 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
687 time_encrypt+=TimeStop(t);
688 //cout<<"Time encrypt "<<
689 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
693 for(int i=0;i<oneD;i++) {
695 data_G[i]=seq2[oneD+i];
696 data_B[i]=seq2[2*oneD+i];
698 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
702 for(int i=0;i<h;i++){
703 cout<<(int)RM3[i]<<" ";
711 if(tgpb>=0 && tgpb<h) {
720 for(i=0;i<nb_test;i++) {
722 encrypt_ctr<4*4,4>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
724 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
728 for(i=0;i<nb_test;i++) {
730 encrypt_ctr<8*8,8>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
732 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
736 for(i=0;i<nb_test;i++) {
738 encrypt_ctr<16*16,16>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
740 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
744 for(i=0;i<nb_test;i++) {
746 encrypt_ctr<32*32,32>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
748 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
752 for(i=0;i<nb_test;i++) {
754 encrypt_ctr<64*64,64>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
756 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
760 for(i=0;i<nb_test;i++) {
762 encrypt_ctr<128*128,128>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
764 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
769 time_decrypt+=TimeStop(t);
770 //cout<<"Time decrypt "
771 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
774 cout<<"\nTAG 2"<<endl;
775 for(int i=0;i<h;i++){
776 cout<<(int)RM4[i]<<" ";
780 for(int i=0;i<oneD;i++) {
782 data_G[i]=seq[oneD+i];
783 data_B[i]=seq[2*oneD+i];
785 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
789 for(int i=0;i<imsize;i++) {
790 //cout<<(int)buffer[i]<<endl;
791 if(buffer[i]!=seq[i]) {
795 // cout<<"RESULT CORRECT: "<<equal<<endl;