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];
223 for(int a=0;a<h2/8;a+=2) {
226 ((unsigned long*)seq_out)[ind1+a]=((unsigned long*)X)[a]^((unsigned long*)seq_in)[ind2+a];
227 ((unsigned long*)seq_out)[ind1+a+1]=((unsigned long*)X)[a+1]^((unsigned long*)seq_in)[ind2+a+1];
232 for(int a=0;a<h2/8;a+=2) {
233 ((unsigned long*)RM1)[a]=xorshift64(((unsigned long*)RM1)[a]);
234 ((unsigned long*)RM1)[a+1]=xorshift64(((unsigned long*)RM1)[a+1]);
238 for(int a=0;a<h2;a+=4) {
239 RM1[a]=Sbox1[RM1[a]];
240 RM1[a+1]=Sbox2[RM1[a+1]];
241 RM1[a+2]=Sbox1[RM1[a+2]];
242 RM1[a+3]=Sbox2[RM1[a+3]];
255 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) {
260 for(int it=0;it<len;it++) {
262 int ind2=Pbox[it]*h2;
266 for(int a=0;a<h2/8;a+=2) {
267 myrand=xorshift64(myrand);
268 ((unsigned long*)X)[a]=((unsigned long*)seq_in)[ind2+a]^myrand;
269 myrand=xorshift64(myrand);
270 ((unsigned long*)X)[a+1]=((unsigned long*)seq_in)[ind2+a+1]^myrand;
274 for(int a=0;a<h2;a+=4){
276 X[a+1]=Sbox2[X[a+1]];
277 X[a+2]=Sbox1[X[a+2]];
278 X[a+3]=Sbox2[X[a+3]];
282 /* for(int a=0;a<h2;a+=4) {
284 X[a+1]=X[a+1]^RM1[a+1];
285 X[a+2]=X[a+2]^RM1[a+2];
286 X[a+3]=X[a+3]^RM1[a+3];
290 /*for(int a=0;a<h2;a+=4) {
291 seq_out[ind1+a]=Sbox2[fX[a]];
292 seq_out[ind1+a+1]=Sbox1[fX[a+1]];
293 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
294 seq_out[ind1+a+3]=Sbox1[fX[a+3]];
297 for(int a=0;a<h2/8;a+=2) {
298 ((unsigned long*)seq_out)[ind1+a]=((unsigned long*)X)[a]^((unsigned long*)RM1)[a];
299 ((unsigned long*)seq_out)[ind1+a+1]=((unsigned long*)X)[a+1]^((unsigned long*)RM1)[a+1];
303 /* for(int a=0;a<h2;a+=4) {
304 RM1[a]=Sbox1[RM1[PboxRM[a]]];
305 RM1[a+1]=Sbox2[RM1[PboxRM[a+1]]];
306 RM1[a+2]=Sbox1[RM1[PboxRM[a+2]]];
307 RM1[a+3]=Sbox2[RM1[PboxRM[a+3]]];
311 for(int a=0;a<h2/8;a+=2) {
312 ((unsigned long*)RM1)[a]=xorshift64(((unsigned long*)RM1)[a]);
313 ((unsigned long*)RM1)[a+1]=xorshift64(((unsigned long*)RM1)[a+1]);
334 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) {
342 for(int it=0;it<len;it++) {
345 int ind2=Pbox[it]*h2;
351 for(int a=0;a<h2;a+=4) {
352 fX[a]=seq_in[ind1+a];
353 fX[a+1]=seq_in[ind1+a+1];
354 fX[a+2]=seq_in[ind1+a+2];
355 fX[a+3]=seq_in[ind1+a+3];
359 for(int a=0;a<h2/8;a+=2) {
360 ((unsigned long*)X)[a]=((unsigned long*)seq_in)[ind1+a]^((unsigned long*)RM1)[a];
361 ((unsigned long*)X)[a+1]=((unsigned long*)seq_in)[ind1+a+1]^((unsigned long*)RM1)[a+1];
364 /* for(int a=0;a<h2;a+=4) {
365 fX[a]=Inv_Sbox2[fX[a]];
366 fX[a+1]=Inv_Sbox1[fX[a+1]];
367 fX[a+2]=Inv_Sbox2[fX[a+2]];
368 fX[a+3]=Inv_Sbox1[fX[a+3]];
371 /* for(int a=0;a<h2;a+=4) {
373 X[a+1]=X[a+1]^RM1[a+1];
374 X[a+2]=X[a+2]^RM1[a+2];
375 X[a+3]=X[a+3]^RM1[a+3];
379 /* for(int a=0;a<h2;a+=4) {
380 RM1[a]=Sbox1[RM1[PboxRM[a]]];
381 RM1[a+1]=Sbox2[RM1[PboxRM[a+1]]];
382 RM1[a+2]=Sbox1[RM1[PboxRM[a+2]]];
383 RM1[a+3]=Sbox2[RM1[PboxRM[a+3]]];
386 for(int a=0;a<h2/8;a+=2) {
387 ((unsigned long*)RM1)[a]=xorshift64(((unsigned long*)RM1)[a]);
388 ((unsigned long*)RM1)[a+1]=xorshift64(((unsigned long*)RM1)[a+1]);
392 for(int a=0;a<h2;a+=4) {
393 X[a]=Inv_Sbox1[X[a]];
394 X[a+1]=Inv_Sbox2[X[a+1]];
395 X[a+2]=Inv_Sbox1[X[a+2]];
396 X[a+3]=Inv_Sbox2[X[a+3]];
400 /* for(int a=0;a<h2;a+=4) {
401 myrand=xorshift32(myrand);
404 seq_out[ind2+a]=Inv_Sbox1[fX[a]]^(mm&255);
406 seq_out[ind2+a+1]=Inv_Sbox2[fX[a+1]]^(mm&255);
408 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]]^(mm&255);
410 seq_out[ind2+a+3]=Inv_Sbox2[fX[a+3]]^(mm&255);
414 for(int a=0;a<h2/8;a+=2) {
415 myrand=xorshift64(myrand);
416 ((unsigned long*)seq_out)[ind2+a]=((unsigned long*)X)[a]^myrand;
417 myrand=xorshift64(myrand);
418 ((unsigned long*)seq_out)[ind2+a+1]=((unsigned long*)X)[a+1]^myrand;
430 int main(int argc, char** argv) {
439 for(int i=1; i<argc; i++){
440 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
441 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
442 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
443 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
444 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
447 /* printf("nb times %d\n",nb_test);
448 printf("ctr %d\n",ctr);
450 printf("lena %d\n",lena);
451 printf("size_buf %d\n",size_buf);
461 uchar Secretkey[key_size];
463 uchar counter[key_size];
465 for(int i=0;i<key_size;i++) {
466 Secretkey[i]=lrand48()&0xFF;
467 counter[i]=lrand48()&0xFF;
480 uchar *data_R, *data_G, *data_B;
489 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
490 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
491 imsize=width*height*3;
492 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
495 width=height=size_buf;
497 buffer=new uchar[imsize];
498 for(int i=0;i<imsize;i++) {
507 uchar* seq= new uchar[imsize];
508 uchar* seq2= new uchar[imsize];
510 int oneD=width*height;
512 for(int i=0;i<oneD;i++) {
514 seq[oneD+i]=data_G[i];
515 seq[2*oneD+i]=data_B[i];
519 for(int i=0;i<oneD;i++) {
528 int total_len=imsize;
530 int len= total_len/h2;
534 uchar *mix=new uchar[256];
539 for (int i = 0; i < 256 ; i++) {
540 mix[i]=Secretkey[i]^counter[i];
545 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
546 // g_print("%s\n", sha512);
556 // cout<<"hash "<<endl;
557 for (int i = 0; i < 64 ; i++) {
564 int *Pbox=new int[len];
565 int *PboxRM=new int[h2];
568 uchar Inv_Sbox1[256];
569 uchar Inv_Sbox2[256];
579 double time_encrypt=0;
580 double time_decrypt=0;
583 double t=TimeStart();
584 rc4key(DK, Sbox1, 8);
587 rc4key(&DK[8], Sbox2, 8);
589 rc4key(&DK[16], sc, 16);
595 rc4keyperm(&DK[32], len, rp, Pbox, 16);
598 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
601 //cout<<"Time initializaton "<<time<<endl;
606 for(int i=0;i<64;i++) {
610 uint64_t myrand_copy=myrand;
617 for(int i=0;i<h2;i++){
622 inverse_tables(Sbox1,256,Inv_Sbox1);
623 inverse_tables(Sbox2,256,Inv_Sbox2);
635 for(i=0;i<nb_test;i++)
638 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
640 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
645 for(i=0;i<nb_test;i++)
648 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
650 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
655 for(i=0;i<nb_test;i++)
658 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
660 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
665 for(i=0;i<nb_test;i++)
668 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
670 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
675 for(i=0;i<nb_test;i++)
678 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
680 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
685 for(i=0;i<nb_test;i++)
688 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
690 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
695 time_encrypt+=TimeStop(t);
696 //cout<<"Time encrypt "<<
697 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
701 for(int i=0;i<oneD;i++) {
703 data_G[i]=seq2[oneD+i];
704 data_B[i]=seq2[2*oneD+i];
706 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
714 for(i=0;i<nb_test;i++) {
716 encrypt_ctr<4*4>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
718 decrypt<4*4>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
722 for(i=0;i<nb_test;i++) {
724 encrypt_ctr<8*8>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
726 decrypt<8*8>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
730 for(i=0;i<nb_test;i++) {
732 encrypt_ctr<16*16>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
734 decrypt<16*16>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
738 for(i=0;i<nb_test;i++) {
740 encrypt_ctr<32*32>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
742 decrypt<32*32>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
746 for(i=0;i<nb_test;i++) {
748 encrypt_ctr<64*64>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
750 decrypt<64*64>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
754 for(i=0;i<nb_test;i++) {
756 encrypt_ctr<128*128>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
758 decrypt<128*128>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
763 time_decrypt+=TimeStop(t);
764 //cout<<"Time decrypt "
765 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
768 for(int i=0;i<oneD;i++) {
770 data_G[i]=seq[oneD+i];
771 data_B[i]=seq[2*oneD+i];
773 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
777 for(int i=0;i<imsize;i++) {
778 //cout<<(int)buffer[i]<<endl;
779 if(buffer[i]!=seq[i]) {
783 // cout<<"RESULT CORRECT: "<<equal<<endl;