From c1233f272a367c9770d6e352b5018887c14a8c9c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Couturier?= <raphael.couturier@univ-fcomte.fr> Date: Sun, 7 Jul 2019 17:41:55 +0200 Subject: [PATCH 1/1] new hash version --- OneRoundIoT/OneRound/Makefile | 5 + OneRoundIoT/OneRound/one_round_hash_new3.cpp | 398 +++++++++++++++++++ 2 files changed, 403 insertions(+) create mode 100644 OneRoundIoT/OneRound/one_round_hash_new3.cpp diff --git a/OneRoundIoT/OneRound/Makefile b/OneRoundIoT/OneRound/Makefile index 2b99c8a..a300f32 100644 --- a/OneRoundIoT/OneRound/Makefile +++ b/OneRoundIoT/OneRound/Makefile @@ -39,6 +39,8 @@ one_round_hash_new.o: one_round_hash_new.cpp one_round_hash_new2.o: one_round_hash_new2.cpp $(CXX) -c -o $@ $< $(CFLAGS) +one_round_hash_new3.o: one_round_hash_new3.cpp + $(CXX) -c -o $@ $< $(CFLAGS) one_round_par2.o: one_round_par2.cpp @@ -67,6 +69,9 @@ one_round_hash_new: pixmap_io.o one_round_hash_new.o one_round_hash_new2: pixmap_io.o one_round_hash_new2.o $(CXX) -o $@ $^ $(CFLAGS) +one_round_hash_new3: pixmap_io.o one_round_hash_new3.o + $(CXX) -o $@ $^ $(CFLAGS) + one_round_auth: pixmap_io.o one_round_auth.o $(CXX) -o $@ $^ $(CFLAGS) diff --git a/OneRoundIoT/OneRound/one_round_hash_new3.cpp b/OneRoundIoT/OneRound/one_round_hash_new3.cpp new file mode 100644 index 0000000..f7c622d --- /dev/null +++ b/OneRoundIoT/OneRound/one_round_hash_new3.cpp @@ -0,0 +1,398 @@ +//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]; + } +} + +inline uchar circ(uchar x,int n) {return (x << n) | (x >> (8 - n));} + + +//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 *Pbox,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 ind1,ind2; + + + + for(int it=0;it<len/2;it++) { + //ind1=Pbox[it]*h; + //ind2=Pbox[(it+len/2)]*h; + + ind1=it*h; + ind2=(it+len/2)*h; + + + // Mix with dynamic RM + + for(int a=0;a<h;a+=4) { + RM1[a]=Sbox1[RM1[a]^seq_in[ind1+a]]^seq_in[ind2+a]; + RM1[a+1]=Sbox1[RM1[a+1]^seq_in[ind1+a+1]]^seq_in[ind2+a+1]; + RM1[a+2]=Sbox1[RM1[a+2]^seq_in[ind1+a+2]]^seq_in[ind2+a+2]; + RM1[a+3]=Sbox1[RM1[a+3]^seq_in[ind1+a+3]]^seq_in[ind2+a+3]; + } + + + for(int a=0;a<h;a+=4) { + RM1[a]=Sbox2[RM1[a]]; + RM1[a+1]=Sbox2[RM1[a+1]]; + RM1[a+2]=Sbox2[RM1[a+2]]; + RM1[a+3]=Sbox2[RM1[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],"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(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]; + } + + + + + int *PboxRM=new int[h]; + uchar Sbox1[256]; + uchar Sbox2[256]; + uchar sc[256]; + uchar RM1[h]; + uchar RM2[h]; + int *Pbox=new int[len]; + + + + 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); + +rc4keyperm(&DK[32], len, rp, Pbox, 16); + + + 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,Pbox,PboxRM,Sbox1,Sbox2,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; + + + + return 0; +} -- 2.39.5