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++) {
152 for(int it=0;it<len;it++) {
164 for(int a=0;a<h2;a++){
165 fX[a]=Sbox1[RM1[X[a]]];
169 for(int a=0;a<h2;a++) {
174 for(int a=0;a<h2;a++) {
179 for(int a=0;a<h2;a++) {
180 seq_out[ind1+a]=fX[a]^seq_in[ind2+a];
183 for(int a=0;a<h2;a++) {
184 RM1[a]=RM1[PboxRM[a]];
196 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
199 uchar *X=new uchar[h2];
200 uchar *fX=new uchar[h2];
201 unsigned int *lX=(unsigned int*)X;
202 unsigned int *lseq_in=(unsigned int*)seq_in;
205 for(int it=0;it<len;it++) {
207 int ind2=Pbox[it]*h2;
209 for(int a=0;a<h2;a+=1) {
211 /* X[a+1]=seq_in[ind2+a+1];
212 X[a+2]=seq_in[ind2+a+2];
213 X[a+3]=seq_in[ind2+a+3];
214 /* X[a+4]=seq_in[ind2+a+4];
215 X[a+5]=seq_in[ind2+a+5];
216 X[a+6]=seq_in[ind2+a+6];
217 X[a+7]=seq_in[ind2+a+7];*/
220 for(int a=0;a<h2;a+=1){
222 /* fX[a+1]=Sbox1[X[a+1]];
223 fX[a+2]=Sbox1[X[a+2]];
224 fX[a+3]=Sbox1[X[a+3]];
225 /* fX[a+4]=Sbox1[X[a+4]];
226 fX[a+5]=Sbox1[X[a+5]];
227 fX[a+6]=Sbox1[X[a+6]];
228 fX[a+7]=Sbox1[X[a+7]];*/
232 for(int a=0;a<h2;a+=1) {
234 /* fX[a+1]=fX[a+1]^RM1[a+1];
235 fX[a+2]=fX[a+2]^RM1[a+2];
236 fX[a+3]=fX[a+3]^RM1[a+3];
237 /* fX[a+4]=fX[a+4]^RM1[a+4];
238 fX[a+5]=fX[a+5]^RM1[a+5];
239 fX[a+6]=fX[a+6]^RM1[a+6];
240 fX[a+7]=fX[a+7]^RM1[a+7];*/
244 for(int a=0;a<h2;a+=1) {
245 seq_out[ind1+a]=Sbox2[fX[a]];
246 /* seq_out[ind1+a+1]=Sbox2[fX[a+1]];
247 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
248 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
249 /* seq_out[ind1+a+4]=Sbox2[fX[a+4]];
250 seq_out[ind1+a+5]=Sbox2[fX[a+5]];
251 seq_out[ind1+a+6]=Sbox2[fX[a+6]];
252 seq_out[ind1+a+7]=Sbox2[fX[a+7]];*/
255 for(int a=0;a<h2;a+=1) {
256 RM1[a]=RM1[PboxRM[a]];
257 /* RM1[a+1]=RM1[PboxRM[a+1]];
258 RM1[a+2]=RM1[PboxRM[a+2]];
259 RM1[a+3]=RM1[PboxRM[a+3]];
260 /* RM1[a+4]=RM1[PboxRM[a+4]];
261 RM1[a+5]=RM1[PboxRM[a+5]];
262 RM1[a+6]=RM1[PboxRM[a+6]];
263 RM1[a+7]=RM1[PboxRM[a+7]];*/
276 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
279 uchar *fX=new uchar[h2];
282 uchar *Inv_Sbox1=new uchar[256];
283 inverse_tables(Sbox1,256,Inv_Sbox1);
285 uchar *Inv_Sbox2=new uchar[256];
286 inverse_tables(Sbox2,256,Inv_Sbox2);
291 for(int it=0;it<len;it++) {
294 int ind2=Pbox[it]*h2;
299 for(int a=0;a<h2;a++) {
300 fX[a]=seq_in[ind1+a];
302 for(int a=0;a<h2;a++) {
303 fX[a]=Inv_Sbox2[fX[a]];
305 for(int a=0;a<h2;a++) {
309 for(int a=0;a<h2;a++) {
310 RM1[a]=RM1[PboxRM[a]];
313 for(int a=0;a<h2;a++) {
314 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
324 int main(int argc, char** argv) {
327 for(int i=1; i<argc; i++){
328 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
329 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
330 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //CTR ? 1 otherwise CBC like
333 printf("nb times %d\n",nb_test);
334 printf("ctr %d\n",ctr);
341 nb_test=atoi(argv[1]);
342 if(nb_test<=0 || nb_test>10000) {
343 printf("nb tests is not correct\n");
347 printf("nb tests = %d\n\n",nb_test);
355 uchar Secretkey[key_size];
357 uchar counter[key_size];
359 for(int i=0;i<key_size;i++) {
360 Secretkey[i]=lrand48()&0xFF;
361 counter[i]=lrand48()&0xFF;
373 uchar *data_R, *data_G, *data_B;
374 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
375 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
381 int imsize=width*height*3;
382 uchar* seq= new uchar[imsize];
383 uchar* seq2= new uchar[imsize];
385 int oneD=width*height;
386 for(int i=0;i<oneD;i++) {
388 seq[oneD+i]=data_G[i];
389 seq[2*oneD+i]=data_B[i];
396 int total_len=imsize;
398 int len= total_len/h2;
402 uchar *mix=new uchar[256];
407 for (int i = 0; i < 256 ; i++) {
408 mix[i]=Secretkey[i]^counter[i];
413 for (int i = 0; i < 64 ; i++) {
422 rc4key(DK, Sbox1, 16);
425 rc4key(&DK[16], Sbox2, 16);
430 rc4key(&DK[32], sc, 16);
432 uchar outd[2*(h * h)];
433 prga(sc, 2*(h * h), outd);
438 for(int i=0;i<h2;i++){
449 for (int i = 48; i < 64; i++)
453 int *Pbox=new int[len];
454 int *PboxRM=new int[h2];
456 rc4keyperm(keyp, len, rp, Pbox, 16);
458 printf("len %d\n",len);
459 for(int i=0;i<len;i++) {
460 // printf("%d \n",Pbox[i]);
463 rc4keyperm(RM2, h2, rp, PboxRM, h2);
465 for(int i=0;i<h2;i++){
471 double t=TimeStart();
474 for(i=0;i<nb_test;i++)
476 // encrypt(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
477 encrypt_ctr(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
481 cout<<"Time encrypt "<<time<<endl;
484 for(int i=0;i<oneD;i++) {
486 data_G[i]=seq2[oneD+i];
487 data_B[i]=seq2[2*oneD+i];
489 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
494 for(i=0;i<nb_test;i++) {
495 // decrypt(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
496 encrypt_ctr(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
500 cout<<"Time decrypt "<<time<<endl;
503 for(int i=0;i<oneD;i++) {
505 data_G[i]=seq[oneD+i];
506 data_B[i]=seq[2*oneD+i];
508 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);