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;
41 typedef __uint64_t ulong;
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;
208 for(int it=0;it<len/2;it++) {
210 int ind2=Pbox[it+len/2]*h;
213 for(int a=0;a<(h>>3);a++) {
222 for(int a=0;a<h;a+=4) {
224 X[a+1]=seq_in[ind2+a+1];
225 X[a+2]=seq_in[ind2+a+2];
226 X[a+3]=seq_in[ind2+a+3];
229 for(int a=0;a<h;a+=4) {
231 Y[a+1]=seq_in[ind1+a+1];
232 Y[a+2]=seq_in[ind1+a+2];
233 Y[a+3]=seq_in[ind1+a+3];
237 for(int a=0;a<h;a+=4) {
238 tmp[a]=Sbox1[X[a]^RM1[a]];
239 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
240 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
241 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
244 for(int a=0;a<h;a+=4) {
245 fX[a]=Sbox2[tmp[a]^Y[a]];
246 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
247 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
248 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
252 /*for(int a=0;a<h;a+=4) {
253 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
254 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
255 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
256 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
262 for(int a=0;a<h;a+=4) {
263 tmp[a]=Sbox2[fX[a]^Y[a]];
264 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
265 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
266 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
269 for(int a=0;a<h;a+=4) {
270 gY[a]=Sbox1[tmp[a]^RM2[a]];
271 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
272 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
273 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
278 /* for(int a=0;a<h;a+=4) {
279 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
280 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
281 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
282 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
291 for(int a=0;a<h;a+=4) {
292 seq_out[ind2+a]=gY[a];
293 seq_out[ind2+a+1]=gY[a+1];
294 seq_out[ind2+a+2]=gY[a+2];
295 seq_out[ind2+a+3]=gY[a+3];
298 for(int a=0;a<h;a+=4) {
299 seq_out[ind1+a]=fX[a];
300 seq_out[ind1+a+1]=fX[a+1];
301 seq_out[ind1+a+2]=fX[a+2];
302 seq_out[ind1+a+3]=fX[a+3];
322 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) {
331 ulong *rm1=(ulong*)RM1;
332 ulong *rm2=(ulong*)RM2;
335 for(int it=0;it<len/2;it++) {
337 int ind2=Pbox[it+len/2]*h;
340 for(int a=0;a<(h>>3);a++) {
349 for(int a=0;a<h;a+=4) {
350 gY[a]=seq_in[ind2+a];
351 gY[a+1]=seq_in[ind2+a+1];
352 gY[a+2]=seq_in[ind2+a+2];
353 gY[a+3]=seq_in[ind2+a+3];
356 for(int a=0;a<h;a+=4) {
357 fX[a]=seq_in[ind1+a];
358 fX[a+1]=seq_in[ind1+a+1];
359 fX[a+2]=seq_in[ind1+a+2];
360 fX[a+3]=seq_in[ind1+a+3];
366 for(int a=0;a<h;a+=4) {
367 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
368 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
369 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
370 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
374 for(int a=0;a<h;a+=4) {
375 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
376 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
377 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
378 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
383 /* for(int a=0;a<h;a+=4) {
384 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
385 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
386 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
387 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
393 for(int a=0;a<h;a+=4) {
394 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
395 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
396 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
397 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
401 for(int a=0;a<h;a+=4) {
402 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
403 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
404 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
405 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
411 for(int a=0;a<h;a+=4) {
412 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
413 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
414 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
415 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
420 for(int a=0;a<h;a+=4) {
421 seq_out[ind2+a]=invfX[a];
422 seq_out[ind2+a+1]=invfX[a+1];
423 seq_out[ind2+a+2]=invfX[a+2];
424 seq_out[ind2+a+3]=invfX[a+3];
427 for(int a=0;a<h;a+=4) {
428 seq_out[ind1+a]=invgY[a];
429 seq_out[ind1+a+1]=invgY[a+1];
430 seq_out[ind1+a+2]=invgY[a+2];
431 seq_out[ind1+a+3]=invgY[a+3];
449 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) {
458 ulong *rm1=(ulong*)RM1;
459 ulong *rm2=(ulong*)RM2;
461 for(int it=0;it<len/2;it++) {
463 int ind2=Pbox[it+len/2]*h;
466 for(int a=0;a<(h>>3);a++) {
475 for(int a=0;a<h;a+=4) {
477 X[a+1]=seq_in[ind2+a+1];
478 X[a+2]=seq_in[ind2+a+2];
479 X[a+3]=seq_in[ind2+a+3];
482 for(int a=0;a<h;a+=4) {
484 Y[a+1]=seq_in[ind1+a+1];
485 Y[a+2]=seq_in[ind1+a+2];
486 Y[a+3]=seq_in[ind1+a+3];
490 for(int a=0;a<h;a+=4) {
491 tmp[a]=Sbox1[X[a]^RM1[a]];
492 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
493 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
494 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
497 for(int a=0;a<h;a+=4) {
498 fX[a]=Sbox2[tmp[a]^Y[a]];
499 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
500 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
501 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
505 /*for(int a=0;a<h;a+=4) {
506 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
507 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
508 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
509 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
515 for(int a=0;a<h;a+=4) {
516 tmp[a]=Sbox2[fX[a]^Y[a]];
517 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
518 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
519 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
522 for(int a=0;a<h;a+=4) {
523 gY[a]=Sbox1[tmp[a]^RM2[a]];
524 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
525 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
526 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
531 /* for(int a=0;a<h;a+=4) {
532 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
533 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
534 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
535 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
544 for(int a=0;a<h;a+=4) {
545 seq_out[ind2+a]=gY[a];
546 seq_out[ind2+a+1]=gY[a+1];
547 seq_out[ind2+a+2]=gY[a+2];
548 seq_out[ind2+a+3]=gY[a+3];
551 for(int a=0;a<h;a+=4) {
552 seq_out[ind1+a]=fX[a];
553 seq_out[ind1+a+1]=fX[a+1];
554 seq_out[ind1+a+2]=fX[a+2];
555 seq_out[ind1+a+3]=fX[a+3];
575 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) {
584 ulong *rm1=(ulong*)RM1;
585 ulong *rm2=(ulong*)RM2;
587 for(int it=0;it<len/2;it++) {
589 int ind2=Pbox[it+len/2]*h;
592 for(int a=0;a<(h>>3);a++) {
601 for(int a=0;a<h;a+=4) {
602 gY[a]=seq_in[ind2+a];
603 gY[a+1]=seq_in[ind2+a+1];
604 gY[a+2]=seq_in[ind2+a+2];
605 gY[a+3]=seq_in[ind2+a+3];
608 for(int a=0;a<h;a+=4) {
609 fX[a]=seq_in[ind1+a];
610 fX[a+1]=seq_in[ind1+a+1];
611 fX[a+2]=seq_in[ind1+a+2];
612 fX[a+3]=seq_in[ind1+a+3];
618 for(int a=0;a<h;a+=4) {
619 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
620 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
621 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
622 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
626 for(int a=0;a<h;a+=4) {
627 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
628 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
629 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
630 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
635 /* for(int a=0;a<h;a+=4) {
636 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
637 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
638 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
639 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
645 for(int a=0;a<h;a+=4) {
646 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
647 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
648 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
649 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
653 for(int a=0;a<h;a+=4) {
654 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
655 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
656 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
657 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
663 for(int a=0;a<h;a+=4) {
664 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
665 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
666 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
667 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
672 for(int a=0;a<h;a+=4) {
673 seq_out[ind2+a]=invfX[a];
674 seq_out[ind2+a+1]=invfX[a+1];
675 seq_out[ind2+a+2]=invfX[a+2];
676 seq_out[ind2+a+3]=invfX[a+3];
679 for(int a=0;a<h;a+=4) {
680 seq_out[ind1+a]=invgY[a];
681 seq_out[ind1+a+1]=invgY[a+1];
682 seq_out[ind1+a+2]=invgY[a+2];
683 seq_out[ind1+a+3]=invgY[a+3];
700 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) {
709 for(int it=0;it<len/2;it++) {
711 int ind2=Pbox[it+len/2]*h;
715 RM1=&RM[PboxSRM[it]*h];
716 RM2=&RM[h*h+PboxSRM[it]*h];
719 for(int a=0;a<h;a+=4) {
721 X[a+1]=seq_in[ind2+a+1];
722 X[a+2]=seq_in[ind2+a+2];
723 X[a+3]=seq_in[ind2+a+3];
726 for(int a=0;a<h;a+=4) {
728 Y[a+1]=seq_in[ind1+a+1];
729 Y[a+2]=seq_in[ind1+a+2];
730 Y[a+3]=seq_in[ind1+a+3];
734 for(int a=0;a<h;a+=4) {
735 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
736 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
737 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
738 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
741 for(int a=0;a<h;a+=4) {
742 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
743 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
744 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
745 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
749 for(int a=0;a<h;a+=4) {
750 seq_out[ind2+a]=gY[a];
751 seq_out[ind2+a+1]=gY[a+1];
752 seq_out[ind2+a+2]=gY[a+2];
753 seq_out[ind2+a+3]=gY[a+3];
756 for(int a=0;a<h;a+=4) {
757 seq_out[ind1+a]=fX[a];
758 seq_out[ind1+a+1]=fX[a+1];
759 seq_out[ind1+a+2]=fX[a+2];
760 seq_out[ind1+a+3]=fX[a+3];
780 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) {
789 for(int it=0;it<len/2;it++) {
791 int ind2=Pbox[it+len/2]*h;
794 RM1=&RM[PboxSRM[it]*h];
795 RM2=&RM[h*h+PboxSRM[it]*h];
798 for(int a=0;a<h;a+=4) {
799 gY[a]=seq_in[ind2+a];
800 gY[a+1]=seq_in[ind2+a+1];
801 gY[a+2]=seq_in[ind2+a+2];
802 gY[a+3]=seq_in[ind2+a+3];
805 for(int a=0;a<h;a+=4) {
806 fX[a]=seq_in[ind1+a];
807 fX[a+1]=seq_in[ind1+a+1];
808 fX[a+2]=seq_in[ind1+a+2];
809 fX[a+3]=seq_in[ind1+a+3];
812 for(int a=0;a<h;a+=4) {
813 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
814 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
815 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
816 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
821 for(int a=0;a<h;a+=4) {
822 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
823 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
824 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
825 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
830 for(int a=0;a<h;a+=4) {
831 seq_out[ind2+a]=invfX[a];
832 seq_out[ind2+a+1]=invfX[a+1];
833 seq_out[ind2+a+2]=invfX[a+2];
834 seq_out[ind2+a+3]=invfX[a+3];
837 for(int a=0;a<h;a+=4) {
838 seq_out[ind1+a]=invgY[a];
839 seq_out[ind1+a+1]=invgY[a+1];
840 seq_out[ind1+a+2]=invgY[a+2];
841 seq_out[ind1+a+3]=invgY[a+3];
860 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) {
871 ulong *rm1=(ulong*)RM1;
872 ulong *rm2=(ulong*)RM2;
877 for(int it=0;it<len/2;it++) {
879 int ind2=Pbox[it+len/2]*h;
882 for(int a=0;a<(h>>3);a++) {
891 for(int a=0;a<h;a+=4) {
893 X[a+1]=seq_in[ind2+a+1];
894 X[a+2]=seq_in[ind2+a+2];
895 X[a+3]=seq_in[ind2+a+3];
898 for(int a=0;a<h;a+=4) {
900 Y[a+1]=seq_in[ind1+a+1];
901 Y[a+2]=seq_in[ind1+a+2];
902 Y[a+3]=seq_in[ind1+a+3];
906 for(int a=0;a<h;a+=4) {
907 tmp[a]=X[a]^RM1[a]^IV1[a];
908 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
909 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
910 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
913 for(int a=0;a<h;a+=4) {
914 tmp[a]=Sbox1[tmp[a]];
915 tmp[a+1]=Sbox1[tmp[a+1]];
916 tmp[a+2]=Sbox1[tmp[a+2]];
917 tmp[a+3]=Sbox1[tmp[a+3]];
921 /*for(int a=0;a<h;a+=4) {
922 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
923 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
924 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
925 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
928 for(int a=0;a<h;a+=4) {
929 fX[a]=Sbox2[tmp[a]^Y[a]];
930 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
931 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
932 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
936 for(int a=0;a<h;a+=4) {
937 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
938 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
939 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
940 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
944 for(int a=0;a<h;a+=4) {
945 tmp[a]=fX[a]^Y[a]^IV2[a];
946 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
947 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
948 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
952 for(int a=0;a<h;a+=4) {
953 tmp[a]=Sbox2[tmp[a]];
954 tmp[a+1]=Sbox2[tmp[a+1]];
955 tmp[a+2]=Sbox2[tmp[a+2]];
956 tmp[a+3]=Sbox2[tmp[a+3]];
961 for(int a=0;a<h;a+=4) {
962 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
963 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
964 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
965 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
970 for(int a=0;a<h;a+=4) {
971 gY[a]=Sbox1[tmp[a]^RM2[a]];
972 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
973 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
974 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
981 for(int a=0;a<h;a+=4) {
982 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
983 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
984 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
985 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
990 for(int a=0;a<h;a+=4) {
991 seq_out[ind2+a]=gY[a];
992 seq_out[ind2+a+1]=gY[a+1];
993 seq_out[ind2+a+2]=gY[a+2];
994 seq_out[ind2+a+3]=gY[a+3];
997 for(int a=0;a<h;a+=4) {
998 seq_out[ind1+a]=fX[a];
999 seq_out[ind1+a+1]=fX[a+1];
1000 seq_out[ind1+a+2]=fX[a+2];
1001 seq_out[ind1+a+3]=fX[a+3];
1003 for(int a=0;a<h;a+=4) {
1010 for(int a=0;a<h;a+=4) {
1032 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) {
1045 ulong *rm1=(ulong*)RM1;
1046 ulong *rm2=(ulong*)RM2;
1048 for(int it=0;it<len/2;it++) {
1049 int ind1=Pbox[it]*h;
1050 int ind2=Pbox[it+len/2]*h;
1053 for(int a=0;a<(h>>3);a++) {
1054 myrand=xorshift64();
1056 myrand=xorshift64();
1060 for(int a=0;a<h;a+=4) {
1061 gY[a]=seq_in[ind2+a];
1062 gY[a+1]=seq_in[ind2+a+1];
1063 gY[a+2]=seq_in[ind2+a+2];
1064 gY[a+3]=seq_in[ind2+a+3];
1067 for(int a=0;a<h;a+=4) {
1068 fX[a]=seq_in[ind1+a];
1069 fX[a+1]=seq_in[ind1+a+1];
1070 fX[a+2]=seq_in[ind1+a+2];
1071 fX[a+3]=seq_in[ind1+a+3];
1075 for(int a=0;a<h;a+=4) {
1076 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1077 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1078 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1079 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1083 for(int a=0;a<h;a+=4) {
1084 tmp[a]=Inv_Sbox2[tmp[a]];
1085 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1086 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1087 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1092 for(int a=0;a<h;a+=4) {
1093 invgY[a]=tmp[a]^fX[a]^IV2[a];
1094 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1095 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1096 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1100 for(int a=0;a<h;a+=4) {
1101 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1102 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1103 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1104 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1109 for(int a=0;a<h;a+=4) {
1110 tmp[a]=Inv_Sbox1[tmp[a]];
1111 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1112 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1113 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1120 for(int a=0;a<h;a+=4) {
1121 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1122 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1123 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1124 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1129 for(int a=0;a<h;a+=4) {
1130 seq_out[ind2+a]=invfX[a];
1131 seq_out[ind2+a+1]=invfX[a+1];
1132 seq_out[ind2+a+2]=invfX[a+2];
1133 seq_out[ind2+a+3]=invfX[a+3];
1136 for(int a=0;a<h;a+=4) {
1137 seq_out[ind1+a]=invgY[a];
1138 seq_out[ind1+a+1]=invgY[a+1];
1139 seq_out[ind1+a+2]=invgY[a+2];
1140 seq_out[ind1+a+3]=invgY[a+3];
1142 for(int a=0;a<h;a+=4) {
1149 for(int a=0;a<h;a+=4) {
1172 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) {
1186 for(int a=0;a<h;a+=4) {
1194 for(int a=0;a<h;a+=4) {
1202 for(int it=0;it<len/2;it++) {
1203 int ind1=Pbox[it]*h;
1204 int ind2=Pbox[it+len/2]*h;
1206 RM1=&RM[PboxSRM[it]*h];
1207 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1210 for(int a=0;a<h;a+=4) {
1211 X[a]=seq_in[ind2+a];
1212 X[a+1]=seq_in[ind2+a+1];
1213 X[a+2]=seq_in[ind2+a+2];
1214 X[a+3]=seq_in[ind2+a+3];
1217 for(int a=0;a<h;a+=4) {
1218 Y[a]=seq_in[ind1+a];
1219 Y[a+1]=seq_in[ind1+a+1];
1220 Y[a+2]=seq_in[ind1+a+2];
1221 Y[a+3]=seq_in[ind1+a+3];
1225 for(int a=0;a<h;a+=4) {
1226 tmp[a]=X[a]^RM1[a]^IV1[a];
1227 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
1228 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
1229 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
1232 for(int a=0;a<h;a+=4) {
1233 tmp[a]=Sbox1[tmp[a]];
1234 tmp[a+1]=Sbox1[tmp[a+1]];
1235 tmp[a+2]=Sbox1[tmp[a+2]];
1236 tmp[a+3]=Sbox1[tmp[a+3]];
1240 /*for(int a=0;a<h;a+=4) {
1241 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
1242 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
1243 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
1244 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
1247 for(int a=0;a<h;a+=4) {
1248 fX[a]=Sbox2[tmp[a]^Y[a]];
1249 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
1250 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
1251 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
1255 for(int a=0;a<h;a+=4) {
1256 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1257 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1258 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1259 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1263 for(int a=0;a<h;a+=4) {
1264 tmp[a]=fX[a]^Y[a]^IV2[a];
1265 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
1266 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
1267 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
1271 for(int a=0;a<h;a+=4) {
1272 tmp[a]=Sbox2[tmp[a]];
1273 tmp[a+1]=Sbox2[tmp[a+1]];
1274 tmp[a+2]=Sbox2[tmp[a+2]];
1275 tmp[a+3]=Sbox2[tmp[a+3]];
1280 for(int a=0;a<h;a+=4) {
1281 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
1282 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
1283 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1284 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1289 for(int a=0;a<h;a+=4) {
1290 gY[a]=Sbox1[tmp[a]^RM2[a]];
1291 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1292 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1293 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1300 for(int a=0;a<h;a+=4) {
1301 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1302 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1303 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1304 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1309 for(int a=0;a<h;a+=4) {
1310 seq_out[ind2+a]=gY[a];
1311 seq_out[ind2+a+1]=gY[a+1];
1312 seq_out[ind2+a+2]=gY[a+2];
1313 seq_out[ind2+a+3]=gY[a+3];
1316 for(int a=0;a<h;a+=4) {
1317 seq_out[ind1+a]=fX[a];
1318 seq_out[ind1+a+1]=fX[a+1];
1319 seq_out[ind1+a+2]=fX[a+2];
1320 seq_out[ind1+a+3]=fX[a+3];
1322 for(int a=0;a<h;a+=4) {
1329 for(int a=0;a<h;a+=4) {
1351 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) {
1364 for(int a=0;a<h;a+=4) {
1372 for(int a=0;a<h;a+=4) {
1380 for(int it=0;it<len/2;it++) {
1381 int ind1=Pbox[it]*h;
1382 int ind2=Pbox[it+len/2]*h;
1385 RM1=&RM[PboxSRM[it]*h];
1386 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1391 for(int a=0;a<h;a+=4) {
1392 gY[a]=seq_in[ind2+a];
1393 gY[a+1]=seq_in[ind2+a+1];
1394 gY[a+2]=seq_in[ind2+a+2];
1395 gY[a+3]=seq_in[ind2+a+3];
1398 for(int a=0;a<h;a+=4) {
1399 fX[a]=seq_in[ind1+a];
1400 fX[a+1]=seq_in[ind1+a+1];
1401 fX[a+2]=seq_in[ind1+a+2];
1402 fX[a+3]=seq_in[ind1+a+3];
1406 for(int a=0;a<h;a+=4) {
1407 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1408 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1409 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1410 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1414 for(int a=0;a<h;a+=4) {
1415 tmp[a]=Inv_Sbox2[tmp[a]];
1416 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1417 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1418 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1423 for(int a=0;a<h;a+=4) {
1424 invgY[a]=tmp[a]^fX[a]^IV2[a];
1425 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1426 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1427 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1431 for(int a=0;a<h;a+=4) {
1432 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1433 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1434 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1435 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1440 for(int a=0;a<h;a+=4) {
1441 tmp[a]=Inv_Sbox1[tmp[a]];
1442 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1443 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1444 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1451 for(int a=0;a<h;a+=4) {
1452 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1453 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1454 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1455 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1460 for(int a=0;a<h;a+=4) {
1461 seq_out[ind2+a]=invfX[a];
1462 seq_out[ind2+a+1]=invfX[a+1];
1463 seq_out[ind2+a+2]=invfX[a+2];
1464 seq_out[ind2+a+3]=invfX[a+3];
1467 for(int a=0;a<h;a+=4) {
1468 seq_out[ind1+a]=invgY[a];
1469 seq_out[ind1+a+1]=invgY[a+1];
1470 seq_out[ind1+a+2]=invgY[a+2];
1471 seq_out[ind1+a+3]=invgY[a+3];
1473 for(int a=0;a<h;a+=4) {
1480 for(int a=0;a<h;a+=4) {
1502 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) {
1518 for(int a=0;a<h;a+=4) {
1519 myrand=xorshift32(myrand);
1530 for(int a=0;a<h;a+=4) {
1531 myrand=xorshift32(myrand);
1545 for(int it=0;it<len/2;it++) {
1546 int ind1=Pbox[it]*h;
1547 int ind2=Pbox[it+len/2]*h;
1551 RM1=&RM[PboxSRM[it]*h];
1552 RM2=&RM[h*h+PboxSRM[it]*h];
1555 for(int a=0;a<h;a+=4) {
1556 X[a]=seq_in[ind2+a];
1557 X[a+1]=seq_in[ind2+a+1];
1558 X[a+2]=seq_in[ind2+a+2];
1559 X[a+3]=seq_in[ind2+a+3];
1562 for(int a=0;a<h;a+=4) {
1563 Y[a]=seq_in[ind1+a];
1564 Y[a+1]=seq_in[ind1+a+1];
1565 Y[a+2]=seq_in[ind1+a+2];
1566 Y[a+3]=seq_in[ind1+a+3];
1570 for(int a=0;a<h;a+=4) {
1571 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1572 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1573 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1574 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1577 for(int a=0;a<h;a+=4) {
1578 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1579 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1580 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1581 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1585 for(int a=0;a<h;a+=4) {
1586 seq_out[ind2+a]=gY[a];
1587 seq_out[ind2+a+1]=gY[a+1];
1588 seq_out[ind2+a+2]=gY[a+2];
1589 seq_out[ind2+a+3]=gY[a+3];
1592 for(int a=0;a<h;a+=4) {
1593 seq_out[ind1+a]=fX[a];
1594 seq_out[ind1+a+1]=fX[a+1];
1595 seq_out[ind1+a+2]=fX[a+2];
1596 seq_out[ind1+a+3]=fX[a+3];
1598 for(int a=0;a<h;a+=4) {
1605 for(int a=0;a<h;a+=4) {
1627 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) {
1638 for(int a=0;a<h;a+=4) {
1639 myrand=xorshift32(myrand);
1650 for(int a=0;a<h;a+=4) {
1651 myrand=xorshift32(myrand);
1668 for(int it=0;it<len/2;it++) {
1669 int ind1=Pbox[it]*h;
1670 int ind2=Pbox[it+len/2]*h;
1673 RM1=&RM[PboxSRM[it]*h];
1674 RM2=&RM[h*h+PboxSRM[it]*h];
1677 for(int a=0;a<h;a+=4) {
1678 gY[a]=seq_in[ind2+a];
1679 gY[a+1]=seq_in[ind2+a+1];
1680 gY[a+2]=seq_in[ind2+a+2];
1681 gY[a+3]=seq_in[ind2+a+3];
1684 for(int a=0;a<h;a+=4) {
1685 fX[a]=seq_in[ind1+a];
1686 fX[a+1]=seq_in[ind1+a+1];
1687 fX[a+2]=seq_in[ind1+a+2];
1688 fX[a+3]=seq_in[ind1+a+3];
1692 for(int a=0;a<h;a+=4) {
1693 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
1694 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1695 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1696 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1700 for(int a=0;a<h;a+=4) {
1701 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
1702 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
1703 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
1704 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
1708 for(int a=0;a<h;a+=4) {
1709 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
1710 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1711 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1712 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1716 for(int a=0;a<h;a+=4) {
1717 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
1718 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
1719 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
1720 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
1725 for(int a=0;a<h;a+=4) {
1726 seq_out[ind2+a]=invfX[a];
1727 seq_out[ind2+a+1]=invfX[a+1];
1728 seq_out[ind2+a+2]=invfX[a+2];
1729 seq_out[ind2+a+3]=invfX[a+3];
1732 for(int a=0;a<h;a+=4) {
1733 seq_out[ind1+a]=invgY[a];
1734 seq_out[ind1+a+1]=invgY[a+1];
1735 seq_out[ind1+a+2]=invgY[a+2];
1736 seq_out[ind1+a+3]=invgY[a+3];
1738 for(int a=0;a<h;a+=4) {
1745 for(int a=0;a<h;a+=4) {
1763 int main(int argc, char** argv) {
1772 for(int i=1; i<argc; i++){
1773 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
1774 if(strncmp(argv[i],"cbcrm",5)==0) cbcrm=1;
1775 if(strncmp(argv[i],"cbcprng",7)==0) {cbcprng=1;cbcrm=0;}
1776 if(strncmp(argv[i],"ecbrm",5)==0) ecbrm = 1;
1777 if(strncmp(argv[i],"ecbprng",7)==0) {ecbprng=1; ecbrm=0;}
1778 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
1779 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
1780 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
1783 printf("nb times %d\n",nb_test);
1784 printf("cbcrm %d\n",cbcrm);
1785 printf("cbcprng %d\n",cbcprng);
1786 printf("ecbrm %d\n",ecbrm);
1787 printf("ecbprng %d\n",ecbprng);
1789 printf("lena %d\n",lena);
1790 printf("size_buf %d\n",size_buf);
1795 int seed=time(NULL);
1796 // cout<<seed<<endl;
1799 uchar Secretkey[key_size];
1801 uchar counter[key_size];
1803 for(int i=0;i<key_size;i++) {
1804 Secretkey[i]=lrand48()&0xFF;
1805 counter[i]=lrand48()&0xFF;
1818 uchar *data_R, *data_G, *data_B;
1827 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
1828 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
1829 imsize=width*height*3;
1830 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
1833 width=height=size_buf;
1834 imsize=width*height;
1835 buffer=new uchar[imsize];
1836 for(int i=0;i<imsize;i++) {
1837 buffer[i]=lrand48();
1845 uchar* seq= new uchar[imsize];
1846 uchar* seq2= new uchar[imsize];
1848 int oneD=width*height;
1850 for(int i=0;i<oneD;i++) {
1852 seq[oneD+i]=data_G[i];
1853 seq[2*oneD+i]=data_B[i];
1857 for(int i=0;i<oneD;i++) {
1866 int total_len=imsize;
1868 int len= total_len/h;
1872 uchar *mix=new uchar[256];
1877 for (int i = 0; i < 256 ; i++) {
1878 mix[i]=Secretkey[i]^counter[i];
1883 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
1884 // g_print("%s\n", sha512);
1894 // cout<<"hash "<<endl;
1895 for (int i = 0; i < 128 ; i++) {
1902 int *Pbox=new int[len];
1903 int *PboxSRM=new int[len/2];
1904 int *PboxSRM2=new int[len/2];
1907 uchar Inv_Sbox1[256];
1908 uchar Inv_Sbox2[256];
1910 uchar RM[h*h*2+256];
1916 double time_encrypt=0;
1917 double time_decrypt=0;
1920 double t=TimeStart();
1921 rc4key(DK, Sbox1, 8);
1924 rc4key(&DK[8], Sbox2, 8);
1926 rc4key(&DK[16], sc, 16);
1927 prga(sc, h*h*2+256, RM);
1933 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1936 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1938 for(int i=0;i<len/2;i++) {
1939 PboxSRM[i]=PboxSRM2[i]&(h-1);
1943 for(int i=0;i<h*2;i++) {
1944 for(int j=0;j<h;j++)
1945 cout<<(int)RM[i*h+j]<<" ";
1952 //time+=TimeStop(t);
1953 //cout<<"Time initializaton "<<time<<endl;
1958 for(int i=0;i<64;i++) {
1969 inverse_tables(Sbox1,256,Inv_Sbox1);
1970 inverse_tables(Sbox2,256,Inv_Sbox2);
1974 // lehmer64_seed(myrand);
1981 for(i=0;i<nb_test;i++)
1984 encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1986 encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1988 encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1990 encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1994 for(i=0;i<nb_test;i++)
1997 encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1999 encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2001 encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2003 encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2007 for(i=0;i<nb_test;i++)
2010 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2012 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2014 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2016 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2020 for(i=0;i<nb_test;i++)
2023 encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2025 encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2027 encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2029 encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2033 for(i=0;i<nb_test;i++)
2036 encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2038 encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2040 encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2042 encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2047 for(i=0;i<nb_test;i++)
2050 encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2052 encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2054 encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2056 encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2061 for(i=0;i<nb_test;i++)
2064 encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2066 encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2068 encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2070 encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2075 time_encrypt+=TimeStop(t);
2076 //cout<<"Time encrypt "<<
2077 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
2081 for(int i=0;i<oneD;i++) {
2083 data_G[i]=seq2[oneD+i];
2084 data_B[i]=seq2[2*oneD+i];
2086 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
2091 // lehmer64_seed(myrand);
2096 for(i=0;i<nb_test;i++) {
2098 decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2100 decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2102 decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2104 decrypt_ecb_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2108 for(i=0;i<nb_test;i++) {
2110 decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2112 decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2114 decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2116 decrypt_ecb_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2120 for(i=0;i<nb_test;i++) {
2122 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2124 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2126 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2128 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2132 for(i=0;i<nb_test;i++) {
2134 decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2136 decrypt_cbc_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2138 decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2140 decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2144 for(i=0;i<nb_test;i++) {
2146 decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2148 decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2150 decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2152 decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2156 for(i=0;i<nb_test;i++) {
2158 decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2160 decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2162 decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2164 decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2168 for(i=0;i<nb_test;i++) {
2170 decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2172 decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2174 decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2176 decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2181 time_decrypt+=TimeStop(t);
2182 //cout<<"Time decrypt "
2183 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
2186 for(int i=0;i<oneD;i++) {
2188 data_G[i]=seq[oneD+i];
2189 data_B[i]=seq[2*oneD+i];
2191 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
2195 for(int i=0;i<imsize;i++) {
2196 //cout<<(int)buffer[i]<<endl;
2197 if(buffer[i]!=seq[i]) {
2201 // cout<<"RESULT CORRECT: "<<equal<<endl;