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];
153 void encrypt_ecb(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
162 for(int it=0;it<len/2;it++) {
164 int ind2=Pbox[it+len/2]*h;
168 RM1=&RM[PboxSRM[it]*h];
169 RM2=&RM[h*h+PboxSRM[it]*h];
172 for(int a=0;a<h;a+=4) {
174 X[a+1]=seq_in[ind2+a+1];
175 X[a+2]=seq_in[ind2+a+2];
176 X[a+3]=seq_in[ind2+a+3];
179 for(int a=0;a<h;a+=4) {
181 Y[a+1]=seq_in[ind1+a+1];
182 Y[a+2]=seq_in[ind1+a+2];
183 Y[a+3]=seq_in[ind1+a+3];
187 for(int a=0;a<h;a+=4) {
188 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
189 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
190 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
191 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
194 for(int a=0;a<h;a+=4) {
195 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
196 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
197 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
198 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
202 for(int a=0;a<h;a+=4) {
203 seq_out[ind2+a]=gY[a];
204 seq_out[ind2+a+1]=gY[a+1];
205 seq_out[ind2+a+2]=gY[a+2];
206 seq_out[ind2+a+3]=gY[a+3];
209 for(int a=0;a<h;a+=4) {
210 seq_out[ind1+a]=fX[a];
211 seq_out[ind1+a+1]=fX[a+1];
212 seq_out[ind1+a+2]=fX[a+2];
213 seq_out[ind1+a+3]=fX[a+3];
233 void decrypt_ecb(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
242 for(int it=0;it<len/2;it++) {
244 int ind2=Pbox[it+len/2]*h;
247 RM1=&RM[PboxSRM[it]*h];
248 RM2=&RM[h*h+PboxSRM[it]*h];
251 for(int a=0;a<h;a+=4) {
252 gY[a]=seq_in[ind2+a];
253 gY[a+1]=seq_in[ind2+a+1];
254 gY[a+2]=seq_in[ind2+a+2];
255 gY[a+3]=seq_in[ind2+a+3];
258 for(int a=0;a<h;a+=4) {
259 fX[a]=seq_in[ind1+a];
260 fX[a+1]=seq_in[ind1+a+1];
261 fX[a+2]=seq_in[ind1+a+2];
262 fX[a+3]=seq_in[ind1+a+3];
265 for(int a=0;a<h;a+=4) {
266 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
267 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
268 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
269 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
274 for(int a=0;a<h;a+=4) {
275 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
276 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
277 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
278 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
283 for(int a=0;a<h;a+=4) {
284 seq_out[ind2+a]=invfX[a];
285 seq_out[ind2+a+1]=invfX[a+1];
286 seq_out[ind2+a+2]=invfX[a+2];
287 seq_out[ind2+a+3]=invfX[a+3];
290 for(int a=0;a<h;a+=4) {
291 seq_out[ind1+a]=invgY[a];
292 seq_out[ind1+a+1]=invgY[a+1];
293 seq_out[ind1+a+2]=invgY[a+2];
294 seq_out[ind1+a+3]=invgY[a+3];
311 void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
327 for(int a=0;a<h;a+=4) {
328 myrand=xorshift32(myrand);
339 for(int a=0;a<h;a+=4) {
340 myrand=xorshift32(myrand);
354 for(int it=0;it<len/2;it++) {
356 int ind2=Pbox[it+len/2]*h;
360 RM1=&RM[PboxSRM[it]*h];
361 RM2=&RM[h*h+PboxSRM[it]*h];
364 for(int a=0;a<h;a+=4) {
366 X[a+1]=seq_in[ind2+a+1];
367 X[a+2]=seq_in[ind2+a+2];
368 X[a+3]=seq_in[ind2+a+3];
371 for(int a=0;a<h;a+=4) {
373 Y[a+1]=seq_in[ind1+a+1];
374 Y[a+2]=seq_in[ind1+a+2];
375 Y[a+3]=seq_in[ind1+a+3];
379 for(int a=0;a<h;a+=4) {
380 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
381 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
382 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
383 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
386 for(int a=0;a<h;a+=4) {
387 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
388 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
389 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
390 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
394 for(int a=0;a<h;a+=4) {
395 seq_out[ind2+a]=gY[a];
396 seq_out[ind2+a+1]=gY[a+1];
397 seq_out[ind2+a+2]=gY[a+2];
398 seq_out[ind2+a+3]=gY[a+3];
401 for(int a=0;a<h;a+=4) {
402 seq_out[ind1+a]=fX[a];
403 seq_out[ind1+a+1]=fX[a+1];
404 seq_out[ind1+a+2]=fX[a+2];
405 seq_out[ind1+a+3]=fX[a+3];
407 for(int a=0;a<h;a+=4) {
414 for(int a=0;a<h;a+=4) {
436 void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
447 for(int a=0;a<h;a+=4) {
448 myrand=xorshift32(myrand);
459 for(int a=0;a<h;a+=4) {
460 myrand=xorshift32(myrand);
477 for(int it=0;it<len/2;it++) {
479 int ind2=Pbox[it+len/2]*h;
482 RM1=&RM[PboxSRM[it]*h];
483 RM2=&RM[h*h+PboxSRM[it]*h];
486 for(int a=0;a<h;a+=4) {
487 gY[a]=seq_in[ind2+a];
488 gY[a+1]=seq_in[ind2+a+1];
489 gY[a+2]=seq_in[ind2+a+2];
490 gY[a+3]=seq_in[ind2+a+3];
493 for(int a=0;a<h;a+=4) {
494 fX[a]=seq_in[ind1+a];
495 fX[a+1]=seq_in[ind1+a+1];
496 fX[a+2]=seq_in[ind1+a+2];
497 fX[a+3]=seq_in[ind1+a+3];
501 for(int a=0;a<h;a+=4) {
502 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
503 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
504 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
505 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
509 for(int a=0;a<h;a+=4) {
510 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
511 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
512 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
513 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
517 for(int a=0;a<h;a+=4) {
518 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
519 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
520 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
521 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
525 for(int a=0;a<h;a+=4) {
526 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
527 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
528 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
529 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
534 for(int a=0;a<h;a+=4) {
535 seq_out[ind2+a]=invfX[a];
536 seq_out[ind2+a+1]=invfX[a+1];
537 seq_out[ind2+a+2]=invfX[a+2];
538 seq_out[ind2+a+3]=invfX[a+3];
541 for(int a=0;a<h;a+=4) {
542 seq_out[ind1+a]=invgY[a];
543 seq_out[ind1+a+1]=invgY[a+1];
544 seq_out[ind1+a+2]=invgY[a+2];
545 seq_out[ind1+a+3]=invgY[a+3];
547 for(int a=0;a<h;a+=4) {
554 for(int a=0;a<h;a+=4) {
570 int main(int argc, char** argv) {
579 for(int i=1; i<argc; i++){
580 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
581 if(strncmp(argv[i],"cbc",3)==0) cbc = atoi(&(argv[i][3])); //CBC ? 1 otherwise CBC like
582 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
583 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
584 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
587 /* printf("nb times %d\n",nb_test);
588 printf("ctr %d\n",ctr);
590 printf("lena %d\n",lena);
591 printf("size_buf %d\n",size_buf);
601 uchar Secretkey[key_size];
603 uchar counter[key_size];
605 for(int i=0;i<key_size;i++) {
606 Secretkey[i]=lrand48()&0xFF;
607 counter[i]=lrand48()&0xFF;
620 uchar *data_R, *data_G, *data_B;
629 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
630 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
631 imsize=width*height*3;
632 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
635 width=height=size_buf;
637 buffer=new uchar[imsize];
638 for(int i=0;i<imsize;i++) {
647 uchar* seq= new uchar[imsize];
648 uchar* seq2= new uchar[imsize];
650 int oneD=width*height;
652 for(int i=0;i<oneD;i++) {
654 seq[oneD+i]=data_G[i];
655 seq[2*oneD+i]=data_B[i];
659 for(int i=0;i<oneD;i++) {
668 int total_len=imsize;
670 int len= total_len/h;
674 uchar *mix=new uchar[256];
679 for (int i = 0; i < 256 ; i++) {
680 mix[i]=Secretkey[i]^counter[i];
685 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
686 // g_print("%s\n", sha512);
696 // cout<<"hash "<<endl;
697 for (int i = 0; i < 128 ; i++) {
704 int *Pbox=new int[len];
705 int *PboxSRM=new int[len/2];
706 int *PboxSRM2=new int[len/2];
709 uchar Inv_Sbox1[256];
710 uchar Inv_Sbox2[256];
718 double time_encrypt=0;
719 double time_decrypt=0;
722 double t=TimeStart();
723 rc4key(DK, Sbox1, 8);
726 rc4key(&DK[8], Sbox2, 8);
728 rc4key(&DK[16], sc, 16);
735 rc4keyperm(&DK[72], len, rp, Pbox, 16);
738 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
740 for(int i=0;i<len/2;i++) {
741 PboxSRM[i]=PboxSRM2[i]&(h-1);
745 for(int i=0;i<h*2;i++) {
747 cout<<(int)RM[i*h+j]<<" ";
755 //cout<<"Time initializaton "<<time<<endl;
760 for(int i=0;i<32;i++) {
764 uint myrand_copy=myrand;
772 inverse_tables(Sbox1,256,Inv_Sbox1);
773 inverse_tables(Sbox2,256,Inv_Sbox2);
784 for(i=0;i<nb_test;i++)
787 encrypt_cbc<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
789 encrypt_ecb<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
793 for(i=0;i<nb_test;i++)
796 encrypt_cbc<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
798 encrypt_ecb<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
802 for(i=0;i<nb_test;i++)
805 encrypt_cbc<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
807 encrypt_ecb<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
811 for(i=0;i<nb_test;i++)
814 encrypt_cbc<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
816 encrypt_ecb<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
820 for(i=0;i<nb_test;i++)
823 encrypt_cbc<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
825 encrypt_ecb<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
830 for(i=0;i<nb_test;i++)
833 encrypt_cbc<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
835 encrypt_ecb<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
840 time_encrypt+=TimeStop(t);
841 //cout<<"Time encrypt "<<
842 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
846 for(int i=0;i<oneD;i++) {
848 data_G[i]=seq2[oneD+i];
849 data_B[i]=seq2[2*oneD+i];
851 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
860 for(i=0;i<nb_test;i++) {
862 decrypt_cbc<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
864 decrypt_ecb<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
868 for(i=0;i<nb_test;i++) {
870 decrypt_cbc<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
872 decrypt_ecb<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
876 for(i=0;i<nb_test;i++) {
878 decrypt_cbc<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
880 decrypt_ecb<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
884 for(i=0;i<nb_test;i++) {
886 decrypt_cbc<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
888 decrypt_ecb<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
892 for(i=0;i<nb_test;i++) {
894 decrypt_cbc<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
896 decrypt_ecb<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
900 for(i=0;i<nb_test;i++) {
902 decrypt_cbc<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
904 decrypt_ecb<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
909 time_decrypt+=TimeStop(t);
910 //cout<<"Time decrypt "
911 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
914 for(int i=0;i<oneD;i++) {
916 data_G[i]=seq[oneD+i];
917 data_B[i]=seq[2*oneD+i];
919 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
923 for(int i=0;i<imsize;i++) {
924 //cout<<(int)buffer[i]<<endl;
925 if(buffer[i]!=seq[i]) {
929 // cout<<"RESULT CORRECT: "<<equal<<endl;