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, uchar* IV,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, uchar* IV,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];
739 void encrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *IV,uint myrand, int debug) {
750 mylong *rm1=(mylong*)RM1;
751 mylong *rm2=(mylong*)RM2;
755 for(int a=0;a<h;a+=4) {
763 for(int a=0;a<h;a+=4) {
772 for(int it=0;it<len/2;it++) {
774 int ind2=Pbox[it+len/2]*h;
777 for(int a=0;a<(h>>3);a++) {
786 for(int a=0;a<h;a+=4) {
788 X[a+1]=seq_in[ind2+a+1];
789 X[a+2]=seq_in[ind2+a+2];
790 X[a+3]=seq_in[ind2+a+3];
793 for(int a=0;a<h;a+=4) {
795 Y[a+1]=seq_in[ind1+a+1];
796 Y[a+2]=seq_in[ind1+a+2];
797 Y[a+3]=seq_in[ind1+a+3];
801 for(int a=0;a<h;a+=4) {
802 tmp[a]=X[a]^RM1[a]^IV1[a];
803 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
804 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
805 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
808 for(int a=0;a<h;a+=4) {
809 tmp[a]=Sbox1[tmp[a]];
810 tmp[a+1]=Sbox1[tmp[a+1]];
811 tmp[a+2]=Sbox1[tmp[a+2]];
812 tmp[a+3]=Sbox1[tmp[a+3]];
816 /* for(int a=0;a<h;a+=4) {
817 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
818 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
819 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
820 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
823 for(int a=0;a<h;a+=4) {
824 fX[a]=Sbox2[tmp[a]^Y[a]];
825 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
826 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
827 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
831 /*for(int a=0;a<h;a+=4) {
832 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
833 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
834 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
835 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
839 for(int a=0;a<h;a+=4) {
840 tmp[a]=fX[a]^Y[a]^IV2[a];
841 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
842 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
843 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
847 for(int a=0;a<h;a+=4) {
848 tmp[a]=Sbox2[tmp[a]];
849 tmp[a+1]=Sbox2[tmp[a+1]];
850 tmp[a+2]=Sbox2[tmp[a+2]];
851 tmp[a+3]=Sbox2[tmp[a+3]];
856 for(int a=0;a<h;a+=4) {
857 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
858 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
859 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
860 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
865 for(int a=0;a<h;a+=4) {
866 gY[a]=Sbox1[tmp[a]^RM2[a]];
867 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
868 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
869 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
876 for(int a=0;a<h;a+=4) {
877 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
878 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
879 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
880 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
885 for(int a=0;a<h;a+=4) {
886 seq_out[ind2+a]=gY[a];
887 seq_out[ind2+a+1]=gY[a+1];
888 seq_out[ind2+a+2]=gY[a+2];
889 seq_out[ind2+a+3]=gY[a+3];
892 for(int a=0;a<h;a+=4) {
893 seq_out[ind1+a]=fX[a];
894 seq_out[ind1+a+1]=fX[a+1];
895 seq_out[ind1+a+2]=fX[a+2];
896 seq_out[ind1+a+3]=fX[a+3];
898 for(int a=0;a<h;a+=4) {
905 for(int a=0;a<h;a+=4) {
927 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, uchar* IV, uint myrand, int debug) {
938 mylong *rm1=(mylong*)RM1;
939 mylong *rm2=(mylong*)RM2;
943 for(int a=0;a<h;a+=4) {
951 for(int a=0;a<h;a+=4) {
960 for(int it=0;it<len/2;it++) {
962 int ind2=Pbox[it+len/2]*h;
965 for(int a=0;a<(h>>3);a++) {
972 for(int a=0;a<h;a+=4) {
973 gY[a]=seq_in[ind2+a];
974 gY[a+1]=seq_in[ind2+a+1];
975 gY[a+2]=seq_in[ind2+a+2];
976 gY[a+3]=seq_in[ind2+a+3];
979 for(int a=0;a<h;a+=4) {
980 fX[a]=seq_in[ind1+a];
981 fX[a+1]=seq_in[ind1+a+1];
982 fX[a+2]=seq_in[ind1+a+2];
983 fX[a+3]=seq_in[ind1+a+3];
987 for(int a=0;a<h;a+=4) {
988 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
989 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
990 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
991 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
995 for(int a=0;a<h;a+=4) {
996 tmp[a]=Inv_Sbox2[tmp[a]];
997 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
998 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
999 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1004 for(int a=0;a<h;a+=4) {
1005 invgY[a]=tmp[a]^fX[a]^IV2[a];
1006 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1007 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1008 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1013 for(int a=0;a<h;a+=4) {
1014 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a]^IV2[a];
1015 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1]^IV2[a+1];
1016 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2]^IV2[a+2];
1017 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3]^IV2[a+3];
1022 for(int a=0;a<h;a+=4) {
1023 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1024 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1025 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1026 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1031 for(int a=0;a<h;a+=4) {
1032 tmp[a]=Inv_Sbox1[tmp[a]];
1033 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1034 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1035 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1042 for(int a=0;a<h;a+=4) {
1043 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1044 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1045 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1046 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1051 for(int a=0;a<h;a+=4) {
1052 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a]^IV1[a];
1053 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1]^IV1[a+1];
1054 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2]^IV1[a+2];
1055 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3]^IV1[a+3];
1063 for(int a=0;a<h;a+=4) {
1064 seq_out[ind2+a]=invfX[a];
1065 seq_out[ind2+a+1]=invfX[a+1];
1066 seq_out[ind2+a+2]=invfX[a+2];
1067 seq_out[ind2+a+3]=invfX[a+3];
1070 for(int a=0;a<h;a+=4) {
1071 seq_out[ind1+a]=invgY[a];
1072 seq_out[ind1+a+1]=invgY[a+1];
1073 seq_out[ind1+a+2]=invgY[a+2];
1074 seq_out[ind1+a+3]=invgY[a+3];
1076 for(int a=0;a<h;a+=4) {
1083 for(int a=0;a<h;a+=4) {
1106 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) {
1120 for(int a=0;a<h;a+=4) {
1128 for(int a=0;a<h;a+=4) {
1136 for(int it=0;it<len/2;it++) {
1137 int ind1=Pbox[it]*h;
1138 int ind2=Pbox[it+len/2]*h;
1140 RM1=&RM[PboxSRM[it]*h];
1141 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1144 for(int a=0;a<h;a+=4) {
1145 X[a]=seq_in[ind2+a];
1146 X[a+1]=seq_in[ind2+a+1];
1147 X[a+2]=seq_in[ind2+a+2];
1148 X[a+3]=seq_in[ind2+a+3];
1151 for(int a=0;a<h;a+=4) {
1152 Y[a]=seq_in[ind1+a];
1153 Y[a+1]=seq_in[ind1+a+1];
1154 Y[a+2]=seq_in[ind1+a+2];
1155 Y[a+3]=seq_in[ind1+a+3];
1159 for(int a=0;a<h;a+=4) {
1160 tmp[a]=X[a]^RM1[a]^IV1[a];
1161 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
1162 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
1163 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
1166 for(int a=0;a<h;a+=4) {
1167 tmp[a]=Sbox1[tmp[a]];
1168 tmp[a+1]=Sbox1[tmp[a+1]];
1169 tmp[a+2]=Sbox1[tmp[a+2]];
1170 tmp[a+3]=Sbox1[tmp[a+3]];
1174 /*for(int a=0;a<h;a+=4) {
1175 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
1176 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
1177 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
1178 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
1181 for(int a=0;a<h;a+=4) {
1182 fX[a]=Sbox2[tmp[a]^Y[a]];
1183 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
1184 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
1185 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
1189 for(int a=0;a<h;a+=4) {
1190 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1191 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1192 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1193 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1197 for(int a=0;a<h;a+=4) {
1198 tmp[a]=fX[a]^Y[a]^IV2[a];
1199 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
1200 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
1201 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
1205 for(int a=0;a<h;a+=4) {
1206 tmp[a]=Sbox2[tmp[a]];
1207 tmp[a+1]=Sbox2[tmp[a+1]];
1208 tmp[a+2]=Sbox2[tmp[a+2]];
1209 tmp[a+3]=Sbox2[tmp[a+3]];
1214 for(int a=0;a<h;a+=4) {
1215 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
1216 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
1217 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1218 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1223 for(int a=0;a<h;a+=4) {
1224 gY[a]=Sbox1[tmp[a]^RM2[a]];
1225 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1226 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1227 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1234 for(int a=0;a<h;a+=4) {
1235 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1236 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1237 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1238 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1243 for(int a=0;a<h;a+=4) {
1244 seq_out[ind2+a]=gY[a];
1245 seq_out[ind2+a+1]=gY[a+1];
1246 seq_out[ind2+a+2]=gY[a+2];
1247 seq_out[ind2+a+3]=gY[a+3];
1250 for(int a=0;a<h;a+=4) {
1251 seq_out[ind1+a]=fX[a];
1252 seq_out[ind1+a+1]=fX[a+1];
1253 seq_out[ind1+a+2]=fX[a+2];
1254 seq_out[ind1+a+3]=fX[a+3];
1256 for(int a=0;a<h;a+=4) {
1263 for(int a=0;a<h;a+=4) {
1285 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) {
1298 for(int a=0;a<h;a+=4) {
1306 for(int a=0;a<h;a+=4) {
1314 for(int it=0;it<len/2;it++) {
1315 int ind1=Pbox[it]*h;
1316 int ind2=Pbox[it+len/2]*h;
1319 RM1=&RM[PboxSRM[it]*h];
1320 RM2=&RM[h*h+PboxSRM[len/2-it]*h];
1325 for(int a=0;a<h;a+=4) {
1326 gY[a]=seq_in[ind2+a];
1327 gY[a+1]=seq_in[ind2+a+1];
1328 gY[a+2]=seq_in[ind2+a+2];
1329 gY[a+3]=seq_in[ind2+a+3];
1332 for(int a=0;a<h;a+=4) {
1333 fX[a]=seq_in[ind1+a];
1334 fX[a+1]=seq_in[ind1+a+1];
1335 fX[a+2]=seq_in[ind1+a+2];
1336 fX[a+3]=seq_in[ind1+a+3];
1340 for(int a=0;a<h;a+=4) {
1341 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1342 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1343 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1344 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1348 for(int a=0;a<h;a+=4) {
1349 tmp[a]=Inv_Sbox2[tmp[a]];
1350 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1351 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1352 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1357 for(int a=0;a<h;a+=4) {
1358 invgY[a]=tmp[a]^fX[a]^IV2[a];
1359 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1360 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1361 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1365 for(int a=0;a<h;a+=4) {
1366 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1367 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1368 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1369 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1374 for(int a=0;a<h;a+=4) {
1375 tmp[a]=Inv_Sbox1[tmp[a]];
1376 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1377 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1378 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1385 for(int a=0;a<h;a+=4) {
1386 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1387 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1388 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1389 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1394 for(int a=0;a<h;a+=4) {
1395 seq_out[ind2+a]=invfX[a];
1396 seq_out[ind2+a+1]=invfX[a+1];
1397 seq_out[ind2+a+2]=invfX[a+2];
1398 seq_out[ind2+a+3]=invfX[a+3];
1401 for(int a=0;a<h;a+=4) {
1402 seq_out[ind1+a]=invgY[a];
1403 seq_out[ind1+a+1]=invgY[a+1];
1404 seq_out[ind1+a+2]=invgY[a+2];
1405 seq_out[ind1+a+3]=invgY[a+3];
1407 for(int a=0;a<h;a+=4) {
1414 for(int a=0;a<h;a+=4) {
1436 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) {
1452 for(int a=0;a<h;a+=4) {
1453 myrand=xorshift32(myrand);
1464 for(int a=0;a<h;a+=4) {
1465 myrand=xorshift32(myrand);
1479 for(int it=0;it<len/2;it++) {
1480 int ind1=Pbox[it]*h;
1481 int ind2=Pbox[it+len/2]*h;
1485 RM1=&RM[PboxSRM[it]*h];
1486 RM2=&RM[h*h+PboxSRM[it]*h];
1489 for(int a=0;a<h;a+=4) {
1490 X[a]=seq_in[ind2+a];
1491 X[a+1]=seq_in[ind2+a+1];
1492 X[a+2]=seq_in[ind2+a+2];
1493 X[a+3]=seq_in[ind2+a+3];
1496 for(int a=0;a<h;a+=4) {
1497 Y[a]=seq_in[ind1+a];
1498 Y[a+1]=seq_in[ind1+a+1];
1499 Y[a+2]=seq_in[ind1+a+2];
1500 Y[a+3]=seq_in[ind1+a+3];
1504 for(int a=0;a<h;a+=4) {
1505 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1506 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1507 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1508 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1511 for(int a=0;a<h;a+=4) {
1512 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1513 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1514 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1515 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1519 for(int a=0;a<h;a+=4) {
1520 seq_out[ind2+a]=gY[a];
1521 seq_out[ind2+a+1]=gY[a+1];
1522 seq_out[ind2+a+2]=gY[a+2];
1523 seq_out[ind2+a+3]=gY[a+3];
1526 for(int a=0;a<h;a+=4) {
1527 seq_out[ind1+a]=fX[a];
1528 seq_out[ind1+a+1]=fX[a+1];
1529 seq_out[ind1+a+2]=fX[a+2];
1530 seq_out[ind1+a+3]=fX[a+3];
1532 for(int a=0;a<h;a+=4) {
1539 for(int a=0;a<h;a+=4) {
1561 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) {
1572 for(int a=0;a<h;a+=4) {
1573 myrand=xorshift32(myrand);
1584 for(int a=0;a<h;a+=4) {
1585 myrand=xorshift32(myrand);
1602 for(int it=0;it<len/2;it++) {
1603 int ind1=Pbox[it]*h;
1604 int ind2=Pbox[it+len/2]*h;
1607 RM1=&RM[PboxSRM[it]*h];
1608 RM2=&RM[h*h+PboxSRM[it]*h];
1611 for(int a=0;a<h;a+=4) {
1612 gY[a]=seq_in[ind2+a];
1613 gY[a+1]=seq_in[ind2+a+1];
1614 gY[a+2]=seq_in[ind2+a+2];
1615 gY[a+3]=seq_in[ind2+a+3];
1618 for(int a=0;a<h;a+=4) {
1619 fX[a]=seq_in[ind1+a];
1620 fX[a+1]=seq_in[ind1+a+1];
1621 fX[a+2]=seq_in[ind1+a+2];
1622 fX[a+3]=seq_in[ind1+a+3];
1626 for(int a=0;a<h;a+=4) {
1627 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
1628 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1629 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1630 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1634 for(int a=0;a<h;a+=4) {
1635 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
1636 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
1637 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
1638 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
1642 for(int a=0;a<h;a+=4) {
1643 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
1644 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1645 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1646 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1650 for(int a=0;a<h;a+=4) {
1651 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
1652 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
1653 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
1654 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
1659 for(int a=0;a<h;a+=4) {
1660 seq_out[ind2+a]=invfX[a];
1661 seq_out[ind2+a+1]=invfX[a+1];
1662 seq_out[ind2+a+2]=invfX[a+2];
1663 seq_out[ind2+a+3]=invfX[a+3];
1666 for(int a=0;a<h;a+=4) {
1667 seq_out[ind1+a]=invgY[a];
1668 seq_out[ind1+a+1]=invgY[a+1];
1669 seq_out[ind1+a+2]=invgY[a+2];
1670 seq_out[ind1+a+3]=invgY[a+3];
1672 for(int a=0;a<h;a+=4) {
1679 for(int a=0;a<h;a+=4) {
1697 int main(int argc, char** argv) {
1706 for(int i=1; i<argc; i++){
1707 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
1708 if(strncmp(argv[i],"cbcrm",5)==0) cbcrm=1;
1709 if(strncmp(argv[i],"cbcprng",7)==0) {cbcprng=1;cbcrm=0;}
1710 if(strncmp(argv[i],"ecbrm",5)==0) ecbrm = 1;
1711 if(strncmp(argv[i],"ecbprng",7)==0) {ecbprng=1; ecbrm=0;}
1712 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
1713 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
1714 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
1717 printf("nb times %d\n",nb_test);
1718 printf("cbcrm %d\n",cbcrm);
1719 printf("cbcprng %d\n",cbcprng);
1720 printf("ecbrm %d\n",ecbrm);
1721 printf("ecbprng %d\n",ecbprng);
1723 printf("lena %d\n",lena);
1724 printf("size_buf %d\n",size_buf);
1729 int seed=time(NULL);
1730 // cout<<seed<<endl;
1733 uchar Secretkey[key_size];
1735 uchar counter[key_size];
1737 for(int i=0;i<key_size;i++) {
1738 Secretkey[i]=lrand48()&0xFF;
1739 counter[i]=lrand48()&0xFF;
1752 uchar *data_R, *data_G, *data_B;
1761 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
1762 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
1763 imsize=width*height*3;
1764 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
1767 width=height=size_buf;
1768 imsize=width*height;
1769 buffer=new uchar[imsize];
1770 for(int i=0;i<imsize;i++) {
1771 buffer[i]=lrand48();
1779 uchar* seq= new uchar[imsize];
1780 uchar* seq2= new uchar[imsize];
1782 int oneD=width*height;
1784 for(int i=0;i<oneD;i++) {
1786 seq[oneD+i]=data_G[i];
1787 seq[2*oneD+i]=data_B[i];
1791 for(int i=0;i<oneD;i++) {
1800 int total_len=imsize;
1802 int len= total_len/h;
1806 uchar *mix=new uchar[256];
1811 for (int i = 0; i < 256 ; i++) {
1812 mix[i]=Secretkey[i]^counter[i];
1817 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
1818 // g_print("%s\n", sha512);
1828 // cout<<"hash "<<endl;
1829 for (int i = 0; i < 128 ; i++) {
1836 int *Pbox=new int[len];
1837 int *PboxSRM=new int[len/2];
1838 int *PboxSRM2=new int[len/2];
1841 uchar Inv_Sbox1[256];
1842 uchar Inv_Sbox2[256];
1844 uchar RM[h*h*2+256];
1850 double time_encrypt=0;
1851 double time_decrypt=0;
1854 double t=TimeStart();
1855 rc4key(DK, Sbox1, 8);
1858 rc4key(&DK[8], Sbox2, 8);
1860 rc4key(&DK[16], sc, 16);
1861 prga(sc, h*h*2+256, RM);
1867 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1870 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1872 for(int i=0;i<len/2;i++) {
1873 PboxSRM[i]=PboxSRM2[i]&(h-1);
1877 for(int i=0;i<h*2;i++) {
1878 for(int j=0;j<h;j++)
1879 cout<<(int)RM[i*h+j]<<" ";
1886 //time+=TimeStop(t);
1887 //cout<<"Time initializaton "<<time<<endl;
1892 for(int i=0;i<64;i++) {
1903 inverse_tables(Sbox1,256,Inv_Sbox1);
1904 inverse_tables(Sbox2,256,Inv_Sbox2);
1908 // lehmer64_seed(myrand);
1915 for(i=0;i<nb_test;i++)
1918 encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1920 encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1922 encrypt_ecb_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1924 encrypt_ecb_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1928 for(i=0;i<nb_test;i++)
1931 encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1933 encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1935 encrypt_ecb_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1937 encrypt_ecb_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1941 for(i=0;i<nb_test;i++)
1944 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1946 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1948 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1950 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1954 for(i=0;i<nb_test;i++)
1957 encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1959 encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1961 encrypt_ecb_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1963 encrypt_ecb_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1967 for(i=0;i<nb_test;i++)
1970 encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1972 encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1974 encrypt_ecb_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1976 encrypt_ecb_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1981 for(i=0;i<nb_test;i++)
1984 encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1986 encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1988 encrypt_ecb_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1990 encrypt_ecb_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1995 for(i=0;i<nb_test;i++)
1998 encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
2000 encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2002 encrypt_ecb_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
2004 encrypt_ecb_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
2009 time_encrypt+=TimeStop(t);
2010 //cout<<"Time encrypt "<<
2011 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
2015 for(int i=0;i<oneD;i++) {
2017 data_G[i]=seq2[oneD+i];
2018 data_B[i]=seq2[2*oneD+i];
2020 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
2025 // lehmer64_seed(myrand);
2030 for(i=0;i<nb_test;i++) {
2032 decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2034 decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2036 decrypt_ecb_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2038 decrypt_ecb_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2042 for(i=0;i<nb_test;i++) {
2044 decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2046 decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2048 decrypt_ecb_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2050 decrypt_ecb_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2054 for(i=0;i<nb_test;i++) {
2056 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2058 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2060 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2062 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2066 for(i=0;i<nb_test;i++) {
2068 decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2070 decrypt_cbc_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2072 decrypt_ecb_rm<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2074 decrypt_ecb_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2078 for(i=0;i<nb_test;i++) {
2080 decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2082 decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2084 decrypt_ecb_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2086 decrypt_ecb_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2090 for(i=0;i<nb_test;i++) {
2092 decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2094 decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2096 decrypt_ecb_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2098 decrypt_ecb_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2102 for(i=0;i<nb_test;i++) {
2104 decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2106 decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2108 decrypt_ecb_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
2110 decrypt_ecb_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
2115 time_decrypt+=TimeStop(t);
2116 //cout<<"Time decrypt "
2117 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
2120 for(int i=0;i<oneD;i++) {
2122 data_G[i]=seq[oneD+i];
2123 data_B[i]=seq[2*oneD+i];
2125 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
2129 for(int i=0;i<imsize;i++) {
2130 //cout<<(int)buffer[i]<<endl;
2131 if(buffer[i]!=seq[i]) {
2135 // cout<<"RESULT CORRECT: "<<equal<<endl;