2 //g++ -O3 one_round_test.cpp pixmap_io.o -o one_round_test -std=c++11
16 /*#include <cryptopp/hex.h>
17 #include <cryptopp/sha.h>
18 #include <cryptopp/osrng.h>
19 #include <cryptopp/secblock.h>
24 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
25 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
29 //using namespace CryptoPP;
43 typedef unsigned char uchar;
48 struct timeval tstart;
49 gettimeofday(&tstart,0);
50 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
53 double TimeStop(double t)
57 gettimeofday(&tend,0);
58 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
67 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
69 for(int i=0;i<size_tab;i++) {
70 inv_perm_tabs[tab[i]] = i;
75 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
77 for(int i=0;i<size_tab;i++) {
78 inv_perm_tabs[tab[i]] = i;
85 void rc4key(uchar *key, uchar *sc, int size_DK) {
87 for(int i=0;i<256;i++) {
93 for(int i0=0; i0<256; i0++) {
94 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
103 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
109 for (int i=0;i<len;i++) {
112 for (int it = 0; it < rp; it++) {
114 for(int i0 = 0; i0<len; i0++) {
115 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
124 void prga(uchar *sc, int ldata, uchar *r) {
128 for (int it=0; it<ldata; it++) {
129 i0 = ((i0+1)&0xFE); //%255);
130 j0 = (j0 + sc[i0])&0xFF;
134 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
138 inline uchar circ(uchar x,int n) {return (x << n) | (x >> (8 - n));}
140 void diff(uchar *Y, uchar *X, int h) {
143 Y[0] = X[1]^X[2]^X[3];
144 Y[1] = X[0]^X[2]^X[3];
145 Y[2] = X[0]^X[1]^X[3];
146 Y[3] = X[0]^X[1]^X[2];
149 /*Y[0] = X[0]^X[2]^X[3]^X[5]^X[6]^X[7];
150 Y[1] = X[0]^X[1]^X[3]^X[4]^X[6]^X[7];
151 Y[2] = X[0]^X[1]^X[2]^X[4]^X[5]^X[7];
152 Y[3] = X[1]^X[2]^X[3]^X[4]^X[5]^X[6];
153 Y[4] = X[0]^X[1]^X[5]^X[6]^X[7];
154 Y[5] = X[1]^X[2]^X[4]^X[6]^X[7];
155 Y[6] = X[2]^X[3]^X[4]^X[5]^X[7];
156 Y[7] = X[0]^X[3]^X[4]^X[5]^X[6];
169 Y[0] = X0^X2^X3^X5^X6^X7;
170 Y[1] = X0^X1^X3^X4^X6^X7;
171 Y[2] = X0^X1^X2^X4^X5^X7;
172 Y[3] = X1^X2^X3^X4^X5^X6;
173 Y[4] = X0^X1^X5^X6^X7;
174 Y[5] = X1^X2^X4^X6^X7;
175 Y[6] = X2^X3^X4^X5^X7;
176 Y[7] = X0^X3^X4^X5^X6;
182 Y[0] = X[3] ^ X[4] ^ X[6] ^ X[8] ^ X[9] ^ X[13] ^ X[14];
183 Y[1] = X[2] ^ X[5] ^ X[7] ^ X[8] ^ X[9] ^ X[12] ^ X[15];
184 Y[2] = X[1] ^ X[4] ^ X[6] ^ X[10] ^ X[11] ^ X[12] ^ X[15];
185 Y[3] = X[0] ^ X[5] ^ X[7] ^ X[10] ^ X[11] ^ X[13] ^ X[14];
186 Y[4] = X[0] ^ X[2] ^ X[5] ^ X[8] ^ X[11] ^ X[14] ^ X[15];
187 Y[5] = X[1] ^ X[3] ^ X[4] ^ X[9] ^ X[10] ^ X[14] ^ X[15];
188 Y[6] = X[0] ^ X[2] ^ X[7] ^ X[9] ^ X[10] ^ X[12] ^ X[13];
189 Y[7] = X[1] ^ X[3] ^ X[6] ^ X[8] ^ X[11] ^ X[12] ^ X[13];
190 Y[8] = X[0] ^ X[1] ^ X[4] ^ X[7] ^ X[10] ^ X[13] ^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 ^ X3 ^ X6 ^ X7 ^ X8 ^ X10 ^ X13;
232 Y[14] = X0 ^ X3 ^ X4 ^ X5 ^ X9 ^ X11 ^ X14;
233 Y[15] = X1 ^ X2 ^ X4 ^ X5 ^ X8 ^ X10 ^ X15;
241 Y[0]=X[0]^X[1]^X[2]^X[3]^X[4]^X[7]^X[8]^X[10]^X[12]^X[15]^X[16]^X[17]^X[18]^X[20]^X[21]^X[24]^X[25]^X[28]^X[30];
242 Y[1]=X[0]^ X[1]^X[2]^X[3]^X[5]^X[6]^X[9]^X[11]^X[13]^X[14]^X[16]^X[17]^X[19]^X[20]^X[21]^ X[24]^X[25]^X[29]^X[31];
243 Y[2]=X[0]^X[1]^X[2]^X[3]^X[5]^X[6]^X[8]^X[10]^X[13]^X[14]^X[16]^X[18]^X[19]^X[22]^X[23]^X[26]^X[27]^X[28]^X[30];
244 Y[3]=X[0]^X[1]^X[2]^X[3]^X[4]^X[7]^X[9]^X[11]^X[12]^X[15]^X[17]^X[18]^X[19]^X[22]^X[23]^X[26]^X[27]^X[29]^X[31];
245 Y[4]=X[0]^X[3]^X[5]^X[6]^X[7]^X[10]^X[11]^ X[12]^X[13]^X[14]^ X[15]^X[16]^X[19]^X[21]^X[23]^ X[25]^X[27]^X[30]^X[31];
246 Y[5]=X[1]^X[2]^X[4]^X[6]^X[7]^X[10]^X[11]^X[12]^X[13]^X[14]^X[16 ]^X[17]^X[18]^X[20]^X[22]^X[24]^X[26]^X[30]^X[31];
247 Y[6]=X[1]^X[2]^X[4]^X[5]^X[7]^X[8]^X[9]^X[12]^X[13]^X[14]^X[15]^ X[17]^X[18]^X[21]^X[23]^X[25]^X[27]^X[28]^X[29];
248 Y[7]=X[0]^X[3]^X[4]^X[5]^X[6]^X[9 ]^X[9]^X[12]^X[13]^X[14]^X[15]^X[16]^X[19]^X[20]^X[22]^X[24]^X[26]^X[28]^X[29];
249 Y[8]=X[0]^X[2]^X[6]^X[7]^X[8]^X[10]^X[11]^X[14]^X[15]^X[16]^X[18]^X[21]^X[22]^X[25]^X[26];
250 Y[9]=X[1]^ X[3]^X[6]^X[7]^X[9]^X[10]^X[11]^X[14]^X[15]^X[17]^X[19]^X[20]^X[23]^X[24]^X[27];
251 Y[10]=X[0]^X[2]^X[4]^X[5]^X[8]^X[9]^X[10]^X[12]^X[13]^X[16]^X[18]^X[20]^X[23]^ X[24]^X[27];
252 Y[11]=X[1]^X[3]^X[4]^X[5]^X[8]^X[9]^X[11]^X[12]^X[13]^X[17]^X[19]^X[21]^X[22]^X[25]^X[26];
253 Y[12]=X[0]^X[3]^X[4]^X[5]^X[6]^X[7]^X[10]^X[11]^X[13]^X[14]^X[15]^X[16]^X[19]^X[21]^X[23]^X[25]^X[27]^X[30]^X[31];
254 Y[13]=X[1]^X[2]^X[4]^X[5]^X[6]^X[7]^X[10]^X[11]^X[12]^X[14]^X[15]^X[17]^ X[18]^X[20]^X[22]^X[24]^X[26]^X[30]^X[31];
255 Y[14]=X[1]^X[2]^X[4]^X[5]^X[6]^X[7]^X[8]^X[9]^X[12]^X[13]^X[15]^X[17]^X[18]^X[21]^X[23]^X[25]^X[27]^X[28]^X[29];
256 Y[15]=X[0]^X[3]^X[4]^X[5]^X[6]^X[7]^X[8]^X[9]^X[12]^X[13]^X[14]^X[16]^X[19]^X[20]^X[22]^ X[24]^X[26]^X[28]^X[29];
257 Y[16]=X[0]^X[1]^X[2]^X[4]^X[8 ]^X[8]^X[10]^X[13 ]^X[15]^X[16]^X[17]^X[18]^X[19]^X[20]^X[21]^X[24]^X[25]^X[28]^X[30];
258 Y[17]=X[0]^X[1]^X[3]^X[5]^X[6]^X[9]^X[11]^X[13]^X[14]^X[16]^X[17]^X[18]^X[19]^X[20]^X[21]^X[24]^X[25]^X[29]^X[31];
259 Y[18]=X[0]^X[2]^X[3]^X[5]^X[6]^X[8]^X[10]^X[13]^X[14]^X[16]^X[17]^X[18]^X[19]^X[22]^X[23]^X[26]^X[27]^X[28]^X[30];
260 Y[19]=X[1]^X[2]^X[3]^X[4]^X[7]^X[9]^X[11]^X[12]^X[15]^X[16]^X[17]^X[18]^X[19]^X[22]^X[23]^X[26]^X[28 ]^X[29]^X[31];
261 Y[20]=X[0]^X[1]^X[5]^X[7]^X[10 ]^X[10]^X[13]^X[15]^X[16]^X[17]^X[20]^X[21]^X[23]^X[29]^X[30];
262 Y[21]=X[0]^X[1]^X[4]^X[6]^X[8]^X[11]^X[12]^X[14]^X[16]^X[17]^X[20]^X[21]^X[22]^X[28]^X[31];
263 Y[22]=X[2]^X[3]^X[5]^X[7]^X[8]^X[11]^X[13]^X[15]^X[18]^X[19]^X[21]^X[22]^X[23]^X[28]^X[31];
264 Y[23]=X[2]^X[3]^X[4]^X[6]^X[9]^X[10]^X[12]^X[14]^ X[18]^X[19]^X[20]^X[22]^X[23]^X[29]^X[30];
265 Y[24]=X[0]^X[1]^X[5]^X[7]^X[9]^X[10]^X[13]^X[15]^X[16]^X[17]^X[24]^X[25]^X[27]^X[29]^X[30];
266 Y[25]=X[0]^X[1]^X[4]^X[6]^X[8]^X[11]^X[12]^X[14]^X[16]^X[17]^X[24]^X[25]^X[26]^X[28]^X[31];
267 Y[26]=X[2]^X[3]^X[5]^X[7]^X[8]^X[11]^X[13]^X[15]^X[18]^X[19]^X[25]^X[26]^X[27]^X[28]^ X[31];
268 Y[27]=X[2]^X[3]^X[4]^X[6]^X[9]^X[10]^X[12]^X[14]^X[18]^X[19]^X[24]^X[26]^X[27]^X[29]^X[30];
269 Y[28]=X[0]^X[2]^X[6]^X[7]^X[14]^X[15]^X[16]^X[18]^X[21]^X[22]^X[25]^X[26]^X[28]^X[30]^X[31];
270 Y[29]=X[2]^X[3]^X[6]^X[7]^X[14]^X[15]^X[17]^X[19]^X[20]^X[23]^X[24]^X[27]^X[29]^X[30]^X[31];
271 Y[30]=X[1]^X[2]^X[4]^X[5]^X[12]^X[13]^X[16]^X[18]^X[20]^X[23]^X[24]^X[27]^X[28]^X[29]^X[30];
272 Y[31]=X[2]^X[3]^X[4]^X[5]^X[12]^X[13]^X[17]^X[19]^X[21]^X[22]^X[25]^X[26]^X[28]^X[29]^X[31];
280 //the proposed hash function, which is based on DSD structure. Sensitivity is ensured by employing the binary diffusion
282 void hash_DSD_BIN(uchar* seq_in, uchar* RM1,int len, int *PboxRM, uchar *Sbox1, int h) {
285 // Goal: Calculate the hash value
286 // Output: RM (hash value)
288 // uchar *X=new uchar[h2];
289 // uchar *fX=new uchar[h2];
296 for(int it=0;it<len;it++) {
300 // Mix with dynamic RM
302 for(int a=0;a<h;a+=4) {
303 fX[a]=RM1[a]^seq_in[ind2+a];
304 fX[a+1]=RM1[a+1]^seq_in[ind2+a+1];
305 fX[a+2]=RM1[a+2]^seq_in[ind2+a+2];
306 fX[a+3]=RM1[a+3]^seq_in[ind2+a+3];
309 // First Diffusion Operation
313 // Substitution Operation
314 for(int a=0;a<h;a+=4) {
315 fX[a]=Sbox1[fX2[a]]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
316 fX[a+1]=Sbox1[fX2[a+1]];
317 fX[a+2]=Sbox1[fX2[a+2]];
318 fX[a+3]=Sbox1[fX2[a+3]];
321 // Second Diffusion Operation
323 /*for(int a=0;a<h;a+=4) {
325 fX2[a+1]=circ(X[a+1],5);
326 fX2[a+2]=circ(X[a+2],1);
327 fX2[a+3]=circ(X[a+3],6);
332 // update RM and mix it with hashed block
333 for(int a=0;a<h;a+=4) {
334 RM1[a]=fX2[a]^RM1[PboxRM[a]];
335 RM1[a+1]=fX2[a+1]^RM1[PboxRM[a+1]];
336 RM1[a+2]=fX2[a+2]^RM1[PboxRM[a+2]];
337 RM1[a+3]=fX2[a+3]^RM1[PboxRM[a+3]];
340 for(int a=0;a<h;a+=4) {
341 RM1[a]=RM1[PboxRM[a]];
342 RM1[a+1]=RM1[PboxRM[a+1]];
343 RM1[a+2]=RM1[PboxRM[a+2]];
344 RM1[a+3]=RM1[PboxRM[a+3]];
347 for(int a=0;a<h;a+=4) {
348 RM1[a]=fX2[a]^RM1[a];
349 RM1[a+1]=fX2[a+1]^RM1[a+1];
350 RM1[a+2]=fX2[a+2]^RM1[a+2];
351 RM1[a+3]=fX2[a+3]^RM1[a+3];
371 int main(int argc, char** argv) {
380 for(int i=1; i<argc; i++){
381 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
382 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
383 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
384 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
385 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
386 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
390 cout<<size_buf<<endl;
396 uchar Secretkey[key_size];
398 uchar counter[key_size];
400 for(int i=0;i<key_size;i++) {
401 Secretkey[i]=lrand48()&0xFF;
402 counter[i]=lrand48()&0xFF;
415 uchar *data_R, *data_G, *data_B;
420 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
421 imsize=width*height*3;
422 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
426 buffer=new uchar[imsize];
427 for(int i=0;i<imsize;i++) {
435 uchar* seq= new uchar[imsize];
436 uchar* seq2= new uchar[imsize];
441 for(int i=0;i<oneD;i++) {
443 seq[oneD+i]=data_G[i];
444 seq[2*oneD+i]=data_B[i];
449 for(int i=0;i<oneD;i++) {
467 int total_len=imsize;
469 int len= total_len/h;
473 uchar *mix=new uchar[256];
478 for (int i = 0; i < 256 ; i++) {
479 mix[i]=Secretkey[i]^counter[i];
483 // cout<<"hash "<<endl;
484 for (int i = 0; i < 64 ; i++) {
492 int *PboxRM=new int[h];
502 double t=TimeStart();
503 rc4key(DK, Sbox1, 8);
506 rc4key(&DK[8], sc, 8);
513 rc4keyperm(&DK[16], h, rp, PboxRM, 8);
516 cout<<"Time initializaton "<<time<<endl;
528 for(int i=0;i<h;i++){
532 cout<<"imsize "<<imsize<<endl;
534 /* for(int i=0;i<imsize;i++){
535 cout<<(int)seq[i]<<" ";
542 for(int i=0;i<nb_test;i++)
544 hash_DSD_BIN(seq, RM1,len,PboxRM,Sbox1,h);
551 cout<<"Hash Time "<<time<<endl;
552 cout<<(double)imsize*nb_test/time<<"\t";
554 for(int i=0;i<h;i++){
555 cout<<(int)RM1[i]<<" ";