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" */
76 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
78 for(int i=0;i<size_tab;i++) {
79 inv_perm_tabs[tab[i]] = i;
84 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
86 for(int i=0;i<size_tab;i++) {
87 inv_perm_tabs[tab[i]] = i;
94 void rc4key(uchar *key, uchar *sc, int size_DK) {
96 for(int i=0;i<256;i++) {
102 for(int i0=0; i0<256; i0++) {
103 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
112 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
118 for (int i=0;i<len;i++) {
121 for (int it = 0; it < rp; it++) {
123 for(int i0 = 0; i0<len; i0++) {
124 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
133 void prga(uchar *sc, int ldata, uchar *r) {
137 for (int it=0; it<ldata; it++) {
138 i0 = ((i0+1)&0xFE); //%255);
139 j0 = (j0 + sc[i0])&0xFF;
143 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
152 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, uint myrand,int enc) {
162 for(int a=0;a<h2;a+=4) {
163 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
164 X[a+1]=Sbox1[(a+1)&0xFF];
165 X[a+2]=Sbox1[(a+2)&0xFF];
166 X[a+3]=Sbox1[(a+3)&0xFF];
170 for(int it=0;it<len;it++) {
181 for(int a=0;a<h2;a+=4) {
183 myrand=xorshift32(myrand);
188 X[a+1]=X[a+1]^(mm&255);
190 X[a+2]=X[a+2]^(mm&255);
192 X[a+3]=X[a+3]^(mm&255);
199 for(int a=0;a<h2;a+=4) {
201 X[a+1]=Sbox1[X[a+1]];
202 X[a+2]=Sbox1[X[a+2]];
203 X[a+3]=Sbox1[X[a+3]];
206 for(int a=0;a<h2;a+=4) {
208 fX[a+1]=X[a+1]^RM1[a+1];
209 fX[a+2]=X[a+2]^RM1[a+2];
210 fX[a+3]=X[a+3]^RM1[a+3];
213 for(int a=0;a<h2;a+=4) {
214 fX[a]=X[a]^seq_in[ind2+a];
215 fX[a+1]=X[a+1]^seq_in[ind2+a+1];
216 fX[a+2]=X[a+2]^seq_in[ind2+a+2];
217 fX[a+3]=X[a+3]^seq_in[ind2+a+3];
222 for(int a=0;a<h2;a+=4) {
223 seq_out[ind1+a]=fX[a];
224 seq_out[ind1+a+1]=fX[a+1];
225 seq_out[ind1+a+2]=fX[a+2];
226 seq_out[ind1+a+3]=fX[a+3];
229 for(int a=0;a<h2;a+=4) {
230 RM1[a]=RM1[PboxRM[a]];
231 RM1[a+1]=RM1[PboxRM[a+1]];
232 RM1[a+2]=RM1[PboxRM[a+2]];
233 RM1[a+3]=RM1[PboxRM[a+3]];
245 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1, int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
251 for(int it=0;it<len;it++) {
253 int ind2=Pbox[it]*h2;
255 for(int a=0;a<h2;a+=4) {
256 myrand=xorshift32(myrand);
259 X[a]=seq_in[ind2+a]^(mm&255);
261 X[a+1]=seq_in[ind2+a+1]^(mm&255);
263 X[a+2]=seq_in[ind2+a+2]^(mm&255);
265 X[a+3]=seq_in[ind2+a+3]^(mm&255);
268 for(int a=0;a<h2;a+=4){
270 fX[a+1]=Sbox1[X[a+1]];
271 fX[a+2]=Sbox1[X[a+2]];
272 fX[a+3]=Sbox1[X[a+3]];
276 for(int a=0;a<h2;a+=4) {
278 fX[a+1]=fX[a+1]^RM1[a+1];
279 fX[a+2]=fX[a+2]^RM1[a+2];
280 fX[a+3]=fX[a+3]^RM1[a+3];
284 for(int a=0;a<h2;a+=4) {
285 seq_out[ind1+a]=Sbox2[fX[a]];
286 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
287 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
288 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
291 for(int a=0;a<h2;a+=4) {
292 RM1[a]=RM1[PboxRM[a]];
293 RM1[a+1]=RM1[PboxRM[a+1]];
294 RM1[a+2]=RM1[PboxRM[a+2]];
295 RM1[a+3]=RM1[PboxRM[a+3]];
317 void decrypt(uchar* seq_in, uchar *seq_out, int len, uchar* RM1, int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
325 for(int it=0;it<len;it++) {
328 int ind2=Pbox[it]*h2;
333 for(int a=0;a<h2;a+=4) {
334 fX[a]=seq_in[ind1+a];
335 fX[a+1]=seq_in[ind1+a+1];
336 fX[a+2]=seq_in[ind1+a+2];
337 fX[a+3]=seq_in[ind1+a+3];
340 for(int a=0;a<h2;a+=4) {
341 fX[a]=Inv_Sbox2[fX[a]];
342 fX[a+1]=Inv_Sbox2[fX[a+1]];
343 fX[a+2]=Inv_Sbox2[fX[a+2]];
344 fX[a+3]=Inv_Sbox2[fX[a+3]];
346 for(int a=0;a<h2;a+=4) {
348 fX[a+1]=fX[a+1]^RM1[a+1];
349 fX[a+2]=fX[a+2]^RM1[a+2];
350 fX[a+3]=fX[a+3]^RM1[a+3];
353 for(int a=0;a<h2;a+=4) {
354 RM1[a]=RM1[PboxRM[a]];
355 RM1[a+1]=RM1[PboxRM[a+1]];
356 RM1[a+2]=RM1[PboxRM[a+2]];
357 RM1[a+3]=RM1[PboxRM[a+3]];
360 for(int a=0;a<h2;a+=4) {
361 myrand=xorshift32(myrand);
364 seq_out[ind2+a]=Inv_Sbox1[fX[a]]^(mm&255);
366 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]]^(mm&255);
368 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]]^(mm&255);
370 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]]^(mm&255);
381 int main(int argc, char** argv) {
390 for(int i=1; i<argc; i++){
391 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
392 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
393 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
394 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
395 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
398 /* printf("nb times %d\n",nb_test);
399 printf("ctr %d\n",ctr);
401 printf("lena %d\n",lena);
402 printf("size_buf %d\n",size_buf);
412 uchar Secretkey[key_size];
414 uchar counter[key_size];
416 for(int i=0;i<key_size;i++) {
417 Secretkey[i]=lrand48()&0xFF;
418 counter[i]=lrand48()&0xFF;
431 uchar *data_R, *data_G, *data_B;
440 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
441 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
442 imsize=width*height*3;
443 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
446 width=height=size_buf;
448 buffer=new uchar[imsize];
449 for(int i=0;i<imsize;i++) {
458 uchar* seq= new uchar[imsize];
459 uchar* seq2= new uchar[imsize];
461 int oneD=width*height;
463 for(int i=0;i<oneD;i++) {
465 seq[oneD+i]=data_G[i];
466 seq[2*oneD+i]=data_B[i];
470 for(int i=0;i<oneD;i++) {
479 int total_len=imsize;
481 int len= total_len/h2;
485 uchar *mix=new uchar[256];
490 for (int i = 0; i < 256 ; i++) {
491 mix[i]=Secretkey[i]^counter[i];
496 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
497 // g_print("%s\n", sha512);
507 // cout<<"hash "<<endl;
508 for (int i = 0; i < 64 ; i++) {
515 int *Pbox=new int[len];
516 int *PboxRM=new int[h2];
519 uchar Inv_Sbox1[256];
520 uchar Inv_Sbox2[256];
530 double time_encrypt=0;
531 double time_decrypt=0;
534 double t=TimeStart();
535 rc4key(DK, Sbox1, 8);
538 rc4key(&DK[8], Sbox2, 8);
540 rc4key(&DK[16], sc, 16);
546 rc4keyperm(&DK[32], len, rp, Pbox, 16);
549 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
552 //cout<<"Time initializaton "<<time<<endl;
557 for(int i=0;i<32;i++) {
561 uint myrand_copy=myrand;
568 for(int i=0;i<h2;i++){
573 inverse_tables(Sbox1,256,Inv_Sbox1);
574 inverse_tables(Sbox2,256,Inv_Sbox2);
586 for(i=0;i<nb_test;i++)
589 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
591 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
596 for(i=0;i<nb_test;i++)
599 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
601 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
606 for(i=0;i<nb_test;i++)
609 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
611 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
616 for(i=0;i<nb_test;i++)
619 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
621 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
626 for(i=0;i<nb_test;i++)
629 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
631 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
636 for(i=0;i<nb_test;i++)
639 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
641 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
646 time_encrypt+=TimeStop(t);
647 //cout<<"Time encrypt "<<
648 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
652 for(int i=0;i<oneD;i++) {
654 data_G[i]=seq2[oneD+i];
655 data_B[i]=seq2[2*oneD+i];
657 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
665 for(i=0;i<nb_test;i++) {
667 encrypt_ctr<4*4>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
669 decrypt<4*4>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
673 for(i=0;i<nb_test;i++) {
675 encrypt_ctr<8*8>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
677 decrypt<8*8>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
681 for(i=0;i<nb_test;i++) {
683 encrypt_ctr<16*16>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
685 decrypt<16*16>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
689 for(i=0;i<nb_test;i++) {
691 encrypt_ctr<32*32>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
693 decrypt<32*32>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
697 for(i=0;i<nb_test;i++) {
699 encrypt_ctr<64*64>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
701 decrypt<64*64>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
705 for(i=0;i<nb_test;i++) {
707 encrypt_ctr<128*128>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
709 decrypt<128*128>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
714 time_decrypt+=TimeStop(t);
715 //cout<<"Time decrypt "
716 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
719 for(int i=0;i<oneD;i++) {
721 data_G[i]=seq[oneD+i];
722 data_B[i]=seq[2*oneD+i];
724 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
728 for(int i=0;i<imsize;i++) {
729 //cout<<(int)buffer[i]<<endl;
730 if(buffer[i]!=seq[i]) {
734 // cout<<"RESULT CORRECT: "<<equal<<endl;