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) {
133 for(int k=0;k<h;k++) {
136 for(int a=0;a<h;a+=4) {
137 X2[a]=RM2[a]^X[k*h+a];
138 X2[a+1]=RM2[a+1]^X[k*h+a+1];
139 X2[a+2]=RM2[a+2]^X[k*h+a+2];
140 X2[a+3]=RM2[a+3]^X[k*h+a+3];
148 for(int a=4;a<h;a+=4) {
151 Y[a+2]=Y[a+1]^X2[a+1];
152 Y[a+3]=Y[a+2]^X2[a+2];
156 for(int a=0;a<h;a+=4) {
158 Y[a+1]=Sbox2[Y[a+1]];
159 Y[a+2]=Sbox2[Y[a+2]];
160 Y[a+3]=Sbox2[Y[a+3]];
165 RM2[h-1]=Y[h-1]^Y[0];
166 RM2[h-2]=RM2[h-1]^Y[h-1];
167 RM2[h-3]=RM2[h-2]^Y[h-2];
168 RM2[h-4]=RM2[h-3]^Y[h-3];
169 for(int a=h-4;a>0;a-=4) {
170 RM2[a-1]=RM2[a]^Y[a];
171 RM2[a-2]=RM2[a-1]^Y[a-1];
172 RM2[a-3]=RM2[a-2]^Y[a-2];
173 RM2[a-4]=RM2[a-3]^Y[a-3];
183 template<int h2, int h>
184 void encrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, uchar *Sbox3, int debug) {
192 for(int it=0;it<len;it++) {
194 int ind2=Pbox[it]*h2;
196 for(int a=0;a<h2;a+=4) {
198 X[a+1]=seq[ind1+a+1];
199 X[a+2]=seq[ind1+a+2];
200 X[a+3]=seq[ind1+a+3];
207 for(int a=0;a<h2;a+=4) {
209 Y[a+1]=seq[ind2+a+1];
210 Y[a+2]=seq[ind2+a+2];
211 Y[a+3]=seq[ind2+a+3];
216 for(int a=0;a<h2;a+=4){
218 fX[a+1]=Sbox1[X[a+1]];
219 fX[a+2]=Sbox1[X[a+2]];
220 fX[a+3]=Sbox1[X[a+3]];
223 for(int a=0;a<h2;a+=4){
225 gY[a+1]=Sbox2[Y[a+1]];
226 gY[a+2]=Sbox2[Y[a+2]];
227 gY[a+3]=Sbox2[Y[a+3]];
229 for(int a=0;a<h2;a+=4) {
230 fX[a]=fX[a]^RM1[a]^Y[a];
231 fX[a+1]=fX[a+1]^RM1[a+1]^Y[a+1];
232 fX[a+2]=fX[a+2]^RM1[a+2]^Y[a+2];
233 fX[a+3]=fX[a+3]^RM1[a+3]^Y[a+3];
235 for(int a=0;a<h2;a+=4){
237 gY[a+1]=gY[a+1]^RM2[a+1];
238 gY[a+2]=gY[a+2]^RM2[a+2];
239 gY[a+3]=gY[a+3]^RM2[a+3];
242 for(int a=0;a<h2;a+=4) {
243 seq[ind1+a]=Sbox2[fX[a]];
244 seq[ind1+a+1]=Sbox2[fX[a+1]];
245 seq[ind1+a+2]=Sbox2[fX[a+2]];
246 seq[ind1+a+3]=Sbox2[fX[a+3]];
248 for(int a=0;a<h2;a+=4){
249 seq[ind2+a]=Sbox1[gY[a]];
250 seq[ind2+a+1]=Sbox1[gY[a+1]];
251 seq[ind2+a+2]=Sbox1[gY[a+2]];
252 seq[ind2+a+3]=Sbox1[gY[a+3]];
257 for(int it=0;it<len;it++) {
259 for(int a=0;a<h2;a+=4) {
261 X[a+1]=seq[ind1+a+1];
262 X[a+2]=seq[ind1+a+2];
263 X[a+3]=seq[ind1+a+3];
266 myhash<h>(X, RM3, Sbox3);
272 template<int h2, int h>
273 void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *Sbox3, int debug) {
280 for(int it=0;it<len;it++) {
282 for(int a=0;a<h2;a+=4) {
284 X[a+1]=seq[ind1+a+1];
285 X[a+2]=seq[ind1+a+2];
286 X[a+3]=seq[ind1+a+3];
289 myhash<h>(X, RM3, Sbox3);
295 for(int it=len-1;it>=0;it--) {
297 int ind2=Pbox[it]*h2;
303 for(int a=0;a<h2;a+=4) {
304 fX[a]=Inv_Sbox2[seq[ind1+a]];
305 fX[a+1]=Inv_Sbox2[seq[ind1+a+1]];
306 fX[a+2]=Inv_Sbox2[seq[ind1+a+2]];
307 fX[a+3]=Inv_Sbox2[seq[ind1+a+3]];
310 for(int a=0;a<h2;a+=4) {
312 fX[a+1]=fX[a+1]^RM1[a+1];
313 fX[a+2]=fX[a+2]^RM1[a+2];
314 fX[a+3]=fX[a+3]^RM1[a+3];
318 for(int a=0;a<h2;a+=4) {
319 gY[a]=Inv_Sbox1[seq[ind2+a]];
320 gY[a+1]=Inv_Sbox1[seq[ind2+a+1]];
321 gY[a+2]=Inv_Sbox1[seq[ind2+a+2]];
322 gY[a+3]=Inv_Sbox1[seq[ind2+a+3]];
324 for(int a=0;a<h2;a+=4) {
325 gY[a]=Inv_Sbox2[gY[a]^RM2[a]];
326 gY[a+1]=Inv_Sbox2[gY[a+1]^RM2[a+1]];
327 gY[a+2]=Inv_Sbox2[gY[a+2]^RM2[a+2]];
328 gY[a+3]=Inv_Sbox2[gY[a+3]^RM2[a+3]];
332 for(int a=0;a<h2;a+=4) {
334 fX[a+1]=fX[a+1]^gY[a+1];
335 fX[a+2]=fX[a+2]^gY[a+2];
336 fX[a+3]=fX[a+3]^gY[a+3];
339 for(int a=0;a<h2;a+=4) {
340 seq[ind1+a]=Inv_Sbox1[fX[a]];
341 seq[ind1+a+1]=Inv_Sbox1[fX[a+1]];
342 seq[ind1+a+2]=Inv_Sbox1[fX[a+2]];
343 seq[ind1+a+3]=Inv_Sbox1[fX[a+3]];
347 for(int a=0;a<h2;a+=4) {
349 seq[ind2+a+1]=gY[a+1];
350 seq[ind2+a+2]=gY[a+2];
351 seq[ind2+a+3]=gY[a+3];
360 int main(int argc, char** argv) {
370 for(int i=1; i<argc; i++){
371 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
372 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
373 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
374 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
375 if(strncmp(argv[i],"impb",4)==0) impb = atoi(&(argv[i][4])); //Use Lena or buffer
376 if(strncmp(argv[i],"tgpb",4)==0) tgpb = atoi(&(argv[i][4])); //Use Lena or buffer
379 /* printf("nb times %d\n",nb_test);
380 printf("ctr %d\n",ctr);
382 printf("lena %d\n",lena);
383 printf("size_buf %d\n",size_buf);
389 int seed=12;//time(NULL);
393 uchar Secretkey[key_size];
395 uchar counter[key_size];
397 for(int i=0;i<key_size;i++) {
398 Secretkey[i]=lrand48()&0xFF;
399 counter[i]=lrand48()&0xFF;
412 uchar *data_R, *data_G, *data_B;
417 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
418 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
419 imsize=width*height*3;
420 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
423 width=height=size_buf;
425 buffer=new uchar[imsize];
426 for(int i=0;i<imsize;i++) {
435 uchar* seq= new uchar[imsize];
436 uchar* seq2= new uchar[imsize];
438 int oneD=width*height;
440 for(int i=0;i<oneD;i++) {
442 seq[oneD+i]=data_G[i];
443 seq[2*oneD+i]=data_B[i];
447 for(int i=0;i<oneD;i++) {
456 int total_len=imsize;
458 int len= total_len/h2;
462 uchar *mix=new uchar[256];
467 for (int i = 0; i < 256 ; i++) {
468 mix[i]=Secretkey[i]^counter[i];
472 // cout<<"hash "<<endl;
473 for (int i = 0; i < 64 ; i++) {
480 int *Pbox=new int[len];
484 uchar Inv_Sbox1[256];
485 uchar Inv_Sbox2[256];
494 double time_encrypt=0;
495 double time_decrypt=0;
498 double t=TimeStart();
499 rc4key(DK, Sbox1, 8);
502 rc4key(&DK[8], Sbox2, 8);
503 rc4key(&DK[16], Sbox3, 8);
505 rc4key(&DK[24], sc, 16);
509 rc4key(&DK[48], sc, 16);
515 rc4keyperm(&DK[64], len, rp, Pbox, 16);
518 rc4key(&DK[80], sc, 16);
524 //cout<<"Time initializaton "<<time<<endl;
529 printf("%d ",RM3[i]);
538 for(int i=0;i<h;i++){
543 inverse_tables(Sbox1,256,Inv_Sbox1);
544 inverse_tables(Sbox2,256,Inv_Sbox2);
556 for(i=0;i<nb_test;i++)
558 encrypt<4*4,4>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
563 for(i=0;i<nb_test;i++)
565 encrypt<8*8,8>(seq, len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
569 for(i=0;i<nb_test;i++)
571 encrypt<16*16,16>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
575 for(i=0;i<nb_test;i++)
577 encrypt<32*32,32>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
581 for(i=0;i<nb_test;i++)
583 encrypt<64*64,64>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
587 for(i=0;i<nb_test;i++)
589 encrypt<128*128,128>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
597 time_encrypt+=TimeStop(t);
598 //cout<<"Time encrypt "<<
599 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
603 for(int i=0;i<oneD;i++) {
605 data_G[i]=seq[oneD+i];
606 data_B[i]=seq[2*oneD+i];
608 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
612 for(int i=0;i<h;i++){
613 cout<<(int)RM3[i]<<" ";
621 if(tgpb>=0 && tgpb<h) {
630 for(i=0;i<nb_test;i++) {
631 decrypt<4*4,4>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
635 for(i=0;i<nb_test;i++) {
636 decrypt<8*8,8>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
640 for(i=0;i<nb_test;i++) {
641 decrypt<16*16,16>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
645 for(i=0;i<nb_test;i++) {
646 decrypt<32*32,32>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
650 for(i=0;i<nb_test;i++) {
651 decrypt<64*64,64>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
655 for(i=0;i<nb_test;i++) {
656 decrypt<128*128,128>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
661 time_decrypt+=TimeStop(t);
662 //cout<<"Time decrypt "
663 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
666 cout<<"\nTAG 2"<<endl;
667 for(int i=0;i<h;i++){
668 cout<<(int)RM4[i]<<" ";
672 for(int i=0;i<oneD;i++) {
674 data_G[i]=seq[oneD+i];
675 data_B[i]=seq[2*oneD+i];
677 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
681 for(int i=0;i<imsize;i++) {
682 //cout<<(int)buffer[i]<<endl;
683 if(buffer[i]!=seq[i]) {
687 // cout<<"RESULT CORRECT: "<<equal<<endl;