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 X[a+1]=Sbox1[X[a+1]];
184 X[a+2]=Sbox1[X[a+2]];
185 X[a+3]=Sbox1[X[a+3]];
188 for(int a=0;a<h2;a+=4) {
190 fX[a+1]=X[a+1]^RM1[a+1];
191 fX[a+2]=X[a+2]^RM1[a+2];
192 fX[a+3]=X[a+3]^RM1[a+3];
195 for(int a=0;a<h2;a+=4) {
197 myrand=xorshift32(myrand);
200 fX[a]=fX[a]^seq_in[ind2+a]^(mm&255);
202 fX[a+1]=fX[a+1]^seq_in[ind2+a+1]^(mm&255);
204 fX[a+2]=fX[a+2]^seq_in[ind2+a+2]^(mm&255);
206 fX[a+3]=fX[a+3]^seq_in[ind2+a+3]^(mm&255);
210 for(int a=0;a<h2;a+=4) {
211 seq_out[ind1+a]=fX[a];
212 seq_out[ind1+a+1]=fX[a+1];
213 seq_out[ind1+a+2]=fX[a+2];
214 seq_out[ind1+a+3]=fX[a+3];
217 for(int a=0;a<h2;a+=4) {
218 RM1[a]=RM1[PboxRM[a]];
219 RM1[a+1]=RM1[PboxRM[a+1]];
220 RM1[a+2]=RM1[PboxRM[a+2]];
221 RM1[a+3]=RM1[PboxRM[a+3]];
233 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1, int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
239 for(int it=0;it<len;it++) {
241 int ind2=Pbox[it]*h2;
243 for(int a=0;a<h2;a+=4) {
244 myrand=xorshift32(myrand);
247 X[a]=seq_in[ind2+a]^(mm&255);
249 X[a+1]=seq_in[ind2+a+1]^(mm&255);
251 X[a+2]=seq_in[ind2+a+2]^(mm&255);
253 X[a+3]=seq_in[ind2+a+3]^(mm&255);
256 for(int a=0;a<h2;a+=4){
258 fX[a+1]=Sbox1[X[a+1]];
259 fX[a+2]=Sbox1[X[a+2]];
260 fX[a+3]=Sbox1[X[a+3]];
264 for(int a=0;a<h2;a+=4) {
266 fX[a+1]=fX[a+1]^RM1[a+1];
267 fX[a+2]=fX[a+2]^RM1[a+2];
268 fX[a+3]=fX[a+3]^RM1[a+3];
272 for(int a=0;a<h2;a+=4) {
273 seq_out[ind1+a]=Sbox2[fX[a]];
274 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
275 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
276 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
279 for(int a=0;a<h2;a+=4) {
280 RM1[a]=RM1[PboxRM[a]];
281 RM1[a+1]=RM1[PboxRM[a+1]];
282 RM1[a+2]=RM1[PboxRM[a+2]];
283 RM1[a+3]=RM1[PboxRM[a+3]];
305 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) {
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 myrand=xorshift32(myrand);
352 seq_out[ind2+a]=Inv_Sbox1[fX[a]]^(mm&255);
354 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]]^(mm&255);
356 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]]^(mm&255);
358 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]]^(mm&255);
369 int main(int argc, char** argv) {
378 for(int i=1; i<argc; i++){
379 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
380 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
381 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
382 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
383 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
386 /* printf("nb times %d\n",nb_test);
387 printf("ctr %d\n",ctr);
389 printf("lena %d\n",lena);
390 printf("size_buf %d\n",size_buf);
400 uchar Secretkey[key_size];
402 uchar counter[key_size];
404 for(int i=0;i<key_size;i++) {
405 Secretkey[i]=lrand48()&0xFF;
406 counter[i]=lrand48()&0xFF;
419 uchar *data_R, *data_G, *data_B;
428 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
429 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
430 imsize=width*height*3;
431 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
434 width=height=size_buf;
436 buffer=new uchar[imsize];
437 for(int i=0;i<imsize;i++) {
446 uchar* seq= new uchar[imsize];
447 uchar* seq2= new uchar[imsize];
449 int oneD=width*height;
451 for(int i=0;i<oneD;i++) {
453 seq[oneD+i]=data_G[i];
454 seq[2*oneD+i]=data_B[i];
458 for(int i=0;i<oneD;i++) {
467 int total_len=imsize;
469 int len= total_len/h2;
473 uchar *mix=new uchar[256];
478 for (int i = 0; i < 256 ; i++) {
479 mix[i]=Secretkey[i]^counter[i];
484 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
485 // g_print("%s\n", sha512);
495 // cout<<"hash "<<endl;
496 for (int i = 0; i < 64 ; i++) {
503 int *Pbox=new int[len];
504 int *PboxRM=new int[h2];
507 uchar Inv_Sbox1[256];
508 uchar Inv_Sbox2[256];
518 double time_encrypt=0;
519 double time_decrypt=0;
522 double t=TimeStart();
523 rc4key(DK, Sbox1, 8);
526 rc4key(&DK[8], Sbox2, 8);
528 rc4key(&DK[16], sc, 16);
534 rc4keyperm(&DK[32], len, rp, Pbox, 16);
537 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
540 //cout<<"Time initializaton "<<time<<endl;
545 for(int i=0;i<32;i++) {
549 uint myrand_copy=myrand;
556 for(int i=0;i<h2;i++){
561 inverse_tables(Sbox1,256,Inv_Sbox1);
562 inverse_tables(Sbox2,256,Inv_Sbox2);
574 for(i=0;i<nb_test;i++)
577 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
579 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
584 for(i=0;i<nb_test;i++)
587 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
589 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
594 for(i=0;i<nb_test;i++)
597 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
599 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
604 for(i=0;i<nb_test;i++)
607 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
609 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
614 for(i=0;i<nb_test;i++)
617 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
619 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
624 for(i=0;i<nb_test;i++)
627 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,1);
629 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
634 time_encrypt+=TimeStop(t);
635 //cout<<"Time encrypt "<<
636 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
640 for(int i=0;i<oneD;i++) {
642 data_G[i]=seq2[oneD+i];
643 data_B[i]=seq2[2*oneD+i];
645 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
653 for(i=0;i<nb_test;i++) {
655 encrypt_ctr<4*4>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
657 decrypt<4*4>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
661 for(i=0;i<nb_test;i++) {
663 encrypt_ctr<8*8>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
665 decrypt<8*8>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
669 for(i=0;i<nb_test;i++) {
671 encrypt_ctr<16*16>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
673 decrypt<16*16>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
677 for(i=0;i<nb_test;i++) {
679 encrypt_ctr<32*32>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
681 decrypt<32*32>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
685 for(i=0;i<nb_test;i++) {
687 encrypt_ctr<64*64>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
689 decrypt<64*64>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
693 for(i=0;i<nb_test;i++) {
695 encrypt_ctr<128*128>(seq2, seq,len,RM1_copy,Pbox,PboxRM,Sbox1,Sbox2,myrand,0);
697 decrypt<128*128>(seq2,seq,len,RM1_copy,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,myrand,0);
702 time_decrypt+=TimeStop(t);
703 //cout<<"Time decrypt "
704 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
707 for(int i=0;i<oneD;i++) {
709 data_G[i]=seq[oneD+i];
710 data_B[i]=seq[2*oneD+i];
712 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
716 for(int i=0;i<imsize;i++) {
717 //cout<<(int)buffer[i]<<endl;
718 if(buffer[i]!=seq[i]) {
722 // cout<<"RESULT CORRECT: "<<equal<<endl;