+//gcc pixmap_io.c -c
+//g++ -O3 one_round_hash_new.cpp pixmap_io.o -o one_round_hash_new -std=c++11
+
+//
+
+
+#include <iostream>
+#include <list>
+#include<math.h>
+#include<stdlib.h>
+#include<stdio.h>
+#include<string.h>
+#include <fstream>
+#include <sys/time.h>
+
+/*#include <cryptopp/hex.h>
+#include <cryptopp/sha.h>
+#include <cryptopp/osrng.h>
+#include <cryptopp/secblock.h>
+*/
+
+
+extern "C" {
+ int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
+ void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
+}
+
+
+//using namespace CryptoPP;
+using namespace std;
+
+
+int key_size=256;
+int nb_test=1;
+int ctr=0;
+
+
+
+
+
+
+
+typedef unsigned char uchar;
+
+
+double TimeStart()
+{
+ struct timeval tstart;
+ gettimeofday(&tstart,0);
+ return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
+}
+
+double TimeStop(double t)
+{
+ struct timeval tend;
+
+ gettimeofday(&tend,0);
+ t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
+ return (t);
+}
+
+
+
+
+
+
+void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
+
+ for(int i=0;i<size_tab;i++) {
+ inv_perm_tabs[tab[i]] = i;
+ }
+
+}
+
+void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
+
+ for(int i=0;i<size_tab;i++) {
+ inv_perm_tabs[tab[i]] = i;
+ }
+
+}
+
+
+
+void rc4key(uchar *key, uchar *sc, int size_DK) {
+
+ for(int i=0;i<256;i++) {
+ sc[i]=i;
+ }
+
+
+ uchar j0 = 0;
+ for(int i0=0; i0<256; i0++) {
+ j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0 ];
+ sc[j0] = tmp;
+ }
+}
+
+
+
+void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
+
+ //sc=1:len;
+
+
+
+ for (int i=0;i<len;i++) {
+ sc[i]=i;
+ }
+ for (int it = 0; it < rp; it++) {
+ int j0 = 1;
+ for(int i0 = 0; i0<len; i0++) {
+ j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
+ int tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ }
+
+ }
+}
+
+void prga(uchar *sc, uchar *X, int ldata, uchar *r, int h) {
+ uchar i0=0;
+ uchar j0=0;
+
+ for (int it=0; it<ldata; it++) {
+ i0 = X[(i0+1)&(h-1)];
+ j0 = (j0 + sc[i0]);
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ r[it]=sc[i0];//sc[(sc[i0]+sc[j0])&255];
+ }
+}
+
+inline uchar circ(uchar x,int n) {return (x << n) | (x >> (8 - n));}
+
+
+uint64_t xorshift64( const uint64_t state)
+{
+ uint64_t x = state;
+ x^= x << 13;
+ x^= x >> 7;
+ x^= x << 17;
+ return x;
+}
+
+
+static inline uint64_t splitmix64(uint64_t index) {
+ uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
+ z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
+ z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
+ return z ^ (z >> 31);
+}
+
+
+
+
+
+
+
+//the proposed hash function, which is based on DSD structure. Sensitivity is ensured by employing the binary diffusion
+
+void hash_DSD_BIN(uchar* seq_in, uchar* RM1,int len, uchar *S, int h) {
+
+
+ // Goal: Calculate the hash value
+ // Output: RM (hash value)
+
+// uchar *X=new uchar[h2];
+// uchar *fX=new uchar[h2];
+ uchar X[h];
+ int ind1,ind2;
+
+
+ uint64_t *rm=(uint64_t*)RM1;
+ uint64_t *xx=(uint64_t*)X;
+ uint64_t *ss=(uint64_t*)seq_in;
+
+
+
+ for(int it=0;it<len;it++) {
+ //ind1=Pbox[it]*h;
+ //ind2=Pbox[(it+len/2)]*h;
+
+ ind1=it*h/8;
+ // Mix with dynamic RM
+ uint64_t sum=0;
+ /* for(int a=0;a<h;a+=4) {
+ X[a]=RM1[a]^seq_in[ind1+a];
+ X[a+1]=RM1[a+1]^seq_in[ind1+a+1];
+ X[a+2]=RM1[a+2]^seq_in[ind1+a+2];
+ X[a+3]=RM1[a+3]^seq_in[ind1+a+3];
+ }
+ */
+
+
+ for(int a=0;a<h/8;a++) {
+ xx[a]=rm[a]^ss[ind1+a];
+ sum+=xx[a];
+ }
+
+
+
+
+ rm[0]=xorshift64(sum);
+ for(int a=1;a<h/8;a++) {
+ rm[a]^=xorshift64(rm[a-1]);
+ }
+
+
+ }
+
+
+}
+
+
+
+
+
+
+
+
+
+
+int main(int argc, char** argv) {
+
+
+ int h=16;
+ int lena=0;
+ int size_buf=1;
+ int change=0;
+
+
+ for(int i=1; i<argc; i++){
+ if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
+ if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
+ if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
+ if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
+ if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
+ }
+
+
+ cout<<size_buf<<endl;
+ int seed=12;//time(NULL);
+ cout<<seed<<endl;
+ srand48(seed);
+
+ uchar Secretkey[key_size];
+
+ uchar counter[key_size];
+
+ for(int i=0;i<key_size;i++) {
+ Secretkey[i]=lrand48()&0xFF;
+ counter[i]=lrand48()&0xFF;
+ }
+
+ int size = 64;
+ uchar DK[size];
+
+
+
+
+ int width;
+ int height;
+
+ uchar *data_R, *data_G, *data_B;
+ int imsize;
+ uchar *buffer;
+
+ if(lena==1) {
+ load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
+ imsize=width*height*3;
+// load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
+ }
+ else {
+ imsize=size_buf;
+ buffer=new uchar[imsize];
+ for(int i=0;i<imsize;i++) {
+ buffer[i]=lrand48();
+ }
+ }
+
+
+
+
+ uchar* seq= new uchar[imsize];
+ uchar* seq2= new uchar[imsize];
+
+ int oneD;
+ if(lena) {
+ oneD=width*height;
+ for(int i=0;i<oneD;i++) {
+ seq[i]=data_R[i];
+ seq[oneD+i]=data_G[i];
+ seq[2*oneD+i]=data_B[i];
+ }
+ }
+ else {
+ oneD=imsize;
+ for(int i=0;i<oneD;i++) {
+ seq[i]=buffer[i];
+ }
+ }
+
+ printf("seq 4 %d\n",seq[4]);
+ if(change==1) {
+
+ seq[4]++;
+ }
+ if(change==2) {
+
+ seq[9]++;
+ }
+
+ printf("seq 4 %d\n",seq[4]);
+
+
+
+
+ int total_len=imsize;
+ int rp=1;
+ int len= total_len/h;
+ cout<<len<<endl;
+
+
+ uchar *mix=new uchar[256];
+
+
+
+
+ for (int i = 0; i < 256 ; i++) {
+ mix[i]=Secretkey[i]^counter[i];
+ }
+
+
+// cout<<"hash "<<endl;
+ for (int i = 0; i < 64 ; i++) {
+// DK[i]=digest[i];
+ DK[i]=mix[i];
+ //cout<<(int)DK[i]<<" ";
+ }
+ //cout<<endl;
+
+
+
+
+
+ uchar Sbox1[256];
+ uchar sc[256];
+ uchar RM1[h];
+
+
+
+ double time=0;
+ double t=TimeStart();
+ rc4key(DK, Sbox1, 8);
+
+ rc4key(&DK[16], sc, 8);
+
+
+
+
+ prga(sc, sc,h,RM1,h);
+
+
+
+
+
+ time+=TimeStop(t);
+ cout<<"Time initializaton "<<time<<endl;
+
+
+
+ cout<<"imsize "<<imsize<<endl;
+
+/* for(int i=0;i<imsize;i++){
+ cout<<(int)seq[i]<<" ";
+ }
+ cout<<endl;
+*/
+
+
+
+ time=0;
+ t=TimeStart();
+ for(int i=0;i<nb_test;i++)
+ {
+ hash_DSD_BIN(seq, RM1,len,Sbox1,h);
+ }
+
+
+
+
+ time+=TimeStop(t);
+ cout<<"Hash Time "<<time<<endl;
+ cout<<(double)imsize*nb_test/time<<"\t";
+
+ for(int i=0;i<h;i++){
+ cout<<(int)RM1[i]<<" ";
+ }
+ cout<<endl;
+
+ // cout<<splitmix64(2490)<<endl;
+ // cout<<splitmix64(2489)<<endl;
+
+
+ return 0;
+}