2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
13 /*#include <cryptopp/hex.h>
14 #include <cryptopp/sha.h>
15 #include <cryptopp/osrng.h>
16 #include <cryptopp/secblock.h>
21 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
22 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
26 //using namespace CryptoPP;
40 typedef unsigned char uchar;
45 struct timeval tstart;
46 gettimeofday(&tstart,0);
47 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
50 double TimeStop(double t)
54 gettimeofday(&tend,0);
55 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
64 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
66 for(int i=0;i<size_tab;i++) {
67 inv_perm_tabs[tab[i]] = i;
72 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
74 for(int i=0;i<size_tab;i++) {
75 inv_perm_tabs[tab[i]] = i;
82 void rc4key(uchar *key, uchar *sc, int size_DK) {
84 for(int i=0;i<256;i++) {
90 for(int i0=0; i0<256; i0++) {
91 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
100 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
106 for (int i=0;i<len;i++) {
109 for (int it = 0; it < rp; it++) {
111 for(int i0 = 0; i0<len; i0++) {
112 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
121 void prga(uchar *sc, int ldata, uchar *r) {
125 for (int it=0; it<ldata; it++) {
126 i0 = ((i0+1)&0xFE); //%255);
127 j0 = (j0 + sc[i0])&0xFF;
131 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
139 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc) {
142 // uchar *X=new uchar[h2];
143 // uchar *fX=new uchar[h2];
150 for(int a=0;a<h2;a++) {
151 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
155 for(int it=0;it<len;it++) {
167 /*for(int a=0;a<h2;a+=4){
174 for(int a=0;a<h2;a+=4){
182 for(int a=0;a<h2;a++)
190 for(int a=0;a<h2;a++)
195 for(int a=0;a<h2;a+=4) {
197 fX[a+1]=fX[a+1]^RM1[a+1];
198 fX[a+2]=fX[a+2]^RM1[a+2];
199 fX[a+3]=fX[a+3]^RM1[a+3];
203 for(int a=0;a<h2;a+=4) {
205 fX[a+1]=Sbox2[fX[a+1]];
206 fX[a+2]=Sbox2[fX[a+2]];
207 fX[a+3]=Sbox2[fX[a+3]];
210 for(int a=0;a<h2;a+=4) {
211 fX[a]=fX[a]^seq_in[ind2+a];
212 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
213 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
214 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
218 for(int a=0;a<h2;a+=4) {
219 seq_out[ind1+a]=fX[a];
220 seq_out[ind1+a+1]=fX[a+1];
221 seq_out[ind1+a+2]=fX[a+2];
222 seq_out[ind1+a+3]=fX[a+3];
225 for(int a=0;a<h2;a+=4) {
226 RM1[a]=RM1[PboxRM[a]];
227 RM1[a+1]=RM1[PboxRM[a+1]];
228 RM1[a+2]=RM1[PboxRM[a+2]];
229 RM1[a+3]=RM1[PboxRM[a+3]];
241 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
244 /* uchar *X=new uchar[h2];
245 uchar *fX=new uchar[h2];
246 unsigned int *lX=(unsigned int*)X;
247 unsigned int *lseq_in=(unsigned int*)seq_in;
251 // unsigned int *lX=(unsigned int*)X;
252 // unsigned int *lseq_in=(unsigned int*)seq_in;
255 for(int it=0;it<len;it++) {
257 int ind2=Pbox[it]*h2;
259 for(int a=0;a<h2;a+=4) {
261 X[a+1]=seq_in[ind2+a+1];
262 X[a+2]=seq_in[ind2+a+2];
263 X[a+3]=seq_in[ind2+a+3];
266 for(int a=0;a<h2;a+=4){
268 fX[a+1]=Sbox1[X[a+1]];
269 fX[a+2]=Sbox1[X[a+2]];
270 fX[a+3]=Sbox1[X[a+3]];
274 for(int a=0;a<h2;a+=4) {
276 fX[a+1]=fX[a+1]^RM1[a+1];
277 fX[a+2]=fX[a+2]^RM1[a+2];
278 fX[a+3]=fX[a+3]^RM1[a+3];
282 for(int a=0;a<h2;a+=4) {
283 seq_out[ind1+a]=Sbox2[fX[a]];
284 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
285 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
286 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
289 for(int a=0;a<h2;a+=4) {
290 RM1[a]=RM1[PboxRM[a]];
291 RM1[a+1]=RM1[PboxRM[a+1]];
292 RM1[a+2]=RM1[PboxRM[a+2]];
293 RM1[a+3]=RM1[PboxRM[a+3]];
307 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug) {
310 /*uchar *fX=new uchar[h2];
311 uchar *Inv_Sbox1=new uchar[256];
312 uchar *Inv_Sbox2=new uchar[256];
318 for(int it=0;it<len;it++) {
321 int ind2=Pbox[it]*h2;
326 for(int a=0;a<h2;a+=4) {
327 fX[a]=seq_in[ind1+a];
328 fX[a+1]=seq_in[ind1+a+1];
329 fX[a+2]=seq_in[ind1+a+2];
330 fX[a+3]=seq_in[ind1+a+3];
333 for(int a=0;a<h2;a+=4) {
334 fX[a]=Inv_Sbox2[fX[a]];
335 fX[a+1]=Inv_Sbox2[fX[a+1]];
336 fX[a+2]=Inv_Sbox2[fX[a+2]];
337 fX[a+3]=Inv_Sbox2[fX[a+3]];
339 for(int a=0;a<h2;a+=4) {
341 fX[a+1]=fX[a+1]^RM1[a+1];
342 fX[a+2]=fX[a+2]^RM1[a+2];
343 fX[a+3]=fX[a+3]^RM1[a+3];
346 for(int a=0;a<h2;a+=4) {
347 RM1[a]=RM1[PboxRM[a]];
348 RM1[a+1]=RM1[PboxRM[a+1]];
349 RM1[a+2]=RM1[PboxRM[a+2]];
350 RM1[a+3]=RM1[PboxRM[a+3]];
353 for(int a=0;a<h2;a+=4) {
354 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
355 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
356 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
357 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
367 int main(int argc, char** argv) {
376 for(int i=1; i<argc; i++){
377 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
378 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
379 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
380 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
381 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
384 /* printf("nb times %d\n",nb_test);
385 printf("ctr %d\n",ctr);
387 printf("lena %d\n",lena);
388 printf("size_buf %d\n",size_buf);
398 uchar Secretkey[key_size];
400 uchar counter[key_size];
402 for(int i=0;i<key_size;i++) {
403 Secretkey[i]=lrand48()&0xFF;
404 counter[i]=lrand48()&0xFF;
417 uchar *data_R, *data_G, *data_B;
422 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
423 imsize=width*height*3;
424 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
427 width=height=size_buf;
429 buffer=new uchar[imsize];
430 for(int i=0;i<imsize;i++) {
439 uchar* seq= new uchar[imsize];
440 uchar* seq2= new uchar[imsize];
442 int oneD=width*height;
444 for(int i=0;i<oneD;i++) {
446 seq[oneD+i]=data_G[i];
447 seq[2*oneD+i]=data_B[i];
451 for(int i=0;i<oneD;i++) {
460 int total_len=imsize;
462 int len= total_len/h2;
466 uchar *mix=new uchar[256];
471 for (int i = 0; i < 256 ; i++) {
472 mix[i]=Secretkey[i]^counter[i];
476 // cout<<"hash "<<endl;
477 for (int i = 0; i < 64 ; i++) {
486 rc4key(DK, Sbox1, 16);
489 rc4key(&DK[16], Sbox2, 16);
490 uchar Inv_Sbox1[256];
491 uchar Inv_Sbox2[256];
492 inverse_tables(Sbox1,256,Inv_Sbox1);
493 inverse_tables(Sbox2,256,Inv_Sbox2);
499 rc4key(&DK[32], sc, 16);
501 uchar outd[2*(h * h)];
502 prga(sc, 2*(h * h), outd);
507 for(int i=0;i<h2;i++){
518 for (int i = 48; i < 64; i++)
522 int *Pbox=new int[len];
523 int *PboxRM=new int[h2];
525 rc4keyperm(keyp, len, rp, Pbox, 16);
527 // printf("len %d\n",len);
528 for(int i=0;i<len;i++) {
529 // printf("%d \n",Pbox[i]);
532 rc4keyperm(RM2, h2, rp, PboxRM, h2);
534 for(int i=0;i<h2;i++){
537 int *Inv_Pbox=new int[len];
538 inverse_tables_int(Pbox,len,Inv_Pbox);
542 double t=TimeStart();
547 for(i=0;i<nb_test;i++)
550 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
552 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
557 for(i=0;i<nb_test;i++)
560 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
562 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
567 for(i=0;i<nb_test;i++)
570 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
572 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
577 for(i=0;i<nb_test;i++)
580 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
582 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
587 for(i=0;i<nb_test;i++)
590 encrypt_ctr<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
592 encrypt<64*64>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
598 cout<<"Time encrypt "<<time<<endl;
602 for(int i=0;i<oneD;i++) {
604 data_G[i]=seq2[oneD+i];
605 data_B[i]=seq2[2*oneD+i];
607 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
615 for(i=0;i<nb_test;i++) {
617 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
619 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
623 for(i=0;i<nb_test;i++) {
625 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
627 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
631 for(i=0;i<nb_test;i++) {
633 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
635 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
639 for(i=0;i<nb_test;i++) {
641 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
643 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
647 for(i=0;i<nb_test;i++) {
649 encrypt_ctr<64*64>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
651 decrypt<64*64>(seq2,seq,len,RM2,Pbox,PboxRM,Inv_Sbox1,Inv_Sbox2,0);
657 cout<<"Time decrypt "<<time<<endl;
660 for(int i=0;i<oneD;i++) {
662 data_G[i]=seq[oneD+i];
663 data_B[i]=seq[2*oneD+i];
665 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
669 for(int i=0;i<imsize;i++) {
670 //cout<<(int)buffer[i]<<endl;
671 if(buffer[i]!=seq[i]) {
675 // cout<<"RESULT CORRECT: "<<equal<<endl;