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" */
76 ulong xorshift64(ulong t)
78 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
88 __uint128_t g_lehmer64_state;
90 inline uint64_t splitmix64_stateless(uint64_t index) {
91 uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
92 z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
93 z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
98 inline void lehmer64_seed(uint64_t seed) {
99 g_lehmer64_state = (((__uint128_t)splitmix64_stateless(seed)) << 64) +
100 splitmix64_stateless(seed + 1);
103 inline uint64_t lehmer64() {
104 g_lehmer64_state *= UINT64_C(0xda942042e4dd58b5);
106 return g_lehmer64_state >> 64;
113 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
115 for(int i=0;i<size_tab;i++) {
116 inv_perm_tabs[tab[i]] = i;
121 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
123 for(int i=0;i<size_tab;i++) {
124 inv_perm_tabs[tab[i]] = i;
131 void rc4key(uchar *key, uchar *sc, int size_DK) {
133 for(int i=0;i<256;i++) {
139 for(int i0=0; i0<256; i0++) {
140 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
149 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
155 for (int i=0;i<len;i++) {
158 for (int it = 0; it < rp; it++) {
160 for(int i0 = 0; i0<len; i0++) {
161 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
170 void prga(uchar *sc, int ldata, uchar *r) {
174 for (int it=0; it<ldata; it++) {
176 j0 = (j0 + sc[i0])&0xFF;
180 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
190 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) {
199 ulong *rm1=(ulong*)RM1;
200 ulong *rm2=(ulong*)RM2;
202 for(int it=0;it<len/2;it++) {
204 int ind2=Pbox[it+len/2]*h;
207 for(int a=0;a<(h>>3);a++) {
216 for(int a=0;a<h;a+=4) {
218 X[a+1]=seq_in[ind2+a+1];
219 X[a+2]=seq_in[ind2+a+2];
220 X[a+3]=seq_in[ind2+a+3];
223 for(int a=0;a<h;a+=4) {
225 Y[a+1]=seq_in[ind1+a+1];
226 Y[a+2]=seq_in[ind1+a+2];
227 Y[a+3]=seq_in[ind1+a+3];
231 for(int a=0;a<h;a+=4) {
232 tmp[a]=Sbox1[X[a]^RM1[a]];
233 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
234 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
235 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
238 for(int a=0;a<h;a+=4) {
239 fX[a]=Sbox2[tmp[a]^Y[a]];
240 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
241 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
242 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
246 /*for(int a=0;a<h;a+=4) {
247 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
248 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
249 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
250 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
256 for(int a=0;a<h;a+=4) {
257 tmp[a]=Sbox2[fX[a]^Y[a]];
258 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
259 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
260 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
263 for(int a=0;a<h;a+=4) {
264 gY[a]=Sbox1[tmp[a]^RM2[a]];
265 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
266 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
267 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
272 /* for(int a=0;a<h;a+=4) {
273 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
274 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
275 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
276 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
285 for(int a=0;a<h;a+=4) {
286 seq_out[ind2+a]=gY[a];
287 seq_out[ind2+a+1]=gY[a+1];
288 seq_out[ind2+a+2]=gY[a+2];
289 seq_out[ind2+a+3]=gY[a+3];
292 for(int a=0;a<h;a+=4) {
293 seq_out[ind1+a]=fX[a];
294 seq_out[ind1+a+1]=fX[a+1];
295 seq_out[ind1+a+2]=fX[a+2];
296 seq_out[ind1+a+3]=fX[a+3];
316 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) {
325 ulong *rm1=(ulong*)RM1;
326 ulong *rm2=(ulong*)RM2;
328 for(int it=0;it<len/2;it++) {
330 int ind2=Pbox[it+len/2]*h;
333 for(int a=0;a<(h>>3);a++) {
342 for(int a=0;a<h;a+=4) {
343 gY[a]=seq_in[ind2+a];
344 gY[a+1]=seq_in[ind2+a+1];
345 gY[a+2]=seq_in[ind2+a+2];
346 gY[a+3]=seq_in[ind2+a+3];
349 for(int a=0;a<h;a+=4) {
350 fX[a]=seq_in[ind1+a];
351 fX[a+1]=seq_in[ind1+a+1];
352 fX[a+2]=seq_in[ind1+a+2];
353 fX[a+3]=seq_in[ind1+a+3];
359 for(int a=0;a<h;a+=4) {
360 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
361 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
362 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
363 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
367 for(int a=0;a<h;a+=4) {
368 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
369 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
370 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
371 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
376 /* for(int a=0;a<h;a+=4) {
377 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
378 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
379 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
380 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
386 for(int a=0;a<h;a+=4) {
387 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
388 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
389 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
390 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
394 for(int a=0;a<h;a+=4) {
395 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
396 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
397 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
398 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
404 for(int a=0;a<h;a+=4) {
405 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
406 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
407 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
408 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
413 for(int a=0;a<h;a+=4) {
414 seq_out[ind2+a]=invfX[a];
415 seq_out[ind2+a+1]=invfX[a+1];
416 seq_out[ind2+a+2]=invfX[a+2];
417 seq_out[ind2+a+3]=invfX[a+3];
420 for(int a=0;a<h;a+=4) {
421 seq_out[ind1+a]=invgY[a];
422 seq_out[ind1+a+1]=invgY[a+1];
423 seq_out[ind1+a+2]=invgY[a+2];
424 seq_out[ind1+a+3]=invgY[a+3];
442 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) {
451 ulong *rm1=(ulong*)RM1;
452 ulong *rm2=(ulong*)RM2;
454 for(int it=0;it<len/2;it++) {
456 int ind2=Pbox[it+len/2]*h;
459 for(int a=0;a<(h>>3);a++) {
468 for(int a=0;a<h;a+=4) {
470 X[a+1]=seq_in[ind2+a+1];
471 X[a+2]=seq_in[ind2+a+2];
472 X[a+3]=seq_in[ind2+a+3];
475 for(int a=0;a<h;a+=4) {
477 Y[a+1]=seq_in[ind1+a+1];
478 Y[a+2]=seq_in[ind1+a+2];
479 Y[a+3]=seq_in[ind1+a+3];
483 for(int a=0;a<h;a+=4) {
484 tmp[a]=Sbox1[X[a]^RM1[a]];
485 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
486 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
487 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
490 for(int a=0;a<h;a+=4) {
491 fX[a]=Sbox2[tmp[a]^Y[a]];
492 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
493 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
494 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
498 /*for(int a=0;a<h;a+=4) {
499 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
500 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
501 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
502 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
508 for(int a=0;a<h;a+=4) {
509 tmp[a]=Sbox2[fX[a]^Y[a]];
510 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
511 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
512 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
515 for(int a=0;a<h;a+=4) {
516 gY[a]=Sbox1[tmp[a]^RM2[a]];
517 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
518 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
519 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
524 /* for(int a=0;a<h;a+=4) {
525 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
526 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
527 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
528 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
537 for(int a=0;a<h;a+=4) {
538 seq_out[ind2+a]=gY[a];
539 seq_out[ind2+a+1]=gY[a+1];
540 seq_out[ind2+a+2]=gY[a+2];
541 seq_out[ind2+a+3]=gY[a+3];
544 for(int a=0;a<h;a+=4) {
545 seq_out[ind1+a]=fX[a];
546 seq_out[ind1+a+1]=fX[a+1];
547 seq_out[ind1+a+2]=fX[a+2];
548 seq_out[ind1+a+3]=fX[a+3];
568 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) {
577 ulong *rm1=(ulong*)RM1;
578 ulong *rm2=(ulong*)RM2;
580 for(int it=0;it<len/2;it++) {
582 int ind2=Pbox[it+len/2]*h;
585 for(int a=0;a<(h>>3);a++) {
594 for(int a=0;a<h;a+=4) {
595 gY[a]=seq_in[ind2+a];
596 gY[a+1]=seq_in[ind2+a+1];
597 gY[a+2]=seq_in[ind2+a+2];
598 gY[a+3]=seq_in[ind2+a+3];
601 for(int a=0;a<h;a+=4) {
602 fX[a]=seq_in[ind1+a];
603 fX[a+1]=seq_in[ind1+a+1];
604 fX[a+2]=seq_in[ind1+a+2];
605 fX[a+3]=seq_in[ind1+a+3];
611 for(int a=0;a<h;a+=4) {
612 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
613 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
614 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
615 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
619 for(int a=0;a<h;a+=4) {
620 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
621 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
622 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
623 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
628 /* for(int a=0;a<h;a+=4) {
629 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
630 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
631 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
632 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
638 for(int a=0;a<h;a+=4) {
639 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
640 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
641 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
642 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
646 for(int a=0;a<h;a+=4) {
647 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
648 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
649 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
650 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
656 for(int a=0;a<h;a+=4) {
657 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
658 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
659 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
660 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
665 for(int a=0;a<h;a+=4) {
666 seq_out[ind2+a]=invfX[a];
667 seq_out[ind2+a+1]=invfX[a+1];
668 seq_out[ind2+a+2]=invfX[a+2];
669 seq_out[ind2+a+3]=invfX[a+3];
672 for(int a=0;a<h;a+=4) {
673 seq_out[ind1+a]=invgY[a];
674 seq_out[ind1+a+1]=invgY[a+1];
675 seq_out[ind1+a+2]=invgY[a+2];
676 seq_out[ind1+a+3]=invgY[a+3];
693 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) {
702 for(int it=0;it<len/2;it++) {
704 int ind2=Pbox[it+len/2]*h;
708 RM1=&RM[PboxSRM[it]*h];
709 RM2=&RM[h*h+PboxSRM[it]*h];
712 for(int a=0;a<h;a+=4) {
714 X[a+1]=seq_in[ind2+a+1];
715 X[a+2]=seq_in[ind2+a+2];
716 X[a+3]=seq_in[ind2+a+3];
719 for(int a=0;a<h;a+=4) {
721 Y[a+1]=seq_in[ind1+a+1];
722 Y[a+2]=seq_in[ind1+a+2];
723 Y[a+3]=seq_in[ind1+a+3];
727 for(int a=0;a<h;a+=4) {
728 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
729 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
730 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
731 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
734 for(int a=0;a<h;a+=4) {
735 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
736 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
737 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
738 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
742 for(int a=0;a<h;a+=4) {
743 seq_out[ind2+a]=gY[a];
744 seq_out[ind2+a+1]=gY[a+1];
745 seq_out[ind2+a+2]=gY[a+2];
746 seq_out[ind2+a+3]=gY[a+3];
749 for(int a=0;a<h;a+=4) {
750 seq_out[ind1+a]=fX[a];
751 seq_out[ind1+a+1]=fX[a+1];
752 seq_out[ind1+a+2]=fX[a+2];
753 seq_out[ind1+a+3]=fX[a+3];
773 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) {
782 for(int it=0;it<len/2;it++) {
784 int ind2=Pbox[it+len/2]*h;
787 RM1=&RM[PboxSRM[it]*h];
788 RM2=&RM[h*h+PboxSRM[it]*h];
791 for(int a=0;a<h;a+=4) {
792 gY[a]=seq_in[ind2+a];
793 gY[a+1]=seq_in[ind2+a+1];
794 gY[a+2]=seq_in[ind2+a+2];
795 gY[a+3]=seq_in[ind2+a+3];
798 for(int a=0;a<h;a+=4) {
799 fX[a]=seq_in[ind1+a];
800 fX[a+1]=seq_in[ind1+a+1];
801 fX[a+2]=seq_in[ind1+a+2];
802 fX[a+3]=seq_in[ind1+a+3];
805 for(int a=0;a<h;a+=4) {
806 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
807 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
808 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
809 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
814 for(int a=0;a<h;a+=4) {
815 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
816 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
817 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
818 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
823 for(int a=0;a<h;a+=4) {
824 seq_out[ind2+a]=invfX[a];
825 seq_out[ind2+a+1]=invfX[a+1];
826 seq_out[ind2+a+2]=invfX[a+2];
827 seq_out[ind2+a+3]=invfX[a+3];
830 for(int a=0;a<h;a+=4) {
831 seq_out[ind1+a]=invgY[a];
832 seq_out[ind1+a+1]=invgY[a+1];
833 seq_out[ind1+a+2]=invgY[a+2];
834 seq_out[ind1+a+3]=invgY[a+3];
853 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) {
864 ulong *rm1=(ulong*)RM1;
865 ulong *rm2=(ulong*)RM2;
867 for(int it=0;it<len/2;it++) {
869 int ind2=Pbox[it+len/2]*h;
872 for(int a=0;a<(h>>3);a++) {
881 for(int a=0;a<h;a+=4) {
883 X[a+1]=seq_in[ind2+a+1];
884 X[a+2]=seq_in[ind2+a+2];
885 X[a+3]=seq_in[ind2+a+3];
888 for(int a=0;a<h;a+=4) {
890 Y[a+1]=seq_in[ind1+a+1];
891 Y[a+2]=seq_in[ind1+a+2];
892 Y[a+3]=seq_in[ind1+a+3];
896 for(int a=0;a<h;a+=4) {
897 tmp[a]=X[a]^RM1[a]^IV1[a];
898 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
899 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
900 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
903 for(int a=0;a<h;a+=4) {
904 tmp[a]=Sbox1[tmp[a]];
905 tmp[a+1]=Sbox1[tmp[a+1]];
906 tmp[a+2]=Sbox1[tmp[a+2]];
907 tmp[a+3]=Sbox1[tmp[a+3]];
911 /*for(int a=0;a<h;a+=4) {
912 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
913 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
914 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
915 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
918 for(int a=0;a<h;a+=4) {
919 fX[a]=Sbox2[tmp[a]^Y[a]];
920 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
921 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
922 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
926 for(int a=0;a<h;a+=4) {
927 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
928 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
929 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
930 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
934 for(int a=0;a<h;a+=4) {
935 tmp[a]=fX[a]^Y[a]^IV2[a];
936 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
937 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
938 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
942 for(int a=0;a<h;a+=4) {
943 tmp[a]=Sbox2[tmp[a]];
944 tmp[a+1]=Sbox2[tmp[a+1]];
945 tmp[a+2]=Sbox2[tmp[a+2]];
946 tmp[a+3]=Sbox2[tmp[a+3]];
951 for(int a=0;a<h;a+=4) {
952 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
953 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
954 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
955 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
960 for(int a=0;a<h;a+=4) {
961 gY[a]=Sbox1[tmp[a]^RM2[a]];
962 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
963 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
964 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
971 for(int a=0;a<h;a+=4) {
972 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
973 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
974 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
975 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
980 for(int a=0;a<h;a+=4) {
981 seq_out[ind2+a]=gY[a];
982 seq_out[ind2+a+1]=gY[a+1];
983 seq_out[ind2+a+2]=gY[a+2];
984 seq_out[ind2+a+3]=gY[a+3];
987 for(int a=0;a<h;a+=4) {
988 seq_out[ind1+a]=fX[a];
989 seq_out[ind1+a+1]=fX[a+1];
990 seq_out[ind1+a+2]=fX[a+2];
991 seq_out[ind1+a+3]=fX[a+3];
993 for(int a=0;a<h;a+=4) {
1000 for(int a=0;a<h;a+=4) {
1022 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) {
1033 ulong *rm1=(ulong*)RM1;
1034 ulong *rm2=(ulong*)RM2;
1036 for(int it=0;it<len/2;it++) {
1037 int ind1=Pbox[it]*h;
1038 int ind2=Pbox[it+len/2]*h;
1041 for(int a=0;a<(h>>3);a++) {
1048 for(int a=0;a<h;a+=4) {
1049 gY[a]=seq_in[ind2+a];
1050 gY[a+1]=seq_in[ind2+a+1];
1051 gY[a+2]=seq_in[ind2+a+2];
1052 gY[a+3]=seq_in[ind2+a+3];
1055 for(int a=0;a<h;a+=4) {
1056 fX[a]=seq_in[ind1+a];
1057 fX[a+1]=seq_in[ind1+a+1];
1058 fX[a+2]=seq_in[ind1+a+2];
1059 fX[a+3]=seq_in[ind1+a+3];
1063 for(int a=0;a<h;a+=4) {
1064 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1065 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1066 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1067 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1071 for(int a=0;a<h;a+=4) {
1072 tmp[a]=Inv_Sbox2[tmp[a]];
1073 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1074 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1075 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1080 for(int a=0;a<h;a+=4) {
1081 invgY[a]=tmp[a]^fX[a]^IV2[a];
1082 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1083 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1084 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1088 for(int a=0;a<h;a+=4) {
1089 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1090 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1091 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1092 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1097 for(int a=0;a<h;a+=4) {
1098 tmp[a]=Inv_Sbox1[tmp[a]];
1099 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1100 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1101 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1108 for(int a=0;a<h;a+=4) {
1109 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1110 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1111 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1112 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1117 for(int a=0;a<h;a+=4) {
1118 seq_out[ind2+a]=invfX[a];
1119 seq_out[ind2+a+1]=invfX[a+1];
1120 seq_out[ind2+a+2]=invfX[a+2];
1121 seq_out[ind2+a+3]=invfX[a+3];
1124 for(int a=0;a<h;a+=4) {
1125 seq_out[ind1+a]=invgY[a];
1126 seq_out[ind1+a+1]=invgY[a+1];
1127 seq_out[ind1+a+2]=invgY[a+2];
1128 seq_out[ind1+a+3]=invgY[a+3];
1130 for(int a=0;a<h;a+=4) {
1137 for(int a=0;a<h;a+=4) {
1160 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) {
1174 for(int a=0;a<h;a+=4) {
1182 for(int a=0;a<h;a+=4) {
1190 for(int it=0;it<len/2;it++) {
1191 int ind1=Pbox[it]*h;
1192 int ind2=Pbox[it+len/2]*h;
1194 RM1=&RM[PboxSRM[it]*h];
1195 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1198 for(int a=0;a<h;a+=4) {
1199 X[a]=seq_in[ind2+a];
1200 X[a+1]=seq_in[ind2+a+1];
1201 X[a+2]=seq_in[ind2+a+2];
1202 X[a+3]=seq_in[ind2+a+3];
1205 for(int a=0;a<h;a+=4) {
1206 Y[a]=seq_in[ind1+a];
1207 Y[a+1]=seq_in[ind1+a+1];
1208 Y[a+2]=seq_in[ind1+a+2];
1209 Y[a+3]=seq_in[ind1+a+3];
1213 for(int a=0;a<h;a+=4) {
1214 tmp[a]=X[a]^RM1[a]^IV1[a];
1215 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
1216 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
1217 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
1220 for(int a=0;a<h;a+=4) {
1221 tmp[a]=Sbox1[tmp[a]];
1222 tmp[a+1]=Sbox1[tmp[a+1]];
1223 tmp[a+2]=Sbox1[tmp[a+2]];
1224 tmp[a+3]=Sbox1[tmp[a+3]];
1228 /*for(int a=0;a<h;a+=4) {
1229 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
1230 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
1231 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
1232 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
1235 for(int a=0;a<h;a+=4) {
1236 fX[a]=Sbox2[tmp[a]^Y[a]];
1237 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
1238 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
1239 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
1243 for(int a=0;a<h;a+=4) {
1244 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1245 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1246 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1247 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1251 for(int a=0;a<h;a+=4) {
1252 tmp[a]=fX[a]^Y[a]^IV2[a];
1253 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
1254 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
1255 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
1259 for(int a=0;a<h;a+=4) {
1260 tmp[a]=Sbox2[tmp[a]];
1261 tmp[a+1]=Sbox2[tmp[a+1]];
1262 tmp[a+2]=Sbox2[tmp[a+2]];
1263 tmp[a+3]=Sbox2[tmp[a+3]];
1268 for(int a=0;a<h;a+=4) {
1269 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
1270 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
1271 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1272 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1277 for(int a=0;a<h;a+=4) {
1278 gY[a]=Sbox1[tmp[a]^RM2[a]];
1279 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1280 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1281 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1288 for(int a=0;a<h;a+=4) {
1289 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1290 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1291 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1292 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1297 for(int a=0;a<h;a+=4) {
1298 seq_out[ind2+a]=gY[a];
1299 seq_out[ind2+a+1]=gY[a+1];
1300 seq_out[ind2+a+2]=gY[a+2];
1301 seq_out[ind2+a+3]=gY[a+3];
1304 for(int a=0;a<h;a+=4) {
1305 seq_out[ind1+a]=fX[a];
1306 seq_out[ind1+a+1]=fX[a+1];
1307 seq_out[ind1+a+2]=fX[a+2];
1308 seq_out[ind1+a+3]=fX[a+3];
1310 for(int a=0;a<h;a+=4) {
1317 for(int a=0;a<h;a+=4) {
1339 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) {
1352 for(int a=0;a<h;a+=4) {
1360 for(int a=0;a<h;a+=4) {
1368 for(int it=0;it<len/2;it++) {
1369 int ind1=Pbox[it]*h;
1370 int ind2=Pbox[it+len/2]*h;
1373 RM1=&RM[PboxSRM[it]*h];
1374 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1379 for(int a=0;a<h;a+=4) {
1380 gY[a]=seq_in[ind2+a];
1381 gY[a+1]=seq_in[ind2+a+1];
1382 gY[a+2]=seq_in[ind2+a+2];
1383 gY[a+3]=seq_in[ind2+a+3];
1386 for(int a=0;a<h;a+=4) {
1387 fX[a]=seq_in[ind1+a];
1388 fX[a+1]=seq_in[ind1+a+1];
1389 fX[a+2]=seq_in[ind1+a+2];
1390 fX[a+3]=seq_in[ind1+a+3];
1394 for(int a=0;a<h;a+=4) {
1395 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1396 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1397 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1398 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1402 for(int a=0;a<h;a+=4) {
1403 tmp[a]=Inv_Sbox2[tmp[a]];
1404 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1405 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1406 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1411 for(int a=0;a<h;a+=4) {
1412 invgY[a]=tmp[a]^fX[a]^IV2[a];
1413 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1414 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1415 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1419 for(int a=0;a<h;a+=4) {
1420 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1421 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1422 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1423 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1428 for(int a=0;a<h;a+=4) {
1429 tmp[a]=Inv_Sbox1[tmp[a]];
1430 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1431 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1432 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1439 for(int a=0;a<h;a+=4) {
1440 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1441 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1442 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1443 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1448 for(int a=0;a<h;a+=4) {
1449 seq_out[ind2+a]=invfX[a];
1450 seq_out[ind2+a+1]=invfX[a+1];
1451 seq_out[ind2+a+2]=invfX[a+2];
1452 seq_out[ind2+a+3]=invfX[a+3];
1455 for(int a=0;a<h;a+=4) {
1456 seq_out[ind1+a]=invgY[a];
1457 seq_out[ind1+a+1]=invgY[a+1];
1458 seq_out[ind1+a+2]=invgY[a+2];
1459 seq_out[ind1+a+3]=invgY[a+3];
1461 for(int a=0;a<h;a+=4) {
1468 for(int a=0;a<h;a+=4) {
1490 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) {
1506 for(int a=0;a<h;a+=4) {
1507 myrand=xorshift32(myrand);
1518 for(int a=0;a<h;a+=4) {
1519 myrand=xorshift32(myrand);
1533 for(int it=0;it<len/2;it++) {
1534 int ind1=Pbox[it]*h;
1535 int ind2=Pbox[it+len/2]*h;
1539 RM1=&RM[PboxSRM[it]*h];
1540 RM2=&RM[h*h+PboxSRM[it]*h];
1543 for(int a=0;a<h;a+=4) {
1544 X[a]=seq_in[ind2+a];
1545 X[a+1]=seq_in[ind2+a+1];
1546 X[a+2]=seq_in[ind2+a+2];
1547 X[a+3]=seq_in[ind2+a+3];
1550 for(int a=0;a<h;a+=4) {
1551 Y[a]=seq_in[ind1+a];
1552 Y[a+1]=seq_in[ind1+a+1];
1553 Y[a+2]=seq_in[ind1+a+2];
1554 Y[a+3]=seq_in[ind1+a+3];
1558 for(int a=0;a<h;a+=4) {
1559 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1560 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1561 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1562 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1565 for(int a=0;a<h;a+=4) {
1566 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1567 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1568 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1569 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1573 for(int a=0;a<h;a+=4) {
1574 seq_out[ind2+a]=gY[a];
1575 seq_out[ind2+a+1]=gY[a+1];
1576 seq_out[ind2+a+2]=gY[a+2];
1577 seq_out[ind2+a+3]=gY[a+3];
1580 for(int a=0;a<h;a+=4) {
1581 seq_out[ind1+a]=fX[a];
1582 seq_out[ind1+a+1]=fX[a+1];
1583 seq_out[ind1+a+2]=fX[a+2];
1584 seq_out[ind1+a+3]=fX[a+3];
1586 for(int a=0;a<h;a+=4) {
1593 for(int a=0;a<h;a+=4) {
1615 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) {
1626 for(int a=0;a<h;a+=4) {
1627 myrand=xorshift32(myrand);
1638 for(int a=0;a<h;a+=4) {
1639 myrand=xorshift32(myrand);
1656 for(int it=0;it<len/2;it++) {
1657 int ind1=Pbox[it]*h;
1658 int ind2=Pbox[it+len/2]*h;
1661 RM1=&RM[PboxSRM[it]*h];
1662 RM2=&RM[h*h+PboxSRM[it]*h];
1665 for(int a=0;a<h;a+=4) {
1666 gY[a]=seq_in[ind2+a];
1667 gY[a+1]=seq_in[ind2+a+1];
1668 gY[a+2]=seq_in[ind2+a+2];
1669 gY[a+3]=seq_in[ind2+a+3];
1672 for(int a=0;a<h;a+=4) {
1673 fX[a]=seq_in[ind1+a];
1674 fX[a+1]=seq_in[ind1+a+1];
1675 fX[a+2]=seq_in[ind1+a+2];
1676 fX[a+3]=seq_in[ind1+a+3];
1680 for(int a=0;a<h;a+=4) {
1681 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
1682 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1683 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1684 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1688 for(int a=0;a<h;a+=4) {
1689 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
1690 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
1691 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
1692 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
1696 for(int a=0;a<h;a+=4) {
1697 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
1698 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1699 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1700 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1704 for(int a=0;a<h;a+=4) {
1705 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
1706 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
1707 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
1708 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
1713 for(int a=0;a<h;a+=4) {
1714 seq_out[ind2+a]=invfX[a];
1715 seq_out[ind2+a+1]=invfX[a+1];
1716 seq_out[ind2+a+2]=invfX[a+2];
1717 seq_out[ind2+a+3]=invfX[a+3];
1720 for(int a=0;a<h;a+=4) {
1721 seq_out[ind1+a]=invgY[a];
1722 seq_out[ind1+a+1]=invgY[a+1];
1723 seq_out[ind1+a+2]=invgY[a+2];
1724 seq_out[ind1+a+3]=invgY[a+3];
1726 for(int a=0;a<h;a+=4) {
1733 for(int a=0;a<h;a+=4) {
1751 int main(int argc, char** argv) {
1760 for(int i=1; i<argc; i++){
1761 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
1762 if(strncmp(argv[i],"cbcrm",5)==0) cbcrm=1;
1763 if(strncmp(argv[i],"cbcprng",7)==0) {cbcprng=1;cbcrm=0;}
1764 if(strncmp(argv[i],"ecbrm",5)==0) ecbrm = 1;
1765 if(strncmp(argv[i],"ecbprng",7)==0) {ecbprng=1; ecbrm=0;}
1766 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
1767 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
1768 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
1771 printf("nb times %d\n",nb_test);
1772 printf("cbcrm %d\n",cbcrm);
1773 printf("cbcprng %d\n",cbcprng);
1774 printf("ecbrm %d\n",ecbrm);
1775 printf("ecbprng %d\n",ecbprng);
1777 printf("lena %d\n",lena);
1778 printf("size_buf %d\n",size_buf);
1783 int seed=time(NULL);
1784 // cout<<seed<<endl;
1787 uchar Secretkey[key_size];
1789 uchar counter[key_size];
1791 for(int i=0;i<key_size;i++) {
1792 Secretkey[i]=lrand48()&0xFF;
1793 counter[i]=lrand48()&0xFF;
1806 uchar *data_R, *data_G, *data_B;
1815 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
1816 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
1817 imsize=width*height*3;
1818 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
1821 width=height=size_buf;
1822 imsize=width*height;
1823 buffer=new uchar[imsize];
1824 for(int i=0;i<imsize;i++) {
1825 buffer[i]=lrand48();
1833 uchar* seq= new uchar[imsize];
1834 uchar* seq2= new uchar[imsize];
1836 int oneD=width*height;
1838 for(int i=0;i<oneD;i++) {
1840 seq[oneD+i]=data_G[i];
1841 seq[2*oneD+i]=data_B[i];
1845 for(int i=0;i<oneD;i++) {
1854 int total_len=imsize;
1856 int len= total_len/h;
1860 uchar *mix=new uchar[256];
1865 for (int i = 0; i < 256 ; i++) {
1866 mix[i]=Secretkey[i]^counter[i];
1871 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
1872 // g_print("%s\n", sha512);
1882 // cout<<"hash "<<endl;
1883 for (int i = 0; i < 128 ; i++) {
1890 int *Pbox=new int[len];
1891 int *PboxSRM=new int[len/2];
1892 int *PboxSRM2=new int[len/2];
1895 uchar Inv_Sbox1[256];
1896 uchar Inv_Sbox2[256];
1898 uchar RM[h*h*2+256];
1904 double time_encrypt=0;
1905 double time_decrypt=0;
1908 double t=TimeStart();
1909 rc4key(DK, Sbox1, 8);
1912 rc4key(&DK[8], Sbox2, 8);
1914 rc4key(&DK[16], sc, 16);
1915 prga(sc, h*h*2+256, RM);
1921 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1924 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1926 for(int i=0;i<len/2;i++) {
1927 PboxSRM[i]=PboxSRM2[i]&(h-1);
1931 for(int i=0;i<h*2;i++) {
1932 for(int j=0;j<h;j++)
1933 cout<<(int)RM[i*h+j]<<" ";
1940 //time+=TimeStop(t);
1941 //cout<<"Time initializaton "<<time<<endl;
1946 for(int i=0;i<64;i++) {
1957 inverse_tables(Sbox1,256,Inv_Sbox1);
1958 inverse_tables(Sbox2,256,Inv_Sbox2);
1962 lehmer64_seed(myrand);
1969 for(i=0;i<nb_test;i++)
1972 encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1974 encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1976 encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1978 encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1982 for(i=0;i<nb_test;i++)
1985 encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1987 encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1989 encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1991 encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1995 for(i=0;i<nb_test;i++)
1998 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2000 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2002 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2004 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2008 for(i=0;i<nb_test;i++)
2011 encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2013 encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2015 encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2017 encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2021 for(i=0;i<nb_test;i++)
2024 encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2026 encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2028 encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2030 encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2035 for(i=0;i<nb_test;i++)
2038 encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2040 encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2042 encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2044 encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2049 for(i=0;i<nb_test;i++)
2052 encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2054 encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2056 encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2058 encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2063 time_encrypt+=TimeStop(t);
2064 //cout<<"Time encrypt "<<
2065 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
2069 for(int i=0;i<oneD;i++) {
2071 data_G[i]=seq2[oneD+i];
2072 data_B[i]=seq2[2*oneD+i];
2074 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
2078 lehmer64_seed(myrand);
2083 for(i=0;i<nb_test;i++) {
2085 decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2087 decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2089 decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2091 decrypt_ecb_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2095 for(i=0;i<nb_test;i++) {
2097 decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2099 decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2101 decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2103 decrypt_ecb_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2107 for(i=0;i<nb_test;i++) {
2109 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2111 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2113 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2115 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2119 for(i=0;i<nb_test;i++) {
2121 decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2123 decrypt_cbc_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2125 decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2127 decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2131 for(i=0;i<nb_test;i++) {
2133 decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2135 decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2137 decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2139 decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2143 for(i=0;i<nb_test;i++) {
2145 decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2147 decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2149 decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2151 decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2155 for(i=0;i<nb_test;i++) {
2157 decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2159 decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2161 decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2163 decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2168 time_decrypt+=TimeStop(t);
2169 //cout<<"Time decrypt "
2170 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
2173 for(int i=0;i<oneD;i++) {
2175 data_G[i]=seq[oneD+i];
2176 data_B[i]=seq[2*oneD+i];
2178 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
2182 for(int i=0;i<imsize;i++) {
2183 //cout<<(int)buffer[i]<<endl;
2184 if(buffer[i]!=seq[i]) {
2188 // cout<<"RESULT CORRECT: "<<equal<<endl;