2 //g++ -O3 one_round_light_v2.cpp pixmap_io.o -o one_round_light_v2 -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+=4) {
154 X[a+1]=seq[ind1+a+1];
155 X[a+2]=seq[ind1+a+2];
156 X[a+3]=seq[ind1+a+3];
164 for(int a=0;a<h2;a+=4) {
166 Y[a+1]=seq[ind2+a+1];
167 Y[a+2]=seq[ind2+a+2];
168 Y[a+3]=seq[ind2+a+3];
175 for(int a=0;a<h2;a+=4){
177 fX[a+1]=Sbox1[X[a+1]];
178 fX[a+2]=Sbox1[X[a+2]];
179 fX[a+3]=Sbox1[X[a+3]];
182 for(int a=0;a<h2;a+=4){
184 gY[a+1]=Sbox2[Y[a+1]];
185 gY[a+2]=Sbox2[Y[a+2]];
186 gY[a+3]=Sbox2[Y[a+3]];
188 for(int a=0;a<h2;a+=4) {
189 fX[a]=fX[a]^RM1[a]^Y[a];
190 fX[a+1]=fX[a+1]^RM1[a+1]^Y[a+1];
191 fX[a+2]=fX[a+2]^RM1[a+2]^Y[a+2];
192 fX[a+3]=fX[a+3]^RM1[a+3]^Y[a+3];
194 for(int a=0;a<h2;a+=4){
196 gY[a+1]=gY[a+1]^RM3[a+1];
197 gY[a+2]=gY[a+2]^RM3[a+2];
198 gY[a+3]=gY[a+3]^RM3[a+3];
201 for(int a=0;a<h2;a+=4) {
202 seq[ind1+a]=Sbox2[fX[a]];
203 seq[ind1+a+1]=Sbox2[fX[a+1]];
204 seq[ind1+a+2]=Sbox2[fX[a+3]];
205 seq[ind1+a+3]=Sbox2[fX[a+3]];
207 for(int a=0;a<h2;a+=4){
208 seq[ind2+a]=Sbox1[gY[a]];
209 seq[ind2+a+1]=Sbox1[gY[a+1]];
210 seq[ind2+a+2]=Sbox1[gY[a+2]];
211 seq[ind2+a+3]=Sbox1[gY[a+3]];
220 void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
223 uchar *fX=new uchar[h2];
224 uchar *gY=new uchar[h2];
227 uchar *Inv_Sbox1=new uchar[256];
228 inverse_tables(Sbox1,256,Inv_Sbox1);
230 uchar *Inv_Sbox2=new uchar[256];
231 inverse_tables(Sbox2,256,Inv_Sbox2);
236 for(int it=len-1;it>=0;it--) {
238 int ind2=Pbox[it]*h2;
242 for(int a=0;a<h2;a++) {
247 for(int a=0;a<h2;a++) {
248 fX[a]=Inv_Sbox2[fX[a]];
251 for(int a=0;a<h2;a++) {
256 for(int a=0;a<h2;a++) {
260 for(int a=0;a<h2;a++) {
261 gY[a]=Inv_Sbox1[gY[a]];
263 for(int a=0;a<h2;a++) {
267 // for(int a=0;a<h2;a++) {
268 // gY[a]=Inv_Sbox2[gY[a]];
271 for(int a=0;a<h2;a++) {
275 for(int a=0;a<h2;a++) {
276 seq[ind1+a]=Inv_Sbox1[fX[a]];
278 for(int a=0;a<h2;a++) {
279 seq[ind2+a]=Inv_Sbox2[gY[a]];
290 void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
293 uchar *fX=new uchar[h2];
294 uchar *gY=new uchar[h2];
297 uchar *Inv_Sbox1=new uchar[256];
298 inverse_tables(Sbox1,256,Inv_Sbox1);
300 uchar *Inv_Sbox2=new uchar[256];
301 inverse_tables(Sbox2,256,Inv_Sbox2);
303 /* int *Inv_Pbox=new int[len];
304 inverse_tables2(Pbox,len,Inv_Pbox);
306 for(int i=0;i<len;i++) {
307 cout<<Pbox[Inv_Pbox[i]]<<" ";
314 for(int it=len-1;it>=0;it--) {
316 int ind2=Pbox[it]*h2;
322 for(int a=0;a<h2;a+=4) {
323 fX[a]=Inv_Sbox2[seq[ind1+a]];
324 fX[a+1]=Inv_Sbox2[seq[ind1+a+1]];
325 fX[a+2]=Inv_Sbox2[seq[ind1+a+2]];
326 fX[a+3]=Inv_Sbox2[seq[ind1+a+3]];
329 for(int a=0;a<h2;a+=4) {
331 fX[a+1]=fX[a+1]^RM1[a+1];
332 fX[a+2]=fX[a+2]^RM1[a+2];
333 fX[a+3]=fX[a+3]^RM1[a+3];
337 for(int a=0;a<h2;a+=4) {
338 gY[a]=Inv_Sbox1[seq[ind2+a]];
339 gY[a+1]=Inv_Sbox1[seq[ind2+a+1]];
340 gY[a+2]=Inv_Sbox1[seq[ind2+a+2]];
341 gY[a+3]=Inv_Sbox1[seq[ind2+a+3]];
343 for(int a=0;a<h2;a+=4) {
344 gY[a]=Inv_Sbox2[gY[a]^RM3[a]];
345 gY[a+1]=Inv_Sbox2[gY[a+1]^RM3[a+1]];
346 gY[a+2]=Inv_Sbox2[gY[a+2]^RM3[a+2]];
347 gY[a+3]=Inv_Sbox2[gY[a+3]^RM3[a+3]];
351 for(int a=0;a<h2;a+=4) {
353 fX[a+1]=fX[a+1]^gY[a+1];
354 fX[a+2]=fX[a+2]^gY[a+2];
355 fX[a+3]=fX[a+2]^gY[a+3];
358 for(int a=0;a<h2;a+=4) {
359 seq[ind1+a]=Inv_Sbox1[fX[a]];
360 seq[ind1+a+1]=Inv_Sbox1[fX[a+1]];
361 seq[ind1+a+2]=Inv_Sbox1[fX[a+2]];
362 seq[ind1+a+3]=Inv_Sbox1[fX[a+3]];
366 for(int a=0;a<h2;a+=4) {
368 seq[ind2+a+1]=gY[a+1];
369 seq[ind2+a+2]=gY[a+2];
370 seq[ind2+a+3]=gY[a+3];
379 void Dynamickeygenerationnew(uchar *Secretkey, uchar *counter) {
388 uchar *data_R, *data_G, *data_B;
389 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
395 int imsize=width*height*3;
396 uchar* seq= new uchar[imsize];
398 int oneD=width*height;
399 for(int i=0;i<oneD;i++) {
401 seq[oneD+i]=data_G[i];
402 seq[2*oneD+i]=data_B[i];
409 int total_len=imsize;
411 int len= total_len/h2;
415 uchar *mix=new uchar[256];
420 for (int i = 0; i < 256 ; i++) {
421 // mix[i]=(int)secret_key.BytePtr()[i]^(int)mycounter.BytePtr()[i];
422 mix[i]=Secretkey[i]^counter[i];
426 SHA512().CalculateDigest(digest, mix, 256);
431 for (int i = 0; i < 64 ; i++) {
440 rc4key(DK, Sbox1, 16);
443 rc4key(&DK[16], Sbox2, 16);
448 rc4key(&DK[32], sc, 16);
450 uchar outd[2*(h * h)];
451 prga(sc, 2*(h * h), outd);
457 for(int i=0;i<h2;i++){
460 RM3[i]=RM1[i]^RM2[i];
469 for (int i = 48; i < 64; i++)
473 int *Pbox=new int[len];
475 rc4keyperm(keyp, len, rp, Pbox, 16);
481 double t=TimeStart();
486 encrypt(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,0);
490 cout<<"Time encrypt "<<time<<endl;
493 for(int i=0;i<oneD;i++) {
495 data_G[i]=seq[oneD+i];
496 data_B[i]=seq[2*oneD+i];
498 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
504 decrypt(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,0);
508 cout<<"Time decrypt "<<time<<endl;
511 for(int i=0;i<oneD;i++) {
513 data_G[i]=seq[oneD+i];
514 data_B[i]=seq[2*oneD+i];
516 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
527 cout << "Hello, World!" << endl;
537 uchar Secretkey[key_size];
539 uchar counter[key_size];
541 for(int i=0;i<key_size;i++) {
542 Secretkey[i]=lrand48()&0xFF;
543 counter[i]=lrand48()&0xFF;
546 Dynamickeygenerationnew(Secretkey, counter);