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;
161 for(int a=0;a<h;a+=4) {
162 out[a]=S[out2[a]^X[a]];
163 out[a+1]=S[out2[a+1]^X[a+1]];
164 out[a+2]=S[out2[a+2]^X[a+2]];
165 out[a+3]=S[out2[a+3]^X[a+3]];
185 int main(int argc, char** argv) {
194 for(int i=1; i<argc; i++){
195 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
196 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
197 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
198 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
199 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
200 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
204 cout<<size_buf<<endl;
211 uchar *data_R, *data_G, *data_B;
216 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
217 imsize=width*height*3;
218 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
222 buffer=new uchar[imsize];
223 for(int i=0;i<imsize;i++) {
231 uchar* seq= new uchar[imsize];
232 uchar* seq2= new uchar[imsize];
237 for(int i=0;i<oneD;i++) {
239 seq[oneD+i]=data_G[i];
240 seq[2*oneD+i]=data_B[i];
245 for(int i=0;i<oneD;i++) {
259 if(change==3 && imsize>=32) {
262 if(change==4 && imsize>=128) {
270 int total_len=imsize;
272 int len= total_len/h;
290 cout<<"imsize "<<imsize<<endl;
293 for(int i=0;i<imsize;i++){
294 cout<<(int)seq[i]<<" ";
299 for(int i=0;i<h;i++){
300 cout<<(int)res[i]<<" ";
306 double t=TimeStart();
307 for(int i=0;i<nb_test;i++)
310 myhash(seq, len,res,h);
317 cout<<"Hash Time "<<time<<endl;
318 cout<<(double)imsize*nb_test/time<<"\t";
320 for(int i=0;i<h;i++){
321 cout<<(int)res[i]<<" ";