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;
69 void rc4key(uchar *key, uchar *sc, int h) {
71 for(int i=0;i<256;i++) {
77 for(int i0=0; i0<256; i0++) {
78 j0 = (j0 + sc[i0] + key[i0%h] + key[j0%h] );
90 void prga(uchar *sc, uchar *block, int h, uchar *r) {
94 for (int it=0; it<h; it++) {
95 i0 = i0+1+block[(i0+1)%h];
96 j0 = j0+1+block[(j0+1)%h];
100 r[it]=sc[(uchar)(sc[i0]+sc[j0])];
106 void myhash(uchar* seq_in, int len, uchar* out, int h) {
124 for(int i=0;i<256;i++)
125 cout<<(int)S[i]<<" ";
129 /* cout<<"out"<<endl;
131 cout<<(int)out[i]<<" ";
137 for(int it=0;it<len;it++) {
141 // Mix with dynamic RM
143 for(int a=0;a<h;a+=4) {
144 X[a]=out[a]^seq_in[ind2+a];
145 X[a+1]=out[a+1]^seq_in[ind2+a+1];
146 X[a+2]=out[a+2]^seq_in[ind2+a+2];
147 X[a+3]=out[a+3]^seq_in[ind2+a+3];
151 /* for(int i=0;i<h;i++)
152 cout<<(int)X[i]<<endl;
160 for(int a=0;a<h;a+=4) {
161 out[a]=S[out2[a]^X[a]];
162 out[a+1]=S[out2[a+1]^X[a+1]];
163 out[a+2]=S[out2[a+2]^X[a+2]];
164 out[a+3]=S[out2[a+3]^X[a+3]];
184 int main(int argc, char** argv) {
193 for(int i=1; i<argc; i++){
194 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
195 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
196 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
197 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
198 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
199 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
203 cout<<size_buf<<endl;
210 uchar *data_R, *data_G, *data_B;
215 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
216 imsize=width*height*3;
217 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
221 buffer=new uchar[imsize];
222 for(int i=0;i<imsize;i++) {
230 uchar* seq= new uchar[imsize];
231 uchar* seq2= new uchar[imsize];
236 for(int i=0;i<oneD;i++) {
238 seq[oneD+i]=data_G[i];
239 seq[2*oneD+i]=data_B[i];
244 for(int i=0;i<oneD;i++) {
264 int total_len=imsize;
266 int len= total_len/h;
284 cout<<"imsize "<<imsize<<endl;
287 for(int i=0;i<imsize;i++){
288 cout<<(int)seq[i]<<" ";
293 for(int i=0;i<h;i++){
294 cout<<(int)res[i]<<" ";
300 double t=TimeStart();
301 for(int i=0;i<nb_test;i++)
304 myhash(seq, len,res,h);
311 cout<<"Hash Time "<<time<<endl;
312 cout<<(double)imsize*nb_test/time<<"\t";
314 for(int i=0;i<h;i++){
315 cout<<(int)res[i]<<" ";