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];
149 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) {
159 for(int a=0;a<h2;a++) {
160 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
163 #pragma omp parallel for
164 for(int it=0;it<len;it++) {
166 int id=omp_get_thread_num();
168 //cout<<id<<" "<<it<<endl;
180 /*for(int a=0;a<h2;a+=4){
187 for(int a=0;a<h2;a+=4){
189 fX[id*h2+a+1]=X[a+1];
190 fX[id*h2+a+2]=X[a+2];
191 fX[id*h2+a+3]=X[a+3];
198 for(int a=0;a<h2;a++)
203 *(int*)&fX[id*h2+0]^=it;
206 for(int a=0;a<h2;a++)
214 /*for(int a=0;a<h2;a+=4) {
215 fX[id*h2+a]=fX[id*h2+a]^RM1[id*h2+a];
216 fX[id*h2+a+1]=fX[id*h2+a+1]^RM1[id*h2+a+1];
217 fX[id*h2+a+2]=fX[id*h2+a+2]^RM1[id*h2+a+2];
218 fX[id*h2+a+3]=fX[id*h2+a+3]^RM1[id*h2+a+3];
224 for(int a=0;a<h2;a+=4) {
225 fX[id*h2+a]=Sbox2[fX[id*h2+a]];
226 fX[id*h2+a+1]=Sbox2[fX[id*h2+a+1]];
227 fX[id*h2+a+2]=Sbox2[fX[id*h2+a+2]];
228 fX[id*h2+a+3]=Sbox2[fX[id*h2+a+3]];
231 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
232 for(int a=0;a<h2;a+=4) {
233 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
234 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
235 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
236 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
241 for(int a=0;a<h2;a+=4) {
242 fX[id*h2+a]=fX[id*h2+a]^seq_in[ind2+a];
243 fX[id*h2+a+1]=fX[id*h2+a+1]^seq_in[ind2+a+1];
244 fX[id*h2+a+2]=fX[id*h2+a+2]^seq_in[ind2+a+2];
245 fX[id*h2+a+3]=fX[id*h2+a+3]^seq_in[ind2+a+3];
249 for(int a=0;a<h2;a+=4) {
250 seq_out[ind1+a]=fX[id*h2+a];
251 seq_out[ind1+a+1]=fX[id*h2+a+1];
252 seq_out[ind1+a+2]=fX[id*h2+a+2];
253 seq_out[ind1+a+3]=fX[id*h2+a+3];
256 /*for(int a=0;a<h2;a+=4) {
257 RM1[id*h2+a]=RM1[id*h2+PboxRM[a]];
258 RM1[id*h2+a+1]=RM1[id*h2+PboxRM[a+1]];
259 RM1[id*h2+a+2]=RM1[id*h2+PboxRM[a+2]];
260 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;
284 // unsigned int *lX=(unsigned int*)X;
285 // unsigned int *lseq_in=(unsigned int*)seq_in;
287 #pragma omp parallel for
288 for(int it=0;it<len;it++) {
290 int ind2=Pbox[it]*h2;
291 int id=omp_get_thread_num();
293 for(int a=0;a<h2;a+=4) {
294 X[id*h2+a]=seq_in[ind2+a];
295 X[id*h2+a+1]=seq_in[ind2+a+1];
296 X[id*h2+a+2]=seq_in[ind2+a+2];
297 X[id*h2+a+3]=seq_in[ind2+a+3];
300 for(int a=0;a<h2;a+=4){
301 fX[id*h2+a]=Sbox1[X[id*h2+a]];
302 fX[id*h2+a+1]=Sbox1[X[id*h2+a+1]];
303 fX[id*h2+a+2]=Sbox1[X[id*h2+a+2]];
304 fX[id*h2+a+3]=Sbox1[X[id*h2+a+3]];
308 /* for(int a=0;a<h2;a+=4) {
310 fX[a+1]=fX[a+1]^RM1[a+1];
311 fX[a+2]=fX[a+2]^RM1[a+2];
312 fX[a+3]=fX[a+3]^RM1[a+3];
314 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
315 for(int a=0;a<h2;a+=4) {
316 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
317 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
318 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
319 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
322 for(int a=0;a<h2;a+=4) {
323 seq_out[ind1+a]=Sbox2[fX[id*h2+a]];
324 seq_out[ind1+a+1]=Sbox2[fX[id*h2+a+1]];
325 seq_out[ind1+a+2]=Sbox2[fX[id*h2+a+2]];
326 seq_out[ind1+a+3]=Sbox2[fX[id*h2+a+3]];
329 /* for(int a=0;a<h2;a+=4) {
330 RM1[a]=RM1[PboxRM[a]];
331 RM1[a+1]=RM1[PboxRM[a+1]];
332 RM1[a+2]=RM1[PboxRM[a+2]];
333 RM1[a+3]=RM1[PboxRM[a+3]];
347 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) {
350 /*uchar *fX=new uchar[h2];
351 uchar *Inv_Sbox1=new uchar[256];
352 uchar *Inv_Sbox2=new uchar[256];
357 #pragma omp parallel for
358 for(int it=0;it<len;it++) {
361 int ind2=Pbox[it]*h2;
362 int id=omp_get_thread_num();
366 for(int a=0;a<h2;a+=4) {
367 fX[id*h2+a]=seq_in[ind1+a];
368 fX[id*h2+a+1]=seq_in[ind1+a+1];
369 fX[id*h2+a+2]=seq_in[ind1+a+2];
370 fX[id*h2+a+3]=seq_in[ind1+a+3];
373 for(int a=0;a<h2;a+=4) {
374 fX[id*h2+a]=Inv_Sbox2[fX[id*h2+a]];
375 fX[id*h2+a+1]=Inv_Sbox2[fX[id*h2+a+1]];
376 fX[id*h2+a+2]=Inv_Sbox2[fX[id*h2+a+2]];
377 fX[id*h2+a+3]=Inv_Sbox2[fX[id*h2+a+3]];
379 /*for(int a=0;a<h2;a+=4) {
381 fX[a+1]=fX[a+1]^RM1[a+1];
382 fX[a+2]=fX[a+2]^RM1[a+2];
383 fX[a+3]=fX[a+3]^RM1[a+3];
386 for(int a=0;a<h2;a+=4) {
387 RM1[a]=RM1[PboxRM[a]];
388 RM1[a+1]=RM1[PboxRM[a+1]];
389 RM1[a+2]=RM1[PboxRM[a+2]];
390 RM1[a+3]=RM1[PboxRM[a+3]];
394 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
395 for(int a=0;a<h2;a+=4) {
396 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
397 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
398 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
399 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
403 for(int a=0;a<h2;a+=4) {
404 seq_out[ind2+a]=Inv_Sbox1[fX[id*h2+a]];
405 seq_out[ind2+a+1]=Inv_Sbox1[fX[id*h2+a+1]];
406 seq_out[ind2+a+2]=Inv_Sbox1[fX[id*h2+a+2]];
407 seq_out[ind2+a+3]=Inv_Sbox1[fX[id*h2+a+3]];
417 int main(int argc, char** argv) {
426 for(int i=1; i<argc; i++){
427 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
428 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
429 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
430 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
431 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
434 /* printf("nb times %d\n",nb_test);
435 printf("ctr %d\n",ctr);
437 printf("lena %d\n",lena);
438 printf("size_buf %d\n",size_buf);
448 uchar Secretkey[key_size];
450 uchar counter[key_size];
452 for(int i=0;i<key_size;i++) {
453 Secretkey[i]=lrand48()&0xFF;
454 counter[i]=lrand48()&0xFF;
467 uchar *data_R, *data_G, *data_B;
472 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
473 imsize=width*height*3;
474 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
477 width=height=size_buf;
479 buffer=new uchar[imsize];
480 for(int i=0;i<imsize;i++) {
489 uchar* seq= new uchar[imsize];
490 uchar* seq2= new uchar[imsize];
492 int oneD=width*height;
494 for(int i=0;i<oneD;i++) {
496 seq[oneD+i]=data_G[i];
497 seq[2*oneD+i]=data_B[i];
501 for(int i=0;i<oneD;i++) {
510 int total_len=imsize;
512 int len= total_len/h2;
516 uchar *mix=new uchar[256];
521 for (int i = 0; i < 256 ; i++) {
522 mix[i]=Secretkey[i]^counter[i];
526 // cout<<"hash "<<endl;
527 for (int i = 0; i < 64 ; i++) {
536 rc4key(DK, Sbox1, 16);
539 rc4key(&DK[16], Sbox2, 16);
540 uchar Inv_Sbox1[256];
541 uchar Inv_Sbox2[256];
542 inverse_tables(Sbox1,256,Inv_Sbox1);
543 inverse_tables(Sbox2,256,Inv_Sbox2);
549 rc4key(&DK[32], sc, 16);
551 uchar outd[2*(h * h)];
552 prga(sc, 2*(h * h), outd);
554 int num=omp_get_max_threads();
555 cout<<"num "<<num<<endl;
559 for(int i=0;i<h2;i++){
570 for (int i = 48; i < 64; i++)
574 int *Pbox=new int[len];
578 int *PboxRM=new int[h2];
580 rc4keyperm(keyp, len, rp, Pbox, 16);
582 // printf("len %d\n",len);
583 for(int i=0;i<len;i++) {
584 // printf("%d \n",Pbox[i]);
587 rc4keyperm(RM2, h2, rp, PboxRM, h2);
589 for(int i=0;i<h2;i++){
595 double t=TimeStart();
600 for(i=0;i<nb_test;i++)
603 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
605 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
610 for(i=0;i<nb_test;i++)
613 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
615 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
620 for(i=0;i<nb_test;i++)
623 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
625 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
630 for(i=0;i<nb_test;i++)
633 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
635 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
640 for(i=0;i<nb_test;i++)
643 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
645 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
650 for(i=0;i<nb_test;i++)
653 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
655 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
661 cout<<"Time encrypt "<<time<<endl;
665 for(int i=0;i<oneD;i++) {
667 data_G[i]=seq2[oneD+i];
668 data_B[i]=seq2[2*oneD+i];
670 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
676 for(int i=0;i<imsize;i++) {
685 for(i=0;i<nb_test;i++) {
687 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
689 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
693 for(i=0;i<nb_test;i++) {
695 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
697 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
701 for(i=0;i<nb_test;i++) {
703 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
705 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
709 for(i=0;i<nb_test;i++) {
711 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
713 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
717 for(i=0;i<nb_test;i++) {
719 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
721 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
725 for(i=0;i<nb_test;i++) {
727 encrypt_ctr<128*128>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
729 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
737 cout<<"Time decrypt "<<time<<endl;
740 for(int i=0;i<oneD;i++) {
742 data_G[i]=seq[oneD+i];
743 data_B[i]=seq[2*oneD+i];
745 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
749 for(int i=0;i<imsize;i++) {
750 //cout<<"sol"<<(int)buffer[i]<<" "<<(int)seq[i]<<" "<<endl;
751 if(buffer[i]!=seq[i]) {
755 cout<<"RESULT CORRECT: "<<equal<<endl;