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]
162 #pragma omp parallel for
163 for(int it=0;it<len;it++) {
165 int id=omp_get_thread_num();
167 //cout<<id<<" "<<it<<endl;
179 /*for(int a=0;a<h2;a+=4){
186 for(int a=0;a<h2;a+=4){
188 fX[id*h2+a+1]=X[a+1];
189 fX[id*h2+a+2]=X[a+2];
190 fX[id*h2+a+3]=X[a+3];
197 for(int a=0;a<h2;a++)
202 *(int*)&fX[id*h2+0]^=it;
205 for(int a=0;a<h2;a++)
213 /*for(int a=0;a<h2;a+=4) {
214 fX[id*h2+a]=fX[id*h2+a]^RM1[id*h2+a];
215 fX[id*h2+a+1]=fX[id*h2+a+1]^RM1[id*h2+a+1];
216 fX[id*h2+a+2]=fX[id*h2+a+2]^RM1[id*h2+a+2];
217 fX[id*h2+a+3]=fX[id*h2+a+3]^RM1[id*h2+a+3];
223 for(int a=0;a<h2;a+=4) {
224 fX[id*h2+a]=Sbox2[fX[id*h2+a]];
225 fX[id*h2+a+1]=Sbox2[fX[id*h2+a+1]];
226 fX[id*h2+a+2]=Sbox2[fX[id*h2+a+2]];
227 fX[id*h2+a+3]=Sbox2[fX[id*h2+a+3]];
230 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
231 for(int a=0;a<h2;a+=4) {
232 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
233 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
234 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
235 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
240 for(int a=0;a<h2;a+=4) {
241 fX[id*h2+a]=fX[id*h2+a]^seq_in[ind2+a];
242 fX[id*h2+a+1]=fX[id*h2+a+1]^seq_in[ind2+a+1];
243 fX[id*h2+a+2]=fX[id*h2+a+2]^seq_in[ind2+a+2];
244 fX[id*h2+a+3]=fX[id*h2+a+3]^seq_in[ind2+a+3];
248 for(int a=0;a<h2;a+=4) {
249 seq_out[ind1+a]=fX[id*h2+a];
250 seq_out[ind1+a+1]=fX[id*h2+a+1];
251 seq_out[ind1+a+2]=fX[id*h2+a+2];
252 seq_out[ind1+a+3]=fX[id*h2+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]];
271 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug, int num) {
274 /* uchar *X=new uchar[h2];
275 uchar *fX=new uchar[h2];
276 unsigned int *lX=(unsigned int*)X;
277 unsigned int *lseq_in=(unsigned int*)seq_in;
282 // unsigned int *lX=(unsigned int*)X;
283 // unsigned int *lseq_in=(unsigned int*)seq_in;
285 #pragma omp parallel for
286 for(int it=0;it<len;it++) {
288 int ind2=Pbox[it]*h2;
289 int id=omp_get_thread_num();
291 for(int a=0;a<h2;a+=4) {
292 X[id*h2+a]=seq_in[ind2+a];
293 X[id*h2+a+1]=seq_in[ind2+a+1];
294 X[id*h2+a+2]=seq_in[ind2+a+2];
295 X[id*h2+a+3]=seq_in[ind2+a+3];
298 for(int a=0;a<h2;a+=4){
299 fX[id*h2+a]=Sbox1[X[id*h2+a]];
300 fX[id*h2+a+1]=Sbox1[X[id*h2+a+1]];
301 fX[id*h2+a+2]=Sbox1[X[id*h2+a+2]];
302 fX[id*h2+a+3]=Sbox1[X[id*h2+a+3]];
306 /* for(int a=0;a<h2;a+=4) {
308 fX[a+1]=fX[a+1]^RM1[a+1];
309 fX[a+2]=fX[a+2]^RM1[a+2];
310 fX[a+3]=fX[a+3]^RM1[a+3];
312 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
313 for(int a=0;a<h2;a+=4) {
314 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
315 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
316 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
317 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
320 for(int a=0;a<h2;a+=4) {
321 seq_out[ind1+a]=Sbox2[fX[id*h2+a]];
322 seq_out[ind1+a+1]=Sbox2[fX[id*h2+a+1]];
323 seq_out[ind1+a+2]=Sbox2[fX[id*h2+a+2]];
324 seq_out[ind1+a+3]=Sbox2[fX[id*h2+a+3]];
327 /* for(int a=0;a<h2;a+=4) {
328 RM1[a]=RM1[PboxRM[a]];
329 RM1[a+1]=RM1[PboxRM[a+1]];
330 RM1[a+2]=RM1[PboxRM[a+2]];
331 RM1[a+3]=RM1[PboxRM[a+3]];
345 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) {
348 /*uchar *fX=new uchar[h2];
349 uchar *Inv_Sbox1=new uchar[256];
350 uchar *Inv_Sbox2=new uchar[256];
355 #pragma omp parallel for
356 for(int it=0;it<len;it++) {
359 int ind2=Pbox[it]*h2;
360 int id=omp_get_thread_num();
364 for(int a=0;a<h2;a+=4) {
365 fX[id*h2+a]=seq_in[ind1+a];
366 fX[id*h2+a+1]=seq_in[ind1+a+1];
367 fX[id*h2+a+2]=seq_in[ind1+a+2];
368 fX[id*h2+a+3]=seq_in[ind1+a+3];
371 for(int a=0;a<h2;a+=4) {
372 fX[id*h2+a]=Inv_Sbox2[fX[id*h2+a]];
373 fX[id*h2+a+1]=Inv_Sbox2[fX[id*h2+a+1]];
374 fX[id*h2+a+2]=Inv_Sbox2[fX[id*h2+a+2]];
375 fX[id*h2+a+3]=Inv_Sbox2[fX[id*h2+a+3]];
377 /*for(int a=0;a<h2;a+=4) {
379 fX[a+1]=fX[a+1]^RM1[a+1];
380 fX[a+2]=fX[a+2]^RM1[a+2];
381 fX[a+3]=fX[a+3]^RM1[a+3];
384 for(int a=0;a<h2;a+=4) {
385 RM1[a]=RM1[PboxRM[a]];
386 RM1[a+1]=RM1[PboxRM[a+1]];
387 RM1[a+2]=RM1[PboxRM[a+2]];
388 RM1[a+3]=RM1[PboxRM[a+3]];
392 rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
393 for(int a=0;a<h2;a+=4) {
394 fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
395 fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
396 fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
397 fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
401 for(int a=0;a<h2;a+=4) {
402 seq_out[ind2+a]=Inv_Sbox1[fX[id*h2+a]];
403 seq_out[ind2+a+1]=Inv_Sbox1[fX[id*h2+a+1]];
404 seq_out[ind2+a+2]=Inv_Sbox1[fX[id*h2+a+2]];
405 seq_out[ind2+a+3]=Inv_Sbox1[fX[id*h2+a+3]];
415 int main(int argc, char** argv) {
424 for(int i=1; i<argc; i++){
425 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
426 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
427 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
428 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
429 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
432 /* printf("nb times %d\n",nb_test);
433 printf("ctr %d\n",ctr);
435 printf("lena %d\n",lena);
436 printf("size_buf %d\n",size_buf);
446 uchar Secretkey[key_size];
448 uchar counter[key_size];
450 for(int i=0;i<key_size;i++) {
451 Secretkey[i]=lrand48()&0xFF;
452 counter[i]=lrand48()&0xFF;
465 uchar *data_R, *data_G, *data_B;
470 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
471 imsize=width*height*3;
472 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
475 width=height=size_buf;
477 buffer=new uchar[imsize];
478 for(int i=0;i<imsize;i++) {
487 uchar* seq= new uchar[imsize];
488 uchar* seq2= new uchar[imsize];
490 int oneD=width*height;
492 for(int i=0;i<oneD;i++) {
494 seq[oneD+i]=data_G[i];
495 seq[2*oneD+i]=data_B[i];
499 for(int i=0;i<oneD;i++) {
508 int total_len=imsize;
510 int len= total_len/h2;
514 uchar *mix=new uchar[256];
519 for (int i = 0; i < 256 ; i++) {
520 mix[i]=Secretkey[i]^counter[i];
524 // cout<<"hash "<<endl;
525 for (int i = 0; i < 64 ; i++) {
534 rc4key(DK, Sbox1, 16);
537 rc4key(&DK[16], Sbox2, 16);
538 uchar Inv_Sbox1[256];
539 uchar Inv_Sbox2[256];
540 inverse_tables(Sbox1,256,Inv_Sbox1);
541 inverse_tables(Sbox2,256,Inv_Sbox2);
547 rc4key(&DK[32], sc, 16);
549 uchar outd[2*(h * h)];
550 prga(sc, 2*(h * h), outd);
552 int num=omp_get_max_threads();
553 cout<<"num "<<num<<endl;
557 for(int i=0;i<h2;i++){
568 for (int i = 48; i < 64; i++)
572 int *Pbox=new int[len];
576 int *PboxRM=new int[h2];
578 rc4keyperm(keyp, len, rp, Pbox, 16);
580 // printf("len %d\n",len);
581 for(int i=0;i<len;i++) {
582 // printf("%d \n",Pbox[i]);
585 rc4keyperm(RM2, h2, rp, PboxRM, h2);
587 for(int i=0;i<h2;i++){
593 double t=TimeStart();
598 for(i=0;i<nb_test;i++)
601 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
603 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
608 for(i=0;i<nb_test;i++)
611 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
613 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
618 for(i=0;i<nb_test;i++)
621 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
623 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
628 for(i=0;i<nb_test;i++)
631 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
633 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
638 for(i=0;i<nb_test;i++)
641 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
643 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
648 for(i=0;i<nb_test;i++)
651 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1,num);
653 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0,num);
659 cout<<"Time encrypt "<<time<<endl;
663 for(int i=0;i<oneD;i++) {
665 data_G[i]=seq2[oneD+i];
666 data_B[i]=seq2[2*oneD+i];
668 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
674 for(int i=0;i<imsize;i++) {
683 for(i=0;i<nb_test;i++) {
685 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
687 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
691 for(i=0;i<nb_test;i++) {
693 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
695 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
699 for(i=0;i<nb_test;i++) {
701 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
703 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
707 for(i=0;i<nb_test;i++) {
709 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
711 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
715 for(i=0;i<nb_test;i++) {
717 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
719 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
723 for(i=0;i<nb_test;i++) {
725 encrypt_ctr<128*128>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0,num);
727 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0,num);
735 cout<<"Time decrypt "<<time<<endl;
738 for(int i=0;i<oneD;i++) {
740 data_G[i]=seq[oneD+i];
741 data_B[i]=seq[2*oneD+i];
743 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
747 for(int i=0;i<imsize;i++) {
748 //cout<<"sol"<<(int)buffer[i]<<" "<<(int)seq[i]<<" "<<endl;
749 if(buffer[i]!=seq[i]) {
753 cout<<"RESULT CORRECT: "<<equal<<endl;