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;
871 for(int it=0;it<len/2;it++) {
873 int ind2=Pbox[it+len/2]*h;
876 for(int a=0;a<(h>>3);a++) {
885 for(int a=0;a<h;a+=4) {
887 X[a+1]=seq_in[ind2+a+1];
888 X[a+2]=seq_in[ind2+a+2];
889 X[a+3]=seq_in[ind2+a+3];
892 for(int a=0;a<h;a+=4) {
894 Y[a+1]=seq_in[ind1+a+1];
895 Y[a+2]=seq_in[ind1+a+2];
896 Y[a+3]=seq_in[ind1+a+3];
900 for(int a=0;a<h;a+=4) {
901 tmp[a]=X[a]^RM1[a]^IV1[a];
902 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
903 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
904 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
907 for(int a=0;a<h;a+=4) {
908 tmp[a]=Sbox1[tmp[a]];
909 tmp[a+1]=Sbox1[tmp[a+1]];
910 tmp[a+2]=Sbox1[tmp[a+2]];
911 tmp[a+3]=Sbox1[tmp[a+3]];
915 /*for(int a=0;a<h;a+=4) {
916 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
917 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
918 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
919 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
922 for(int a=0;a<h;a+=4) {
923 fX[a]=Sbox2[tmp[a]^Y[a]];
924 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
925 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
926 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
930 for(int a=0;a<h;a+=4) {
931 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
932 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
933 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
934 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
938 for(int a=0;a<h;a+=4) {
939 tmp[a]=fX[a]^Y[a]^IV2[a];
940 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
941 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
942 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
946 for(int a=0;a<h;a+=4) {
947 tmp[a]=Sbox2[tmp[a]];
948 tmp[a+1]=Sbox2[tmp[a+1]];
949 tmp[a+2]=Sbox2[tmp[a+2]];
950 tmp[a+3]=Sbox2[tmp[a+3]];
955 for(int a=0;a<h;a+=4) {
956 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
957 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
958 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
959 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
964 for(int a=0;a<h;a+=4) {
965 gY[a]=Sbox1[tmp[a]^RM2[a]];
966 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
967 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
968 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
975 for(int a=0;a<h;a+=4) {
976 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
977 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
978 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
979 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
984 for(int a=0;a<h;a+=4) {
985 seq_out[ind2+a]=gY[a];
986 seq_out[ind2+a+1]=gY[a+1];
987 seq_out[ind2+a+2]=gY[a+2];
988 seq_out[ind2+a+3]=gY[a+3];
991 for(int a=0;a<h;a+=4) {
992 seq_out[ind1+a]=fX[a];
993 seq_out[ind1+a+1]=fX[a+1];
994 seq_out[ind1+a+2]=fX[a+2];
995 seq_out[ind1+a+3]=fX[a+3];
997 for(int a=0;a<h;a+=4) {
1004 for(int a=0;a<h;a+=4) {
1026 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) {
1037 ulong *rm1=(ulong*)RM1;
1038 ulong *rm2=(ulong*)RM2;
1040 for(int it=0;it<len/2;it++) {
1041 int ind1=Pbox[it]*h;
1042 int ind2=Pbox[it+len/2]*h;
1045 for(int a=0;a<(h>>3);a++) {
1046 myrand=xorshift64();
1048 myrand=xorshift64();
1052 for(int a=0;a<h;a+=4) {
1053 gY[a]=seq_in[ind2+a];
1054 gY[a+1]=seq_in[ind2+a+1];
1055 gY[a+2]=seq_in[ind2+a+2];
1056 gY[a+3]=seq_in[ind2+a+3];
1059 for(int a=0;a<h;a+=4) {
1060 fX[a]=seq_in[ind1+a];
1061 fX[a+1]=seq_in[ind1+a+1];
1062 fX[a+2]=seq_in[ind1+a+2];
1063 fX[a+3]=seq_in[ind1+a+3];
1067 for(int a=0;a<h;a+=4) {
1068 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1069 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1070 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1071 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1075 for(int a=0;a<h;a+=4) {
1076 tmp[a]=Inv_Sbox2[tmp[a]];
1077 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1078 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1079 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1084 for(int a=0;a<h;a+=4) {
1085 invgY[a]=tmp[a]^fX[a]^IV2[a];
1086 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1087 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1088 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1092 for(int a=0;a<h;a+=4) {
1093 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1094 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1095 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1096 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1101 for(int a=0;a<h;a+=4) {
1102 tmp[a]=Inv_Sbox1[tmp[a]];
1103 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1104 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1105 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1112 for(int a=0;a<h;a+=4) {
1113 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1114 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1115 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1116 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1121 for(int a=0;a<h;a+=4) {
1122 seq_out[ind2+a]=invfX[a];
1123 seq_out[ind2+a+1]=invfX[a+1];
1124 seq_out[ind2+a+2]=invfX[a+2];
1125 seq_out[ind2+a+3]=invfX[a+3];
1128 for(int a=0;a<h;a+=4) {
1129 seq_out[ind1+a]=invgY[a];
1130 seq_out[ind1+a+1]=invgY[a+1];
1131 seq_out[ind1+a+2]=invgY[a+2];
1132 seq_out[ind1+a+3]=invgY[a+3];
1134 for(int a=0;a<h;a+=4) {
1141 for(int a=0;a<h;a+=4) {
1164 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) {
1178 for(int a=0;a<h;a+=4) {
1186 for(int a=0;a<h;a+=4) {
1194 for(int it=0;it<len/2;it++) {
1195 int ind1=Pbox[it]*h;
1196 int ind2=Pbox[it+len/2]*h;
1198 RM1=&RM[PboxSRM[it]*h];
1199 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1202 for(int a=0;a<h;a+=4) {
1203 X[a]=seq_in[ind2+a];
1204 X[a+1]=seq_in[ind2+a+1];
1205 X[a+2]=seq_in[ind2+a+2];
1206 X[a+3]=seq_in[ind2+a+3];
1209 for(int a=0;a<h;a+=4) {
1210 Y[a]=seq_in[ind1+a];
1211 Y[a+1]=seq_in[ind1+a+1];
1212 Y[a+2]=seq_in[ind1+a+2];
1213 Y[a+3]=seq_in[ind1+a+3];
1217 for(int a=0;a<h;a+=4) {
1218 tmp[a]=X[a]^RM1[a]^IV1[a];
1219 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
1220 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
1221 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
1224 for(int a=0;a<h;a+=4) {
1225 tmp[a]=Sbox1[tmp[a]];
1226 tmp[a+1]=Sbox1[tmp[a+1]];
1227 tmp[a+2]=Sbox1[tmp[a+2]];
1228 tmp[a+3]=Sbox1[tmp[a+3]];
1232 /*for(int a=0;a<h;a+=4) {
1233 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
1234 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
1235 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
1236 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
1239 for(int a=0;a<h;a+=4) {
1240 fX[a]=Sbox2[tmp[a]^Y[a]];
1241 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
1242 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
1243 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
1247 for(int a=0;a<h;a+=4) {
1248 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1249 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1250 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1251 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1255 for(int a=0;a<h;a+=4) {
1256 tmp[a]=fX[a]^Y[a]^IV2[a];
1257 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
1258 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
1259 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
1263 for(int a=0;a<h;a+=4) {
1264 tmp[a]=Sbox2[tmp[a]];
1265 tmp[a+1]=Sbox2[tmp[a+1]];
1266 tmp[a+2]=Sbox2[tmp[a+2]];
1267 tmp[a+3]=Sbox2[tmp[a+3]];
1272 for(int a=0;a<h;a+=4) {
1273 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
1274 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
1275 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1276 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1281 for(int a=0;a<h;a+=4) {
1282 gY[a]=Sbox1[tmp[a]^RM2[a]];
1283 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1284 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1285 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1292 for(int a=0;a<h;a+=4) {
1293 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1294 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1295 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1296 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1301 for(int a=0;a<h;a+=4) {
1302 seq_out[ind2+a]=gY[a];
1303 seq_out[ind2+a+1]=gY[a+1];
1304 seq_out[ind2+a+2]=gY[a+2];
1305 seq_out[ind2+a+3]=gY[a+3];
1308 for(int a=0;a<h;a+=4) {
1309 seq_out[ind1+a]=fX[a];
1310 seq_out[ind1+a+1]=fX[a+1];
1311 seq_out[ind1+a+2]=fX[a+2];
1312 seq_out[ind1+a+3]=fX[a+3];
1314 for(int a=0;a<h;a+=4) {
1321 for(int a=0;a<h;a+=4) {
1343 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) {
1356 for(int a=0;a<h;a+=4) {
1364 for(int a=0;a<h;a+=4) {
1372 for(int it=0;it<len/2;it++) {
1373 int ind1=Pbox[it]*h;
1374 int ind2=Pbox[it+len/2]*h;
1377 RM1=&RM[PboxSRM[it]*h];
1378 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1383 for(int a=0;a<h;a+=4) {
1384 gY[a]=seq_in[ind2+a];
1385 gY[a+1]=seq_in[ind2+a+1];
1386 gY[a+2]=seq_in[ind2+a+2];
1387 gY[a+3]=seq_in[ind2+a+3];
1390 for(int a=0;a<h;a+=4) {
1391 fX[a]=seq_in[ind1+a];
1392 fX[a+1]=seq_in[ind1+a+1];
1393 fX[a+2]=seq_in[ind1+a+2];
1394 fX[a+3]=seq_in[ind1+a+3];
1398 for(int a=0;a<h;a+=4) {
1399 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1400 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1401 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1402 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1406 for(int a=0;a<h;a+=4) {
1407 tmp[a]=Inv_Sbox2[tmp[a]];
1408 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1409 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1410 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1415 for(int a=0;a<h;a+=4) {
1416 invgY[a]=tmp[a]^fX[a]^IV2[a];
1417 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1418 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1419 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1423 for(int a=0;a<h;a+=4) {
1424 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1425 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1426 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1427 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1432 for(int a=0;a<h;a+=4) {
1433 tmp[a]=Inv_Sbox1[tmp[a]];
1434 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1435 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1436 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1443 for(int a=0;a<h;a+=4) {
1444 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1445 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1446 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1447 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1452 for(int a=0;a<h;a+=4) {
1453 seq_out[ind2+a]=invfX[a];
1454 seq_out[ind2+a+1]=invfX[a+1];
1455 seq_out[ind2+a+2]=invfX[a+2];
1456 seq_out[ind2+a+3]=invfX[a+3];
1459 for(int a=0;a<h;a+=4) {
1460 seq_out[ind1+a]=invgY[a];
1461 seq_out[ind1+a+1]=invgY[a+1];
1462 seq_out[ind1+a+2]=invgY[a+2];
1463 seq_out[ind1+a+3]=invgY[a+3];
1465 for(int a=0;a<h;a+=4) {
1472 for(int a=0;a<h;a+=4) {
1494 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) {
1510 for(int a=0;a<h;a+=4) {
1511 myrand=xorshift32(myrand);
1522 for(int a=0;a<h;a+=4) {
1523 myrand=xorshift32(myrand);
1537 for(int it=0;it<len/2;it++) {
1538 int ind1=Pbox[it]*h;
1539 int ind2=Pbox[it+len/2]*h;
1543 RM1=&RM[PboxSRM[it]*h];
1544 RM2=&RM[h*h+PboxSRM[it]*h];
1547 for(int a=0;a<h;a+=4) {
1548 X[a]=seq_in[ind2+a];
1549 X[a+1]=seq_in[ind2+a+1];
1550 X[a+2]=seq_in[ind2+a+2];
1551 X[a+3]=seq_in[ind2+a+3];
1554 for(int a=0;a<h;a+=4) {
1555 Y[a]=seq_in[ind1+a];
1556 Y[a+1]=seq_in[ind1+a+1];
1557 Y[a+2]=seq_in[ind1+a+2];
1558 Y[a+3]=seq_in[ind1+a+3];
1562 for(int a=0;a<h;a+=4) {
1563 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1564 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1565 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1566 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1569 for(int a=0;a<h;a+=4) {
1570 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1571 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1572 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1573 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1577 for(int a=0;a<h;a+=4) {
1578 seq_out[ind2+a]=gY[a];
1579 seq_out[ind2+a+1]=gY[a+1];
1580 seq_out[ind2+a+2]=gY[a+2];
1581 seq_out[ind2+a+3]=gY[a+3];
1584 for(int a=0;a<h;a+=4) {
1585 seq_out[ind1+a]=fX[a];
1586 seq_out[ind1+a+1]=fX[a+1];
1587 seq_out[ind1+a+2]=fX[a+2];
1588 seq_out[ind1+a+3]=fX[a+3];
1590 for(int a=0;a<h;a+=4) {
1597 for(int a=0;a<h;a+=4) {
1619 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) {
1630 for(int a=0;a<h;a+=4) {
1631 myrand=xorshift32(myrand);
1642 for(int a=0;a<h;a+=4) {
1643 myrand=xorshift32(myrand);
1660 for(int it=0;it<len/2;it++) {
1661 int ind1=Pbox[it]*h;
1662 int ind2=Pbox[it+len/2]*h;
1665 RM1=&RM[PboxSRM[it]*h];
1666 RM2=&RM[h*h+PboxSRM[it]*h];
1669 for(int a=0;a<h;a+=4) {
1670 gY[a]=seq_in[ind2+a];
1671 gY[a+1]=seq_in[ind2+a+1];
1672 gY[a+2]=seq_in[ind2+a+2];
1673 gY[a+3]=seq_in[ind2+a+3];
1676 for(int a=0;a<h;a+=4) {
1677 fX[a]=seq_in[ind1+a];
1678 fX[a+1]=seq_in[ind1+a+1];
1679 fX[a+2]=seq_in[ind1+a+2];
1680 fX[a+3]=seq_in[ind1+a+3];
1684 for(int a=0;a<h;a+=4) {
1685 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
1686 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1687 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1688 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1692 for(int a=0;a<h;a+=4) {
1693 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
1694 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
1695 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
1696 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
1700 for(int a=0;a<h;a+=4) {
1701 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
1702 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1703 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1704 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1708 for(int a=0;a<h;a+=4) {
1709 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
1710 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
1711 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
1712 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
1717 for(int a=0;a<h;a+=4) {
1718 seq_out[ind2+a]=invfX[a];
1719 seq_out[ind2+a+1]=invfX[a+1];
1720 seq_out[ind2+a+2]=invfX[a+2];
1721 seq_out[ind2+a+3]=invfX[a+3];
1724 for(int a=0;a<h;a+=4) {
1725 seq_out[ind1+a]=invgY[a];
1726 seq_out[ind1+a+1]=invgY[a+1];
1727 seq_out[ind1+a+2]=invgY[a+2];
1728 seq_out[ind1+a+3]=invgY[a+3];
1730 for(int a=0;a<h;a+=4) {
1737 for(int a=0;a<h;a+=4) {
1755 int main(int argc, char** argv) {
1764 for(int i=1; i<argc; i++){
1765 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
1766 if(strncmp(argv[i],"cbcrm",5)==0) cbcrm=1;
1767 if(strncmp(argv[i],"cbcprng",7)==0) {cbcprng=1;cbcrm=0;}
1768 if(strncmp(argv[i],"ecbrm",5)==0) ecbrm = 1;
1769 if(strncmp(argv[i],"ecbprng",7)==0) {ecbprng=1; ecbrm=0;}
1770 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
1771 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
1772 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
1775 printf("nb times %d\n",nb_test);
1776 printf("cbcrm %d\n",cbcrm);
1777 printf("cbcprng %d\n",cbcprng);
1778 printf("ecbrm %d\n",ecbrm);
1779 printf("ecbprng %d\n",ecbprng);
1781 printf("lena %d\n",lena);
1782 printf("size_buf %d\n",size_buf);
1787 int seed=time(NULL);
1788 // cout<<seed<<endl;
1791 uchar Secretkey[key_size];
1793 uchar counter[key_size];
1795 for(int i=0;i<key_size;i++) {
1796 Secretkey[i]=lrand48()&0xFF;
1797 counter[i]=lrand48()&0xFF;
1810 uchar *data_R, *data_G, *data_B;
1819 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
1820 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
1821 imsize=width*height*3;
1822 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
1825 width=height=size_buf;
1826 imsize=width*height;
1827 buffer=new uchar[imsize];
1828 for(int i=0;i<imsize;i++) {
1829 buffer[i]=lrand48();
1837 uchar* seq= new uchar[imsize];
1838 uchar* seq2= new uchar[imsize];
1840 int oneD=width*height;
1842 for(int i=0;i<oneD;i++) {
1844 seq[oneD+i]=data_G[i];
1845 seq[2*oneD+i]=data_B[i];
1849 for(int i=0;i<oneD;i++) {
1858 int total_len=imsize;
1860 int len= total_len/h;
1864 uchar *mix=new uchar[256];
1869 for (int i = 0; i < 256 ; i++) {
1870 mix[i]=Secretkey[i]^counter[i];
1875 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
1876 // g_print("%s\n", sha512);
1886 // cout<<"hash "<<endl;
1887 for (int i = 0; i < 128 ; i++) {
1894 int *Pbox=new int[len];
1895 int *PboxSRM=new int[len/2];
1896 int *PboxSRM2=new int[len/2];
1899 uchar Inv_Sbox1[256];
1900 uchar Inv_Sbox2[256];
1902 uchar RM[h*h*2+256];
1908 double time_encrypt=0;
1909 double time_decrypt=0;
1912 double t=TimeStart();
1913 rc4key(DK, Sbox1, 8);
1916 rc4key(&DK[8], Sbox2, 8);
1918 rc4key(&DK[16], sc, 16);
1919 prga(sc, h*h*2+256, RM);
1925 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1928 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1930 for(int i=0;i<len/2;i++) {
1931 PboxSRM[i]=PboxSRM2[i]&(h-1);
1935 for(int i=0;i<h*2;i++) {
1936 for(int j=0;j<h;j++)
1937 cout<<(int)RM[i*h+j]<<" ";
1944 //time+=TimeStop(t);
1945 //cout<<"Time initializaton "<<time<<endl;
1950 for(int i=0;i<64;i++) {
1961 inverse_tables(Sbox1,256,Inv_Sbox1);
1962 inverse_tables(Sbox2,256,Inv_Sbox2);
1966 // lehmer64_seed(myrand);
1973 for(i=0;i<nb_test;i++)
1976 encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1978 encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1980 encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1982 encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1986 for(i=0;i<nb_test;i++)
1989 encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1991 encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1993 encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1995 encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1999 for(i=0;i<nb_test;i++)
2002 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2004 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2006 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2008 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2012 for(i=0;i<nb_test;i++)
2015 encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2017 encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2019 encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2021 encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2025 for(i=0;i<nb_test;i++)
2028 encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2030 encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2032 encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2034 encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2039 for(i=0;i<nb_test;i++)
2042 encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2044 encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2046 encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2048 encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2053 for(i=0;i<nb_test;i++)
2056 encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2058 encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2060 encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2062 encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2067 time_encrypt+=TimeStop(t);
2068 //cout<<"Time encrypt "<<
2069 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
2073 for(int i=0;i<oneD;i++) {
2075 data_G[i]=seq2[oneD+i];
2076 data_B[i]=seq2[2*oneD+i];
2078 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
2083 // lehmer64_seed(myrand);
2088 for(i=0;i<nb_test;i++) {
2090 decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2092 decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2094 decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2096 decrypt_ecb_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2100 for(i=0;i<nb_test;i++) {
2102 decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2104 decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2106 decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2108 decrypt_ecb_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2112 for(i=0;i<nb_test;i++) {
2114 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2116 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2118 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2120 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2124 for(i=0;i<nb_test;i++) {
2126 decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2128 decrypt_cbc_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2130 decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2132 decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2136 for(i=0;i<nb_test;i++) {
2138 decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2140 decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2142 decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2144 decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2148 for(i=0;i<nb_test;i++) {
2150 decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2152 decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2154 decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2156 decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2160 for(i=0;i<nb_test;i++) {
2162 decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2164 decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2166 decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2168 decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2173 time_decrypt+=TimeStop(t);
2174 //cout<<"Time decrypt "
2175 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
2178 for(int i=0;i<oneD;i++) {
2180 data_G[i]=seq[oneD+i];
2181 data_B[i]=seq[2*oneD+i];
2183 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
2187 for(int i=0;i<imsize;i++) {
2188 //cout<<(int)buffer[i]<<endl;
2189 if(buffer[i]!=seq[i]) {
2193 // cout<<"RESULT CORRECT: "<<equal<<endl;