2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
15 /*#include <cryptopp/hex.h>
16 #include <cryptopp/sha.h>
17 #include <cryptopp/osrng.h>
18 #include <cryptopp/secblock.h>
23 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
24 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
28 //using namespace CryptoPP;
42 typedef unsigned char uchar;
47 struct timeval tstart;
48 gettimeofday(&tstart,0);
49 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
52 double TimeStop(double t)
56 gettimeofday(&tend,0);
57 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
64 uint xorshift32(const uint t)
66 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
74 uint64_t xorshift64( const uint64_t state)
86 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
88 for(int i=0;i<size_tab;i++) {
89 inv_perm_tabs[tab[i]] = i;
94 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
96 for(int i=0;i<size_tab;i++) {
97 inv_perm_tabs[tab[i]] = i;
104 void rc4key(uchar *key, uchar *sc, int size_DK) {
106 for(int i=0;i<256;i++) {
112 for(int i0=0; i0<256; i0++) {
113 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
122 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
128 for (int i=0;i<len;i++) {
131 for (int it = 0; it < rp; it++) {
133 for(int i0 = 0; i0<len; i0++) {
134 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
143 void prga(uchar *sc, int ldata, uchar *r) {
147 for (int it=0; it<ldata; it++) {
149 j0 = (j0 + sc[i0])&0xFF;
153 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
162 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, uint64_t myrand,int enc) {
171 for(int a=0;a<h2/8;a+=2) {
173 myrand=xorshift64(myrand);
174 ((unsigned long*)X)[a]=myrand;
175 myrand=xorshift64(myrand);
176 ((unsigned long*)X)[a+1]=myrand;
179 for(int a=0;a<h2;a+=4) {
181 X[a+1]=Sbox1[X[a+1]];
182 X[a+2]=Sbox2[X[a+2]];
183 X[a+3]=Sbox1[X[a+3]];
187 for(int it=0;it<len;it++) {
200 for(int a=0;a<h2/8;a+=2) {
201 myrand=xorshift64(myrand);
202 ((unsigned long*)X)[a]=((unsigned long *)X)[a]^myrand;
203 myrand=xorshift64(myrand);
204 ((unsigned long*)X)[a+1]=((unsigned long *)X)[a+1]^myrand;
209 for(int a=0;a<h2;a+=4) {
211 X[a+1]=Sbox2[X[a+1]];
212 X[a+2]=Sbox1[X[a+2]];
213 X[a+3]=Sbox2[X[a+3]];
216 for(int a=0;a<h2/8;a+=2) {
217 ((unsigned long*)X)[a]=((unsigned long*)X)[a]^((unsigned long*)RM1)[a];
218 ((unsigned long*)X)[a+1]=((unsigned long*)X)[a+1]^((unsigned long*)RM1)[a+1];
222 for(int a=0;a<h2/8;a+=2) {
223 ((unsigned long*)X)[a]=((unsigned long*)X)[a]^((unsigned long*)RM1)[a];
224 ((unsigned long*)X)[a+1]=((unsigned long*)X)[a+1]^((unsigned long*)RM1)[a+1];
230 for(int a=0;a<h2/8;a+=2) {
233 ((unsigned long*)seq_out)[ind1+a]=((unsigned long*)X)[a]^((unsigned long*)seq_in)[ind2+a];
234 ((unsigned long*)seq_out)[ind1+a+1]=((unsigned long*)X)[a+1]^((unsigned long*)seq_in)[ind2+a+1];
239 for(int a=0;a<h2/8;a+=2) {
240 ((unsigned long*)RM1)[a]=xorshift64(((unsigned long*)RM1)[a]);
241 ((unsigned long*)RM1)[a+1]=xorshift64(((unsigned long*)RM1)[a+1]);
245 for(int a=0;a<h2;a+=4) {
246 RM1[a]=Sbox1[RM1[a]];
247 RM1[a+1]=Sbox2[RM1[a+1]];
248 RM1[a+2]=Sbox1[RM1[a+2]];
249 RM1[a+3]=Sbox2[RM1[a+3]];
262 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1, int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, uint64_t myrand, int debug) {
267 for(int it=0;it<len;it++) {
269 int ind2=Pbox[it]*h2;
273 for(int a=0;a<h2/8;a+=2) {
274 myrand=xorshift64(myrand);
275 ((unsigned long*)X)[a]=((unsigned long*)seq_in)[ind2+a]^myrand;
276 myrand=xorshift64(myrand);
277 ((unsigned long*)X)[a+1]=((unsigned long*)seq_in)[ind2+a+1]^myrand;
281 for(int a=0;a<h2;a+=4){
283 X[a+1]=Sbox2[X[a+1]];
284 X[a+2]=Sbox1[X[a+2]];
285 X[a+3]=Sbox2[X[a+3]];
289 /* for(int a=0;a<h2;a+=4) {
291 X[a+1]=X[a+1]^RM1[a+1];
292 X[a+2]=X[a+2]^RM1[a+2];
293 X[a+3]=X[a+3]^RM1[a+3];
297 /*for(int a=0;a<h2;a+=4) {
298 seq_out[ind1+a]=Sbox2[fX[a]];
299 seq_out[ind1+a+1]=Sbox1[fX[a+1]];
300 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
301 seq_out[ind1+a+3]=Sbox1[fX[a+3]];
304 for(int a=0;a<h2/8;a+=2) {
305 ((unsigned long*)seq_out)[ind1+a]=((unsigned long*)X)[a]^((unsigned long*)RM1)[a];
306 ((unsigned long*)seq_out)[ind1+a+1]=((unsigned long*)X)[a+1]^((unsigned long*)RM1)[a+1];
310 /* for(int a=0;a<h2;a+=4) {
311 RM1[a]=Sbox1[RM1[PboxRM[a]]];
312 RM1[a+1]=Sbox2[RM1[PboxRM[a+1]]];
313 RM1[a+2]=Sbox1[RM1[PboxRM[a+2]]];
314 RM1[a+3]=Sbox2[RM1[PboxRM[a+3]]];
318 for(int a=0;a<h2/8;a+=2) {
319 ((unsigned long*)RM1)[a]=xorshift64(((unsigned long*)RM1)[a]);
320 ((unsigned long*)RM1)[a+1]=xorshift64(((unsigned long*)RM1)[a+1]);
341 void decrypt(uchar* seq_in, uchar *seq_out, int len, uchar* RM1, int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint64_t myrand, int debug) {
349 for(int it=0;it<len;it++) {
352 int ind2=Pbox[it]*h2;
358 for(int a=0;a<h2;a+=4) {
359 fX[a]=seq_in[ind1+a];
360 fX[a+1]=seq_in[ind1+a+1];
361 fX[a+2]=seq_in[ind1+a+2];
362 fX[a+3]=seq_in[ind1+a+3];
366 for(int a=0;a<h2/8;a+=2) {
367 ((unsigned long*)X)[a]=((unsigned long*)seq_in)[ind1+a]^((unsigned long*)RM1)[a];
368 ((unsigned long*)X)[a+1]=((unsigned long*)seq_in)[ind1+a+1]^((unsigned long*)RM1)[a+1];
371 /* for(int a=0;a<h2;a+=4) {
372 fX[a]=Inv_Sbox2[fX[a]];
373 fX[a+1]=Inv_Sbox1[fX[a+1]];
374 fX[a+2]=Inv_Sbox2[fX[a+2]];
375 fX[a+3]=Inv_Sbox1[fX[a+3]];
378 /* for(int a=0;a<h2;a+=4) {
380 X[a+1]=X[a+1]^RM1[a+1];
381 X[a+2]=X[a+2]^RM1[a+2];
382 X[a+3]=X[a+3]^RM1[a+3];
386 /* for(int a=0;a<h2;a+=4) {
387 RM1[a]=Sbox1[RM1[PboxRM[a]]];
388 RM1[a+1]=Sbox2[RM1[PboxRM[a+1]]];
389 RM1[a+2]=Sbox1[RM1[PboxRM[a+2]]];
390 RM1[a+3]=Sbox2[RM1[PboxRM[a+3]]];
393 for(int a=0;a<h2/8;a+=2) {
394 ((unsigned long*)RM1)[a]=xorshift64(((unsigned long*)RM1)[a]);
395 ((unsigned long*)RM1)[a+1]=xorshift64(((unsigned long*)RM1)[a+1]);
399 for(int a=0;a<h2;a+=4) {
400 X[a]=Inv_Sbox1[X[a]];
401 X[a+1]=Inv_Sbox2[X[a+1]];
402 X[a+2]=Inv_Sbox1[X[a+2]];
403 X[a+3]=Inv_Sbox2[X[a+3]];
407 /* for(int a=0;a<h2;a+=4) {
408 myrand=xorshift32(myrand);
411 seq_out[ind2+a]=Inv_Sbox1[fX[a]]^(mm&255);
413 seq_out[ind2+a+1]=Inv_Sbox2[fX[a+1]]^(mm&255);
415 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]]^(mm&255);
417 seq_out[ind2+a+3]=Inv_Sbox2[fX[a+3]]^(mm&255);
421 for(int a=0;a<h2/8;a+=2) {
422 myrand=xorshift64(myrand);
423 ((unsigned long*)seq_out)[ind2+a]=((unsigned long*)X)[a]^myrand;
424 myrand=xorshift64(myrand);
425 ((unsigned long*)seq_out)[ind2+a+1]=((unsigned long*)X)[a+1]^myrand;
437 int main(int argc, char** argv) {
446 for(int i=1; i<argc; i++){
447 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
448 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
449 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
450 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
451 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
454 /* printf("nb times %d\n",nb_test);
455 printf("ctr %d\n",ctr);
457 printf("lena %d\n",lena);
458 printf("size_buf %d\n",size_buf);
468 uchar Secretkey[key_size];
470 uchar counter[key_size];
472 for(int i=0;i<key_size;i++) {
473 Secretkey[i]=lrand48()&0xFF;
474 counter[i]=lrand48()&0xFF;
487 uchar *data_R, *data_G, *data_B;
496 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
497 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
498 imsize=width*height*3;
499 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
502 width=height=size_buf;
504 buffer=new uchar[imsize];
505 for(int i=0;i<imsize;i++) {
514 uchar* seq= new uchar[imsize];
515 uchar* seq2= new uchar[imsize];
517 int oneD=width*height;
519 for(int i=0;i<oneD;i++) {
521 seq[oneD+i]=data_G[i];
522 seq[2*oneD+i]=data_B[i];
526 for(int i=0;i<oneD;i++) {
535 int total_len=imsize;
537 int len= total_len/h2;
541 uchar *mix=new uchar[256];
546 for (int i = 0; i < 256 ; i++) {
547 mix[i]=Secretkey[i]^counter[i];
552 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
553 // g_print("%s\n", sha512);
563 // cout<<"hash "<<endl;
564 for (int i = 0; i < 64 ; i++) {
571 int *Pbox=new int[len];
572 int *PboxRM=new int[h2];
575 uchar Inv_Sbox1[256];
576 uchar Inv_Sbox2[256];
586 double time_encrypt=0;
587 double time_decrypt=0;
590 double t=TimeStart();
591 rc4key(DK, Sbox1, 8);
594 rc4key(&DK[8], Sbox2, 8);
596 rc4key(&DK[16], sc, 16);
602 rc4keyperm(&DK[32], len, rp, Pbox, 16);
605 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
608 //cout<<"Time initializaton "<<time<<endl;
613 for(int i=0;i<64;i++) {
617 uint64_t myrand_copy=myrand;
624 for(int i=0;i<h2;i++){
629 inverse_tables(Sbox1,256,Inv_Sbox1);
630 inverse_tables(Sbox2,256,Inv_Sbox2);
642 for(i=0;i<nb_test;i++)
645 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
647 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
652 for(i=0;i<nb_test;i++)
655 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
657 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
662 for(i=0;i<nb_test;i++)
665 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
667 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
672 for(i=0;i<nb_test;i++)
675 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
677 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
682 for(i=0;i<nb_test;i++)
685 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
687 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
692 for(i=0;i<nb_test;i++)
695 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
697 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
702 time_encrypt+=TimeStop(t);
703 //cout<<"Time encrypt "<<
704 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
708 for(int i=0;i<oneD;i++) {
710 data_G[i]=seq2[oneD+i];
711 data_B[i]=seq2[2*oneD+i];
713 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
721 for(i=0;i<nb_test;i++) {
723 encrypt_ctr<4*4>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
725 decrypt<4*4>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
729 for(i=0;i<nb_test;i++) {
731 encrypt_ctr<8*8>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
733 decrypt<8*8>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
737 for(i=0;i<nb_test;i++) {
739 encrypt_ctr<16*16>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
741 decrypt<16*16>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
745 for(i=0;i<nb_test;i++) {
747 encrypt_ctr<32*32>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
749 decrypt<32*32>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
753 for(i=0;i<nb_test;i++) {
755 encrypt_ctr<64*64>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
757 decrypt<64*64>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
761 for(i=0;i<nb_test;i++) {
763 encrypt_ctr<128*128>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
765 decrypt<128*128>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
770 time_decrypt+=TimeStop(t);
771 //cout<<"Time decrypt "
772 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
775 for(int i=0;i<oneD;i++) {
777 data_G[i]=seq[oneD+i];
778 data_B[i]=seq[2*oneD+i];
780 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
784 for(int i=0;i<imsize;i++) {
785 //cout<<(int)buffer[i]<<endl;
786 if(buffer[i]!=seq[i]) {
790 // cout<<"RESULT CORRECT: "<<equal<<endl;