2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
15 /*#include <cryptopp/hex.h>
16 #include <cryptopp/sha.h>
17 #include <cryptopp/osrng.h>
18 #include <cryptopp/secblock.h>
23 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
24 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
28 //using namespace CryptoPP;
44 typedef unsigned char uchar;
49 struct timeval tstart;
50 gettimeofday(&tstart,0);
51 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
54 double TimeStop(double t)
58 gettimeofday(&tend,0);
59 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
66 uint xorshift32(const uint t)
68 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
81 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
92 __uint128_t g_lehmer64_state;
94 inline uint64_t splitmix64_stateless(uint64_t index) {
95 uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
96 z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
97 z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
102 inline void lehmer64_seed(uint64_t seed) {
103 g_lehmer64_state = (((__uint128_t)splitmix64_stateless(seed)) << 64) +
104 splitmix64_stateless(seed + 1);
107 inline uint64_t lehmer64() {
108 g_lehmer64_state *= UINT64_C(0xda942042e4dd58b5);
110 return g_lehmer64_state >> 64;
117 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
119 for(int i=0;i<size_tab;i++) {
120 inv_perm_tabs[tab[i]] = i;
125 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
127 for(int i=0;i<size_tab;i++) {
128 inv_perm_tabs[tab[i]] = i;
135 void rc4key(uchar *key, uchar *sc, int size_DK) {
137 for(int i=0;i<256;i++) {
143 for(int i0=0; i0<256; i0++) {
144 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
153 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
159 for (int i=0;i<len;i++) {
162 for (int it = 0; it < rp; it++) {
164 for(int i0 = 0; i0<len; i0++) {
165 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
174 void prga(uchar *sc, int ldata, uchar *r) {
178 for (int it=0; it<ldata; it++) {
180 j0 = (j0 + sc[i0])&0xFF;
184 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
194 void encrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, ulong myrand, int debug) {
203 ulong *rm1=(ulong*)RM1;
204 ulong *rm2=(ulong*)RM2;
206 for(int it=0;it<len/2;it++) {
208 int ind2=Pbox[it+len/2]*h;
211 for(int a=0;a<(h>>3);a++) {
220 for(int a=0;a<h;a+=4) {
222 X[a+1]=seq_in[ind2+a+1];
223 X[a+2]=seq_in[ind2+a+2];
224 X[a+3]=seq_in[ind2+a+3];
227 for(int a=0;a<h;a+=4) {
229 Y[a+1]=seq_in[ind1+a+1];
230 Y[a+2]=seq_in[ind1+a+2];
231 Y[a+3]=seq_in[ind1+a+3];
235 for(int a=0;a<h;a+=4) {
236 tmp[a]=Sbox1[X[a]^RM1[a]];
237 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
238 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
239 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
242 for(int a=0;a<h;a+=4) {
243 fX[a]=Sbox2[tmp[a]^Y[a]];
244 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
245 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
246 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
250 /*for(int a=0;a<h;a+=4) {
251 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
252 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
253 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
254 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
260 for(int a=0;a<h;a+=4) {
261 tmp[a]=Sbox2[fX[a]^Y[a]];
262 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
263 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
264 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
267 for(int a=0;a<h;a+=4) {
268 gY[a]=Sbox1[tmp[a]^RM2[a]];
269 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
270 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
271 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
276 /* for(int a=0;a<h;a+=4) {
277 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
278 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
279 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
280 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
289 for(int a=0;a<h;a+=4) {
290 seq_out[ind2+a]=gY[a];
291 seq_out[ind2+a+1]=gY[a+1];
292 seq_out[ind2+a+2]=gY[a+2];
293 seq_out[ind2+a+3]=gY[a+3];
296 for(int a=0;a<h;a+=4) {
297 seq_out[ind1+a]=fX[a];
298 seq_out[ind1+a+1]=fX[a+1];
299 seq_out[ind1+a+2]=fX[a+2];
300 seq_out[ind1+a+3]=fX[a+3];
320 void decrypt_ecb_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, ulong myrand, int debug) {
329 ulong *rm1=(ulong*)RM1;
330 ulong *rm2=(ulong*)RM2;
332 for(int it=0;it<len/2;it++) {
334 int ind2=Pbox[it+len/2]*h;
337 for(int a=0;a<(h>>3);a++) {
346 for(int a=0;a<h;a+=4) {
347 gY[a]=seq_in[ind2+a];
348 gY[a+1]=seq_in[ind2+a+1];
349 gY[a+2]=seq_in[ind2+a+2];
350 gY[a+3]=seq_in[ind2+a+3];
353 for(int a=0;a<h;a+=4) {
354 fX[a]=seq_in[ind1+a];
355 fX[a+1]=seq_in[ind1+a+1];
356 fX[a+2]=seq_in[ind1+a+2];
357 fX[a+3]=seq_in[ind1+a+3];
363 for(int a=0;a<h;a+=4) {
364 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
365 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
366 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
367 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
371 for(int a=0;a<h;a+=4) {
372 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
373 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
374 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
375 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
380 /* for(int a=0;a<h;a+=4) {
381 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
382 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
383 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
384 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
390 for(int a=0;a<h;a+=4) {
391 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
392 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
393 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
394 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
398 for(int a=0;a<h;a+=4) {
399 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
400 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
401 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
402 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
408 for(int a=0;a<h;a+=4) {
409 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
410 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
411 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
412 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
417 for(int a=0;a<h;a+=4) {
418 seq_out[ind2+a]=invfX[a];
419 seq_out[ind2+a+1]=invfX[a+1];
420 seq_out[ind2+a+2]=invfX[a+2];
421 seq_out[ind2+a+3]=invfX[a+3];
424 for(int a=0;a<h;a+=4) {
425 seq_out[ind1+a]=invgY[a];
426 seq_out[ind1+a+1]=invgY[a+1];
427 seq_out[ind1+a+2]=invgY[a+2];
428 seq_out[ind1+a+3]=invgY[a+3];
446 void encrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, ulong myrand, int debug) {
455 ulong *rm1=(ulong*)RM1;
456 ulong *rm2=(ulong*)RM2;
458 for(int it=0;it<len/2;it++) {
460 int ind2=Pbox[it+len/2]*h;
463 for(int a=0;a<(h>>3);a++) {
472 for(int a=0;a<h;a+=4) {
474 X[a+1]=seq_in[ind2+a+1];
475 X[a+2]=seq_in[ind2+a+2];
476 X[a+3]=seq_in[ind2+a+3];
479 for(int a=0;a<h;a+=4) {
481 Y[a+1]=seq_in[ind1+a+1];
482 Y[a+2]=seq_in[ind1+a+2];
483 Y[a+3]=seq_in[ind1+a+3];
487 for(int a=0;a<h;a+=4) {
488 tmp[a]=Sbox1[X[a]^RM1[a]];
489 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
490 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
491 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
494 for(int a=0;a<h;a+=4) {
495 fX[a]=Sbox2[tmp[a]^Y[a]];
496 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
497 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
498 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
502 /*for(int a=0;a<h;a+=4) {
503 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
504 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
505 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
506 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
512 for(int a=0;a<h;a+=4) {
513 tmp[a]=Sbox2[fX[a]^Y[a]];
514 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
515 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
516 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
519 for(int a=0;a<h;a+=4) {
520 gY[a]=Sbox1[tmp[a]^RM2[a]];
521 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
522 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
523 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
528 /* for(int a=0;a<h;a+=4) {
529 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
530 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
531 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
532 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
541 for(int a=0;a<h;a+=4) {
542 seq_out[ind2+a]=gY[a];
543 seq_out[ind2+a+1]=gY[a+1];
544 seq_out[ind2+a+2]=gY[a+2];
545 seq_out[ind2+a+3]=gY[a+3];
548 for(int a=0;a<h;a+=4) {
549 seq_out[ind1+a]=fX[a];
550 seq_out[ind1+a+1]=fX[a+1];
551 seq_out[ind1+a+2]=fX[a+2];
552 seq_out[ind1+a+3]=fX[a+3];
572 void decrypt_ecb_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, ulong myrand, int debug) {
581 ulong *rm1=(ulong*)RM1;
582 ulong *rm2=(ulong*)RM2;
584 for(int it=0;it<len/2;it++) {
586 int ind2=Pbox[it+len/2]*h;
589 for(int a=0;a<(h>>3);a++) {
598 for(int a=0;a<h;a+=4) {
599 gY[a]=seq_in[ind2+a];
600 gY[a+1]=seq_in[ind2+a+1];
601 gY[a+2]=seq_in[ind2+a+2];
602 gY[a+3]=seq_in[ind2+a+3];
605 for(int a=0;a<h;a+=4) {
606 fX[a]=seq_in[ind1+a];
607 fX[a+1]=seq_in[ind1+a+1];
608 fX[a+2]=seq_in[ind1+a+2];
609 fX[a+3]=seq_in[ind1+a+3];
615 for(int a=0;a<h;a+=4) {
616 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
617 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
618 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
619 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
623 for(int a=0;a<h;a+=4) {
624 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
625 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
626 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
627 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
632 /* for(int a=0;a<h;a+=4) {
633 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
634 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
635 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
636 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
642 for(int a=0;a<h;a+=4) {
643 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
644 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
645 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
646 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
650 for(int a=0;a<h;a+=4) {
651 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
652 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
653 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
654 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
660 for(int a=0;a<h;a+=4) {
661 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
662 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
663 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
664 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
669 for(int a=0;a<h;a+=4) {
670 seq_out[ind2+a]=invfX[a];
671 seq_out[ind2+a+1]=invfX[a+1];
672 seq_out[ind2+a+2]=invfX[a+2];
673 seq_out[ind2+a+3]=invfX[a+3];
676 for(int a=0;a<h;a+=4) {
677 seq_out[ind1+a]=invgY[a];
678 seq_out[ind1+a+1]=invgY[a+1];
679 seq_out[ind1+a+2]=invgY[a+2];
680 seq_out[ind1+a+3]=invgY[a+3];
697 void encrypt_ecb(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
706 for(int it=0;it<len/2;it++) {
708 int ind2=Pbox[it+len/2]*h;
712 RM1=&RM[PboxSRM[it]*h];
713 RM2=&RM[h*h+PboxSRM[it]*h];
716 for(int a=0;a<h;a+=4) {
718 X[a+1]=seq_in[ind2+a+1];
719 X[a+2]=seq_in[ind2+a+2];
720 X[a+3]=seq_in[ind2+a+3];
723 for(int a=0;a<h;a+=4) {
725 Y[a+1]=seq_in[ind1+a+1];
726 Y[a+2]=seq_in[ind1+a+2];
727 Y[a+3]=seq_in[ind1+a+3];
731 for(int a=0;a<h;a+=4) {
732 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
733 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
734 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
735 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
738 for(int a=0;a<h;a+=4) {
739 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
740 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
741 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
742 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
746 for(int a=0;a<h;a+=4) {
747 seq_out[ind2+a]=gY[a];
748 seq_out[ind2+a+1]=gY[a+1];
749 seq_out[ind2+a+2]=gY[a+2];
750 seq_out[ind2+a+3]=gY[a+3];
753 for(int a=0;a<h;a+=4) {
754 seq_out[ind1+a]=fX[a];
755 seq_out[ind1+a+1]=fX[a+1];
756 seq_out[ind1+a+2]=fX[a+2];
757 seq_out[ind1+a+3]=fX[a+3];
777 void decrypt_ecb(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
786 for(int it=0;it<len/2;it++) {
788 int ind2=Pbox[it+len/2]*h;
791 RM1=&RM[PboxSRM[it]*h];
792 RM2=&RM[h*h+PboxSRM[it]*h];
795 for(int a=0;a<h;a+=4) {
796 gY[a]=seq_in[ind2+a];
797 gY[a+1]=seq_in[ind2+a+1];
798 gY[a+2]=seq_in[ind2+a+2];
799 gY[a+3]=seq_in[ind2+a+3];
802 for(int a=0;a<h;a+=4) {
803 fX[a]=seq_in[ind1+a];
804 fX[a+1]=seq_in[ind1+a+1];
805 fX[a+2]=seq_in[ind1+a+2];
806 fX[a+3]=seq_in[ind1+a+3];
809 for(int a=0;a<h;a+=4) {
810 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
811 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
812 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
813 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
818 for(int a=0;a<h;a+=4) {
819 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
820 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
821 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
822 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
827 for(int a=0;a<h;a+=4) {
828 seq_out[ind2+a]=invfX[a];
829 seq_out[ind2+a+1]=invfX[a+1];
830 seq_out[ind2+a+2]=invfX[a+2];
831 seq_out[ind2+a+3]=invfX[a+3];
834 for(int a=0;a<h;a+=4) {
835 seq_out[ind1+a]=invgY[a];
836 seq_out[ind1+a+1]=invgY[a+1];
837 seq_out[ind1+a+2]=invgY[a+2];
838 seq_out[ind1+a+3]=invgY[a+3];
857 void encrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
868 ulong *rm1=(ulong*)RM1;
869 ulong *rm2=(ulong*)RM2;
874 for(int it=0;it<len/2;it++) {
876 int ind2=Pbox[it+len/2]*h;
879 for(int a=0;a<(h>>3);a++) {
888 for(int a=0;a<h;a+=4) {
890 X[a+1]=seq_in[ind2+a+1];
891 X[a+2]=seq_in[ind2+a+2];
892 X[a+3]=seq_in[ind2+a+3];
895 for(int a=0;a<h;a+=4) {
897 Y[a+1]=seq_in[ind1+a+1];
898 Y[a+2]=seq_in[ind1+a+2];
899 Y[a+3]=seq_in[ind1+a+3];
903 for(int a=0;a<h;a+=4) {
904 tmp[a]=X[a]^RM1[a]^IV1[a];
905 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
906 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
907 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
910 for(int a=0;a<h;a+=4) {
911 tmp[a]=Sbox1[tmp[a]];
912 tmp[a+1]=Sbox1[tmp[a+1]];
913 tmp[a+2]=Sbox1[tmp[a+2]];
914 tmp[a+3]=Sbox1[tmp[a+3]];
918 /*for(int a=0;a<h;a+=4) {
919 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
920 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
921 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
922 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
925 for(int a=0;a<h;a+=4) {
926 fX[a]=Sbox2[tmp[a]^Y[a]];
927 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
928 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
929 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
933 for(int a=0;a<h;a+=4) {
934 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
935 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
936 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
937 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
941 for(int a=0;a<h;a+=4) {
942 tmp[a]=fX[a]^Y[a]^IV2[a];
943 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
944 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
945 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
949 for(int a=0;a<h;a+=4) {
950 tmp[a]=Sbox2[tmp[a]];
951 tmp[a+1]=Sbox2[tmp[a+1]];
952 tmp[a+2]=Sbox2[tmp[a+2]];
953 tmp[a+3]=Sbox2[tmp[a+3]];
958 for(int a=0;a<h;a+=4) {
959 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
960 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
961 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
962 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
967 for(int a=0;a<h;a+=4) {
968 gY[a]=Sbox1[tmp[a]^RM2[a]];
969 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
970 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
971 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
978 for(int a=0;a<h;a+=4) {
979 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
980 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
981 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
982 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
987 for(int a=0;a<h;a+=4) {
988 seq_out[ind2+a]=gY[a];
989 seq_out[ind2+a+1]=gY[a+1];
990 seq_out[ind2+a+2]=gY[a+2];
991 seq_out[ind2+a+3]=gY[a+3];
994 for(int a=0;a<h;a+=4) {
995 seq_out[ind1+a]=fX[a];
996 seq_out[ind1+a+1]=fX[a+1];
997 seq_out[ind1+a+2]=fX[a+2];
998 seq_out[ind1+a+3]=fX[a+3];
1000 for(int a=0;a<h;a+=4) {
1007 for(int a=0;a<h;a+=4) {
1029 void decrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
1042 ulong *rm1=(ulong*)RM1;
1043 ulong *rm2=(ulong*)RM2;
1045 for(int it=0;it<len/2;it++) {
1046 int ind1=Pbox[it]*h;
1047 int ind2=Pbox[it+len/2]*h;
1050 for(int a=0;a<(h>>3);a++) {
1051 myrand=xorshift64();
1053 myrand=xorshift64();
1057 for(int a=0;a<h;a+=4) {
1058 gY[a]=seq_in[ind2+a];
1059 gY[a+1]=seq_in[ind2+a+1];
1060 gY[a+2]=seq_in[ind2+a+2];
1061 gY[a+3]=seq_in[ind2+a+3];
1064 for(int a=0;a<h;a+=4) {
1065 fX[a]=seq_in[ind1+a];
1066 fX[a+1]=seq_in[ind1+a+1];
1067 fX[a+2]=seq_in[ind1+a+2];
1068 fX[a+3]=seq_in[ind1+a+3];
1072 for(int a=0;a<h;a+=4) {
1073 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1074 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1075 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1076 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1080 for(int a=0;a<h;a+=4) {
1081 tmp[a]=Inv_Sbox2[tmp[a]];
1082 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1083 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1084 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1089 for(int a=0;a<h;a+=4) {
1090 invgY[a]=tmp[a]^fX[a]^IV2[a];
1091 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1092 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1093 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1097 for(int a=0;a<h;a+=4) {
1098 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1099 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1100 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1101 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1106 for(int a=0;a<h;a+=4) {
1107 tmp[a]=Inv_Sbox1[tmp[a]];
1108 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1109 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1110 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1117 for(int a=0;a<h;a+=4) {
1118 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1119 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1120 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1121 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1126 for(int a=0;a<h;a+=4) {
1127 seq_out[ind2+a]=invfX[a];
1128 seq_out[ind2+a+1]=invfX[a+1];
1129 seq_out[ind2+a+2]=invfX[a+2];
1130 seq_out[ind2+a+3]=invfX[a+3];
1133 for(int a=0;a<h;a+=4) {
1134 seq_out[ind1+a]=invgY[a];
1135 seq_out[ind1+a+1]=invgY[a+1];
1136 seq_out[ind1+a+2]=invgY[a+2];
1137 seq_out[ind1+a+3]=invgY[a+3];
1139 for(int a=0;a<h;a+=4) {
1146 for(int a=0;a<h;a+=4) {
1169 void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *IV, int debug) {
1183 for(int a=0;a<h;a+=4) {
1191 for(int a=0;a<h;a+=4) {
1199 for(int it=0;it<len/2;it++) {
1200 int ind1=Pbox[it]*h;
1201 int ind2=Pbox[it+len/2]*h;
1203 RM1=&RM[PboxSRM[it]*h];
1204 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1207 for(int a=0;a<h;a+=4) {
1208 X[a]=seq_in[ind2+a];
1209 X[a+1]=seq_in[ind2+a+1];
1210 X[a+2]=seq_in[ind2+a+2];
1211 X[a+3]=seq_in[ind2+a+3];
1214 for(int a=0;a<h;a+=4) {
1215 Y[a]=seq_in[ind1+a];
1216 Y[a+1]=seq_in[ind1+a+1];
1217 Y[a+2]=seq_in[ind1+a+2];
1218 Y[a+3]=seq_in[ind1+a+3];
1222 for(int a=0;a<h;a+=4) {
1223 tmp[a]=X[a]^RM1[a]^IV1[a];
1224 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
1225 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
1226 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
1229 for(int a=0;a<h;a+=4) {
1230 tmp[a]=Sbox1[tmp[a]];
1231 tmp[a+1]=Sbox1[tmp[a+1]];
1232 tmp[a+2]=Sbox1[tmp[a+2]];
1233 tmp[a+3]=Sbox1[tmp[a+3]];
1237 /*for(int a=0;a<h;a+=4) {
1238 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
1239 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
1240 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
1241 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
1244 for(int a=0;a<h;a+=4) {
1245 fX[a]=Sbox2[tmp[a]^Y[a]];
1246 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
1247 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
1248 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
1252 for(int a=0;a<h;a+=4) {
1253 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1254 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1255 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1256 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1260 for(int a=0;a<h;a+=4) {
1261 tmp[a]=fX[a]^Y[a]^IV2[a];
1262 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
1263 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
1264 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
1268 for(int a=0;a<h;a+=4) {
1269 tmp[a]=Sbox2[tmp[a]];
1270 tmp[a+1]=Sbox2[tmp[a+1]];
1271 tmp[a+2]=Sbox2[tmp[a+2]];
1272 tmp[a+3]=Sbox2[tmp[a+3]];
1277 for(int a=0;a<h;a+=4) {
1278 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
1279 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
1280 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1281 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1286 for(int a=0;a<h;a+=4) {
1287 gY[a]=Sbox1[tmp[a]^RM2[a]];
1288 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1289 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1290 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1297 for(int a=0;a<h;a+=4) {
1298 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1299 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1300 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1301 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1306 for(int a=0;a<h;a+=4) {
1307 seq_out[ind2+a]=gY[a];
1308 seq_out[ind2+a+1]=gY[a+1];
1309 seq_out[ind2+a+2]=gY[a+2];
1310 seq_out[ind2+a+3]=gY[a+3];
1313 for(int a=0;a<h;a+=4) {
1314 seq_out[ind1+a]=fX[a];
1315 seq_out[ind1+a+1]=fX[a+1];
1316 seq_out[ind1+a+2]=fX[a+2];
1317 seq_out[ind1+a+3]=fX[a+3];
1319 for(int a=0;a<h;a+=4) {
1326 for(int a=0;a<h;a+=4) {
1348 void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *IV, int debug) {
1361 for(int a=0;a<h;a+=4) {
1369 for(int a=0;a<h;a+=4) {
1377 for(int it=0;it<len/2;it++) {
1378 int ind1=Pbox[it]*h;
1379 int ind2=Pbox[it+len/2]*h;
1382 RM1=&RM[PboxSRM[it]*h];
1383 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1388 for(int a=0;a<h;a+=4) {
1389 gY[a]=seq_in[ind2+a];
1390 gY[a+1]=seq_in[ind2+a+1];
1391 gY[a+2]=seq_in[ind2+a+2];
1392 gY[a+3]=seq_in[ind2+a+3];
1395 for(int a=0;a<h;a+=4) {
1396 fX[a]=seq_in[ind1+a];
1397 fX[a+1]=seq_in[ind1+a+1];
1398 fX[a+2]=seq_in[ind1+a+2];
1399 fX[a+3]=seq_in[ind1+a+3];
1403 for(int a=0;a<h;a+=4) {
1404 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1405 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1406 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1407 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1411 for(int a=0;a<h;a+=4) {
1412 tmp[a]=Inv_Sbox2[tmp[a]];
1413 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1414 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1415 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1420 for(int a=0;a<h;a+=4) {
1421 invgY[a]=tmp[a]^fX[a]^IV2[a];
1422 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1423 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1424 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1428 for(int a=0;a<h;a+=4) {
1429 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1430 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1431 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1432 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1437 for(int a=0;a<h;a+=4) {
1438 tmp[a]=Inv_Sbox1[tmp[a]];
1439 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1440 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1441 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1448 for(int a=0;a<h;a+=4) {
1449 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1450 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1451 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1452 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1457 for(int a=0;a<h;a+=4) {
1458 seq_out[ind2+a]=invfX[a];
1459 seq_out[ind2+a+1]=invfX[a+1];
1460 seq_out[ind2+a+2]=invfX[a+2];
1461 seq_out[ind2+a+3]=invfX[a+3];
1464 for(int a=0;a<h;a+=4) {
1465 seq_out[ind1+a]=invgY[a];
1466 seq_out[ind1+a+1]=invgY[a+1];
1467 seq_out[ind1+a+2]=invgY[a+2];
1468 seq_out[ind1+a+3]=invgY[a+3];
1470 for(int a=0;a<h;a+=4) {
1477 for(int a=0;a<h;a+=4) {
1499 void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
1515 for(int a=0;a<h;a+=4) {
1516 myrand=xorshift32(myrand);
1527 for(int a=0;a<h;a+=4) {
1528 myrand=xorshift32(myrand);
1542 for(int it=0;it<len/2;it++) {
1543 int ind1=Pbox[it]*h;
1544 int ind2=Pbox[it+len/2]*h;
1548 RM1=&RM[PboxSRM[it]*h];
1549 RM2=&RM[h*h+PboxSRM[it]*h];
1552 for(int a=0;a<h;a+=4) {
1553 X[a]=seq_in[ind2+a];
1554 X[a+1]=seq_in[ind2+a+1];
1555 X[a+2]=seq_in[ind2+a+2];
1556 X[a+3]=seq_in[ind2+a+3];
1559 for(int a=0;a<h;a+=4) {
1560 Y[a]=seq_in[ind1+a];
1561 Y[a+1]=seq_in[ind1+a+1];
1562 Y[a+2]=seq_in[ind1+a+2];
1563 Y[a+3]=seq_in[ind1+a+3];
1567 for(int a=0;a<h;a+=4) {
1568 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1569 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1570 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1571 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1574 for(int a=0;a<h;a+=4) {
1575 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1576 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1577 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1578 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1582 for(int a=0;a<h;a+=4) {
1583 seq_out[ind2+a]=gY[a];
1584 seq_out[ind2+a+1]=gY[a+1];
1585 seq_out[ind2+a+2]=gY[a+2];
1586 seq_out[ind2+a+3]=gY[a+3];
1589 for(int a=0;a<h;a+=4) {
1590 seq_out[ind1+a]=fX[a];
1591 seq_out[ind1+a+1]=fX[a+1];
1592 seq_out[ind1+a+2]=fX[a+2];
1593 seq_out[ind1+a+3]=fX[a+3];
1595 for(int a=0;a<h;a+=4) {
1602 for(int a=0;a<h;a+=4) {
1624 void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
1635 for(int a=0;a<h;a+=4) {
1636 myrand=xorshift32(myrand);
1647 for(int a=0;a<h;a+=4) {
1648 myrand=xorshift32(myrand);
1665 for(int it=0;it<len/2;it++) {
1666 int ind1=Pbox[it]*h;
1667 int ind2=Pbox[it+len/2]*h;
1670 RM1=&RM[PboxSRM[it]*h];
1671 RM2=&RM[h*h+PboxSRM[it]*h];
1674 for(int a=0;a<h;a+=4) {
1675 gY[a]=seq_in[ind2+a];
1676 gY[a+1]=seq_in[ind2+a+1];
1677 gY[a+2]=seq_in[ind2+a+2];
1678 gY[a+3]=seq_in[ind2+a+3];
1681 for(int a=0;a<h;a+=4) {
1682 fX[a]=seq_in[ind1+a];
1683 fX[a+1]=seq_in[ind1+a+1];
1684 fX[a+2]=seq_in[ind1+a+2];
1685 fX[a+3]=seq_in[ind1+a+3];
1689 for(int a=0;a<h;a+=4) {
1690 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
1691 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1692 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1693 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1697 for(int a=0;a<h;a+=4) {
1698 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
1699 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
1700 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
1701 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
1705 for(int a=0;a<h;a+=4) {
1706 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
1707 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1708 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1709 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1713 for(int a=0;a<h;a+=4) {
1714 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
1715 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
1716 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
1717 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
1722 for(int a=0;a<h;a+=4) {
1723 seq_out[ind2+a]=invfX[a];
1724 seq_out[ind2+a+1]=invfX[a+1];
1725 seq_out[ind2+a+2]=invfX[a+2];
1726 seq_out[ind2+a+3]=invfX[a+3];
1729 for(int a=0;a<h;a+=4) {
1730 seq_out[ind1+a]=invgY[a];
1731 seq_out[ind1+a+1]=invgY[a+1];
1732 seq_out[ind1+a+2]=invgY[a+2];
1733 seq_out[ind1+a+3]=invgY[a+3];
1735 for(int a=0;a<h;a+=4) {
1742 for(int a=0;a<h;a+=4) {
1760 int main(int argc, char** argv) {
1769 for(int i=1; i<argc; i++){
1770 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
1771 if(strncmp(argv[i],"cbcrm",5)==0) cbcrm=1;
1772 if(strncmp(argv[i],"cbcprng",7)==0) {cbcprng=1;cbcrm=0;}
1773 if(strncmp(argv[i],"ecbrm",5)==0) ecbrm = 1;
1774 if(strncmp(argv[i],"ecbprng",7)==0) {ecbprng=1; ecbrm=0;}
1775 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
1776 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
1777 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
1780 printf("nb times %d\n",nb_test);
1781 printf("cbcrm %d\n",cbcrm);
1782 printf("cbcprng %d\n",cbcprng);
1783 printf("ecbrm %d\n",ecbrm);
1784 printf("ecbprng %d\n",ecbprng);
1786 printf("lena %d\n",lena);
1787 printf("size_buf %d\n",size_buf);
1792 int seed=time(NULL);
1793 // cout<<seed<<endl;
1796 uchar Secretkey[key_size];
1798 uchar counter[key_size];
1800 for(int i=0;i<key_size;i++) {
1801 Secretkey[i]=lrand48()&0xFF;
1802 counter[i]=lrand48()&0xFF;
1815 uchar *data_R, *data_G, *data_B;
1824 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
1825 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
1826 imsize=width*height*3;
1827 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
1830 width=height=size_buf;
1831 imsize=width*height;
1832 buffer=new uchar[imsize];
1833 for(int i=0;i<imsize;i++) {
1834 buffer[i]=lrand48();
1842 uchar* seq= new uchar[imsize];
1843 uchar* seq2= new uchar[imsize];
1845 int oneD=width*height;
1847 for(int i=0;i<oneD;i++) {
1849 seq[oneD+i]=data_G[i];
1850 seq[2*oneD+i]=data_B[i];
1854 for(int i=0;i<oneD;i++) {
1863 int total_len=imsize;
1865 int len= total_len/h;
1869 uchar *mix=new uchar[256];
1874 for (int i = 0; i < 256 ; i++) {
1875 mix[i]=Secretkey[i]^counter[i];
1880 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
1881 // g_print("%s\n", sha512);
1891 // cout<<"hash "<<endl;
1892 for (int i = 0; i < 128 ; i++) {
1899 int *Pbox=new int[len];
1900 int *PboxSRM=new int[len/2];
1901 int *PboxSRM2=new int[len/2];
1904 uchar Inv_Sbox1[256];
1905 uchar Inv_Sbox2[256];
1907 uchar RM[h*h*2+256];
1913 double time_encrypt=0;
1914 double time_decrypt=0;
1917 double t=TimeStart();
1918 rc4key(DK, Sbox1, 8);
1921 rc4key(&DK[8], Sbox2, 8);
1923 rc4key(&DK[16], sc, 16);
1924 prga(sc, h*h*2+256, RM);
1930 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1933 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1935 for(int i=0;i<len/2;i++) {
1936 PboxSRM[i]=PboxSRM2[i]&(h-1);
1940 for(int i=0;i<h*2;i++) {
1941 for(int j=0;j<h;j++)
1942 cout<<(int)RM[i*h+j]<<" ";
1949 //time+=TimeStop(t);
1950 //cout<<"Time initializaton "<<time<<endl;
1955 for(int i=0;i<64;i++) {
1966 inverse_tables(Sbox1,256,Inv_Sbox1);
1967 inverse_tables(Sbox2,256,Inv_Sbox2);
1971 // lehmer64_seed(myrand);
1978 for(i=0;i<nb_test;i++)
1981 encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1983 encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1985 encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1987 encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1991 for(i=0;i<nb_test;i++)
1994 encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1996 encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1998 encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2000 encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2004 for(i=0;i<nb_test;i++)
2007 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2009 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2011 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2013 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2017 for(i=0;i<nb_test;i++)
2020 encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2022 encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2024 encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2026 encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2030 for(i=0;i<nb_test;i++)
2033 encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2035 encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2037 encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2039 encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2044 for(i=0;i<nb_test;i++)
2047 encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2049 encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2051 encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2053 encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2058 for(i=0;i<nb_test;i++)
2061 encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2063 encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2065 encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2067 encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2072 time_encrypt+=TimeStop(t);
2073 //cout<<"Time encrypt "<<
2074 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
2078 for(int i=0;i<oneD;i++) {
2080 data_G[i]=seq2[oneD+i];
2081 data_B[i]=seq2[2*oneD+i];
2083 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
2088 // lehmer64_seed(myrand);
2093 for(i=0;i<nb_test;i++) {
2095 decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2097 decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2099 decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2101 decrypt_ecb_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2105 for(i=0;i<nb_test;i++) {
2107 decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2109 decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2111 decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2113 decrypt_ecb_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2117 for(i=0;i<nb_test;i++) {
2119 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2121 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2123 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2125 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2129 for(i=0;i<nb_test;i++) {
2131 decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2133 decrypt_cbc_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2135 decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2137 decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2141 for(i=0;i<nb_test;i++) {
2143 decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2145 decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2147 decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2149 decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2153 for(i=0;i<nb_test;i++) {
2155 decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2157 decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2159 decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2161 decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2165 for(i=0;i<nb_test;i++) {
2167 decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2169 decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2171 decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2173 decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2178 time_decrypt+=TimeStop(t);
2179 //cout<<"Time decrypt "
2180 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
2183 for(int i=0;i<oneD;i++) {
2185 data_G[i]=seq[oneD+i];
2186 data_B[i]=seq[2*oneD+i];
2188 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
2192 for(int i=0;i<imsize;i++) {
2193 //cout<<(int)buffer[i]<<endl;
2194 if(buffer[i]!=seq[i]) {
2198 // cout<<"RESULT CORRECT: "<<equal<<endl;