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++) {
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];
147 for(int a=0;a<h2;a++) {
148 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
152 for(int it=0;it<len;it++) {
164 /*for(int a=0;a<h2;a+=4){
171 for(int a=0;a<h2;a+=4){
179 for(int a=0;a<h2;a++)
187 for(int a=0;a<h2;a++)
192 for(int a=0;a<h2;a+=4) {
194 fX[a+1]=fX[a+1]^RM1[a+1];
195 fX[a+2]=fX[a+2]^RM1[a+2];
196 fX[a+3]=fX[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;
247 for(int it=0;it<len;it++) {
249 int ind2=Pbox[it]*h2;
251 for(int a=0;a<h2;a+=4) {
253 X[a+1]=seq_in[ind2+a+1];
254 X[a+2]=seq_in[ind2+a+2];
255 X[a+3]=seq_in[ind2+a+3];
258 for(int a=0;a<h2;a+=4){
260 fX[a+1]=Sbox1[X[a+1]];
261 fX[a+2]=Sbox1[X[a+2]];
262 fX[a+3]=Sbox1[X[a+3]];
266 for(int a=0;a<h2;a+=4) {
268 fX[a+1]=fX[a+1]^RM1[a+1];
269 fX[a+2]=fX[a+2]^RM1[a+2];
270 fX[a+3]=fX[a+3]^RM1[a+3];
274 for(int a=0;a<h2;a+=4) {
275 seq_out[ind1+a]=Sbox2[fX[a]];
276 seq_out[ind1+a+1]=Sbox2[fX[a+1]];
277 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
278 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
281 for(int a=0;a<h2;a+=4) {
282 RM1[a]=RM1[PboxRM[a]];
283 RM1[a+1]=RM1[PboxRM[a+1]];
284 RM1[a+2]=RM1[PboxRM[a+2]];
285 RM1[a+3]=RM1[PboxRM[a+3]];
299 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
302 uchar *fX=new uchar[h2];
305 uchar *Inv_Sbox1=new uchar[256];
306 inverse_tables(Sbox1,256,Inv_Sbox1);
308 uchar *Inv_Sbox2=new uchar[256];
309 inverse_tables(Sbox2,256,Inv_Sbox2);
314 for(int it=0;it<len;it++) {
317 int ind2=Pbox[it]*h2;
322 for(int a=0;a<h2;a+=4) {
323 fX[a]=seq_in[ind1+a];
324 fX[a+1]=seq_in[ind1+a+1];
325 fX[a+2]=seq_in[ind1+a+2];
326 fX[a+3]=seq_in[ind1+a+3];
329 for(int a=0;a<h2;a+=4) {
330 fX[a]=Inv_Sbox2[fX[a]];
331 fX[a+1]=Inv_Sbox2[fX[a+1]];
332 fX[a+2]=Inv_Sbox2[fX[a+2]];
333 fX[a+3]=Inv_Sbox2[fX[a+3]];
335 for(int a=0;a<h2;a+=4) {
337 fX[a+1]=fX[a+1]^RM1[a+1];
338 fX[a+2]=fX[a+2]^RM1[a+2];
339 fX[a+3]=fX[a+3]^RM1[a+3];
342 for(int a=0;a<h2;a+=4) {
343 RM1[a]=RM1[PboxRM[a]];
344 RM1[a+1]=RM1[PboxRM[a+1]];
345 RM1[a+2]=RM1[PboxRM[a+2]];
346 RM1[a+3]=RM1[PboxRM[a+3]];
349 for(int a=0;a<h2;a+=4) {
350 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
351 seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
352 seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
353 seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
363 int main(int argc, char** argv) {
368 for(int i=1; i<argc; i++){
369 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
370 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
371 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //CTR ? 1 otherwise CBC like
374 printf("nb times %d\n",nb_test);
375 printf("ctr %d\n",ctr);
386 uchar Secretkey[key_size];
388 uchar counter[key_size];
390 for(int i=0;i<key_size;i++) {
391 Secretkey[i]=lrand48()&0xFF;
392 counter[i]=lrand48()&0xFF;
404 uchar *data_R, *data_G, *data_B;
405 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
406 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
412 int imsize=width*height*3;
413 uchar* seq= new uchar[imsize];
414 uchar* seq2= new uchar[imsize];
416 int oneD=width*height;
417 for(int i=0;i<oneD;i++) {
419 seq[oneD+i]=data_G[i];
420 seq[2*oneD+i]=data_B[i];
427 int total_len=imsize;
429 int len= total_len/h2;
433 uchar *mix=new uchar[256];
438 for (int i = 0; i < 256 ; i++) {
439 mix[i]=Secretkey[i]^counter[i];
444 for (int i = 0; i < 64 ; i++) {
453 rc4key(DK, Sbox1, 16);
456 rc4key(&DK[16], Sbox2, 16);
461 rc4key(&DK[32], sc, 16);
463 uchar outd[2*(h * h)];
464 prga(sc, 2*(h * h), outd);
469 for(int i=0;i<h2;i++){
480 for (int i = 48; i < 64; i++)
484 int *Pbox=new int[len];
485 int *PboxRM=new int[h2];
487 rc4keyperm(keyp, len, rp, Pbox, 16);
489 printf("len %d\n",len);
490 for(int i=0;i<len;i++) {
491 // printf("%d \n",Pbox[i]);
494 rc4keyperm(RM2, h2, rp, PboxRM, h2);
496 for(int i=0;i<h2;i++){
499 int *Inv_Pbox=new int[len];
500 inverse_tables_int(Pbox,len,Inv_Pbox);
504 double t=TimeStart();
509 for(i=0;i<nb_test;i++)
512 encrypt_ctr<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
514 encrypt<4*4>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
519 for(i=0;i<nb_test;i++)
522 encrypt_ctr<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
524 encrypt<8*8>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
529 for(i=0;i<nb_test;i++)
532 encrypt_ctr<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
534 encrypt<16*16>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
539 for(i=0;i<nb_test;i++)
542 encrypt_ctr<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
544 encrypt<32*32>(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
550 cout<<"Time encrypt "<<time<<endl;
553 for(int i=0;i<oneD;i++) {
555 data_G[i]=seq2[oneD+i];
556 data_B[i]=seq2[2*oneD+i];
558 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
565 for(i=0;i<nb_test;i++) {
567 encrypt_ctr<4*4>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
569 decrypt<4*4>(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
573 for(i=0;i<nb_test;i++) {
575 encrypt_ctr<8*8>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
577 decrypt<8*8>(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
581 for(i=0;i<nb_test;i++) {
583 encrypt_ctr<16*16>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
585 decrypt<16*16>(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
589 for(i=0;i<nb_test;i++) {
591 encrypt_ctr<32*32>(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
593 decrypt<32*32>(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
599 cout<<"Time decrypt "<<time<<endl;
602 for(int i=0;i<oneD;i++) {
604 data_G[i]=seq[oneD+i];
605 data_B[i]=seq[2*oneD+i];
607 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);