2 //g++ -O3 one_round_hash_new.cpp pixmap_io.o -o one_round_hash_new -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));}
141 //the proposed hash function, which is based on DSD structure. Sensitivity is ensured by employing the binary diffusion
143 void hash_DSD_BIN(uchar* seq_in, uchar* RM1,int len, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int h) {
146 // Goal: Calculate the hash value
147 // Output: RM (hash value)
149 // uchar *X=new uchar[h2];
150 // uchar *fX=new uchar[h2];
158 for(int it=0;it<len;it++) {
162 // Mix with dynamic RM
164 for(int a=0;a<h;a+=4) {
165 X[a]=RM1[a]^seq_in[ind2+a];
166 X[a+1]=RM1[a+1]^seq_in[ind2+a+1];
167 X[a+2]=RM1[a+2]^seq_in[ind2+a+2];
168 X[a+3]=RM1[a+3]^seq_in[ind2+a+3];
177 for(int a=4;a<h;a+=4) {
180 Y[a+2]=Y[a+1]^X[a+1];
181 Y[a+3]=Y[a+2]^X[a+2];
184 for(int a=0;a<h;a+=4) {
186 Y[a+1]=Sbox1[Y[a+1]];
187 Y[a+2]=Sbox1[Y[a+2]];
188 Y[a+3]=Sbox1[Y[a+3]];
192 RM1[h-1]=Y[h-1]^Y[0];
193 RM1[h-2]=RM1[h-1]^Y[h-1];
194 RM1[h-3]=RM1[h-2]^Y[h-2];
195 RM1[h-4]=RM1[h-3]^Y[h-3];
196 for(int a=h-4;a>0;a-=4) {
197 RM1[a-1]=RM1[a]^Y[a];
198 RM1[a-2]=RM1[a-1]^Y[a-1];
199 RM1[a-3]=RM1[a-2]^Y[a-2];
200 RM1[a-4]=RM1[a-3]^Y[a-3];
205 // Z[h-1]=Y[h-1]^Y[0];
206 // Z[h-2]=Z[h-1]^Y[h-1];
207 // for(int a=h-2;a>0;a-=2) {
209 // Z[a-2]=Z[a-1]^Y[a-1];
213 // Z[h-1]=Y[h-1]^Y[0];
214 // Z[h-2]=Z[h-1]^Y[h-1];
215 // Z[h-3]=Z[h-2]^Y[h-2];
216 // Z[h-4]=Z[h-3]^Y[h-3];
217 // for(int a=h-4;a>0;a-=4) {
219 // Z[a]=Z[a+1]^Y[a+1];
220 // Z[a+1]=Z[a+2]^Y[a+2];
221 // Z[a+2]=Z[a+3]^Y[a+3];
225 // for(int a=0;a<h;a+=4) {
248 int main(int argc, char** argv) {
257 for(int i=1; i<argc; i++){
258 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
259 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
260 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
261 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
262 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
263 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
267 cout<<size_buf<<endl;
273 uchar Secretkey[key_size];
275 uchar counter[key_size];
277 for(int i=0;i<key_size;i++) {
278 Secretkey[i]=lrand48()&0xFF;
279 counter[i]=lrand48()&0xFF;
292 uchar *data_R, *data_G, *data_B;
297 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
298 imsize=width*height*3;
299 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
303 buffer=new uchar[imsize];
304 for(int i=0;i<imsize;i++) {
312 uchar* seq= new uchar[imsize];
313 uchar* seq2= new uchar[imsize];
318 for(int i=0;i<oneD;i++) {
320 seq[oneD+i]=data_G[i];
321 seq[2*oneD+i]=data_B[i];
326 for(int i=0;i<oneD;i++) {
345 int total_len=imsize;
347 int len= total_len/h;
351 uchar *mix=new uchar[256];
356 for (int i = 0; i < 256 ; i++) {
357 mix[i]=Secretkey[i]^counter[i];
361 // cout<<"hash "<<endl;
362 for (int i = 0; i < 64 ; i++) {
370 int *PboxRM=new int[h];
381 double t=TimeStart();
382 rc4key(DK, Sbox1, 8);
383 rc4key(&DK[8], Sbox2, 8);
385 rc4key(&DK[16], sc, 8);
392 rc4keyperm(&DK[24], h, rp, PboxRM, 8);
395 cout<<"Time initializaton "<<time<<endl;
407 for(int i=0;i<h;i++){
411 cout<<"imsize "<<imsize<<endl;
413 /* for(int i=0;i<imsize;i++){
414 cout<<(int)seq[i]<<" ";
421 for(int i=0;i<nb_test;i++)
423 hash_DSD_BIN(seq, RM1,len,PboxRM,Sbox1,Sbox2,h);
430 cout<<"Hash Time "<<time<<endl;
431 cout<<(double)imsize*nb_test/time<<"\t";
433 for(int i=0;i<h;i++){
434 cout<<(int)RM1[i]<<" ";