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+10*id)&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) {
196 X[a+1]=X[Sbox1[a+1]];
197 X[a+2]=X[Sbox1[a+2]];
198 X[a+3]=X[Sbox1[a+3]];
201 for(int a=0;a<h2;a+=4){
215 for(int a=0;a<h2;a+=4) {
217 fX[a+1]=Sbox2[fX[a+1]];
218 fX[a+2]=Sbox2[fX[a+2]];
219 fX[a+3]=Sbox2[fX[a+3]];
222 // rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
224 for(int a=0;a<h2;a+=4) {
225 RM2[a]=RM2[PboxRM[a]];
226 RM2[a+1]=RM2[PboxRM[a+1]];
227 RM2[a+2]=RM2[PboxRM[a+2]];
228 RM2[a+3]=RM2[PboxRM[a+3]];
231 for(int a=0;a<h2;a+=4) {
233 fX[a+1]=fX[a+1]^RM2[a+1];
234 fX[a+2]=fX[a+2]^RM2[a+2];
235 fX[a+3]=fX[a+3]^RM2[a+3];
240 for(int a=0;a<h2;a+=4) {
241 fX[a]=fX[a]^seq_in[ind2+a];
242 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
243 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
244 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
248 for(int a=0;a<h2;a+=4) {
249 seq_out[ind1+a]=fX[a];
250 seq_out[ind1+a+1]=fX[a+1];
251 seq_out[ind1+a+2]=fX[a+2];
252 seq_out[ind1+a+3]=fX[a+3];
255 /*for(int a=0;a<h2;a+=4) {
256 RM1[id*h2+a]=RM1[id*h2+PboxRM[a]];
257 RM1[id*h2+a+1]=RM1[id*h2+PboxRM[a+1]];
258 RM1[id*h2+a+2]=RM1[id*h2+PboxRM[a+2]];
259 RM1[id*h2+a+3]=RM1[id*h2+PboxRM[a+3]];
273 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug, int num) {
276 /* uchar *X=new uchar[h2];
277 uchar *fX=new uchar[h2];
278 unsigned int *lX=(unsigned int*)X;
279 unsigned int *lseq_in=(unsigned int*)seq_in;
282 // unsigned int *lX=(unsigned int*)X;
283 // unsigned int *lseq_in=(unsigned int*)seq_in;
288 #pragma omp parallel for
289 for(int p=0;p<num;p++) {
291 int id=omp_get_thread_num();
297 for(int a=0;a<h2;a++) {
302 int offset=p*loc_len;
305 for(int it=offset;it<offset+loc_len;it++) {
310 int ind2=Pbox[it]*h2;
313 for(int a=0;a<h2;a+=4) {
314 fX[a]=seq_in[ind2+a];
315 fX[a+1]=seq_in[ind2+a+1];
316 fX[a+2]=seq_in[ind2+a+2];
317 fX[a+3]=seq_in[ind2+a+3];
321 for(int a=0;a<h2;a+=4){
323 fX[a+1]=Sbox1[fX[a+1]];
324 fX[a+2]=Sbox1[fX[a+2]];
325 fX[a+3]=Sbox1[fX[a+3]];
329 for(int a=0;a<h2;a+=4) {
331 fX[a+1]=fX[a+1]^RM2[a+1];
332 fX[a+2]=fX[a+2]^RM2[a+2];
333 fX[a+3]=fX[a+3]^RM2[a+3];
339 for(int a=0;a<h2;a+=4) {
340 seq_out[ind1+a]=Sbox2[fX[a]];
341 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
342 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
343 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
347 for(int a=0;a<h2;a+=4) {
348 RM2[a]=RM2[PboxRM[a]];
349 RM2[a+1]=RM2[PboxRM[a+1]];
350 RM2[a+2]=RM2[PboxRM[a+2]];
351 RM2[a+3]=RM2[PboxRM[a+3]];
366 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) {
369 /*uchar *fX=new uchar[h2];
370 uchar *Inv_Sbox1=new uchar[256];
371 uchar *Inv_Sbox2=new uchar[256];
380 #pragma omp parallel for
381 for(int p=0;p<num;p++) {
383 int id=omp_get_thread_num();
385 for(int a=0;a<h2;a++) {
386 RM2[a]=RM1[id*h2+a]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
390 int offset=p*loc_len;
393 for(int it=offset;it<offset+loc_len;it++) {
397 int ind2=Pbox[it]*h2;
402 for(int a=0;a<h2;a+=4) {
403 fX[a]=seq_in[ind1+a];
404 fX[a+1]=seq_in[ind1+a+1];
405 fX[a+2]=seq_in[ind1+a+2];
406 fX[a+3]=seq_in[ind1+a+3];
409 for(int a=0;a<h2;a+=4) {
410 fX[a]=Inv_Sbox2[fX[a]];
411 fX[a+1]=Inv_Sbox2[fX[a+1]];
412 fX[a+2]=Inv_Sbox2[fX[a+2]];
413 fX[a+3]=Inv_Sbox2[fX[a+3]];
418 for(int a=0;a<h2;a+=4) {
420 fX[a+1]=fX[a+1]^RM2[a+1];
421 fX[a+2]=fX[a+2]^RM2[a+2];
422 fX[a+3]=fX[a+3]^RM2[a+3];
426 for(int a=0;a<h2;a+=4) {
427 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
428 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
429 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
430 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
432 for(int a=0;a<h2;a+=4) {
433 RM2[a]=RM2[PboxRM[a]];
434 RM2[a+1]=RM2[PboxRM[a+1]];
435 RM2[a+2]=RM2[PboxRM[a+2]];
436 RM2[a+3]=RM2[PboxRM[a+3]];
448 int main(int argc, char** argv) {
457 for(int i=1; i<argc; i++){
458 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
459 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
460 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
461 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
462 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
465 /* printf("nb times %d\n",nb_test);
466 printf("ctr %d\n",ctr);
468 printf("lena %d\n",lena);
469 printf("size_buf %d\n",size_buf);
479 uchar Secretkey[key_size];
481 uchar counter[key_size];
483 for(int i=0;i<key_size;i++) {
484 Secretkey[i]=lrand48()&0xFF;
485 counter[i]=lrand48()&0xFF;
498 uchar *data_R, *data_G, *data_B;
503 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
504 imsize=width*height*3;
505 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
508 width=height=size_buf;
510 buffer=new uchar[imsize];
511 for(int i=0;i<imsize;i++) {
520 uchar* seq= new uchar[imsize];
521 uchar* seq2= new uchar[imsize];
523 int oneD=width*height;
525 for(int i=0;i<oneD;i++) {
527 seq[oneD+i]=data_G[i];
528 seq[2*oneD+i]=data_B[i];
532 for(int i=0;i<oneD;i++) {
541 int total_len=imsize;
543 int len= total_len/h2;
547 uchar *mix=new uchar[256];
552 for (int i = 0; i < 256 ; i++) {
553 mix[i]=Secretkey[i]^counter[i];
557 // cout<<"hash "<<endl;
558 for (int i = 0; i < 64 ; i++) {
567 rc4key(DK, Sbox1, 16);
570 rc4key(&DK[16], Sbox2, 16);
571 uchar Inv_Sbox1[256];
572 uchar Inv_Sbox2[256];
573 inverse_tables(Sbox1,256,Inv_Sbox1);
574 inverse_tables(Sbox2,256,Inv_Sbox2);
580 rc4key(&DK[32], sc, 16);
582 int num=omp_get_max_threads();
583 cout<<"num "<<num<<endl;
594 uchar RM1[num*(h * h)];
595 uchar RM2[num*(h * h)];
596 /*for(int i=0;i<num;i++) {
598 rc4key(&DK[48+i*16], sc, 16);
599 prga(sc, h2, &RM1[h2*i]);
600 for(int a=0;a<h2;a++) {
601 cout<<(int)RM1[h2*i+a]<<" ";
606 rc4key(&DK[48], sc, 16);
607 prga(sc, h2*num, RM1);
609 rc4key(&DK[64], sc, 16);
617 int *Pbox=new int[len];
621 int *PboxRM=new int[h2];
623 rc4keyperm(&DK[48+16*num], len, rp, Pbox, 16);
626 rc4keyperm(RM2, h2, rp, PboxRM, h2);
628 for(int i=0;i<num*h2;i++) {
633 double t=TimeStart();
638 for(i=0;i<nb_test;i++)
641 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
643 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
648 for(i=0;i<nb_test;i++)
651 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
653 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
658 for(i=0;i<nb_test;i++)
661 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
663 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
668 for(i=0;i<nb_test;i++)
671 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
673 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
678 for(i=0;i<nb_test;i++)
681 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
683 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
688 for(i=0;i<nb_test;i++)
691 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
693 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
699 cout<<"Time encrypt "<<time<<endl;
703 for(int i=0;i<oneD;i++) {
705 data_G[i]=seq2[oneD+i];
706 data_B[i]=seq2[2*oneD+i];
708 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
714 for(int i=0;i<imsize;i++) {
723 for(i=0;i<nb_test;i++) {
725 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
727 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
731 for(i=0;i<nb_test;i++) {
733 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
735 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
739 for(i=0;i<nb_test;i++) {
741 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
743 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
747 for(i=0;i<nb_test;i++) {
749 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
751 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
755 for(i=0;i<nb_test;i++) {
757 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
759 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
763 for(i=0;i<nb_test;i++) {
765 encrypt_ctr<128*128>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
767 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
775 cout<<"Time decrypt "<<time<<endl;
778 for(int i=0;i<oneD;i++) {
780 data_G[i]=seq[oneD+i];
781 data_B[i]=seq[2*oneD+i];
783 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
787 for(int i=0;i<imsize;i++) {
788 //cout<<"sol"<<(int)buffer[i]<<" "<<(int)seq[i]<<" "<<endl;
789 if(buffer[i]!=seq[i]) {
793 cout<<"RESULT CORRECT: "<<equal<<endl;