2 //g++ -O3 one_round_light.cpp pixmap_io.o -o one_round_light -std=c++11
12 /*#include <cryptopp/hex.h>
13 #include <cryptopp/sha.h>
14 #include <cryptopp/osrng.h>
15 #include <cryptopp/secblock.h>
20 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
21 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
25 //using namespace CryptoPP;
37 typedef unsigned char uchar;
42 struct timeval tstart;
43 gettimeofday(&tstart,0);
44 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
47 double TimeStop(double t)
51 gettimeofday(&tend,0);
52 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
61 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
63 for(int i=0;i<size_tab;i++) {
64 inv_perm_tabs[tab[i]] = i;
70 void inverse_tables2(int *tab, int size_tab,int *inv_perm_tabs) {
72 for(int i=0;i<size_tab;i++) {
73 inv_perm_tabs[tab[i]] = i;
79 void rc4key(uchar *key, uchar *sc, int size_DK) {
81 for(int i=0;i<256;i++) {
87 for(int i0=0; i0<256; i0++) {
88 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
97 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
103 for (int i=0;i<len;i++) {
106 for (int it = 0; it < rp; it++) {
108 for(int i0 = 0; i0<len; i0++) {
109 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
118 void prga(uchar *sc, int ldata, uchar *r) {
122 for (int it=0; it<ldata; it++) {
124 j0 = (j0 + sc[i0])&0xFF;
128 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
138 void encrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
141 uchar *X=new uchar[h2];
142 uchar *Y=new uchar[h2];
143 uchar *fX=new uchar[h2];
144 uchar *gY=new uchar[h2];
146 for(int it=0;it<len;it++) {
148 int ind2=Pbox[it]*h2;
151 uchar *p2=&seq[ind1];
152 for(int a=0;a<h2;a++) {
161 for(int a=0;a<h2;a++) {
169 for(int a=0;a<h2;a++){
171 //*p1++=Sbox1[*p2++];
174 for(int a=0;a<h2;a++){
177 for(int a=0;a<h2;a++) {
178 fX[a]=fX[a]^RM1[a]^Y[a];
180 for(int a=0;a<h2;a++){
184 for(int a=0;a<h2;a++) {
185 seq[ind1+a]=Sbox2[fX[a]];
187 for(int a=0;a<h2;a++){
188 seq[ind2+a]=Sbox1[gY[a]];
197 void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
200 uchar *fX=new uchar[h2];
201 uchar *gY=new uchar[h2];
204 uchar *Inv_Sbox1=new uchar[256];
205 inverse_tables(Sbox1,256,Inv_Sbox1);
207 uchar *Inv_Sbox2=new uchar[256];
208 inverse_tables(Sbox2,256,Inv_Sbox2);
213 for(int it=len-1;it>=0;it--) {
215 int ind2=Pbox[it]*h2;
219 for(int a=0;a<h2;a++) {
224 for(int a=0;a<h2;a++) {
225 fX[a]=Inv_Sbox2[fX[a]];
228 for(int a=0;a<h2;a++) {
233 for(int a=0;a<h2;a++) {
237 for(int a=0;a<h2;a++) {
238 gY[a]=Inv_Sbox1[gY[a]];
240 for(int a=0;a<h2;a++) {
244 // for(int a=0;a<h2;a++) {
245 // gY[a]=Inv_Sbox2[gY[a]];
248 for(int a=0;a<h2;a++) {
252 for(int a=0;a<h2;a++) {
253 seq[ind1+a]=Inv_Sbox1[fX[a]];
255 for(int a=0;a<h2;a++) {
256 seq[ind2+a]=Inv_Sbox2[gY[a]];
267 void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
270 uchar *fX=new uchar[h2];
271 uchar *gY=new uchar[h2];
274 uchar *Inv_Sbox1=new uchar[256];
275 inverse_tables(Sbox1,256,Inv_Sbox1);
277 uchar *Inv_Sbox2=new uchar[256];
278 inverse_tables(Sbox2,256,Inv_Sbox2);
280 /* int *Inv_Pbox=new int[len];
281 inverse_tables2(Pbox,len,Inv_Pbox);
283 for(int i=0;i<len;i++) {
284 cout<<Pbox[Inv_Pbox[i]]<<" ";
291 for(int it=len-1;it>=0;it--) {
293 int ind2=Pbox[it]*h2;
299 for(int a=0;a<h2;a++) {
300 fX[a]=Inv_Sbox2[seq[ind1+a]];
303 for(int a=0;a<h2;a++) {
308 for(int a=0;a<h2;a++) {
309 gY[a]=Inv_Sbox1[seq[ind2+a]];
311 for(int a=0;a<h2;a++) {
312 gY[a]=Inv_Sbox2[gY[a]^RM3[a]];
316 for(int a=0;a<h2;a++) {
320 for(int a=0;a<h2;a++) {
321 seq[ind1+a]=Inv_Sbox1[fX[a]];
325 for(int a=0;a<h2;a++) {
335 void Dynamickeygenerationnew(uchar *Secretkey, uchar *counter) {
344 uchar *data_R, *data_G, *data_B;
345 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
351 int imsize=width*height*3;
352 uchar* seq= new uchar[imsize];
354 int oneD=width*height;
355 for(int i=0;i<oneD;i++) {
357 seq[oneD+i]=data_G[i];
358 seq[2*oneD+i]=data_B[i];
365 int total_len=imsize;
367 int len= total_len/h2;
371 uchar *mix=new uchar[256];
376 for (int i = 0; i < 256 ; i++) {
377 // mix[i]=(int)secret_key.BytePtr()[i]^(int)mycounter.BytePtr()[i];
378 mix[i]=Secretkey[i]^counter[i];
382 SHA512().CalculateDigest(digest, mix, 256);
387 for (int i = 0; i < 64 ; i++) {
396 rc4key(DK, Sbox1, 16);
399 rc4key(&DK[16], Sbox2, 16);
404 rc4key(&DK[32], sc, 16);
406 uchar outd[2*(h * h)];
407 prga(sc, 2*(h * h), outd);
413 for(int i=0;i<h2;i++){
416 RM3[i]=RM1[i]^RM2[i];
425 for (int i = 48; i < 64; i++)
429 int *Pbox=new int[len];
431 rc4keyperm(keyp, len, rp, Pbox, 16);
437 double t=TimeStart();
442 encrypt(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,0);
446 cout<<"Time encrypt "<<time<<endl;
449 for(int i=0;i<oneD;i++) {
451 data_G[i]=seq[oneD+i];
452 data_B[i]=seq[2*oneD+i];
454 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
460 decrypt(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,0);
464 cout<<"Time decrypt "<<time<<endl;
467 for(int i=0;i<oneD;i++) {
469 data_G[i]=seq[oneD+i];
470 data_B[i]=seq[2*oneD+i];
472 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
483 cout << "Hello, World!" << endl;
493 uchar Secretkey[key_size];
495 uchar counter[key_size];
497 for(int i=0;i<key_size;i++) {
498 Secretkey[i]=lrand48()&0xFF;
499 counter[i]=lrand48()&0xFF;
502 Dynamickeygenerationnew(Secretkey, counter);