2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
14 /*#include <cryptopp/hex.h>
15 #include <cryptopp/sha.h>
16 #include <cryptopp/osrng.h>
17 #include <cryptopp/secblock.h>
22 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
23 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
27 //using namespace CryptoPP;
41 typedef unsigned char uchar;
46 struct timeval tstart;
47 gettimeofday(&tstart,0);
48 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
51 double TimeStop(double t)
55 gettimeofday(&tend,0);
56 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
65 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
67 for(int i=0;i<size_tab;i++) {
68 inv_perm_tabs[tab[i]] = i;
73 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
75 for(int i=0;i<size_tab;i++) {
76 inv_perm_tabs[tab[i]] = i;
83 void rc4key(uchar *key, uchar *sc, int size_DK) {
85 for(int i=0;i<256;i++) {
91 for(int i0=0; i0<256; i0++) {
92 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
101 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
107 for (int i=0;i<len;i++) {
110 for (int it = 0; it < rp; it++) {
112 for(int i0 = 0; i0<len; i0++) {
113 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
122 void prga(uchar *sc, int ldata, uchar *r) {
126 for (int it=0; it<ldata; it++) {
127 i0 = ((i0+1)&0xFE); //%255);
128 j0 = (j0 + sc[i0])&0xFF;
132 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
136 void rotate(uchar *RM1, uchar *RM2, int size, int n)
139 for (i = 0; i< size-n; i++)
142 for (i = 0; i< n; i++)
143 RM2[i] = RM1[size-n-1+i];
148 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc, int num) {
155 #pragma omp parallel for
156 for(int p=0;p<num;p++) {
158 int id=omp_get_thread_num();
162 for(int a=0;a<h2;a++) {
163 RM2[a]=RM1[id*h2+a]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
169 for(int a=0;a<h2;a++) {
170 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
175 int offset=p*loc_len;
178 for(int it=offset;it<offset+loc_len;it++) {
182 //cout<<id<<" "<<it<<endl;
194 /*for(int a=0;a<h2;a+=4){
201 for(int a=0;a<h2;a+=4){
212 for(int a=0;a<h2;a++)
220 for(int a=0;a<h2;a++)
228 /*for(int a=0;a<h2;a+=4) {
229 fX[id*h2+a]=fX[id*h2+a]^RM1[id*h2+a];
230 fX[id*h2+a+1]=fX[id*h2+a+1]^RM1[id*h2+a+1];
231 fX[id*h2+a+2]=fX[id*h2+a+2]^RM1[id*h2+a+2];
232 fX[id*h2+a+3]=fX[id*h2+a+3]^RM1[id*h2+a+3];
238 for(int a=0;a<h2;a+=4) {
240 fX[a+1]=Sbox2[fX[a+1]];
241 fX[a+2]=Sbox2[fX[a+2]];
242 fX[a+3]=Sbox2[fX[a+3]];
245 // rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
247 for(int a=0;a<h2;a+=4) {
248 RM2[a]=RM2[PboxRM[a]];
249 RM2[a+1]=RM2[PboxRM[a+1]];
250 RM2[a+2]=RM2[PboxRM[a+2]];
251 RM2[a+3]=RM2[PboxRM[a+3]];
254 for(int a=0;a<h2;a+=4) {
256 fX[a+1]=fX[a+1]^RM2[a+1];
257 fX[a+2]=fX[a+2]^RM2[a+2];
258 fX[a+3]=fX[a+3]^RM2[a+3];
263 for(int a=0;a<h2;a+=4) {
264 fX[a]=fX[a]^seq_in[ind2+a];
265 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
266 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
267 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
271 for(int a=0;a<h2;a+=4) {
272 seq_out[ind1+a]=fX[a];
273 seq_out[ind1+a+1]=fX[a+1];
274 seq_out[ind1+a+2]=fX[a+2];
275 seq_out[ind1+a+3]=fX[a+3];
278 /*for(int a=0;a<h2;a+=4) {
279 RM1[id*h2+a]=RM1[id*h2+PboxRM[a]];
280 RM1[id*h2+a+1]=RM1[id*h2+PboxRM[a+1]];
281 RM1[id*h2+a+2]=RM1[id*h2+PboxRM[a+2]];
282 RM1[id*h2+a+3]=RM1[id*h2+PboxRM[a+3]];
296 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug, int num) {
299 /* uchar *X=new uchar[h2];
300 uchar *fX=new uchar[h2];
301 unsigned int *lX=(unsigned int*)X;
302 unsigned int *lseq_in=(unsigned int*)seq_in;
305 // unsigned int *lX=(unsigned int*)X;
306 // unsigned int *lseq_in=(unsigned int*)seq_in;
311 #pragma omp parallel for
312 for(int p=0;p<num;p++) {
314 int id=omp_get_thread_num();
320 for(int a=0;a<h2;a++) {
325 int offset=p*loc_len;
328 for(int it=offset;it<offset+loc_len;it++) {
333 int ind2=Pbox[it]*h2;
336 for(int a=0;a<h2;a+=4) {
337 fX[a]=seq_in[ind2+a];
338 fX[a+1]=seq_in[ind2+a+1];
339 fX[a+2]=seq_in[ind2+a+2];
340 fX[a+3]=seq_in[ind2+a+3];
344 for(int a=0;a<h2;a+=4){
346 fX[a+1]=Sbox1[fX[a+1]];
347 fX[a+2]=Sbox1[fX[a+2]];
348 fX[a+3]=Sbox1[fX[a+3]];
352 for(int a=0;a<h2;a+=4) {
354 fX[a+1]=fX[a+1]^RM2[a+1];
355 fX[a+2]=fX[a+2]^RM2[a+2];
356 fX[a+3]=fX[a+3]^RM2[a+3];
362 for(int a=0;a<h2;a+=4) {
363 seq_out[ind1+a]=Sbox2[fX[a]];
364 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
365 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
366 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
370 for(int a=0;a<h2;a+=4) {
371 RM2[a]=RM2[PboxRM[a]];
372 RM2[a+1]=RM2[PboxRM[a+1]];
373 RM2[a+2]=RM2[PboxRM[a+2]];
374 RM2[a+3]=RM2[PboxRM[a+3]];
389 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug, int num) {
392 /*uchar *fX=new uchar[h2];
393 uchar *Inv_Sbox1=new uchar[256];
394 uchar *Inv_Sbox2=new uchar[256];
403 #pragma omp parallel for
404 for(int p=0;p<num;p++) {
406 int id=omp_get_thread_num();
408 for(int a=0;a<h2;a++) {
409 RM2[a]=RM1[id*h2+a]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
413 int offset=p*loc_len;
416 for(int it=offset;it<offset+loc_len;it++) {
420 int ind2=Pbox[it]*h2;
425 for(int a=0;a<h2;a+=4) {
426 fX[a]=seq_in[ind1+a];
427 fX[a+1]=seq_in[ind1+a+1];
428 fX[a+2]=seq_in[ind1+a+2];
429 fX[a+3]=seq_in[ind1+a+3];
432 for(int a=0;a<h2;a+=4) {
433 fX[a]=Inv_Sbox2[fX[a]];
434 fX[a+1]=Inv_Sbox2[fX[a+1]];
435 fX[a+2]=Inv_Sbox2[fX[a+2]];
436 fX[a+3]=Inv_Sbox2[fX[a+3]];
441 for(int a=0;a<h2;a+=4) {
443 fX[a+1]=fX[a+1]^RM2[a+1];
444 fX[a+2]=fX[a+2]^RM2[a+2];
445 fX[a+3]=fX[a+3]^RM2[a+3];
449 for(int a=0;a<h2;a+=4) {
450 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
451 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
452 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
453 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
455 for(int a=0;a<h2;a+=4) {
456 RM2[a]=RM2[PboxRM[a]];
457 RM2[a+1]=RM2[PboxRM[a+1]];
458 RM2[a+2]=RM2[PboxRM[a+2]];
459 RM2[a+3]=RM2[PboxRM[a+3]];
471 int main(int argc, char** argv) {
480 for(int i=1; i<argc; i++){
481 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
482 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
483 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
484 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
485 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
488 /* printf("nb times %d\n",nb_test);
489 printf("ctr %d\n",ctr);
491 printf("lena %d\n",lena);
492 printf("size_buf %d\n",size_buf);
502 uchar Secretkey[key_size];
504 uchar counter[key_size];
506 for(int i=0;i<key_size;i++) {
507 Secretkey[i]=lrand48()&0xFF;
508 counter[i]=lrand48()&0xFF;
521 uchar *data_R, *data_G, *data_B;
526 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
527 imsize=width*height*3;
528 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
531 width=height=size_buf;
533 buffer=new uchar[imsize];
534 for(int i=0;i<imsize;i++) {
543 uchar* seq= new uchar[imsize];
544 uchar* seq2= new uchar[imsize];
546 int oneD=width*height;
548 for(int i=0;i<oneD;i++) {
550 seq[oneD+i]=data_G[i];
551 seq[2*oneD+i]=data_B[i];
555 for(int i=0;i<oneD;i++) {
564 int total_len=imsize;
566 int len= total_len/h2;
570 uchar *mix=new uchar[256];
575 for (int i = 0; i < 256 ; i++) {
576 mix[i]=Secretkey[i]^counter[i];
580 // cout<<"hash "<<endl;
581 for (int i = 0; i < 64 ; i++) {
590 rc4key(DK, Sbox1, 16);
593 rc4key(&DK[16], Sbox2, 16);
594 uchar Inv_Sbox1[256];
595 uchar Inv_Sbox2[256];
596 inverse_tables(Sbox1,256,Inv_Sbox1);
597 inverse_tables(Sbox2,256,Inv_Sbox2);
603 rc4key(&DK[32], sc, 16);
605 int num=omp_get_max_threads();
606 cout<<"num "<<num<<endl;
608 uchar RM1[num*(h * h)];
609 uchar RM2[num*(h * h)];
610 prga(sc, num*(h * h), RM1);
611 for(int i=0;i<num*h2;i++) {
622 for (int i = 48; i < 64; i++)
626 int *Pbox=new int[len];
630 int *PboxRM=new int[h2];
632 rc4keyperm(keyp, len, rp, Pbox, 16);
634 // printf("len %d\n",len);
635 for(int i=0;i<len;i++) {
636 // printf("%d \n",Pbox[i]);
643 double t=TimeStart();
648 for(i=0;i<nb_test;i++)
651 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
653 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
658 for(i=0;i<nb_test;i++)
661 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
663 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
668 for(i=0;i<nb_test;i++)
671 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
673 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
678 for(i=0;i<nb_test;i++)
681 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
683 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
688 for(i=0;i<nb_test;i++)
691 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
693 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
698 for(i=0;i<nb_test;i++)
701 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
703 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
709 cout<<"Time encrypt "<<time<<endl;
713 for(int i=0;i<oneD;i++) {
715 data_G[i]=seq2[oneD+i];
716 data_B[i]=seq2[2*oneD+i];
718 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
724 for(int i=0;i<imsize;i++) {
733 for(i=0;i<nb_test;i++) {
735 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
737 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
741 for(i=0;i<nb_test;i++) {
743 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
745 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
749 for(i=0;i<nb_test;i++) {
751 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
753 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
757 for(i=0;i<nb_test;i++) {
759 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
761 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
765 for(i=0;i<nb_test;i++) {
767 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
769 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
773 for(i=0;i<nb_test;i++) {
775 encrypt_ctr<128*128>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
777 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
785 cout<<"Time decrypt "<<time<<endl;
788 for(int i=0;i<oneD;i++) {
790 data_G[i]=seq[oneD+i];
791 data_B[i]=seq[2*oneD+i];
793 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
797 for(int i=0;i<imsize;i++) {
798 //cout<<"sol"<<(int)buffer[i]<<" "<<(int)seq[i]<<" "<<endl;
799 if(buffer[i]!=seq[i]) {
803 cout<<"RESULT CORRECT: "<<equal<<endl;