+//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, int ldata, uchar *r) {
+ uchar i0=0;
+ uchar j0=0;
+
+ for (int it=0; it<ldata; it++) {
+ i0 = ((i0+1)&0xFE); //%255);
+ j0 = (j0 + sc[i0])&0xFF;
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ r[it]=sc[(sc[i0]+sc[j0])&0xFF];
+ }
+}
+
+
+
+
+//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, int *PboxRM, uchar *Sbox1, uchar *Sbox2, 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 ind2;
+
+ uchar Y[h];
+ uchar Z[h];
+
+
+ for(int it=0;it<len;it++) {
+
+ ind2=it*h;
+
+ // Mix with dynamic RM
+
+ for(int a=0;a<h;a+=4) {
+ X[a]=RM1[a]^seq_in[ind2+a];
+ X[a+1]=RM1[a+1]^seq_in[ind2+a+1];
+ X[a+2]=RM1[a+2]^seq_in[ind2+a+2];
+ X[a+3]=RM1[a+3]^seq_in[ind2+a+3];
+ }
+
+/* Y[0]=Sbox1[X[0]^X[h-1]];
+ for(int a=1;a<h;a++) {
+ Y[a]=Sbox1[Y[a-1]^X[a-1]];
+ }
+
+ Z[h-1]=Sbox2[Y[h-1]^Y[0]];
+ for(int a=h-1;a>0;a--) {
+ Z[a-1]=Sbox2[Z[a]^Y[a]];
+ }
+*/
+
+ Y[0]=X[0]^X[h-1];
+ for(int a=1;a<h;a++) {
+ Y[a]=Y[a-1]^X[a-1];
+ }
+
+ for(int a=0;a<h;a+=4) {
+ Y[a]=Sbox1[Y[a]];
+ }
+
+ Z[h-1]=Y[h-1]^Y[0];
+ for(int a=h-1;a>0;a--) {
+ Z[a-1]=Z[a]^Y[a];
+ }
+
+ for(int a=0;a<h;a+=4) {
+ Z[a]=Sbox2[Z[a]];
+ }
+
+
+
+ for(int a=0;a<h;a+=4) {
+ /* RM1[a]=Z[a];
+ RM1[a+1]=Z[a+1];
+ RM1[a+2]=Z[a+2];
+ RM1[a+3]=Z[a+3];
+ */
+
+ RM1[a]=Z[a]^RM1[PboxRM[a]];
+ RM1[a+1]=Z[a+1]^RM1[PboxRM[a+1]];
+ RM1[a+2]=Z[a+2]^RM1[PboxRM[a+2]];
+ RM1[a+3]=Z[a+3]^RM1[PboxRM[a+3]];
+
+ }
+
+ }
+
+
+}
+
+
+
+
+
+
+
+
+
+
+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],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
+ 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=time(NULL);
+// cout<<seed<<endl;
+ srand48(12);
+
+ 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];
+ }
+ }
+
+
+ if(change==1) {
+
+ seq[4]++;
+ }
+ if(change==2) {
+
+ seq[9]++;
+ }
+
+
+
+
+
+ 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];
+ }
+
+
+
+
+ int *PboxRM=new int[h];
+ uchar Sbox1[256];
+ uchar Sbox2[256];
+ uchar sc[256];
+ uchar RM1[h];
+ uchar RM2[h];
+
+
+
+
+ double time=0;
+ double t=TimeStart();
+ rc4key(DK, Sbox1, 8);
+ rc4key(&DK[8], Sbox2, 8);
+
+ rc4key(&DK[16], sc, 8);
+
+
+ prga(sc, h, RM1);
+
+
+
+ rc4keyperm(&DK[24], h, rp, PboxRM, 8);
+
+ time+=TimeStop(t);
+ cout<<"Time initializaton "<<time<<endl;
+
+
+
+
+
+
+
+
+
+
+
+ for(int i=0;i<h;i++){
+ RM2[i]=RM1[i];
+ }
+
+ 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,PboxRM,Sbox1,Sbox2,h);
+ }
+
+
+
+
+ time+=TimeStop(t);
+ cout<<"Hash Time "<<time<<endl;
+
+
+ for(int i=0;i<h;i++){
+ cout<<(int)RM1[i]<<" ";
+ }
+ cout<<endl;
+
+
+
+ return 0;
+}