2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
13 /*#include <cryptopp/hex.h>
14 #include <cryptopp/sha.h>
15 #include <cryptopp/osrng.h>
16 #include <cryptopp/secblock.h>
21 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
22 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
26 //using namespace CryptoPP;
40 typedef unsigned char uchar;
45 struct timeval tstart;
46 gettimeofday(&tstart,0);
47 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
50 double TimeStop(double t)
54 gettimeofday(&tend,0);
55 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
64 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
66 for(int i=0;i<size_tab;i++) {
67 inv_perm_tabs[tab[i]] = i;
72 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
74 for(int i=0;i<size_tab;i++) {
75 inv_perm_tabs[tab[i]] = i;
82 void rc4key(uchar *key, uchar *sc, int size_DK) {
84 for(int i=0;i<256;i++) {
90 for(int i0=0; i0<256; i0++) {
91 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
100 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
106 for (int i=0;i<len;i++) {
109 for (int it = 0; it < rp; it++) {
111 for(int i0 = 0; i0<len; i0++) {
112 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
121 void prga(uchar *sc, int ldata, uchar *r) {
125 for (int it=0; it<ldata; it++) {
126 i0 = ((i0+1)&0xFE); //%255);
127 j0 = (j0 + sc[i0])&0xFF;
131 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
137 void myhash(uchar *X, uchar* RM2, uchar *Sbox2) {
141 for(int k=0;k<h;k++) {
144 for(int a=0;a<h;a+=4) {
145 X2[a]=RM2[a]^X[k*h+a];
146 X2[a+1]=RM2[a+1]^X[k*h+a+1];
147 X2[a+2]=RM2[a+2]^X[k*h+a+2];
148 X2[a+3]=RM2[a+3]^X[k*h+a+3];
156 for(int a=4;a<h;a+=4) {
159 Y[a+2]=Y[a+1]^X2[a+1];
160 Y[a+3]=Y[a+2]^X2[a+2];
164 for(int a=0;a<h;a+=4) {
166 Y[a+1]=Sbox2[Y[a+1]];
167 Y[a+2]=Sbox2[Y[a+2]];
168 Y[a+3]=Sbox2[Y[a+3]];
173 RM2[h-1]=Y[h-1]^Y[0];
174 RM2[h-2]=RM2[h-1]^Y[h-1];
175 RM2[h-3]=RM2[h-2]^Y[h-2];
176 RM2[h-4]=RM2[h-3]^Y[h-3];
177 for(int a=h-4;a>0;a-=4) {
178 RM2[a-1]=RM2[a]^Y[a];
179 RM2[a-2]=RM2[a-1]^Y[a-1];
180 RM2[a-3]=RM2[a-2]^Y[a-2];
181 RM2[a-4]=RM2[a-3]^Y[a-3];
187 template<int h2, int h>
188 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,uchar *RM2,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc) {
191 // uchar *X=new uchar[h2];
192 // uchar *fX=new uchar[h2];
202 for(int a=0;a<h2;a+=4) {
203 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
204 X[a+1]=Sbox1[(a+1)&0xFF];
205 X[a+2]=Sbox1[(a+2)&0xFF];
206 X[a+3]=Sbox1[(a+3)&0xFF];
210 for(int it=0;it<len;it++) {
221 for(int a=0;a<h2;a+=4) {
223 X[a+1]=Sbox1[X[a+1]];
224 X[a+2]=Sbox1[X[a+2]];
225 X[a+3]=Sbox1[X[a+3]];
228 for(int a=0;a<h2;a+=4) {
230 fX[a+1]=X[a+1]^RM1[a+1];
231 fX[a+2]=X[a+2]^RM1[a+2];
232 fX[a+3]=X[a+3]^RM1[a+3];
235 for(int a=0;a<h2;a+=4) {
236 fX[a]=fX[a]^seq_in[ind2+a];
237 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
238 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
239 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
243 myhash<h>(&seq_in[ind2], RM2, Sbox2);
251 for(int a=0;a<h2;a+=4) {
252 seq_out[ind1+a]=fX[a];
253 seq_out[ind1+a+1]=fX[a+1];
254 seq_out[ind1+a+2]=fX[a+2];
255 seq_out[ind1+a+3]=fX[a+3];
259 myhash<h>(fX, RM2, Sbox2);
262 for(int a=0;a<h2;a+=4) {
263 RM1[a]=RM1[PboxRM[a]];
264 RM1[a+1]=RM1[PboxRM[a+1]];
265 RM1[a+2]=RM1[PboxRM[a+2]];
266 RM1[a+3]=RM1[PboxRM[a+3]];
274 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
277 /* uchar *X=new uchar[h2];
278 uchar *fX=new uchar[h2];
279 unsigned int *lX=(unsigned int*)X;
280 unsigned int *lseq_in=(unsigned int*)seq_in;
284 // unsigned int *lX=(unsigned int*)X;
285 // unsigned int *lseq_in=(unsigned int*)seq_in;
288 for(int it=0;it<len;it++) {
290 int ind2=Pbox[it]*h2;
292 for(int a=0;a<h2;a+=4) {
294 X[a+1]=seq_in[ind2+a+1];
295 X[a+2]=seq_in[ind2+a+2];
296 X[a+3]=seq_in[ind2+a+3];
299 for(int a=0;a<h2;a+=4){
301 fX[a+1]=Sbox1[X[a+1]];
302 fX[a+2]=Sbox1[X[a+2]];
303 fX[a+3]=Sbox1[X[a+3]];
307 for(int a=0;a<h2;a+=4) {
309 fX[a+1]=fX[a+1]^RM1[a+1];
310 fX[a+2]=fX[a+2]^RM1[a+2];
311 fX[a+3]=fX[a+3]^RM1[a+3];
315 for(int a=0;a<h2;a+=4) {
316 seq_out[ind1+a]=Sbox2[fX[a]];
317 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
318 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
319 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
322 for(int a=0;a<h2;a+=4) {
323 RM1[a]=RM1[PboxRM[a]];
324 RM1[a+1]=RM1[PboxRM[a+1]];
325 RM1[a+2]=RM1[PboxRM[a+2]];
326 RM1[a+3]=RM1[PboxRM[a+3]];
340 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug) {
343 /*uchar *fX=new uchar[h2];
344 uchar *Inv_Sbox1=new uchar[256];
345 uchar *Inv_Sbox2=new uchar[256];
351 for(int it=0;it<len;it++) {
354 int ind2=Pbox[it]*h2;
359 for(int a=0;a<h2;a+=4) {
360 fX[a]=seq_in[ind1+a];
361 fX[a+1]=seq_in[ind1+a+1];
362 fX[a+2]=seq_in[ind1+a+2];
363 fX[a+3]=seq_in[ind1+a+3];
366 for(int a=0;a<h2;a+=4) {
367 fX[a]=Inv_Sbox2[fX[a]];
368 fX[a+1]=Inv_Sbox2[fX[a+1]];
369 fX[a+2]=Inv_Sbox2[fX[a+2]];
370 fX[a+3]=Inv_Sbox2[fX[a+3]];
372 for(int a=0;a<h2;a+=4) {
374 fX[a+1]=fX[a+1]^RM1[a+1];
375 fX[a+2]=fX[a+2]^RM1[a+2];
376 fX[a+3]=fX[a+3]^RM1[a+3];
379 for(int a=0;a<h2;a+=4) {
380 RM1[a]=RM1[PboxRM[a]];
381 RM1[a+1]=RM1[PboxRM[a+1]];
382 RM1[a+2]=RM1[PboxRM[a+2]];
383 RM1[a+3]=RM1[PboxRM[a+3]];
386 for(int a=0;a<h2;a+=4) {
387 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
388 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
389 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
390 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
400 int main(int argc, char** argv) {
410 for(int i=1; i<argc; i++){
411 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
412 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
413 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
414 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
415 if(strncmp(argv[i],"impb",4)==0) impb = atoi(&(argv[i][4])); //Use Lena or buffer
416 if(strncmp(argv[i],"tgpb",4)==0) tgpb = atoi(&(argv[i][4])); //Use Lena or buffer
419 /* printf("nb times %d\n",nb_test);
420 printf("ctr %d\n",ctr);
422 printf("lena %d\n",lena);
423 printf("size_buf %d\n",size_buf);
429 int seed=12;//time(NULL);
433 uchar Secretkey[key_size];
435 uchar counter[key_size];
437 for(int i=0;i<key_size;i++) {
438 Secretkey[i]=lrand48()&0xFF;
439 counter[i]=lrand48()&0xFF;
452 uchar *data_R, *data_G, *data_B;
457 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
458 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
459 imsize=width*height*3;
460 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
463 width=height=size_buf;
465 buffer=new uchar[imsize];
466 for(int i=0;i<imsize;i++) {
475 uchar* seq= new uchar[imsize];
476 uchar* seq2= new uchar[imsize];
478 int oneD=width*height;
480 for(int i=0;i<oneD;i++) {
482 seq[oneD+i]=data_G[i];
483 seq[2*oneD+i]=data_B[i];
487 for(int i=0;i<oneD;i++) {
496 int total_len=imsize;
498 int len= total_len/h2;
502 uchar *mix=new uchar[256];
507 for (int i = 0; i < 256 ; i++) {
508 mix[i]=Secretkey[i]^counter[i];
512 // cout<<"hash "<<endl;
513 for (int i = 0; i < 64 ; i++) {
520 int *Pbox=new int[len];
521 int *PboxRM=new int[h2];
524 uchar Inv_Sbox1[256];
525 uchar Inv_Sbox2[256];
534 double time_encrypt=0;
535 double time_decrypt=0;
538 double t=TimeStart();
539 rc4key(DK, Sbox1, 8);
542 rc4key(&DK[8], Sbox2, 8);
544 rc4key(&DK[16], sc, 16);
550 rc4keyperm(&DK[32], len, rp, Pbox, 16);
553 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
556 rc4key(&DK[64], sc, 16);
562 //cout<<"Time initializaton "<<time<<endl;
567 printf("%d ",RM3[i]);
576 for(int i=0;i<h2;i++){
580 for(int i=0;i<h;i++){
585 inverse_tables(Sbox1,256,Inv_Sbox1);
586 inverse_tables(Sbox2,256,Inv_Sbox2);
598 for(i=0;i<nb_test;i++)
601 encrypt_ctr<4*4,4>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
603 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
608 for(i=0;i<nb_test;i++)
611 encrypt_ctr<8*8,8>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
613 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
618 for(i=0;i<nb_test;i++)
621 encrypt_ctr<16*16,16>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
623 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
628 for(i=0;i<nb_test;i++)
631 encrypt_ctr<32*32,32>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
633 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
638 for(i=0;i<nb_test;i++)
641 encrypt_ctr<64*64,64>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
643 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
648 for(i=0;i<nb_test;i++)
651 encrypt_ctr<128*128,128>(seq, seq2,len,RM1,RM3,Pbox,PboxRM,Sbox1,Sbox2,1);
653 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
658 time_encrypt+=TimeStop(t);
659 //cout<<"Time encrypt "<<
660 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
664 for(int i=0;i<oneD;i++) {
666 data_G[i]=seq2[oneD+i];
667 data_B[i]=seq2[2*oneD+i];
669 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
673 for(int i=0;i<h;i++){
674 cout<<(int)RM3[i]<<" ";
682 if(tgpb>=0 && tgpb<h) {
691 for(i=0;i<nb_test;i++) {
693 encrypt_ctr<4*4,4>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
695 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
699 for(i=0;i<nb_test;i++) {
701 encrypt_ctr<8*8,8>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
703 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
707 for(i=0;i<nb_test;i++) {
709 encrypt_ctr<16*16,16>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
711 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
715 for(i=0;i<nb_test;i++) {
717 encrypt_ctr<32*32,32>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
719 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
723 for(i=0;i<nb_test;i++) {
725 encrypt_ctr<64*64,64>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
727 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
731 for(i=0;i<nb_test;i++) {
733 encrypt_ctr<128*128,128>(seq2, seq,len,RM2,RM4,Pbox,PboxRM,Sbox1,Sbox2,0);
735 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
740 time_decrypt+=TimeStop(t);
741 //cout<<"Time decrypt "
742 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
745 cout<<"\nTAG 2"<<endl;
746 for(int i=0;i<h;i++){
747 cout<<(int)RM4[i]<<" ";
751 for(int i=0;i<oneD;i++) {
753 data_G[i]=seq[oneD+i];
754 data_B[i]=seq[2*oneD+i];
756 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
760 for(int i=0;i<imsize;i++) {
761 //cout<<(int)buffer[i]<<endl;
762 if(buffer[i]!=seq[i]) {
766 // cout<<"RESULT CORRECT: "<<equal<<endl;