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) {
295 for(int a=0;a<h2;a+=4) {
296 RM1[a]=RM1[PboxRM[a]];
297 RM1[a+1]=RM1[PboxRM[a+1]];
298 RM1[a+2]=RM1[PboxRM[a+2]];
299 RM1[a+3]=RM1[PboxRM[a+3]];
306 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
309 /* uchar *X=new uchar[h2];
310 uchar *fX=new uchar[h2];
311 unsigned int *lX=(unsigned int*)X;
312 unsigned int *lseq_in=(unsigned int*)seq_in;
316 // unsigned int *lX=(unsigned int*)X;
317 // unsigned int *lseq_in=(unsigned int*)seq_in;
320 for(int it=0;it<len;it++) {
322 int ind2=Pbox[it]*h2;
324 for(int a=0;a<h2;a+=4) {
326 X[a+1]=seq_in[ind2+a+1];
327 X[a+2]=seq_in[ind2+a+2];
328 X[a+3]=seq_in[ind2+a+3];
331 for(int a=0;a<h2;a+=4){
333 fX[a+1]=Sbox1[X[a+1]];
334 fX[a+2]=Sbox1[X[a+2]];
335 fX[a+3]=Sbox1[X[a+3]];
339 for(int a=0;a<h2;a+=4) {
341 fX[a+1]=fX[a+1]^RM1[a+1];
342 fX[a+2]=fX[a+2]^RM1[a+2];
343 fX[a+3]=fX[a+3]^RM1[a+3];
347 for(int a=0;a<h2;a+=4) {
348 seq_out[ind1+a]=Sbox2[fX[a]];
349 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
350 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
351 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
354 for(int a=0;a<h2;a+=4) {
355 RM1[a]=RM1[PboxRM[a]];
356 RM1[a+1]=RM1[PboxRM[a+1]];
357 RM1[a+2]=RM1[PboxRM[a+2]];
358 RM1[a+3]=RM1[PboxRM[a+3]];
372 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug) {
375 /*uchar *fX=new uchar[h2];
376 uchar *Inv_Sbox1=new uchar[256];
377 uchar *Inv_Sbox2=new uchar[256];
383 for(int it=0;it<len;it++) {
386 int ind2=Pbox[it]*h2;
391 for(int a=0;a<h2;a+=4) {
392 fX[a]=seq_in[ind1+a];
393 fX[a+1]=seq_in[ind1+a+1];
394 fX[a+2]=seq_in[ind1+a+2];
395 fX[a+3]=seq_in[ind1+a+3];
398 for(int a=0;a<h2;a+=4) {
399 fX[a]=Inv_Sbox2[fX[a]];
400 fX[a+1]=Inv_Sbox2[fX[a+1]];
401 fX[a+2]=Inv_Sbox2[fX[a+2]];
402 fX[a+3]=Inv_Sbox2[fX[a+3]];
404 for(int a=0;a<h2;a+=4) {
406 fX[a+1]=fX[a+1]^RM1[a+1];
407 fX[a+2]=fX[a+2]^RM1[a+2];
408 fX[a+3]=fX[a+3]^RM1[a+3];
411 for(int a=0;a<h2;a+=4) {
412 RM1[a]=RM1[PboxRM[a]];
413 RM1[a+1]=RM1[PboxRM[a+1]];
414 RM1[a+2]=RM1[PboxRM[a+2]];
415 RM1[a+3]=RM1[PboxRM[a+3]];
418 for(int a=0;a<h2;a+=4) {
419 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
420 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
421 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
422 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
432 int main(int argc, char** argv) {
442 for(int i=1; i<argc; i++){
443 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
444 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
445 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
446 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
447 if(strncmp(argv[i],"impb",4)==0) impb = atoi(&(argv[i][4])); //Use Lena or buffer
448 if(strncmp(argv[i],"tgpb",4)==0) tgpb = atoi(&(argv[i][4])); //Use Lena or buffer
451 /* printf("nb times %d\n",nb_test);
452 printf("ctr %d\n",ctr);
454 printf("lena %d\n",lena);
455 printf("size_buf %d\n",size_buf);
465 uchar Secretkey[key_size];
467 uchar counter[key_size];
469 for(int i=0;i<key_size;i++) {
470 Secretkey[i]=lrand48()&0xFF;
471 counter[i]=lrand48()&0xFF;
484 uchar *data_R, *data_G, *data_B;
489 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
490 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
491 imsize=width*height*3;
492 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
495 width=height=size_buf;
497 buffer=new uchar[imsize];
498 for(int i=0;i<imsize;i++) {
507 uchar* seq= new uchar[imsize];
508 uchar* seq2= new uchar[imsize];
510 int oneD=width*height;
512 for(int i=0;i<oneD;i++) {
514 seq[oneD+i]=data_G[i];
515 seq[2*oneD+i]=data_B[i];
519 for(int i=0;i<oneD;i++) {
528 int total_len=imsize;
530 int len= total_len/h2;
534 uchar *mix=new uchar[256];
539 for (int i = 0; i < 256 ; i++) {
540 mix[i]=Secretkey[i]^counter[i];
544 // cout<<"hash "<<endl;
545 for (int i = 0; i < 64 ; i++) {
552 int *Pbox=new int[len];
553 int *PboxRM=new int[h2];
556 uchar Inv_Sbox1[256];
557 uchar Inv_Sbox2[256];
566 double time_encrypt=0;
567 double time_decrypt=0;
570 double t=TimeStart();
571 rc4key(DK, Sbox1, 8);
574 rc4key(&DK[8], Sbox2, 8);
576 rc4key(&DK[16], sc, 16);
582 rc4keyperm(&DK[32], len, rp, Pbox, 16);
585 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
588 rc4key(&DK[64], sc, 16);
594 //cout<<"Time initializaton "<<time<<endl;
606 for(int i=0;i<h2;i++){
610 for(int i=0;i<h;i++){
615 inverse_tables(Sbox1,256,Inv_Sbox1);
616 inverse_tables(Sbox2,256,Inv_Sbox2);
628 for(i=0;i<nb_test;i++)
631 encrypt_ctr<4*4,4>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
633 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
638 for(i=0;i<nb_test;i++)
641 encrypt_ctr<8*8,8>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
643 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
648 for(i=0;i<nb_test;i++)
651 encrypt_ctr<16*16,16>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
653 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
658 for(i=0;i<nb_test;i++)
661 encrypt_ctr<32*32,32>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
663 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
668 for(i=0;i<nb_test;i++)
671 encrypt_ctr<64*64,64>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
673 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
678 for(i=0;i<nb_test;i++)
681 encrypt_ctr<128*128,128>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
683 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
688 time_encrypt+=TimeStop(t);
689 //cout<<"Time encrypt "<<
690 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
694 for(int i=0;i<oneD;i++) {
696 data_G[i]=seq2[oneD+i];
697 data_B[i]=seq2[2*oneD+i];
699 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
703 for(int i=0;i<h;i++){
704 cout<<(int)RM3[i]<<" ";
712 if(tgpb>=0 && tgpb<h) {
721 for(i=0;i<nb_test;i++) {
723 encrypt_ctr<4*4,4>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
725 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
729 for(i=0;i<nb_test;i++) {
731 encrypt_ctr<8*8,8>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
733 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
737 for(i=0;i<nb_test;i++) {
739 encrypt_ctr<16*16,16>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
741 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
745 for(i=0;i<nb_test;i++) {
747 encrypt_ctr<32*32,32>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
749 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
753 for(i=0;i<nb_test;i++) {
755 encrypt_ctr<64*64,64>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
757 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
761 for(i=0;i<nb_test;i++) {
763 encrypt_ctr<128*128,128>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
765 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
770 time_decrypt+=TimeStop(t);
771 //cout<<"Time decrypt "
772 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
775 cout<<"\nTAG 2"<<endl;
776 for(int i=0;i<h;i++){
777 cout<<(int)RM4[i]<<" ";
781 for(int i=0;i<oneD;i++) {
783 data_G[i]=seq[oneD+i];
784 data_B[i]=seq[2*oneD+i];
786 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
790 for(int i=0;i<imsize;i++) {
791 //cout<<(int)buffer[i]<<endl;
792 if(buffer[i]!=seq[i]) {
796 // cout<<"RESULT CORRECT: "<<equal<<endl;