13 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
14 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
18 //using namespace CryptoPP;
32 typedef unsigned char uchar;
37 struct timeval tstart;
38 gettimeofday(&tstart,0);
39 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
42 double TimeStop(double t)
46 gettimeofday(&tend,0);
47 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
56 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
58 for(int i=0;i<size_tab;i++) {
59 inv_perm_tabs[tab[i]] = i;
64 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
66 for(int i=0;i<size_tab;i++) {
67 inv_perm_tabs[tab[i]] = i;
74 void rc4key(uchar *key, uchar *sc, int size_DK) {
76 for(int i=0;i<256;i++) {
82 for(int i0=0; i0<256; i0++) {
83 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
92 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
98 for (int i=0;i<len;i++) {
101 for (int it = 0; it < rp; it++) {
103 for(int i0 = 0; i0<len; i0++) {
104 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
113 void prga(uchar *sc, int ldata, uchar *r) {
117 for (int it=0; it<ldata; it++) {
118 i0 = ((i0+1)&0xFE); //%255);
119 j0 = (j0 + sc[i0])&0xFF;
123 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
129 void myhash(uchar *X, uchar* RM2, uchar *Sbox2) {
134 for(int k=0;k<h;k++) {
137 for(int a=0;a<h;a++) {
138 X2[a]=RM2[a]^X[k*h+a];
143 for(int a=4;a<h;a++) {
148 for(int a=0;a<h;a++) {
154 for(int a=h-1;a>0;a--) {
158 for(int a=0;a<h;a++) {
171 template<int h2, int h>
172 void encrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, uchar *Sbox3, int debug) {
180 for(int it=0;it<len;it++) {
182 int ind2=Pbox[it]*h2;
184 for(int a=0;a<h2;a+=4) {
186 X[a+1]=seq[ind1+a+1];
187 X[a+2]=seq[ind1+a+2];
188 X[a+3]=seq[ind1+a+3];
195 for(int a=0;a<h2;a+=4) {
197 Y[a+1]=seq[ind2+a+1];
198 Y[a+2]=seq[ind2+a+2];
199 Y[a+3]=seq[ind2+a+3];
204 for(int a=0;a<h2;a+=4){
206 fX[a+1]=Sbox1[X[a+1]];
207 fX[a+2]=Sbox1[X[a+2]];
208 fX[a+3]=Sbox1[X[a+3]];
211 for(int a=0;a<h2;a+=4){
213 gY[a+1]=Sbox2[Y[a+1]];
214 gY[a+2]=Sbox2[Y[a+2]];
215 gY[a+3]=Sbox2[Y[a+3]];
217 for(int a=0;a<h2;a+=4) {
218 fX[a]=fX[a]^RM1[a]^Y[a];
219 fX[a+1]=fX[a+1]^RM1[a+1]^Y[a+1];
220 fX[a+2]=fX[a+2]^RM1[a+2]^Y[a+2];
221 fX[a+3]=fX[a+3]^RM1[a+3]^Y[a+3];
223 for(int a=0;a<h2;a+=4){
225 gY[a+1]=gY[a+1]^RM2[a+1];
226 gY[a+2]=gY[a+2]^RM2[a+2];
227 gY[a+3]=gY[a+3]^RM2[a+3];
230 for(int a=0;a<h2;a+=4) {
231 seq[ind1+a]=Sbox2[fX[a]];
232 seq[ind1+a+1]=Sbox2[fX[a+1]];
233 seq[ind1+a+2]=Sbox2[fX[a+2]];
234 seq[ind1+a+3]=Sbox2[fX[a+3]];
236 for(int a=0;a<h2;a+=4){
237 seq[ind2+a]=Sbox1[gY[a]];
238 seq[ind2+a+1]=Sbox1[gY[a+1]];
239 seq[ind2+a+2]=Sbox1[gY[a+2]];
240 seq[ind2+a+3]=Sbox1[gY[a+3]];
245 for(int it=0;it<len;it++) {
247 for(int a=0;a<h2;a+=4) {
249 X[a+1]=seq[ind1+a+1];
250 X[a+2]=seq[ind1+a+2];
251 X[a+3]=seq[ind1+a+3];
254 myhash<h>(X, RM3, Sbox3);
260 template<int h2, int h>
261 void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *Sbox3, int debug) {
268 for(int it=0;it<len;it++) {
270 for(int a=0;a<h2;a+=4) {
272 X[a+1]=seq[ind1+a+1];
273 X[a+2]=seq[ind1+a+2];
274 X[a+3]=seq[ind1+a+3];
277 myhash<h>(X, RM3, Sbox3);
283 for(int it=len-1;it>=0;it--) {
285 int ind2=Pbox[it]*h2;
291 for(int a=0;a<h2;a+=4) {
292 fX[a]=Inv_Sbox2[seq[ind1+a]];
293 fX[a+1]=Inv_Sbox2[seq[ind1+a+1]];
294 fX[a+2]=Inv_Sbox2[seq[ind1+a+2]];
295 fX[a+3]=Inv_Sbox2[seq[ind1+a+3]];
298 for(int a=0;a<h2;a+=4) {
300 fX[a+1]=fX[a+1]^RM1[a+1];
301 fX[a+2]=fX[a+2]^RM1[a+2];
302 fX[a+3]=fX[a+3]^RM1[a+3];
306 for(int a=0;a<h2;a+=4) {
307 gY[a]=Inv_Sbox1[seq[ind2+a]];
308 gY[a+1]=Inv_Sbox1[seq[ind2+a+1]];
309 gY[a+2]=Inv_Sbox1[seq[ind2+a+2]];
310 gY[a+3]=Inv_Sbox1[seq[ind2+a+3]];
312 for(int a=0;a<h2;a+=4) {
313 gY[a]=Inv_Sbox2[gY[a]^RM2[a]];
314 gY[a+1]=Inv_Sbox2[gY[a+1]^RM2[a+1]];
315 gY[a+2]=Inv_Sbox2[gY[a+2]^RM2[a+2]];
316 gY[a+3]=Inv_Sbox2[gY[a+3]^RM2[a+3]];
320 for(int a=0;a<h2;a+=4) {
322 fX[a+1]=fX[a+1]^gY[a+1];
323 fX[a+2]=fX[a+2]^gY[a+2];
324 fX[a+3]=fX[a+3]^gY[a+3];
327 for(int a=0;a<h2;a+=4) {
328 seq[ind1+a]=Inv_Sbox1[fX[a]];
329 seq[ind1+a+1]=Inv_Sbox1[fX[a+1]];
330 seq[ind1+a+2]=Inv_Sbox1[fX[a+2]];
331 seq[ind1+a+3]=Inv_Sbox1[fX[a+3]];
335 for(int a=0;a<h2;a+=4) {
337 seq[ind2+a+1]=gY[a+1];
338 seq[ind2+a+2]=gY[a+2];
339 seq[ind2+a+3]=gY[a+3];
348 int main(int argc, char** argv) {
358 for(int i=1; i<argc; i++){
359 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
360 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
361 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
362 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
363 if(strncmp(argv[i],"impb",4)==0) impb = atoi(&(argv[i][4])); //Use Lena or buffer
364 if(strncmp(argv[i],"tgpb",4)==0) tgpb = atoi(&(argv[i][4])); //Use Lena or buffer
367 /* printf("nb times %d\n",nb_test);
368 printf("ctr %d\n",ctr);
370 printf("lena %d\n",lena);
371 printf("size_buf %d\n",size_buf);
377 int seed=12;//time(NULL);
381 uchar Secretkey[key_size];
383 uchar counter[key_size];
385 for(int i=0;i<key_size;i++) {
386 Secretkey[i]=lrand48()&0xFF;
387 counter[i]=lrand48()&0xFF;
400 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("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
407 imsize=width*height*3;
408 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
411 width=height=size_buf;
413 buffer=new uchar[imsize];
414 for(int i=0;i<imsize;i++) {
423 uchar* seq= new uchar[imsize];
424 uchar* seq2= new uchar[imsize];
426 int oneD=width*height;
428 for(int i=0;i<oneD;i++) {
430 seq[oneD+i]=data_G[i];
431 seq[2*oneD+i]=data_B[i];
435 for(int i=0;i<oneD;i++) {
444 int total_len=imsize;
446 int len= total_len/h2;
450 uchar *mix=new uchar[256];
455 for (int i = 0; i < 256 ; i++) {
456 mix[i]=Secretkey[i]^counter[i];
460 // cout<<"hash "<<endl;
461 for (int i = 0; i < 64 ; i++) {
468 int *Pbox=new int[len];
472 uchar Inv_Sbox1[256];
473 uchar Inv_Sbox2[256];
482 double time_encrypt=0;
483 double time_decrypt=0;
486 double t=TimeStart();
487 rc4key(DK, Sbox1, 8);
490 rc4key(&DK[8], Sbox2, 8);
491 rc4key(&DK[16], Sbox3, 8);
493 rc4key(&DK[24], sc, 16);
497 rc4key(&DK[48], sc, 16);
503 rc4keyperm(&DK[64], len, rp, Pbox, 16);
506 rc4key(&DK[80], sc, 16);
512 //cout<<"Time initializaton "<<time<<endl;
517 printf("%d ",RM3[i]);
526 for(int i=0;i<h;i++){
531 inverse_tables(Sbox1,256,Inv_Sbox1);
532 inverse_tables(Sbox2,256,Inv_Sbox2);
544 for(i=0;i<nb_test;i++)
546 encrypt<4*4,4>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
551 for(i=0;i<nb_test;i++)
553 encrypt<8*8,8>(seq, len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
557 for(i=0;i<nb_test;i++)
559 encrypt<16*16,16>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
563 for(i=0;i<nb_test;i++)
565 encrypt<32*32,32>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
569 for(i=0;i<nb_test;i++)
571 encrypt<64*64,64>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
575 for(i=0;i<nb_test;i++)
577 encrypt<128*128,128>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
585 time_encrypt+=TimeStop(t);
586 //cout<<"Time encrypt "<<
587 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
591 for(int i=0;i<oneD;i++) {
593 data_G[i]=seq[oneD+i];
594 data_B[i]=seq[2*oneD+i];
596 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
600 for(int i=0;i<h;i++){
601 cout<<(int)RM3[i]<<" ";
609 if(tgpb>=0 && tgpb<h) {
618 for(i=0;i<nb_test;i++) {
619 decrypt<4*4,4>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
623 for(i=0;i<nb_test;i++) {
624 decrypt<8*8,8>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
628 for(i=0;i<nb_test;i++) {
629 decrypt<16*16,16>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
633 for(i=0;i<nb_test;i++) {
634 decrypt<32*32,32>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
638 for(i=0;i<nb_test;i++) {
639 decrypt<64*64,64>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
643 for(i=0;i<nb_test;i++) {
644 decrypt<128*128,128>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
649 time_decrypt+=TimeStop(t);
650 //cout<<"Time decrypt "
651 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
654 cout<<"\nTAG 2"<<endl;
655 for(int i=0;i<h;i++){
656 cout<<(int)RM4[i]<<" ";
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;