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;
39 typedef unsigned char uchar;
44 struct timeval tstart;
45 gettimeofday(&tstart,0);
46 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
49 double TimeStop(double t)
53 gettimeofday(&tend,0);
54 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
63 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
65 for(int i=0;i<size_tab;i++) {
66 inv_perm_tabs[tab[i]] = i;
71 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
73 for(int i=0;i<size_tab;i++) {
74 inv_perm_tabs[tab[i]] = i;
81 void rc4key(uchar *key, uchar *sc, int size_DK) {
83 for(int i=0;i<256;i++) {
89 for(int i0=0; i0<256; i0++) {
90 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
99 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
105 for (int i=0;i<len;i++) {
108 for (int it = 0; it < rp; it++) {
110 for(int i0 = 0; i0<len; i0++) {
111 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
120 void prga(uchar *sc, int ldata, uchar *r) {
124 for (int it=0; it<ldata; it++) {
126 j0 = (j0 + sc[i0])&0xFF;
130 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
138 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc) {
141 uchar *X=new uchar[h2];
142 uchar *fX=new uchar[h2];
146 for(int a=0;a<h2;a++) {
151 for(int it=0;it<len;it++) {
163 for(int a=0;a<h2;a++){
164 fX[a]=Sbox1[RM1[X[a]]];
168 for(int a=0;a<h2;a++) {
173 for(int a=0;a<h2;a++) {
178 for(int a=0;a<h2;a++) {
179 seq_out[ind1+a]=fX[a]^seq_in[ind2+a];
182 for(int a=0;a<h2;a++) {
183 RM1[a]=RM1[PboxRM[a]];
195 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
198 uchar *X=new uchar[h2];
199 uchar *fX=new uchar[h2];
200 unsigned int *lX=(unsigned int*)X;
201 unsigned int *lseq_in=(unsigned int*)seq_in;
204 for(int it=0;it<len;it++) {
206 int ind2=Pbox[it]*h2;
208 for(int a=0;a<h2;a+=1) {
210 /* X[a+1]=seq_in[ind2+a+1];
211 X[a+2]=seq_in[ind2+a+2];
212 X[a+3]=seq_in[ind2+a+3];
213 /* X[a+4]=seq_in[ind2+a+4];
214 X[a+5]=seq_in[ind2+a+5];
215 X[a+6]=seq_in[ind2+a+6];
216 X[a+7]=seq_in[ind2+a+7];*/
219 for(int a=0;a<h2;a+=1){
221 /* fX[a+1]=Sbox1[X[a+1]];
222 fX[a+2]=Sbox1[X[a+2]];
223 fX[a+3]=Sbox1[X[a+3]];
224 /* fX[a+4]=Sbox1[X[a+4]];
225 fX[a+5]=Sbox1[X[a+5]];
226 fX[a+6]=Sbox1[X[a+6]];
227 fX[a+7]=Sbox1[X[a+7]];*/
231 for(int a=0;a<h2;a+=1) {
233 /* fX[a+1]=fX[a+1]^RM1[a+1];
234 fX[a+2]=fX[a+2]^RM1[a+2];
235 fX[a+3]=fX[a+3]^RM1[a+3];
236 /* fX[a+4]=fX[a+4]^RM1[a+4];
237 fX[a+5]=fX[a+5]^RM1[a+5];
238 fX[a+6]=fX[a+6]^RM1[a+6];
239 fX[a+7]=fX[a+7]^RM1[a+7];*/
243 for(int a=0;a<h2;a+=1) {
244 seq_out[ind1+a]=Sbox2[fX[a]];
245 /* seq_out[ind1+a+1]=Sbox2[fX[a+1]];
246 seq_out[ind1+a+2]=Sbox2[fX[a+2]];
247 seq_out[ind1+a+3]=Sbox2[fX[a+3]];
248 /* seq_out[ind1+a+4]=Sbox2[fX[a+4]];
249 seq_out[ind1+a+5]=Sbox2[fX[a+5]];
250 seq_out[ind1+a+6]=Sbox2[fX[a+6]];
251 seq_out[ind1+a+7]=Sbox2[fX[a+7]];*/
254 for(int a=0;a<h2;a+=1) {
255 RM1[a]=RM1[PboxRM[a]];
256 /* RM1[a+1]=RM1[PboxRM[a+1]];
257 RM1[a+2]=RM1[PboxRM[a+2]];
258 RM1[a+3]=RM1[PboxRM[a+3]];
259 /* RM1[a+4]=RM1[PboxRM[a+4]];
260 RM1[a+5]=RM1[PboxRM[a+5]];
261 RM1[a+6]=RM1[PboxRM[a+6]];
262 RM1[a+7]=RM1[PboxRM[a+7]];*/
275 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
278 uchar *fX=new uchar[h2];
281 uchar *Inv_Sbox1=new uchar[256];
282 inverse_tables(Sbox1,256,Inv_Sbox1);
284 uchar *Inv_Sbox2=new uchar[256];
285 inverse_tables(Sbox2,256,Inv_Sbox2);
290 for(int it=0;it<len;it++) {
293 int ind2=Pbox[it]*h2;
298 for(int a=0;a<h2;a++) {
299 fX[a]=seq_in[ind1+a];
301 for(int a=0;a<h2;a++) {
302 fX[a]=Inv_Sbox2[fX[a]];
304 for(int a=0;a<h2;a++) {
308 for(int a=0;a<h2;a++) {
309 RM1[a]=RM1[PboxRM[a]];
312 for(int a=0;a<h2;a++) {
313 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
323 int main(int argc, char** argv) {
326 for(int i=1; i<argc; i++){
327 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
328 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
331 printf("nb times %d\n",nb_test);
332 printf("ctr %d\n",ctr);
336 nb_test=atoi(argv[1]);
337 if(nb_test<=0 || nb_test>10000) {
338 printf("nb tests is not correct\n");
342 printf("nb tests = %d\n\n",nb_test);
350 uchar Secretkey[key_size];
352 uchar counter[key_size];
354 for(int i=0;i<key_size;i++) {
355 Secretkey[i]=lrand48()&0xFF;
356 counter[i]=lrand48()&0xFF;
368 uchar *data_R, *data_G, *data_B;
369 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
370 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
376 int imsize=width*height*3;
377 uchar* seq= new uchar[imsize];
378 uchar* seq2= new uchar[imsize];
380 int oneD=width*height;
381 for(int i=0;i<oneD;i++) {
383 seq[oneD+i]=data_G[i];
384 seq[2*oneD+i]=data_B[i];
391 int total_len=imsize;
393 int len= total_len/h2;
397 uchar *mix=new uchar[256];
402 for (int i = 0; i < 256 ; i++) {
403 mix[i]=Secretkey[i]^counter[i];
408 for (int i = 0; i < 64 ; i++) {
417 rc4key(DK, Sbox1, 16);
420 rc4key(&DK[16], Sbox2, 16);
425 rc4key(&DK[32], sc, 16);
427 uchar outd[2*(h * h)];
428 prga(sc, 2*(h * h), outd);
433 for(int i=0;i<h2;i++){
444 for (int i = 48; i < 64; i++)
448 int *Pbox=new int[len];
449 int *PboxRM=new int[h2];
451 rc4keyperm(keyp, len, rp, Pbox, 16);
453 printf("len %d\n",len);
454 for(int i=0;i<len;i++) {
455 // printf("%d \n",Pbox[i]);
458 rc4keyperm(RM2, h2, rp, PboxRM, h2);
460 for(int i=0;i<h2;i++){
466 double t=TimeStart();
469 for(i=0;i<nb_test;i++)
471 // encrypt(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
472 encrypt_ctr(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
476 cout<<"Time encrypt "<<time<<endl;
479 for(int i=0;i<oneD;i++) {
481 data_G[i]=seq2[oneD+i];
482 data_B[i]=seq2[2*oneD+i];
484 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
489 for(i=0;i<nb_test;i++) {
490 // decrypt(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
491 encrypt_ctr(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
495 cout<<"Time decrypt "<<time<<endl;
498 for(int i=0;i<oneD;i++) {
500 data_G[i]=seq[oneD+i];
501 data_B[i]=seq[2*oneD+i];
503 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);