2 //g++ -O3 one_round_test.cpp pixmap_io.o -o one_round_test -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++) {
126 i0 = ((i0+1)&0xFE); //%255);
127 j0 = (j0 + sc[i0])&0xFF;
131 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
136 void diff(uchar *Y, uchar *X, int h) {
139 Y[0] = X[1]^X[2]^X[3];
140 Y[1] = X[0]^X[2]^X[3];
141 Y[2] = X[0]^X[1]^X[3];
142 Y[3] = X[0]^X[1]^X[2];
145 Y[0] = X[0]^X[2]^X[3]^X[5]^X[6]^X[7];
146 Y[1] = X[0]^X[1]^X[3]^X[4]^X[6]^X[7];
147 Y[2] = X[0]^X[1]^X[2]^X[4]^X[5]^X[7];
148 Y[3] = X[1]^X[2]^X[3]^X[4]^X[5]^X[6];
149 Y[4] = X[0]^X[1]^X[5]^X[6]^X[7];
150 Y[5] = X[1]^X[2]^X[4]^X[6]^X[7];
151 Y[6] = X[2]^X[3]^X[4]^X[5]^X[7];
152 Y[7] = X[0]^X[3]^X[4]^X[5]^X[6];
156 Y[0] = X[3] ^ X[4] ^ X[6] ^ X[8] ^ X[9] ^ X[13] ^ X[14];
157 Y[1] = X[2] ^ X[5] ^ X[7] ^ X[8] ^ X[9] ^ X[12] ^ X[15];
158 Y[2] = X[1] ^ X[4] ^ X[6] ^ X[10] ^ X[11] ^ X[12] ^ X[15];
159 Y[3] = X[0] ^ X[5] ^ X[7] ^ X[10] ^ X[11] ^ X[13] ^ X[14];
160 Y[4] = X[0] ^ X[2] ^ X[5] ^ X[8] ^ X[11] ^ X[14] ^ X[15];
161 Y[5] = X[1] ^ X[3] ^ X[4] ^ X[9] ^ X[10] ^ X[14] ^ X[15];
162 Y[6] = X[0] ^ X[2] ^ X[7] ^ X[9] ^ X[10] ^ X[12] ^ X[13];
163 Y[7] = X[1] ^ X[3] ^ X[6] ^ X[8] ^ X[11] ^ X[12] ^ X[13];
164 Y[8] = X[0] ^ X[1] ^ X[4] ^ X[7] ^ X[10] ^ X[13] ^ X[15];
165 Y[9] = X[0] ^ X[1] ^ X[5] ^ X[6] ^ X[11] ^ X[12] ^ X[14];
166 Y[10] = X[2] ^ X[3] ^ X[5] ^ X[6] ^ X[8] ^ X[13] ^ X[15];
167 Y[11] = X[2] ^ X[3] ^ X[4] ^ X[7] ^ X[9] ^ X[12] ^ X[14];
168 Y[12] = X[1] ^ X[2] ^ X[6] ^ X[7] ^ X[9] ^ X[11] ^ X[12];
169 Y[13] = X[0] ^ X[3] ^ X[6] ^ X[7] ^ X[8] ^ X[10] ^ X[13];
170 Y[14] = X[0] ^ X[3] ^ X[4] ^ X[5] ^ X[9] ^ X[11] ^ X[14];
171 Y[15] = X[1] ^ X[2] ^ X[4] ^ X[5] ^ X[8] ^ X[10] ^ X[15];
176 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];
177 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];
178 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];
179 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];
180 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];
181 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];
182 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];
183 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];
184 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];
185 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];
186 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];
187 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];
188 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];
189 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];
190 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];
191 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];
192 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];
193 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];
194 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];
195 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];
196 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];
197 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];
198 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];
199 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];
200 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];
201 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];
202 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];
203 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];
204 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];
205 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];
206 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];
207 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];
215 //the proposed hash function, which is based on DSD structure. Sensitivity is ensured by employing the binary diffusion
217 void hash_DSD_BIN(uchar* seq_in, uchar* RM1,int len, int *PboxRM, uchar *Sbox1, int h) {
220 // Goal: Calculate the hash value
221 // Output: RM (hash value)
223 // uchar *X=new uchar[h2];
224 // uchar *fX=new uchar[h2];
231 for(int it=0;it<len;it++) {
235 // Mix with dynamic RM
237 for(int a=0;a<h;a+=4) {
238 fX[a]=RM1[a]^seq_in[ind2+a];
239 fX[a+1]=RM1[a+1]^seq_in[ind2+a+1];
240 fX[a+2]=RM1[a+2]^seq_in[ind2+a+2];
241 fX[a+3]=RM1[a+3]^seq_in[ind2+a+3];
244 // First Diffusion Operation
248 // Substitution Operation
249 for(int a=0;a<h;a+=4) {
250 fX[a]=Sbox1[fX2[a]]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
251 fX[a+1]=Sbox1[fX2[a+1]];
252 fX[a+2]=Sbox1[fX2[a+2]];
253 fX[a+3]=Sbox1[fX2[a+3]];
256 // Second Diffusion Operation
260 // update RM and mix it with hashed block
261 for(int a=0;a<h;a+=4) {
262 RM1[a]=fX2[a]^RM1[PboxRM[a]];
263 RM1[a+1]=fX2[a+1]^RM1[PboxRM[a+1]];
264 RM1[a+2]=fX2[a+2]^RM1[PboxRM[a+2]];
265 RM1[a+3]=fX2[a+3]^RM1[PboxRM[a+3]];
282 int main(int argc, char** argv) {
291 for(int i=1; i<argc; i++){
292 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
293 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
294 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
295 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
296 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
297 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
301 cout<<size_buf<<endl;
307 uchar Secretkey[key_size];
309 uchar counter[key_size];
311 for(int i=0;i<key_size;i++) {
312 Secretkey[i]=lrand48()&0xFF;
313 counter[i]=lrand48()&0xFF;
326 uchar *data_R, *data_G, *data_B;
331 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
332 imsize=width*height*3;
333 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
337 buffer=new uchar[imsize];
338 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++) {
372 int total_len=imsize;
374 int len= total_len/h;
378 uchar *mix=new uchar[256];
383 for (int i = 0; i < 256 ; i++) {
384 mix[i]=Secretkey[i]^counter[i];
388 // cout<<"hash "<<endl;
389 for (int i = 0; i < 64 ; i++) {
397 int *PboxRM=new int[h];
407 double t=TimeStart();
408 rc4key(DK, Sbox1, 8);
411 rc4key(&DK[8], sc, 8);
418 rc4keyperm(&DK[16], h, rp, PboxRM, 8);
421 cout<<"Time initializaton "<<time<<endl;
433 for(int i=0;i<h;i++){
438 for(int i=0;i<imsize;i++){
439 cout<<(int)seq[i]<<" ";
447 hash_DSD_BIN(seq, RM1,len,PboxRM,Sbox1,h);
454 cout<<"Hash Time "<<time<<endl;
457 for(int i=0;i<h;i++){
458 cout<<(int)RM1[i]<<" ";