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;
66 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
68 for(int i=0;i<size_tab;i++) {
69 inv_perm_tabs[tab[i]] = i;
74 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
76 for(int i=0;i<size_tab;i++) {
77 inv_perm_tabs[tab[i]] = i;
84 void rc4key(uchar *key, uchar *sc, int size_DK) {
86 for(int i=0;i<256;i++) {
92 for(int i0=0; i0<256; i0++) {
93 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
102 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
108 for (int i=0;i<len;i++) {
111 for (int it = 0; it < rp; it++) {
113 for(int i0 = 0; i0<len; i0++) {
114 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
123 void prga(uchar *sc, int ldata, uchar *r) {
127 for (int it=0; it<ldata; it++) {
128 i0 = ((i0+1)&0xFE); //%255);
129 j0 = (j0 + sc[i0])&0xFF;
133 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
141 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc) {
144 // uchar *X=new uchar[h2];
145 // uchar *fX=new uchar[h2];
152 for(int a=0;a<h2;a+=4) {
153 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
154 X[a+1]=Sbox1[(a+1)&0xFF];
155 X[a+2]=Sbox1[(a+2)&0xFF];
156 X[a+3]=Sbox1[(a+3)&0xFF];
160 for(int it=0;it<len;it++) {
171 for(int a=0;a<h2;a+=4) {
173 X[a+1]=Sbox1[X[a+1]];
174 X[a+2]=Sbox1[X[a+2]];
175 X[a+3]=Sbox1[X[a+3]];
180 // *(int*)&fX[0]^=it;
183 /* for(int a=0;a<h2;a+=16) {
187 *(int*)&fX[a+12]^=it;
192 for(int a=0;a<h2;a+=4) {
194 fX[a+1]=X[a+1]^RM1[a+1];
195 fX[a+2]=X[a+2]^RM1[a+2];
196 fX[a+3]=X[a+3]^RM1[a+3];
200 /* for(int a=0;a<h2;a+=4) {
202 fX[a+1]=Sbox2[fX[a+1]];
203 fX[a+2]=Sbox2[fX[a+2]];
204 fX[a+3]=Sbox2[fX[a+3]];
207 for(int a=0;a<h2;a+=4) {
208 fX[a]=fX[a]^seq_in[ind2+a];
209 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
210 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
211 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
215 for(int a=0;a<h2;a+=4) {
216 seq_out[ind1+a]=fX[a];
217 seq_out[ind1+a+1]=fX[a+1];
218 seq_out[ind1+a+2]=fX[a+2];
219 seq_out[ind1+a+3]=fX[a+3];
222 for(int a=0;a<h2;a+=4) {
223 RM1[a]=RM1[PboxRM[a]];
224 RM1[a+1]=RM1[PboxRM[a+1]];
225 RM1[a+2]=RM1[PboxRM[a+2]];
226 RM1[a+3]=RM1[PboxRM[a+3]];
238 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
241 /* uchar *X=new uchar[h2];
242 uchar *fX=new uchar[h2];
243 unsigned int *lX=(unsigned int*)X;
244 unsigned int *lseq_in=(unsigned int*)seq_in;
248 // unsigned int *lX=(unsigned int*)X;
249 // unsigned int *lseq_in=(unsigned int*)seq_in;
252 for(int it=0;it<len;it++) {
254 int ind2=Pbox[it]*h2;
256 for(int a=0;a<h2;a+=4) {
258 X[a+1]=seq_in[ind2+a+1];
259 X[a+2]=seq_in[ind2+a+2];
260 X[a+3]=seq_in[ind2+a+3];
263 for(int a=0;a<h2;a+=4){
265 fX[a+1]=Sbox1[X[a+1]];
266 fX[a+2]=Sbox1[X[a+2]];
267 fX[a+3]=Sbox1[X[a+3]];
271 for(int a=0;a<h2;a+=4) {
273 fX[a+1]=fX[a+1]^RM1[a+1];
274 fX[a+2]=fX[a+2]^RM1[a+2];
275 fX[a+3]=fX[a+3]^RM1[a+3];
279 for(int a=0;a<h2;a+=4) {
280 seq_out[ind1+a]=Sbox2[fX[a]];
281 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
282 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
283 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
286 for(int a=0;a<h2;a+=4) {
287 RM1[a]=RM1[PboxRM[a]];
288 RM1[a+1]=RM1[PboxRM[a+1]];
289 RM1[a+2]=RM1[PboxRM[a+2]];
290 RM1[a+3]=RM1[PboxRM[a+3]];
304 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug) {
307 /*uchar *fX=new uchar[h2];
308 uchar *Inv_Sbox1=new uchar[256];
309 uchar *Inv_Sbox2=new uchar[256];
315 for(int it=0;it<len;it++) {
318 int ind2=Pbox[it]*h2;
323 for(int a=0;a<h2;a+=4) {
324 fX[a]=seq_in[ind1+a];
325 fX[a+1]=seq_in[ind1+a+1];
326 fX[a+2]=seq_in[ind1+a+2];
327 fX[a+3]=seq_in[ind1+a+3];
330 for(int a=0;a<h2;a+=4) {
331 fX[a]=Inv_Sbox2[fX[a]];
332 fX[a+1]=Inv_Sbox2[fX[a+1]];
333 fX[a+2]=Inv_Sbox2[fX[a+2]];
334 fX[a+3]=Inv_Sbox2[fX[a+3]];
336 for(int a=0;a<h2;a+=4) {
338 fX[a+1]=fX[a+1]^RM1[a+1];
339 fX[a+2]=fX[a+2]^RM1[a+2];
340 fX[a+3]=fX[a+3]^RM1[a+3];
343 for(int a=0;a<h2;a+=4) {
344 RM1[a]=RM1[PboxRM[a]];
345 RM1[a+1]=RM1[PboxRM[a+1]];
346 RM1[a+2]=RM1[PboxRM[a+2]];
347 RM1[a+3]=RM1[PboxRM[a+3]];
350 for(int a=0;a<h2;a+=4) {
351 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
352 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
353 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
354 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
364 int main(int argc, char** argv) {
373 for(int i=1; i<argc; i++){
374 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
375 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
376 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
377 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
378 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
381 /* printf("nb times %d\n",nb_test);
382 printf("ctr %d\n",ctr);
384 printf("lena %d\n",lena);
385 printf("size_buf %d\n",size_buf);
395 uchar Secretkey[key_size];
397 uchar counter[key_size];
399 for(int i=0;i<key_size;i++) {
400 Secretkey[i]=lrand48()&0xFF;
401 counter[i]=lrand48()&0xFF;
414 uchar *data_R, *data_G, *data_B;
423 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
424 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
425 imsize=width*height*3;
426 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
429 width=height=size_buf;
431 buffer=new uchar[imsize];
432 for(int i=0;i<imsize;i++) {
441 uchar* seq= new uchar[imsize];
442 uchar* seq2= new uchar[imsize];
444 int oneD=width*height;
446 for(int i=0;i<oneD;i++) {
448 seq[oneD+i]=data_G[i];
449 seq[2*oneD+i]=data_B[i];
453 for(int i=0;i<oneD;i++) {
462 int total_len=imsize;
464 int len= total_len/h2;
468 uchar *mix=new uchar[256];
473 for (int i = 0; i < 256 ; i++) {
474 mix[i]=Secretkey[i]^counter[i];
479 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
480 // g_print("%s\n", sha512);
490 // cout<<"hash "<<endl;
491 for (int i = 0; i < 64 ; i++) {
498 int *Pbox=new int[len];
499 int *PboxRM=new int[h2];
502 uchar Inv_Sbox1[256];
503 uchar Inv_Sbox2[256];
511 double time_encrypt=0;
512 double time_decrypt=0;
515 double t=TimeStart();
516 rc4key(DK, Sbox1, 8);
519 rc4key(&DK[8], Sbox2, 8);
521 rc4key(&DK[16], sc, 16);
527 rc4keyperm(&DK[32], len, rp, Pbox, 16);
530 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
533 //cout<<"Time initializaton "<<time<<endl;
545 for(int i=0;i<h2;i++){
550 inverse_tables(Sbox1,256,Inv_Sbox1);
551 inverse_tables(Sbox2,256,Inv_Sbox2);
563 for(i=0;i<nb_test;i++)
566 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
568 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
573 for(i=0;i<nb_test;i++)
576 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
578 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
583 for(i=0;i<nb_test;i++)
586 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
588 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
593 for(i=0;i<nb_test;i++)
596 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
598 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
603 for(i=0;i<nb_test;i++)
606 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
608 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
613 for(i=0;i<nb_test;i++)
616 encrypt_ctr<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
618 encrypt<128*128>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
623 time_encrypt+=TimeStop(t);
624 //cout<<"Time encrypt "<<
625 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
629 for(int i=0;i<oneD;i++) {
631 data_G[i]=seq2[oneD+i];
632 data_B[i]=seq2[2*oneD+i];
634 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
642 for(i=0;i<nb_test;i++) {
644 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
646 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
650 for(i=0;i<nb_test;i++) {
652 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
654 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
658 for(i=0;i<nb_test;i++) {
660 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
662 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
666 for(i=0;i<nb_test;i++) {
668 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
670 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
674 for(i=0;i<nb_test;i++) {
676 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
678 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
682 for(i=0;i<nb_test;i++) {
684 encrypt_ctr<128*128>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
686 decrypt<128*128>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
691 time_decrypt+=TimeStop(t);
692 //cout<<"Time decrypt "
693 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
696 for(int i=0;i<oneD;i++) {
698 data_G[i]=seq[oneD+i];
699 data_B[i]=seq[2*oneD+i];
701 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
705 for(int i=0;i<imsize;i++) {
706 //cout<<(int)buffer[i]<<endl;
707 if(buffer[i]!=seq[i]) {
711 // cout<<"RESULT CORRECT: "<<equal<<endl;