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) {
158 for(int a=0;a<h2;a++) {
159 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
165 #pragma omp parallel for
166 for(int p=0;p<num;p++) {
168 int id=omp_get_thread_num();
170 for(int a=0;a<h2;a++) {
171 RM2[a]=RM1[id*h2+a]; //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){
203 fX[id*h2+a+1]=X[a+1];
204 fX[id*h2+a+2]=X[a+2];
205 fX[id*h2+a+3]=X[a+3];
212 for(int a=0;a<h2;a++)
217 *(int*)&fX[id*h2+0]^=it;
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) {
239 fX[id*h2+a]=Sbox2[fX[id*h2+a]];
240 fX[id*h2+a+1]=Sbox2[fX[id*h2+a+1]];
241 fX[id*h2+a+2]=Sbox2[fX[id*h2+a+2]];
242 fX[id*h2+a+3]=Sbox2[fX[id*h2+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) {
255 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
256 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
257 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
258 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
263 for(int a=0;a<h2;a+=4) {
264 fX[id*h2+a]=fX[id*h2+a]^seq_in[ind2+a];
265 fX[id*h2+a+1]=fX[id*h2+a+1]^seq_in[ind2+a+1];
266 fX[id*h2+a+2]=fX[id*h2+a+2]^seq_in[ind2+a+2];
267 fX[id*h2+a+3]=fX[id*h2+a+3]^seq_in[ind2+a+3];
271 for(int a=0;a<h2;a+=4) {
272 seq_out[ind1+a]=fX[id*h2+a];
273 seq_out[ind1+a+1]=fX[id*h2+a+1];
274 seq_out[ind1+a+2]=fX[id*h2+a+2];
275 seq_out[ind1+a+3]=fX[id*h2+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;
307 // unsigned int *lX=(unsigned int*)X;
308 // unsigned int *lseq_in=(unsigned int*)seq_in;
310 #pragma omp parallel for
311 for(int it=0;it<len;it++) {
313 int ind2=Pbox[it]*h2;
314 int id=omp_get_thread_num();
316 for(int a=0;a<h2;a+=4) {
317 X[id*h2+a]=seq_in[ind2+a];
318 X[id*h2+a+1]=seq_in[ind2+a+1];
319 X[id*h2+a+2]=seq_in[ind2+a+2];
320 X[id*h2+a+3]=seq_in[ind2+a+3];
323 for(int a=0;a<h2;a+=4){
324 fX[id*h2+a]=Sbox1[X[id*h2+a]];
325 fX[id*h2+a+1]=Sbox1[X[id*h2+a+1]];
326 fX[id*h2+a+2]=Sbox1[X[id*h2+a+2]];
327 fX[id*h2+a+3]=Sbox1[X[id*h2+a+3]];
331 /* for(int a=0;a<h2;a+=4) {
333 fX[a+1]=fX[a+1]^RM1[a+1];
334 fX[a+2]=fX[a+2]^RM1[a+2];
335 fX[a+3]=fX[a+3]^RM1[a+3];
337 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
338 for(int a=0;a<h2;a+=4) {
339 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
340 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
341 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
342 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
345 for(int a=0;a<h2;a+=4) {
346 seq_out[ind1+a]=Sbox2[fX[id*h2+a]];
347 seq_out[ind1+a+1]=Sbox2[fX[id*h2+a+1]];
348 seq_out[ind1+a+2]=Sbox2[fX[id*h2+a+2]];
349 seq_out[ind1+a+3]=Sbox2[fX[id*h2+a+3]];
352 /* for(int a=0;a<h2;a+=4) {
353 RM1[a]=RM1[PboxRM[a]];
354 RM1[a+1]=RM1[PboxRM[a+1]];
355 RM1[a+2]=RM1[PboxRM[a+2]];
356 RM1[a+3]=RM1[PboxRM[a+3]];
370 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) {
373 /*uchar *fX=new uchar[h2];
374 uchar *Inv_Sbox1=new uchar[256];
375 uchar *Inv_Sbox2=new uchar[256];
380 #pragma omp parallel for
381 for(int it=0;it<len;it++) {
384 int ind2=Pbox[it]*h2;
385 int id=omp_get_thread_num();
389 for(int a=0;a<h2;a+=4) {
390 fX[id*h2+a]=seq_in[ind1+a];
391 fX[id*h2+a+1]=seq_in[ind1+a+1];
392 fX[id*h2+a+2]=seq_in[ind1+a+2];
393 fX[id*h2+a+3]=seq_in[ind1+a+3];
396 for(int a=0;a<h2;a+=4) {
397 fX[id*h2+a]=Inv_Sbox2[fX[id*h2+a]];
398 fX[id*h2+a+1]=Inv_Sbox2[fX[id*h2+a+1]];
399 fX[id*h2+a+2]=Inv_Sbox2[fX[id*h2+a+2]];
400 fX[id*h2+a+3]=Inv_Sbox2[fX[id*h2+a+3]];
402 /*for(int a=0;a<h2;a+=4) {
404 fX[a+1]=fX[a+1]^RM1[a+1];
405 fX[a+2]=fX[a+2]^RM1[a+2];
406 fX[a+3]=fX[a+3]^RM1[a+3];
409 for(int a=0;a<h2;a+=4) {
410 RM1[a]=RM1[PboxRM[a]];
411 RM1[a+1]=RM1[PboxRM[a+1]];
412 RM1[a+2]=RM1[PboxRM[a+2]];
413 RM1[a+3]=RM1[PboxRM[a+3]];
417 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
418 for(int a=0;a<h2;a+=4) {
419 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
420 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
421 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
422 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
426 for(int a=0;a<h2;a+=4) {
427 seq_out[ind2+a]=Inv_Sbox1[fX[id*h2+a]];
428 seq_out[ind2+a+1]=Inv_Sbox1[fX[id*h2+a+1]];
429 seq_out[ind2+a+2]=Inv_Sbox1[fX[id*h2+a+2]];
430 seq_out[ind2+a+3]=Inv_Sbox1[fX[id*h2+a+3]];
440 int main(int argc, char** argv) {
449 for(int i=1; i<argc; i++){
450 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
451 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
452 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
453 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
454 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
457 /* printf("nb times %d\n",nb_test);
458 printf("ctr %d\n",ctr);
460 printf("lena %d\n",lena);
461 printf("size_buf %d\n",size_buf);
471 uchar Secretkey[key_size];
473 uchar counter[key_size];
475 for(int i=0;i<key_size;i++) {
476 Secretkey[i]=lrand48()&0xFF;
477 counter[i]=lrand48()&0xFF;
490 uchar *data_R, *data_G, *data_B;
495 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
496 imsize=width*height*3;
497 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
500 width=height=size_buf;
502 buffer=new uchar[imsize];
503 for(int i=0;i<imsize;i++) {
512 uchar* seq= new uchar[imsize];
513 uchar* seq2= new uchar[imsize];
515 int oneD=width*height;
517 for(int i=0;i<oneD;i++) {
519 seq[oneD+i]=data_G[i];
520 seq[2*oneD+i]=data_B[i];
524 for(int i=0;i<oneD;i++) {
533 int total_len=imsize;
535 int len= total_len/h2;
539 uchar *mix=new uchar[256];
544 for (int i = 0; i < 256 ; i++) {
545 mix[i]=Secretkey[i]^counter[i];
549 // cout<<"hash "<<endl;
550 for (int i = 0; i < 64 ; i++) {
559 rc4key(DK, Sbox1, 16);
562 rc4key(&DK[16], Sbox2, 16);
563 uchar Inv_Sbox1[256];
564 uchar Inv_Sbox2[256];
565 inverse_tables(Sbox1,256,Inv_Sbox1);
566 inverse_tables(Sbox2,256,Inv_Sbox2);
572 rc4key(&DK[32], sc, 16);
574 int num=omp_get_max_threads();
575 cout<<"num "<<num<<endl;
577 uchar RM1[num*(h * h)];
578 uchar RM2[num*(h * h)];
579 prga(sc, num*(h * h), RM1);
580 for(int i=0;i<num*h2;i++) {
591 for (int i = 48; i < 64; i++)
595 int *Pbox=new int[len];
599 int *PboxRM=new int[h2];
601 rc4keyperm(keyp, len, rp, Pbox, 16);
603 // printf("len %d\n",len);
604 for(int i=0;i<len;i++) {
605 // printf("%d \n",Pbox[i]);
612 double t=TimeStart();
617 for(i=0;i<nb_test;i++)
620 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
622 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
627 for(i=0;i<nb_test;i++)
630 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
632 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
637 for(i=0;i<nb_test;i++)
640 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
642 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
647 for(i=0;i<nb_test;i++)
650 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
652 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
657 for(i=0;i<nb_test;i++)
660 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
662 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
667 for(i=0;i<nb_test;i++)
670 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
672 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
678 cout<<"Time encrypt "<<time<<endl;
682 for(int i=0;i<oneD;i++) {
684 data_G[i]=seq2[oneD+i];
685 data_B[i]=seq2[2*oneD+i];
687 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
693 for(int i=0;i<imsize;i++) {
702 for(i=0;i<nb_test;i++) {
704 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
706 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
710 for(i=0;i<nb_test;i++) {
712 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
714 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
718 for(i=0;i<nb_test;i++) {
720 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
722 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
726 for(i=0;i<nb_test;i++) {
728 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
730 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
734 for(i=0;i<nb_test;i++) {
736 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
738 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
742 for(i=0;i<nb_test;i++) {
744 encrypt_ctr<128*128>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
746 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
754 cout<<"Time decrypt "<<time<<endl;
757 for(int i=0;i<oneD;i++) {
759 data_G[i]=seq[oneD+i];
760 data_B[i]=seq[2*oneD+i];
762 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
766 for(int i=0;i<imsize;i++) {
767 //cout<<"sol"<<(int)buffer[i]<<" "<<(int)seq[i]<<" "<<endl;
768 if(buffer[i]!=seq[i]) {
772 cout<<"RESULT CORRECT: "<<equal<<endl;