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, uchar *X, int ldata, uchar *r, int h) {
128 for (int it=0; it<ldata; it++) {
129 i0 = X[(i0+1)&(h-1)];
134 r[it]=sc[i0];//sc[(sc[i0]+sc[j0])&255];
138 inline uchar circ(uchar x,int n) {return (x << n) | (x >> (8 - n));}
141 uint64_t xorshift64( const uint64_t state)
151 static inline uint64_t splitmix64(uint64_t index) {
152 uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
153 z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
154 z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
155 return z ^ (z >> 31);
164 //the proposed hash function, which is based on DSD structure. Sensitivity is ensured by employing the binary diffusion
166 void hash_DSD_BIN(uchar* seq_in, uchar* RM1,int len, uchar *S, int h) {
169 // Goal: Calculate the hash value
170 // Output: RM (hash value)
175 uint64_t *rm=(uint64_t*)RM1;
176 // uint64_t *xx=(uint64_t*)X;
177 uint64_t *ss=(uint64_t*)seq_in;
181 for(int it=0;it<len;it++) {
184 // Mix with dynamic RM
188 for(int a=0;a<(h>>3);a++) {
189 rm[a]=rm[a]^ss[ind1+a];
196 rm[0]=xorshift64(sum);
197 for(int a=1;a<(h>>3);a++) {
198 rm[a]=xorshift64(rm[a-1]);
199 //rm[a]=splitmix64(rm[a-1]);
217 int main(int argc, char** argv) {
226 for(int i=1; i<argc; i++){
227 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
228 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
229 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
230 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
231 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
235 cout<<size_buf<<endl;
236 int seed=12;//time(NULL);
240 uchar Secretkey[key_size];
242 uchar counter[key_size];
244 for(int i=0;i<key_size;i++) {
245 Secretkey[i]=lrand48()&0xFF;
246 counter[i]=lrand48()&0xFF;
258 uchar *data_R, *data_G, *data_B;
263 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
264 imsize=width*height*3;
265 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
269 buffer=new uchar[imsize];
270 for(int i=0;i<imsize;i++) {
278 uchar* seq= new uchar[imsize];
279 uchar* seq2= new uchar[imsize];
284 for(int i=0;i<oneD;i++) {
286 seq[oneD+i]=data_G[i];
287 seq[2*oneD+i]=data_B[i];
292 for(int i=0;i<oneD;i++) {
297 printf("seq 4 %d\n",seq[4]);
309 printf("seq 4 %d\n",seq[4]);
314 int total_len=imsize;
316 int len= total_len/h;
320 uchar *mix=new uchar[256];
325 for (int i = 0; i < 256 ; i++) {
326 mix[i]=Secretkey[i]^counter[i];
330 // cout<<"hash "<<endl;
331 for (int i = 0; i < 64 ; i++) {
334 //cout<<(int)DK[i]<<" ";
349 double t=TimeStart();
350 rc4key(DK, Sbox1, 8);
352 rc4key(&DK[16], sc, 8);
357 prga(sc, sc,h,RM1,h);
364 cout<<"Time initializaton "<<time<<endl;
368 cout<<"imsize "<<imsize<<endl;
370 /* for(int i=0;i<imsize;i++){
371 cout<<(int)seq[i]<<" ";
380 for(int i=0;i<nb_test;i++)
382 hash_DSD_BIN(seq, RM1,len,Sbox1,h);
389 cout<<"Hash Time "<<time<<endl;
390 cout<<(double)imsize*nb_test/time<<"\t";
392 for(int i=0;i<h;i++){
393 cout<<(int)RM1[i]<<" ";
397 // cout<<splitmix64(2490)<<endl;
398 // cout<<splitmix64(2489)<<endl;