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];
139 void diff(uchar *Y, uchar *X, int h) {
142 Y[0] = X[1]^X[2]^X[3];
143 Y[1] = X[0]^X[2]^X[3];
144 Y[2] = X[0]^X[1]^X[3];
145 Y[3] = X[0]^X[1]^X[2];
148 Y[0] = X[0]^X[2]^X[3]^X[5]^X[6]^X[7];
149 Y[1] = X[0]^X[1]^X[3]^X[4]^X[6]^X[7];
150 Y[2] = X[0]^X[1]^X[2]^X[4]^X[5]^X[7];
151 Y[3] = X[1]^X[2]^X[3]^X[4]^X[5]^X[6];
152 Y[4] = X[0]^X[1]^X[5]^X[6]^X[7];
153 Y[5] = X[1]^X[2]^X[4]^X[6]^X[7];
154 Y[6] = X[2]^X[3]^X[4]^X[5]^X[7];
155 Y[7] = X[0]^X[3]^X[4]^X[5]^X[6];
159 Y[0] = X[3] ^ X[4] ^ X[6] ^ X[8] ^ X[9] ^ X[13] ^ X[14];
160 Y[1] = X[2] ^ X[5] ^ X[7] ^ X[8] ^ X[9] ^ X[12] ^ X[15];
161 Y[2] = X[1] ^ X[4] ^ X[6] ^ X[10] ^ X[11] ^ X[12] ^ X[15];
162 Y[3] = X[0] ^ X[5] ^ X[7] ^ X[10] ^ X[11] ^ X[13] ^ X[14];
163 Y[4] = X[0] ^ X[2] ^ X[5] ^ X[8] ^ X[11] ^ X[14] ^ X[15];
164 Y[5] = X[1] ^ X[3] ^ X[4] ^ X[9] ^ X[10] ^ X[14] ^ X[15];
165 Y[6] = X[0] ^ X[2] ^ X[7] ^ X[9] ^ X[10] ^ X[12] ^ X[13];
166 Y[7] = X[1] ^ X[3] ^ X[6] ^ X[8] ^ X[11] ^ X[12] ^ X[13];
167 Y[8] = X[0] ^ X[1] ^ X[4] ^ X[7] ^ X[10] ^ X[13] ^ X[15];
168 Y[9] = X[0] ^ X[1] ^ X[5] ^ X[6] ^ X[11] ^ X[12] ^ X[14];
169 Y[10] = X[2] ^ X[3] ^ X[5] ^ X[6] ^ X[8] ^ X[13] ^ X[15];
170 Y[11] = X[2] ^ X[3] ^ X[4] ^ X[7] ^ X[9] ^ X[12] ^ X[14];
171 Y[12] = X[1] ^ X[2] ^ X[6] ^ X[7] ^ X[9] ^ X[11] ^ X[12];
172 Y[13] = X[0] ^ X[3] ^ X[6] ^ X[7] ^ X[8] ^ X[10] ^ X[13];
173 Y[14] = X[0] ^ X[3] ^ X[4] ^ X[5] ^ X[9] ^ X[11] ^ X[14];
174 Y[15] = X[1] ^ X[2] ^ X[4] ^ X[5] ^ X[8] ^ X[10] ^ X[15];
179 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];
180 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];
181 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];
182 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];
183 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];
184 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];
185 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];
186 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];
187 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];
188 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];
189 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];
190 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];
191 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];
192 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];
193 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];
194 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];
195 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];
196 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];
197 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];
198 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];
199 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];
200 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];
201 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];
202 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];
203 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];
204 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];
205 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];
206 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];
207 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];
208 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];
209 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];
210 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];
218 //the proposed hash function, which is based on DSD structure. Sensitivity is ensured by employing the binary diffusion
220 void hash_DSD_BIN(uchar* seq_in, uchar* RM1,int len, int *PboxRM, uchar *Sbox1, int h) {
223 // Goal: Calculate the hash value
224 // Output: RM (hash value)
226 // uchar *X=new uchar[h2];
227 // uchar *fX=new uchar[h2];
234 for(int it=0;it<len;it++) {
238 // Mix with dynamic RM
240 for(int a=0;a<h;a+=4) {
241 fX[a]=RM1[a]^seq_in[ind2+a];
242 fX[a+1]=RM1[a+1]^seq_in[ind2+a+1];
243 fX[a+2]=RM1[a+2]^seq_in[ind2+a+2];
244 fX[a+3]=RM1[a+3]^seq_in[ind2+a+3];
247 // First Diffusion Operation
251 // Substitution Operation
252 for(int a=0;a<h;a+=4) {
253 fX[a]=Sbox1[fX2[a]]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
254 fX[a+1]=Sbox1[fX2[a+1]];
255 fX[a+2]=Sbox1[fX2[a+2]];
256 fX[a+3]=Sbox1[fX2[a+3]];
259 // Second Diffusion Operation
263 // update RM and mix it with hashed block
264 for(int a=0;a<h;a+=4) {
265 RM1[a]=fX2[a]^RM1[PboxRM[a]];
266 RM1[a+1]=fX2[a+1]^RM1[PboxRM[a+1]];
267 RM1[a+2]=fX2[a+2]^RM1[PboxRM[a+2]];
268 RM1[a+3]=fX2[a+3]^RM1[PboxRM[a+3]];
285 int main(int argc, char** argv) {
294 for(int i=1; i<argc; i++){
295 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
296 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
297 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
298 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
299 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
300 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
304 cout<<size_buf<<endl;
310 uchar Secretkey[key_size];
312 uchar counter[key_size];
314 for(int i=0;i<key_size;i++) {
315 Secretkey[i]=lrand48()&0xFF;
316 counter[i]=lrand48()&0xFF;
329 uchar *data_R, *data_G, *data_B;
334 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
335 imsize=width*height*3;
336 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
340 buffer=new uchar[imsize];
341 for(int i=0;i<imsize;i++) {
349 uchar* seq= new uchar[imsize];
350 uchar* seq2= new uchar[imsize];
355 for(int i=0;i<oneD;i++) {
357 seq[oneD+i]=data_G[i];
358 seq[2*oneD+i]=data_B[i];
363 for(int i=0;i<oneD;i++) {
378 int total_len=imsize;
380 int len= total_len/h;
384 uchar *mix=new uchar[256];
389 for (int i = 0; i < 256 ; i++) {
390 mix[i]=Secretkey[i]^counter[i];
394 // cout<<"hash "<<endl;
395 for (int i = 0; i < 64 ; i++) {
403 int *PboxRM=new int[h];
413 double t=TimeStart();
414 rc4key(DK, Sbox1, 8);
417 rc4key(&DK[8], sc, 8);
424 rc4keyperm(&DK[16], h, rp, PboxRM, 8);
427 cout<<"Time initializaton "<<time<<endl;
439 for(int i=0;i<h;i++){
443 cout<<"imsize "<<imsize<<endl;
445 /* for(int i=0;i<imsize;i++){
446 cout<<(int)seq[i]<<" ";
453 for(int i=0;i<nb_test;i++)
455 hash_DSD_BIN(seq, RM1,len,PboxRM,Sbox1,h);
462 cout<<"Hash Time "<<time<<endl;
465 for(int i=0;i<h;i++){
466 cout<<(int)RM1[i]<<" ";