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 mylong;
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, mylong myrand, int debug) {
203 mylong *rm1=(mylong*)RM1;
204 mylong *rm2=(mylong*)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, mylong myrand, int debug) {
331 mylong *rm1=(mylong*)RM1;
332 mylong *rm2=(mylong*)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, uchar *IV, int debug) {
463 for(int a=0;a<h;a+=4) {
471 for(int a=0;a<h;a+=4) {
484 for(int it=0;it<len/2;it++) {
486 int ind2=Pbox[it+len/2]*h;
489 RM1=&RM[PboxSRM[it]*h];
490 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
494 for(int a=0;a<h;a+=4) {
496 X[a+1]=seq_in[ind2+a+1];
497 X[a+2]=seq_in[ind2+a+2];
498 X[a+3]=seq_in[ind2+a+3];
501 for(int a=0;a<h;a+=4) {
503 Y[a+1]=seq_in[ind1+a+1];
504 Y[a+2]=seq_in[ind1+a+2];
505 Y[a+3]=seq_in[ind1+a+3];
509 for(int a=0;a<h;a+=4) {
510 tmp[a]=Sbox1[X[a]^RM1[a]];
511 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
512 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
513 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
516 for(int a=0;a<h;a+=4) {
517 fX[a]=Sbox2[tmp[a]^Y[a]];
518 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
519 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
520 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
524 /*for(int a=0;a<h;a+=4) {
525 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
526 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
527 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
528 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
534 for(int a=0;a<h;a+=4) {
535 tmp[a]=Sbox2[fX[a]^Y[a]];
536 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
537 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
538 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
541 for(int a=0;a<h;a+=4) {
542 gY[a]=Sbox1[tmp[a]^RM2[a]];
543 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
544 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
545 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
550 /* for(int a=0;a<h;a+=4) {
551 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
552 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
553 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
554 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
563 for(int a=0;a<h;a+=4) {
564 seq_out[ind2+a]=gY[a];
565 seq_out[ind2+a+1]=gY[a+1];
566 seq_out[ind2+a+2]=gY[a+2];
567 seq_out[ind2+a+3]=gY[a+3];
570 for(int a=0;a<h;a+=4) {
571 seq_out[ind1+a]=fX[a];
572 seq_out[ind1+a+1]=fX[a+1];
573 seq_out[ind1+a+2]=fX[a+2];
574 seq_out[ind1+a+3]=fX[a+3];
594 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, uchar *IV, int debug) {
608 for(int a=0;a<h;a+=4) {
616 for(int a=0;a<h;a+=4) {
626 for(int it=0;it<len/2;it++) {
628 int ind2=Pbox[it+len/2]*h;
631 RM1=&RM[PboxSRM[it]*h];
632 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
636 for(int a=0;a<h;a+=4) {
637 gY[a]=seq_in[ind2+a];
638 gY[a+1]=seq_in[ind2+a+1];
639 gY[a+2]=seq_in[ind2+a+2];
640 gY[a+3]=seq_in[ind2+a+3];
643 for(int a=0;a<h;a+=4) {
644 fX[a]=seq_in[ind1+a];
645 fX[a+1]=seq_in[ind1+a+1];
646 fX[a+2]=seq_in[ind1+a+2];
647 fX[a+3]=seq_in[ind1+a+3];
653 for(int a=0;a<h;a+=4) {
654 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
655 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
656 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
657 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
661 for(int a=0;a<h;a+=4) {
662 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
663 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
664 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
665 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
670 /* for(int a=0;a<h;a+=4) {
671 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
672 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
673 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
674 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
680 for(int a=0;a<h;a+=4) {
681 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
682 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
683 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
684 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
688 for(int a=0;a<h;a+=4) {
689 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
690 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
691 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
692 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
698 for(int a=0;a<h;a+=4) {
699 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
700 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
701 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
702 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
707 for(int a=0;a<h;a+=4) {
708 seq_out[ind2+a]=invfX[a];
709 seq_out[ind2+a+1]=invfX[a+1];
710 seq_out[ind2+a+2]=invfX[a+2];
711 seq_out[ind2+a+3]=invfX[a+3];
714 for(int a=0;a<h;a+=4) {
715 seq_out[ind1+a]=invgY[a];
716 seq_out[ind1+a+1]=invgY[a+1];
717 seq_out[ind1+a+2]=invgY[a+2];
718 seq_out[ind1+a+3]=invgY[a+3];
735 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) {
744 for(int it=0;it<len/2;it++) {
746 int ind2=Pbox[it+len/2]*h;
750 RM1=&RM[PboxSRM[it]*h];
751 RM2=&RM[h*h+PboxSRM[it]*h];
754 for(int a=0;a<h;a+=4) {
756 X[a+1]=seq_in[ind2+a+1];
757 X[a+2]=seq_in[ind2+a+2];
758 X[a+3]=seq_in[ind2+a+3];
761 for(int a=0;a<h;a+=4) {
763 Y[a+1]=seq_in[ind1+a+1];
764 Y[a+2]=seq_in[ind1+a+2];
765 Y[a+3]=seq_in[ind1+a+3];
769 for(int a=0;a<h;a+=4) {
770 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
771 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
772 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
773 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
776 for(int a=0;a<h;a+=4) {
777 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
778 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
779 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
780 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
784 for(int a=0;a<h;a+=4) {
785 seq_out[ind2+a]=gY[a];
786 seq_out[ind2+a+1]=gY[a+1];
787 seq_out[ind2+a+2]=gY[a+2];
788 seq_out[ind2+a+3]=gY[a+3];
791 for(int a=0;a<h;a+=4) {
792 seq_out[ind1+a]=fX[a];
793 seq_out[ind1+a+1]=fX[a+1];
794 seq_out[ind1+a+2]=fX[a+2];
795 seq_out[ind1+a+3]=fX[a+3];
815 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) {
824 for(int it=0;it<len/2;it++) {
826 int ind2=Pbox[it+len/2]*h;
829 RM1=&RM[PboxSRM[it]*h];
830 RM2=&RM[h*h+PboxSRM[it]*h];
833 for(int a=0;a<h;a+=4) {
834 gY[a]=seq_in[ind2+a];
835 gY[a+1]=seq_in[ind2+a+1];
836 gY[a+2]=seq_in[ind2+a+2];
837 gY[a+3]=seq_in[ind2+a+3];
840 for(int a=0;a<h;a+=4) {
841 fX[a]=seq_in[ind1+a];
842 fX[a+1]=seq_in[ind1+a+1];
843 fX[a+2]=seq_in[ind1+a+2];
844 fX[a+3]=seq_in[ind1+a+3];
847 for(int a=0;a<h;a+=4) {
848 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
849 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
850 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
851 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
856 for(int a=0;a<h;a+=4) {
857 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
858 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
859 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
860 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
865 for(int a=0;a<h;a+=4) {
866 seq_out[ind2+a]=invfX[a];
867 seq_out[ind2+a+1]=invfX[a+1];
868 seq_out[ind2+a+2]=invfX[a+2];
869 seq_out[ind2+a+3]=invfX[a+3];
872 for(int a=0;a<h;a+=4) {
873 seq_out[ind1+a]=invgY[a];
874 seq_out[ind1+a+1]=invgY[a+1];
875 seq_out[ind1+a+2]=invgY[a+2];
876 seq_out[ind1+a+3]=invgY[a+3];
895 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) {
906 mylong *rm1=(mylong*)RM1;
907 mylong *rm2=(mylong*)RM2;
912 for(int it=0;it<len/2;it++) {
914 int ind2=Pbox[it+len/2]*h;
917 for(int a=0;a<(h>>3);a++) {
926 for(int a=0;a<h;a+=4) {
928 X[a+1]=seq_in[ind2+a+1];
929 X[a+2]=seq_in[ind2+a+2];
930 X[a+3]=seq_in[ind2+a+3];
933 for(int a=0;a<h;a+=4) {
935 Y[a+1]=seq_in[ind1+a+1];
936 Y[a+2]=seq_in[ind1+a+2];
937 Y[a+3]=seq_in[ind1+a+3];
941 for(int a=0;a<h;a+=4) {
942 tmp[a]=X[a]^RM1[a]^IV1[a];
943 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
944 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
945 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
948 for(int a=0;a<h;a+=4) {
949 tmp[a]=Sbox1[tmp[a]];
950 tmp[a+1]=Sbox1[tmp[a+1]];
951 tmp[a+2]=Sbox1[tmp[a+2]];
952 tmp[a+3]=Sbox1[tmp[a+3]];
956 /*for(int a=0;a<h;a+=4) {
957 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
958 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
959 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
960 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
963 for(int a=0;a<h;a+=4) {
964 fX[a]=Sbox2[tmp[a]^Y[a]];
965 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
966 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
967 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
971 for(int a=0;a<h;a+=4) {
972 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
973 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
974 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
975 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
979 for(int a=0;a<h;a+=4) {
980 tmp[a]=fX[a]^Y[a]^IV2[a];
981 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
982 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
983 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
987 for(int a=0;a<h;a+=4) {
988 tmp[a]=Sbox2[tmp[a]];
989 tmp[a+1]=Sbox2[tmp[a+1]];
990 tmp[a+2]=Sbox2[tmp[a+2]];
991 tmp[a+3]=Sbox2[tmp[a+3]];
996 for(int a=0;a<h;a+=4) {
997 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
998 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
999 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1000 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1005 for(int a=0;a<h;a+=4) {
1006 gY[a]=Sbox1[tmp[a]^RM2[a]];
1007 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1008 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1009 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1016 for(int a=0;a<h;a+=4) {
1017 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1018 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1019 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1020 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1025 for(int a=0;a<h;a+=4) {
1026 seq_out[ind2+a]=gY[a];
1027 seq_out[ind2+a+1]=gY[a+1];
1028 seq_out[ind2+a+2]=gY[a+2];
1029 seq_out[ind2+a+3]=gY[a+3];
1032 for(int a=0;a<h;a+=4) {
1033 seq_out[ind1+a]=fX[a];
1034 seq_out[ind1+a+1]=fX[a+1];
1035 seq_out[ind1+a+2]=fX[a+2];
1036 seq_out[ind1+a+3]=fX[a+3];
1038 for(int a=0;a<h;a+=4) {
1045 for(int a=0;a<h;a+=4) {
1067 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) {
1080 mylong *rm1=(mylong*)RM1;
1081 mylong *rm2=(mylong*)RM2;
1083 for(int it=0;it<len/2;it++) {
1084 int ind1=Pbox[it]*h;
1085 int ind2=Pbox[it+len/2]*h;
1088 for(int a=0;a<(h>>3);a++) {
1089 myrand=xorshift64();
1091 myrand=xorshift64();
1095 for(int a=0;a<h;a+=4) {
1096 gY[a]=seq_in[ind2+a];
1097 gY[a+1]=seq_in[ind2+a+1];
1098 gY[a+2]=seq_in[ind2+a+2];
1099 gY[a+3]=seq_in[ind2+a+3];
1102 for(int a=0;a<h;a+=4) {
1103 fX[a]=seq_in[ind1+a];
1104 fX[a+1]=seq_in[ind1+a+1];
1105 fX[a+2]=seq_in[ind1+a+2];
1106 fX[a+3]=seq_in[ind1+a+3];
1110 for(int a=0;a<h;a+=4) {
1111 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1112 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1113 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1114 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1118 for(int a=0;a<h;a+=4) {
1119 tmp[a]=Inv_Sbox2[tmp[a]];
1120 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1121 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1122 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1127 for(int a=0;a<h;a+=4) {
1128 invgY[a]=tmp[a]^fX[a]^IV2[a];
1129 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1130 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1131 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1135 for(int a=0;a<h;a+=4) {
1136 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1137 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1138 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1139 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1144 for(int a=0;a<h;a+=4) {
1145 tmp[a]=Inv_Sbox1[tmp[a]];
1146 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1147 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1148 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1155 for(int a=0;a<h;a+=4) {
1156 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1157 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1158 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1159 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1164 for(int a=0;a<h;a+=4) {
1165 seq_out[ind2+a]=invfX[a];
1166 seq_out[ind2+a+1]=invfX[a+1];
1167 seq_out[ind2+a+2]=invfX[a+2];
1168 seq_out[ind2+a+3]=invfX[a+3];
1171 for(int a=0;a<h;a+=4) {
1172 seq_out[ind1+a]=invgY[a];
1173 seq_out[ind1+a+1]=invgY[a+1];
1174 seq_out[ind1+a+2]=invgY[a+2];
1175 seq_out[ind1+a+3]=invgY[a+3];
1177 for(int a=0;a<h;a+=4) {
1184 for(int a=0;a<h;a+=4) {
1207 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) {
1221 for(int a=0;a<h;a+=4) {
1229 for(int a=0;a<h;a+=4) {
1237 for(int it=0;it<len/2;it++) {
1238 int ind1=Pbox[it]*h;
1239 int ind2=Pbox[it+len/2]*h;
1241 RM1=&RM[PboxSRM[it]*h];
1242 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1245 for(int a=0;a<h;a+=4) {
1246 X[a]=seq_in[ind2+a];
1247 X[a+1]=seq_in[ind2+a+1];
1248 X[a+2]=seq_in[ind2+a+2];
1249 X[a+3]=seq_in[ind2+a+3];
1252 for(int a=0;a<h;a+=4) {
1253 Y[a]=seq_in[ind1+a];
1254 Y[a+1]=seq_in[ind1+a+1];
1255 Y[a+2]=seq_in[ind1+a+2];
1256 Y[a+3]=seq_in[ind1+a+3];
1260 for(int a=0;a<h;a+=4) {
1261 tmp[a]=X[a]^RM1[a]^IV1[a];
1262 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
1263 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
1264 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
1267 for(int a=0;a<h;a+=4) {
1268 tmp[a]=Sbox1[tmp[a]];
1269 tmp[a+1]=Sbox1[tmp[a+1]];
1270 tmp[a+2]=Sbox1[tmp[a+2]];
1271 tmp[a+3]=Sbox1[tmp[a+3]];
1275 /*for(int a=0;a<h;a+=4) {
1276 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
1277 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
1278 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
1279 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
1282 for(int a=0;a<h;a+=4) {
1283 fX[a]=Sbox2[tmp[a]^Y[a]];
1284 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
1285 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
1286 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
1290 for(int a=0;a<h;a+=4) {
1291 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1292 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1293 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1294 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1298 for(int a=0;a<h;a+=4) {
1299 tmp[a]=fX[a]^Y[a]^IV2[a];
1300 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
1301 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
1302 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
1306 for(int a=0;a<h;a+=4) {
1307 tmp[a]=Sbox2[tmp[a]];
1308 tmp[a+1]=Sbox2[tmp[a+1]];
1309 tmp[a+2]=Sbox2[tmp[a+2]];
1310 tmp[a+3]=Sbox2[tmp[a+3]];
1315 for(int a=0;a<h;a+=4) {
1316 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
1317 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
1318 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1319 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1324 for(int a=0;a<h;a+=4) {
1325 gY[a]=Sbox1[tmp[a]^RM2[a]];
1326 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1327 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1328 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1335 for(int a=0;a<h;a+=4) {
1336 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1337 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1338 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1339 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1344 for(int a=0;a<h;a+=4) {
1345 seq_out[ind2+a]=gY[a];
1346 seq_out[ind2+a+1]=gY[a+1];
1347 seq_out[ind2+a+2]=gY[a+2];
1348 seq_out[ind2+a+3]=gY[a+3];
1351 for(int a=0;a<h;a+=4) {
1352 seq_out[ind1+a]=fX[a];
1353 seq_out[ind1+a+1]=fX[a+1];
1354 seq_out[ind1+a+2]=fX[a+2];
1355 seq_out[ind1+a+3]=fX[a+3];
1357 for(int a=0;a<h;a+=4) {
1364 for(int a=0;a<h;a+=4) {
1386 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) {
1399 for(int a=0;a<h;a+=4) {
1407 for(int a=0;a<h;a+=4) {
1415 for(int it=0;it<len/2;it++) {
1416 int ind1=Pbox[it]*h;
1417 int ind2=Pbox[it+len/2]*h;
1420 RM1=&RM[PboxSRM[it]*h];
1421 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1426 for(int a=0;a<h;a+=4) {
1427 gY[a]=seq_in[ind2+a];
1428 gY[a+1]=seq_in[ind2+a+1];
1429 gY[a+2]=seq_in[ind2+a+2];
1430 gY[a+3]=seq_in[ind2+a+3];
1433 for(int a=0;a<h;a+=4) {
1434 fX[a]=seq_in[ind1+a];
1435 fX[a+1]=seq_in[ind1+a+1];
1436 fX[a+2]=seq_in[ind1+a+2];
1437 fX[a+3]=seq_in[ind1+a+3];
1441 for(int a=0;a<h;a+=4) {
1442 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1443 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1444 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1445 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1449 for(int a=0;a<h;a+=4) {
1450 tmp[a]=Inv_Sbox2[tmp[a]];
1451 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1452 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1453 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1458 for(int a=0;a<h;a+=4) {
1459 invgY[a]=tmp[a]^fX[a]^IV2[a];
1460 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1461 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1462 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1466 for(int a=0;a<h;a+=4) {
1467 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1468 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1469 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1470 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1475 for(int a=0;a<h;a+=4) {
1476 tmp[a]=Inv_Sbox1[tmp[a]];
1477 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1478 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1479 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1486 for(int a=0;a<h;a+=4) {
1487 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1488 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1489 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1490 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1495 for(int a=0;a<h;a+=4) {
1496 seq_out[ind2+a]=invfX[a];
1497 seq_out[ind2+a+1]=invfX[a+1];
1498 seq_out[ind2+a+2]=invfX[a+2];
1499 seq_out[ind2+a+3]=invfX[a+3];
1502 for(int a=0;a<h;a+=4) {
1503 seq_out[ind1+a]=invgY[a];
1504 seq_out[ind1+a+1]=invgY[a+1];
1505 seq_out[ind1+a+2]=invgY[a+2];
1506 seq_out[ind1+a+3]=invgY[a+3];
1508 for(int a=0;a<h;a+=4) {
1515 for(int a=0;a<h;a+=4) {
1537 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) {
1553 for(int a=0;a<h;a+=4) {
1554 myrand=xorshift32(myrand);
1565 for(int a=0;a<h;a+=4) {
1566 myrand=xorshift32(myrand);
1580 for(int it=0;it<len/2;it++) {
1581 int ind1=Pbox[it]*h;
1582 int ind2=Pbox[it+len/2]*h;
1586 RM1=&RM[PboxSRM[it]*h];
1587 RM2=&RM[h*h+PboxSRM[it]*h];
1590 for(int a=0;a<h;a+=4) {
1591 X[a]=seq_in[ind2+a];
1592 X[a+1]=seq_in[ind2+a+1];
1593 X[a+2]=seq_in[ind2+a+2];
1594 X[a+3]=seq_in[ind2+a+3];
1597 for(int a=0;a<h;a+=4) {
1598 Y[a]=seq_in[ind1+a];
1599 Y[a+1]=seq_in[ind1+a+1];
1600 Y[a+2]=seq_in[ind1+a+2];
1601 Y[a+3]=seq_in[ind1+a+3];
1605 for(int a=0;a<h;a+=4) {
1606 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1607 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1608 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1609 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1612 for(int a=0;a<h;a+=4) {
1613 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1614 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1615 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1616 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1620 for(int a=0;a<h;a+=4) {
1621 seq_out[ind2+a]=gY[a];
1622 seq_out[ind2+a+1]=gY[a+1];
1623 seq_out[ind2+a+2]=gY[a+2];
1624 seq_out[ind2+a+3]=gY[a+3];
1627 for(int a=0;a<h;a+=4) {
1628 seq_out[ind1+a]=fX[a];
1629 seq_out[ind1+a+1]=fX[a+1];
1630 seq_out[ind1+a+2]=fX[a+2];
1631 seq_out[ind1+a+3]=fX[a+3];
1633 for(int a=0;a<h;a+=4) {
1640 for(int a=0;a<h;a+=4) {
1662 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) {
1673 for(int a=0;a<h;a+=4) {
1674 myrand=xorshift32(myrand);
1685 for(int a=0;a<h;a+=4) {
1686 myrand=xorshift32(myrand);
1703 for(int it=0;it<len/2;it++) {
1704 int ind1=Pbox[it]*h;
1705 int ind2=Pbox[it+len/2]*h;
1708 RM1=&RM[PboxSRM[it]*h];
1709 RM2=&RM[h*h+PboxSRM[it]*h];
1712 for(int a=0;a<h;a+=4) {
1713 gY[a]=seq_in[ind2+a];
1714 gY[a+1]=seq_in[ind2+a+1];
1715 gY[a+2]=seq_in[ind2+a+2];
1716 gY[a+3]=seq_in[ind2+a+3];
1719 for(int a=0;a<h;a+=4) {
1720 fX[a]=seq_in[ind1+a];
1721 fX[a+1]=seq_in[ind1+a+1];
1722 fX[a+2]=seq_in[ind1+a+2];
1723 fX[a+3]=seq_in[ind1+a+3];
1727 for(int a=0;a<h;a+=4) {
1728 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
1729 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1730 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1731 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1735 for(int a=0;a<h;a+=4) {
1736 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
1737 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
1738 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
1739 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
1743 for(int a=0;a<h;a+=4) {
1744 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
1745 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1746 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1747 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1751 for(int a=0;a<h;a+=4) {
1752 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
1753 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
1754 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
1755 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
1760 for(int a=0;a<h;a+=4) {
1761 seq_out[ind2+a]=invfX[a];
1762 seq_out[ind2+a+1]=invfX[a+1];
1763 seq_out[ind2+a+2]=invfX[a+2];
1764 seq_out[ind2+a+3]=invfX[a+3];
1767 for(int a=0;a<h;a+=4) {
1768 seq_out[ind1+a]=invgY[a];
1769 seq_out[ind1+a+1]=invgY[a+1];
1770 seq_out[ind1+a+2]=invgY[a+2];
1771 seq_out[ind1+a+3]=invgY[a+3];
1773 for(int a=0;a<h;a+=4) {
1780 for(int a=0;a<h;a+=4) {
1798 int main(int argc, char** argv) {
1807 for(int i=1; i<argc; i++){
1808 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
1809 if(strncmp(argv[i],"cbcrm",5)==0) cbcrm=1;
1810 if(strncmp(argv[i],"cbcprng",7)==0) {cbcprng=1;cbcrm=0;}
1811 if(strncmp(argv[i],"ecbrm",5)==0) ecbrm = 1;
1812 if(strncmp(argv[i],"ecbprng",7)==0) {ecbprng=1; ecbrm=0;}
1813 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
1814 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
1815 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
1818 printf("nb times %d\n",nb_test);
1819 printf("cbcrm %d\n",cbcrm);
1820 printf("cbcprng %d\n",cbcprng);
1821 printf("ecbrm %d\n",ecbrm);
1822 printf("ecbprng %d\n",ecbprng);
1824 printf("lena %d\n",lena);
1825 printf("size_buf %d\n",size_buf);
1830 int seed=time(NULL);
1831 // cout<<seed<<endl;
1834 uchar Secretkey[key_size];
1836 uchar counter[key_size];
1838 for(int i=0;i<key_size;i++) {
1839 Secretkey[i]=lrand48()&0xFF;
1840 counter[i]=lrand48()&0xFF;
1853 uchar *data_R, *data_G, *data_B;
1862 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
1863 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
1864 imsize=width*height*3;
1865 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
1868 width=height=size_buf;
1869 imsize=width*height;
1870 buffer=new uchar[imsize];
1871 for(int i=0;i<imsize;i++) {
1872 buffer[i]=lrand48();
1880 uchar* seq= new uchar[imsize];
1881 uchar* seq2= new uchar[imsize];
1883 int oneD=width*height;
1885 for(int i=0;i<oneD;i++) {
1887 seq[oneD+i]=data_G[i];
1888 seq[2*oneD+i]=data_B[i];
1892 for(int i=0;i<oneD;i++) {
1901 int total_len=imsize;
1903 int len= total_len/h;
1907 uchar *mix=new uchar[256];
1912 for (int i = 0; i < 256 ; i++) {
1913 mix[i]=Secretkey[i]^counter[i];
1918 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
1919 // g_print("%s\n", sha512);
1929 // cout<<"hash "<<endl;
1930 for (int i = 0; i < 128 ; i++) {
1937 int *Pbox=new int[len];
1938 int *PboxSRM=new int[len/2];
1939 int *PboxSRM2=new int[len/2];
1942 uchar Inv_Sbox1[256];
1943 uchar Inv_Sbox2[256];
1945 uchar RM[h*h*2+256];
1951 double time_encrypt=0;
1952 double time_decrypt=0;
1955 double t=TimeStart();
1956 rc4key(DK, Sbox1, 8);
1959 rc4key(&DK[8], Sbox2, 8);
1961 rc4key(&DK[16], sc, 16);
1962 prga(sc, h*h*2+256, RM);
1968 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1971 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1973 for(int i=0;i<len/2;i++) {
1974 PboxSRM[i]=PboxSRM2[i]&(h-1);
1978 for(int i=0;i<h*2;i++) {
1979 for(int j=0;j<h;j++)
1980 cout<<(int)RM[i*h+j]<<" ";
1987 //time+=TimeStop(t);
1988 //cout<<"Time initializaton "<<time<<endl;
1993 for(int i=0;i<64;i++) {
2004 inverse_tables(Sbox1,256,Inv_Sbox1);
2005 inverse_tables(Sbox2,256,Inv_Sbox2);
2009 // lehmer64_seed(myrand);
2016 for(i=0;i<nb_test;i++)
2019 encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2021 encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2023 encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2025 encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2029 for(i=0;i<nb_test;i++)
2032 encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2034 encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2036 encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2038 encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2042 for(i=0;i<nb_test;i++)
2045 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2047 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2049 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2051 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2055 for(i=0;i<nb_test;i++)
2058 encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2060 encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2062 encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2064 encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2068 for(i=0;i<nb_test;i++)
2071 encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2073 encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2075 encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2077 encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2082 for(i=0;i<nb_test;i++)
2085 encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2087 encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2089 encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2091 encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2096 for(i=0;i<nb_test;i++)
2099 encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2101 encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2103 encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2105 encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
2110 time_encrypt+=TimeStop(t);
2111 //cout<<"Time encrypt "<<
2112 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
2116 for(int i=0;i<oneD;i++) {
2118 data_G[i]=seq2[oneD+i];
2119 data_B[i]=seq2[2*oneD+i];
2121 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
2126 // lehmer64_seed(myrand);
2131 for(i=0;i<nb_test;i++) {
2133 decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2135 decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2137 decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2139 decrypt_ecb_prng<4>(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<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2147 decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2149 decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2151 decrypt_ecb_prng<8>(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<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2159 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2161 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2163 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2167 for(i=0;i<nb_test;i++) {
2169 decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2171 decrypt_cbc_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2173 decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2175 decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2179 for(i=0;i<nb_test;i++) {
2181 decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2183 decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2185 decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2187 decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2191 for(i=0;i<nb_test;i++) {
2193 decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2195 decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2197 decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2199 decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2203 for(i=0;i<nb_test;i++) {
2205 decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2207 decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2209 decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2211 decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
2216 time_decrypt+=TimeStop(t);
2217 //cout<<"Time decrypt "
2218 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
2221 for(int i=0;i<oneD;i++) {
2223 data_G[i]=seq[oneD+i];
2224 data_B[i]=seq[2*oneD+i];
2226 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
2230 for(int i=0;i<imsize;i++) {
2231 //cout<<(int)buffer[i]<<endl;
2232 if(buffer[i]!=seq[i]) {
2236 // cout<<"RESULT CORRECT: "<<equal<<endl;