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];
139 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc) {
142 // uchar *X=new uchar[h2];
143 // uchar *fX=new uchar[h2];
150 for(int a=0;a<h2;a+=4) {
151 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
152 X[a+1]=Sbox1[(a+1)&0xFF];
153 X[a+2]=Sbox1[(a+2)&0xFF];
154 X[a+3]=Sbox1[(a+3)&0xFF];
158 for(int it=0;it<len;it++) {
169 for(int a=0;a<h2;a+=4) {
171 X[a+1]=Sbox1[X[a+1]];
172 X[a+2]=Sbox1[X[a+2]];
173 X[a+3]=Sbox1[X[a+3]];
178 // *(int*)&fX[0]^=it;
181 /* for(int a=0;a<h2;a+=16) {
185 *(int*)&fX[a+12]^=it;
190 for(int a=0;a<h2;a+=4) {
192 fX[a+1]=X[a+1]^RM1[a+1];
193 fX[a+2]=X[a+2]^RM1[a+2];
194 fX[a+3]=X[a+3]^RM1[a+3];
198 /* for(int a=0;a<h2;a+=4) {
200 fX[a+1]=Sbox2[fX[a+1]];
201 fX[a+2]=Sbox2[fX[a+2]];
202 fX[a+3]=Sbox2[fX[a+3]];
205 for(int a=0;a<h2;a+=4) {
206 fX[a]=fX[a]^seq_in[ind2+a];
207 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
208 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
209 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
213 for(int a=0;a<h2;a+=4) {
214 seq_out[ind1+a]=fX[a];
215 seq_out[ind1+a+1]=fX[a+1];
216 seq_out[ind1+a+2]=fX[a+2];
217 seq_out[ind1+a+3]=fX[a+3];
220 for(int a=0;a<h2;a+=4) {
221 RM1[a]=RM1[PboxRM[a]];
222 RM1[a+1]=RM1[PboxRM[a+1]];
223 RM1[a+2]=RM1[PboxRM[a+2]];
224 RM1[a+3]=RM1[PboxRM[a+3]];
236 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
239 /* uchar *X=new uchar[h2];
240 uchar *fX=new uchar[h2];
241 unsigned int *lX=(unsigned int*)X;
242 unsigned int *lseq_in=(unsigned int*)seq_in;
246 // unsigned int *lX=(unsigned int*)X;
247 // unsigned int *lseq_in=(unsigned int*)seq_in;
250 for(int it=0;it<len;it++) {
252 int ind2=Pbox[it]*h2;
254 for(int a=0;a<h2;a+=4) {
256 X[a+1]=seq_in[ind2+a+1];
257 X[a+2]=seq_in[ind2+a+2];
258 X[a+3]=seq_in[ind2+a+3];
261 for(int a=0;a<h2;a+=4){
263 fX[a+1]=Sbox1[X[a+1]];
264 fX[a+2]=Sbox1[X[a+2]];
265 fX[a+3]=Sbox1[X[a+3]];
269 for(int a=0;a<h2;a+=4) {
271 fX[a+1]=fX[a+1]^RM1[a+1];
272 fX[a+2]=fX[a+2]^RM1[a+2];
273 fX[a+3]=fX[a+3]^RM1[a+3];
277 for(int a=0;a<h2;a+=4) {
278 seq_out[ind1+a]=Sbox2[fX[a]];
279 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
280 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
281 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
284 for(int a=0;a<h2;a+=4) {
285 RM1[a]=RM1[PboxRM[a]];
286 RM1[a+1]=RM1[PboxRM[a+1]];
287 RM1[a+2]=RM1[PboxRM[a+2]];
288 RM1[a+3]=RM1[PboxRM[a+3]];
302 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug) {
305 /*uchar *fX=new uchar[h2];
306 uchar *Inv_Sbox1=new uchar[256];
307 uchar *Inv_Sbox2=new uchar[256];
313 for(int it=0;it<len;it++) {
316 int ind2=Pbox[it]*h2;
321 for(int a=0;a<h2;a+=4) {
322 fX[a]=seq_in[ind1+a];
323 fX[a+1]=seq_in[ind1+a+1];
324 fX[a+2]=seq_in[ind1+a+2];
325 fX[a+3]=seq_in[ind1+a+3];
328 for(int a=0;a<h2;a+=4) {
329 fX[a]=Inv_Sbox2[fX[a]];
330 fX[a+1]=Inv_Sbox2[fX[a+1]];
331 fX[a+2]=Inv_Sbox2[fX[a+2]];
332 fX[a+3]=Inv_Sbox2[fX[a+3]];
334 for(int a=0;a<h2;a+=4) {
336 fX[a+1]=fX[a+1]^RM1[a+1];
337 fX[a+2]=fX[a+2]^RM1[a+2];
338 fX[a+3]=fX[a+3]^RM1[a+3];
341 for(int a=0;a<h2;a+=4) {
342 RM1[a]=RM1[PboxRM[a]];
343 RM1[a+1]=RM1[PboxRM[a+1]];
344 RM1[a+2]=RM1[PboxRM[a+2]];
345 RM1[a+3]=RM1[PboxRM[a+3]];
348 for(int a=0;a<h2;a+=4) {
349 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
350 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
351 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
352 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
362 int main(int argc, char** argv) {
371 for(int i=1; i<argc; i++){
372 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
373 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
374 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
375 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
376 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
379 /* printf("nb times %d\n",nb_test);
380 printf("ctr %d\n",ctr);
382 printf("lena %d\n",lena);
383 printf("size_buf %d\n",size_buf);
393 uchar Secretkey[key_size];
395 uchar counter[key_size];
397 for(int i=0;i<key_size;i++) {
398 Secretkey[i]=lrand48()&0xFF;
399 counter[i]=lrand48()&0xFF;
412 uchar *data_R, *data_G, *data_B;
417 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
418 imsize=width*height*3;
419 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
422 width=height=size_buf;
424 buffer=new uchar[imsize];
425 for(int i=0;i<imsize;i++) {
434 uchar* seq= new uchar[imsize];
435 uchar* seq2= new uchar[imsize];
437 int oneD=width*height;
439 for(int i=0;i<oneD;i++) {
441 seq[oneD+i]=data_G[i];
442 seq[2*oneD+i]=data_B[i];
446 for(int i=0;i<oneD;i++) {
455 int total_len=imsize;
457 int len= total_len/h2;
461 uchar *mix=new uchar[256];
466 for (int i = 0; i < 256 ; i++) {
467 mix[i]=Secretkey[i]^counter[i];
471 // cout<<"hash "<<endl;
472 for (int i = 0; i < 64 ; i++) {
479 int *Pbox=new int[len];
480 int *PboxRM=new int[h2];
483 uchar Inv_Sbox1[256];
484 uchar Inv_Sbox2[256];
492 double time_encrypt=0;
493 double time_decrypt=0;
496 double t=TimeStart();
497 rc4key(DK, Sbox1, 8);
500 rc4key(&DK[8], Sbox2, 8);
502 rc4key(&DK[16], sc, 16);
508 rc4keyperm(&DK[32], len, rp, Pbox, 16);
511 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
514 //cout<<"Time initializaton "<<time<<endl;
526 for(int i=0;i<h2;i++){
531 inverse_tables(Sbox1,256,Inv_Sbox1);
532 inverse_tables(Sbox2,256,Inv_Sbox2);
544 for(i=0;i<nb_test;i++)
547 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
549 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
554 for(i=0;i<nb_test;i++)
557 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
559 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
564 for(i=0;i<nb_test;i++)
567 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
569 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
574 for(i=0;i<nb_test;i++)
577 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
579 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
584 for(i=0;i<nb_test;i++)
587 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
589 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
594 for(i=0;i<nb_test;i++)
597 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
599 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
604 time_encrypt+=TimeStop(t);
605 //cout<<"Time encrypt "<<
606 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
610 for(int i=0;i<oneD;i++) {
612 data_G[i]=seq2[oneD+i];
613 data_B[i]=seq2[2*oneD+i];
615 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
623 for(i=0;i<nb_test;i++) {
625 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
627 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
631 for(i=0;i<nb_test;i++) {
633 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
635 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
639 for(i=0;i<nb_test;i++) {
641 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
643 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
647 for(i=0;i<nb_test;i++) {
649 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
651 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
655 for(i=0;i<nb_test;i++) {
657 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
659 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
663 for(i=0;i<nb_test;i++) {
665 encrypt_ctr<128*128>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
667 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
672 time_decrypt+=TimeStop(t);
673 //cout<<"Time decrypt "
674 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
677 for(int i=0;i<oneD;i++) {
679 data_G[i]=seq[oneD+i];
680 data_B[i]=seq[2*oneD+i];
682 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
686 for(int i=0;i<imsize;i++) {
687 //cout<<(int)buffer[i]<<endl;
688 if(buffer[i]!=seq[i]) {
692 // cout<<"RESULT CORRECT: "<<equal<<endl;