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;
42 typedef __uint64_t mylong;
45 typedef unsigned char uchar;
50 struct timeval tstart;
51 gettimeofday(&tstart,0);
52 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
55 double TimeStop(double t)
59 gettimeofday(&tend,0);
60 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
67 uint xorshift32(const uint t)
69 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
82 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
93 __uint128_t g_lehmer64_state;
95 inline uint64_t splitmix64_stateless(uint64_t index) {
96 uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
97 z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
98 z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
103 inline void lehmer64_seed(uint64_t seed) {
104 g_lehmer64_state = (((__uint128_t)splitmix64_stateless(seed)) << 64) +
105 splitmix64_stateless(seed + 1);
108 inline uint64_t lehmer64() {
109 g_lehmer64_state *= UINT64_C(0xda942042e4dd58b5);
111 return g_lehmer64_state >> 64;
118 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
120 for(int i=0;i<size_tab;i++) {
121 inv_perm_tabs[tab[i]] = i;
126 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
128 for(int i=0;i<size_tab;i++) {
129 inv_perm_tabs[tab[i]] = i;
136 void rc4key(uchar *key, uchar *sc, int size_DK) {
138 for(int i=0;i<256;i++) {
144 for(int i0=0; i0<256; i0++) {
145 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
154 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
160 for (int i=0;i<len;i++) {
163 for (int it = 0; it < rp; it++) {
165 for(int i0 = 0; i0<len; i0++) {
166 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
175 void prga(uchar *sc, int ldata, uchar *r) {
179 for (int it=0; it<ldata; it++) {
181 j0 = (j0 + sc[i0])&0xFF;
185 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
193 void encrypt_authenticate_algorithm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar* IV,uchar* MAC,mylong myrand) {
199 mylong *rm1=(mylong*)RM1;
200 mylong *rm2=(mylong*)RM2;
202 for(int it=0;it<len;it++) {
206 for(int a=0;a<(h>>3);a++) {
215 for(int a=0;a<h;a+=4) {
216 tmp[a]=Sbox1[seq_in[ind2+a]^RM1[a]];
217 tmp[a+1]=Sbox1[seq_in[ind2+a+1]^RM1[a+1]];
218 tmp[a+2]=Sbox1[seq_in[ind2+a+2]^RM1[a+2]];
219 tmp[a+3]=Sbox1[seq_in[ind2+a+3]^RM1[a+3]];
222 for(int a=0;a<h;a+=4) {
223 X[a]=Sbox2[tmp[a]^RM2[a]];
224 X[a+1]=Sbox2[tmp[a+1]^RM2[a+1]];
225 X[a+2]=Sbox2[tmp[a+2]^RM2[a+2]];
226 X[a+3]=Sbox2[tmp[a+3]^RM2[a+3]];
229 for(int a=0;a<h;a+=4) {
230 seq_out[ind1+a]=X[a];
231 seq_out[ind1+a+1]=X[a+1];
232 seq_out[ind1+a+2]=X[a+2];
233 seq_out[ind1+a+3]=X[a+3];
236 for(int a=0;a<h;a+=4) {
237 IV[a]=Sbox2[IV[a]^tmp[a]];
238 IV[a+1]=Sbox2[IV[a+1]^tmp[a+1]];
239 IV[a+2]=Sbox2[IV[a+2]^tmp[a+2]];
240 IV[a+3]=Sbox2[IV[a+3]^tmp[a+3]];
245 for(int a=0;a<h;a+=4) {
247 MAC[a+1]=Sbox1[IV[a+1]];
248 MAC[a+2]=Sbox1[IV[a+2]];
249 MAC[a+3]=Sbox1[IV[a+3]];
255 void decrypt_authenticate_algorithm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2,uchar *Sbox1,uchar *Sbox2, uchar* IV, uchar* MAC,mylong myrand) {
262 mylong *rm1=(mylong*)RM1;
263 mylong *rm2=(mylong*)RM2;
265 for(int it=0;it<len;it++) {
270 for(int a=0;a<(h>>3);a++) {
277 /* for(int a=0;a<h;a+=4) {
279 X[a+1]=seq_in[ind2+a+1];
280 X[a+2]=seq_in[ind2+a+2];
281 X[a+3]=seq_in[ind2+a+3];
284 for(int a=0;a<h;a+=4) {
285 tmp[a]=Inv_Sbox2[seq_in[ind2+a]]^RM2[a];
286 tmp[a+1]=Inv_Sbox2[seq_in[ind2+a+1]]^RM2[a+1];
287 tmp[a+2]=Inv_Sbox2[seq_in[ind2+a+2]]^RM2[a+2];
288 tmp[a+3]=Inv_Sbox2[seq_in[ind2+a+3]]^RM2[a+3];
291 for(int a=0;a<h;a+=4) {
292 X[a]=Inv_Sbox1[tmp[a]]^RM1[a];
293 X[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
294 X[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
295 X[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
298 for(int a=0;a<h;a+=4) {
299 seq_out[ind1+a]=X[a];
300 seq_out[ind1+a+1]=X[a+1];
301 seq_out[ind1+a+2]=X[a+2];
302 seq_out[ind1+a+3]=X[a+3];
305 for(int a=0;a<h;a+=4) {
306 IV[a]=Sbox2[IV[a]^tmp[a]];
307 IV[a+1]=Sbox2[IV[a+1]^tmp[a+1]];
308 IV[a+2]=Sbox2[IV[a+2]^tmp[a+2]];
309 IV[a+3]=Sbox2[IV[a+3]^tmp[a+3]];
314 for(int a=0;a<h;a+=4) {
316 MAC[a+1]=Sbox1[IV[a+1]];
317 MAC[a+2]=Sbox1[IV[a+2]];
318 MAC[a+3]=Sbox1[IV[a+3]];
327 void encrypt_authenticate_algorithm_2Blocks(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar* IV1,uchar* IV2,uchar* MAC,mylong myrand) {
334 mylong *rm1=(mylong*)RM1;
335 mylong *rm2=(mylong*)RM2;
337 for(int it=0;it<len/2;it++) {
339 int ind2=Pbox[it+len/2]*h;
341 for(int a=0;a<(h>>3);a++) {
348 for(int a=0;a<h;a+=4) {
350 X[a+1]=seq_in[ind1+a+1];
351 X[a+2]=seq_in[ind1+a+2];
352 X[a+3]=seq_in[ind1+a+3];
355 for(int a=0;a<h;a+=4) {
357 Y[a+1]=seq_in[ind2+a+1];
358 Y[a+2]=seq_in[ind2+a+2];
359 Y[a+3]=seq_in[ind2+a+3];
362 for(int a=0;a<h;a+=4) {
363 tmp1[a]=Sbox1[X[a]^RM1[a]];
364 tmp1[a+1]=Sbox1[X[a+1]^RM1[a+1]];
365 tmp1[a+2]=Sbox1[X[a+2]^RM1[a+2]];
366 tmp1[a+3]=Sbox1[X[a+3]^RM1[a+3]];
369 for(int a=0;a<h;a+=4) {
370 tmp2[a]=Sbox2[Y[a]^RM2[a]];
371 tmp2[a+1]=Sbox2[Y[a+1]^RM2[a+1]];
372 tmp2[a+2]=Sbox2[Y[a+2]^RM2[a+2]];
373 tmp2[a+3]=Sbox2[Y[a+3]^RM2[a+3]];
376 for(int a=0;a<h;a+=4) {
377 X[a]=Sbox2[tmp1[a]^RM2[a]];
378 X[a+1]=Sbox2[tmp1[a+1]^RM2[a+1]];
379 X[a+2]=Sbox2[tmp1[a+2]^RM2[a+2]];
380 X[a+3]=Sbox2[tmp1[a+3]^RM2[a+3]];
383 for(int a=0;a<h;a+=4) {
384 Y[a]=Sbox1[tmp2[a]^RM1[a]];
385 Y[a+1]=Sbox1[tmp2[a+1]^RM1[a+1]];
386 Y[a+2]=Sbox1[tmp2[a+2]^RM1[a+2]];
387 Y[a+3]=Sbox1[tmp2[a+3]^RM1[a+3]];
391 for(int a=0;a<h;a+=4) {
392 seq_out[ind2+a]=Y[a];
393 seq_out[ind2+a+1]=Y[a+1];
394 seq_out[ind2+a+2]=Y[a+2];
395 seq_out[ind2+a+3]=Y[a+3];
398 for(int a=0;a<h;a+=4) {
399 seq_out[ind1+a]=X[a];
400 seq_out[ind1+a+1]=X[a+1];
401 seq_out[ind1+a+2]=X[a+2];
402 seq_out[ind1+a+3]=X[a+3];
406 for(int a=0;a<h;a+=4) {
407 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
408 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
409 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
410 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
413 for(int a=0;a<h;a+=4) {
414 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
415 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
416 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
417 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
421 for(int a=0;a<h;a+=4) {
422 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
423 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
424 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
425 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
433 void decrypt_authenticate_algorithm_2Blocks(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *Sbox1, uchar *Sbox2, uchar* IV1,uchar* IV2,uchar* MAC,mylong myrand) {
440 mylong *rm1=(mylong*)RM1;
441 mylong *rm2=(mylong*)RM2;
443 for(int it=0;it<len/2;it++) {
445 int ind2=Pbox[it+len/2]*h;
447 for(int a=0;a<(h>>3);a++) {
454 for(int a=0;a<h;a+=4) {
456 X[a+1]=seq_in[ind1+a+1];
457 X[a+2]=seq_in[ind1+a+2];
458 X[a+3]=seq_in[ind1+a+3];
461 for(int a=0;a<h;a+=4) {
463 Y[a+1]=seq_in[ind2+a+1];
464 Y[a+2]=seq_in[ind2+a+2];
465 Y[a+3]=seq_in[ind2+a+3];
468 for(int a=0;a<h;a+=4) {
469 tmp1[a]=Inv_Sbox2[X[a]]^RM2[a];
470 tmp1[a+1]=Inv_Sbox2[X[a+1]]^RM2[a+1];
471 tmp1[a+2]=Inv_Sbox2[X[a+2]]^RM2[a+2];
472 tmp1[a+3]=Inv_Sbox2[X[a+3]]^RM2[a+3];
475 for(int a=0;a<h;a+=4) {
476 tmp2[a]=Inv_Sbox1[Y[a]]^RM1[a];
477 tmp2[a+1]=Inv_Sbox1[Y[a+1]]^RM1[a+1];
478 tmp2[a+2]=Inv_Sbox1[Y[a+2]]^RM1[a+2];
479 tmp2[a+3]=Inv_Sbox1[Y[a+3]]^RM1[a+3];
483 for(int a=0;a<h;a+=4) {
484 X[a]=Inv_Sbox1[tmp1[a]]^RM1[a];
485 X[a+1]=Inv_Sbox1[tmp1[a+1]]^RM1[a+1];
486 X[a+2]=Inv_Sbox1[tmp1[a+2]]^RM1[a+2];
487 X[a+3]=Inv_Sbox1[tmp1[a+3]]^RM1[a+3];
490 for(int a=0;a<h;a+=4) {
491 Y[a]=Inv_Sbox2[tmp2[a]]^RM2[a];
492 Y[a+1]=Inv_Sbox2[tmp2[a+1]]^RM2[a+1];
493 Y[a+2]=Inv_Sbox2[tmp2[a+2]]^RM2[a+2];
494 Y[a+3]=Inv_Sbox2[tmp2[a+3]]^RM2[a+3];
497 for(int a=0;a<h;a+=4) {
498 seq_out[ind2+a]=Y[a];
499 seq_out[ind2+a+1]=Y[a+1];
500 seq_out[ind2+a+2]=Y[a+2];
501 seq_out[ind2+a+3]=Y[a+3];
504 for(int a=0;a<h;a+=4) {
505 seq_out[ind1+a]=X[a];
506 seq_out[ind1+a+1]=X[a+1];
507 seq_out[ind1+a+2]=X[a+2];
508 seq_out[ind1+a+3]=X[a+3];
512 for(int a=0;a<h;a+=4) {
513 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
514 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
515 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
516 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
519 for(int a=0;a<h;a+=4) {
520 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
521 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
522 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
523 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
527 for(int a=0;a<h;a+=4) {
528 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
529 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
530 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
531 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
539 void encrypt_authenticate_algorithm_2Blocks_V2(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar* IV1,uchar* IV2,uchar* MAC,mylong myrand) {
546 mylong *rm1=(mylong*)RM1;
547 mylong *rm2=(mylong*)RM2;
549 for(int it=0;it<len/2;it++) {
551 int ind2=Pbox[it+len/2]*h;
553 for(int a=0;a<(h>>3);a++) {
560 for(int a=0;a<h;a+=4) {
562 X[a+1]=seq_in[ind1+a+1];
563 X[a+2]=seq_in[ind1+a+2];
564 X[a+3]=seq_in[ind1+a+3];
567 for(int a=0;a<h;a+=4) {
569 Y[a+1]=seq_in[ind2+a+1];
570 Y[a+2]=seq_in[ind2+a+2];
571 Y[a+3]=seq_in[ind2+a+3];
574 for(int a=0;a<h;a+=4) {
575 tmp1[a]=Sbox1[X[a]^RM1[a]]^Y[a];
576 tmp1[a+1]=Sbox1[X[a+1]^RM1[a+1]]^Y[a+1];
577 tmp1[a+2]=Sbox1[X[a+2]^RM1[a+2]]^Y[a+2];
578 tmp1[a+3]=Sbox1[X[a+3]^RM1[a+3]]^Y[a+3];
581 for(int a=0;a<h;a+=4) {
582 tmp2[a]=Sbox2[Y[a]^RM2[a]];
583 tmp2[a+1]=Sbox2[Y[a+1]^RM2[a+1]];
584 tmp2[a+2]=Sbox2[Y[a+2]^RM2[a+2]];
585 tmp2[a+3]=Sbox2[Y[a+3]^RM2[a+3]];
588 for(int a=0;a<h;a+=4) {
589 X[a]=Sbox2[tmp1[a]^RM2[a]];
590 X[a+1]=Sbox2[tmp1[a+1]^RM2[a+1]];
591 X[a+2]=Sbox2[tmp1[a+2]^RM2[a+2]];
592 X[a+3]=Sbox2[tmp1[a+3]^RM2[a+3]];
595 for(int a=0;a<h;a+=4) {
596 Y[a]=Sbox1[tmp2[a]^RM1[a]];
597 Y[a+1]=Sbox1[tmp2[a+1]^RM1[a+1]];
598 Y[a+2]=Sbox1[tmp2[a+2]^RM1[a+2]];
599 Y[a+3]=Sbox1[tmp2[a+3]^RM1[a+3]];
603 for(int a=0;a<h;a+=4) {
604 seq_out[ind2+a]=Y[a];
605 seq_out[ind2+a+1]=Y[a+1];
606 seq_out[ind2+a+2]=Y[a+2];
607 seq_out[ind2+a+3]=Y[a+3];
610 for(int a=0;a<h;a+=4) {
611 seq_out[ind1+a]=X[a];
612 seq_out[ind1+a+1]=X[a+1];
613 seq_out[ind1+a+2]=X[a+2];
614 seq_out[ind1+a+3]=X[a+3];
618 for(int a=0;a<h;a+=4) {
619 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
620 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
621 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
622 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
625 for(int a=0;a<h;a+=4) {
626 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
627 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
628 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
629 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
633 for(int a=0;a<h;a+=4) {
634 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
635 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
636 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
637 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
646 void decrypt_authenticate_algorithm_2Blocks_V2(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *Sbox1, uchar *Sbox2, uchar* IV1,uchar* IV2, uchar* MAC,mylong myrand) {
653 mylong *rm1=(mylong*)RM1;
654 mylong *rm2=(mylong*)RM2;
656 for(int it=0;it<len/2;it++) {
658 int ind2=Pbox[it+len/2]*h;
660 for(int a=0;a<(h>>3);a++) {
667 for(int a=0;a<h;a+=4) {
669 X[a+1]=seq_in[ind1+a+1];
670 X[a+2]=seq_in[ind1+a+2];
671 X[a+3]=seq_in[ind1+a+3];
674 for(int a=0;a<h;a+=4) {
676 Y[a+1]=seq_in[ind2+a+1];
677 Y[a+2]=seq_in[ind2+a+2];
678 Y[a+3]=seq_in[ind2+a+3];
682 for(int a=0;a<h;a+=4) {
683 tmp2[a]=Inv_Sbox1[Y[a]]^RM1[a];
684 tmp2[a+1]=Inv_Sbox1[Y[a+1]]^RM1[a+1];
685 tmp2[a+2]=Inv_Sbox1[Y[a+2]]^RM1[a+2];
686 tmp2[a+3]=Inv_Sbox1[Y[a+3]]^RM1[a+3];
689 for(int a=0;a<h;a+=4) {
690 tmp1[a]=Inv_Sbox2[X[a]]^RM2[a];
691 tmp1[a+1]=Inv_Sbox2[X[a+1]]^RM2[a+1];
692 tmp1[a+2]=Inv_Sbox2[X[a+2]]^RM2[a+2];
693 tmp1[a+3]=Inv_Sbox2[X[a+3]]^RM2[a+3];
696 for(int a=0;a<h;a+=4) {
697 Y[a]=Inv_Sbox2[tmp2[a]]^RM2[a];
698 Y[a+1]=Inv_Sbox2[tmp2[a+1]]^RM2[a+1];
699 Y[a+2]=Inv_Sbox2[tmp2[a+2]]^RM2[a+2];
700 Y[a+3]=Inv_Sbox2[tmp2[a+3]]^RM2[a+3];
703 for(int a=0;a<h;a+=4) {
704 X[a] =Inv_Sbox1[tmp1[a]^Y[a]]^RM1[a];
705 X[a+1] =Inv_Sbox1[tmp1[a+1]^Y[a+1]]^RM1[a+1];
706 X[a+2] =Inv_Sbox1[tmp1[a+2]^Y[a+2]]^RM1[a+2];
707 X[a+3] =Inv_Sbox1[tmp1[a+3]^Y[a+3]]^RM1[a+3];
711 for(int a=0;a<h;a+=4) {
712 seq_out[ind2+a]=Y[a];
713 seq_out[ind2+a+1]=Y[a+1];
714 seq_out[ind2+a+2]=Y[a+2];
715 seq_out[ind2+a+3]=Y[a+3];
718 for(int a=0;a<h;a+=4) {
719 seq_out[ind1+a]=X[a];
720 seq_out[ind1+a+1]=X[a+1];
721 seq_out[ind1+a+2]=X[a+2];
722 seq_out[ind1+a+3]=X[a+3];
725 for(int a=0;a<h;a+=4) {
726 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
727 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
728 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
729 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
732 for(int a=0;a<h;a+=4) {
733 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
734 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
735 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
736 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
740 for(int a=0;a<h;a+=4) {
741 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
742 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
743 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
744 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
749 void encrypt_authenticate_algorithm_2Blocks_V3(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar* IV,uchar* MAC,mylong myrand) {
752 mylong *rm1=(mylong*)RM1;
754 for(int it=0;it<len/2;it++) {
756 int ind2=Pbox[it+len/2]*h;
758 for(int a=0;a<(h>>3);a++) {
763 for(int a=0;a<h;a+=4) {
764 tmp1[a]=Sbox1[seq_in[ind1+a]^RM1[a]];
765 tmp1[a+1]=Sbox1[seq_in[ind1+a+1]^RM1[a+1]];
766 tmp1[a+2]=Sbox1[seq_in[ind1+a+2]^RM1[a+2]];
767 tmp1[a+3]=Sbox1[seq_in[ind1+a+3]^RM1[a+3]];
770 for(int a=0;a<h;a+=4) {
771 seq_out[ind2+a]=Sbox2[seq_in[ind2+a]^tmp1[a]];
772 seq_out[ind2+a+1]=Sbox2[seq_in[ind2+a+1]^tmp1[a+1]];
773 seq_out[ind2+a+2]=Sbox2[seq_in[ind2+a+2]^tmp1[a+2]];
774 seq_out[ind2+a+3]=Sbox2[seq_in[ind2+a+3]^tmp1[a+3]];
778 for(int a=0;a<h;a+=4) {
779 seq_out[ind1+a]=Sbox2[tmp1[a]];
780 seq_out[ind1+a+1]=Sbox2[tmp1[a+1]];
781 seq_out[ind1+a+2]=Sbox2[tmp1[a+2]];
782 seq_out[ind1+a+3]=Sbox2[tmp1[a+3]];
785 for(int a=0;a<h;a+=4) {
786 IV[a]=Sbox1[IV[a]^seq_out[ind2+a]]^tmp1[a];
787 IV[a+1]=Sbox1[IV[a+1]^seq_out[ind2+a+1]]^tmp1[a+1];
788 IV[a+2]=Sbox1[IV[a+2]^seq_out[ind2+a+2]]^tmp1[a+2];
789 IV[a+3]=Sbox1[IV[a+3]^seq_out[ind2+a+3]]^tmp1[a+3];
793 for(int a=0;a<h;a+=4) {
795 MAC[a+1]=Sbox2[IV[a+1]];
796 MAC[a+2]=Sbox2[IV[a+2]];
797 MAC[a+3]=Sbox2[IV[a+3]];
803 void decrypt_authenticate_algorithm_2Blocks_V3(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2 ,uchar *Sbox1, uchar *Sbox2, uchar* IV,uchar* MAC,mylong myrand) {
806 mylong *rm1=(mylong*)RM1;
808 for(int it=0;it<len/2;it++) {
810 int ind2=Pbox[it+len/2]*h;
812 for(int a=0;a<(h>>3);a++) {
817 for(int a=0;a<h;a+=4) {
818 tmp1[a]=Inv_Sbox2[seq_in[ind1+a]];
819 tmp1[a+1]=Inv_Sbox2[seq_in[ind1+a+1]];
820 tmp1[a+2]=Inv_Sbox2[seq_in[ind1+a+2]];
821 tmp1[a+3]=Inv_Sbox2[seq_in[ind1+a+3]];
824 for(int a=0;a<h;a+=4) {
825 seq_out[ind2+a]=Inv_Sbox2[seq_in[ind2+a]]^tmp1[a];
826 seq_out[ind2+a+1]=Inv_Sbox2[seq_in[ind2+a+1]]^tmp1[a+1];
827 seq_out[ind2+a+2]=Inv_Sbox2[seq_in[ind2+a+2]]^tmp1[a+2];
828 seq_out[ind2+a+3]=Inv_Sbox2[seq_in[ind2+a+3]]^tmp1[a+3];
831 for(int a=0;a<h;a+=4) {
832 seq_out[ind1+a] =Inv_Sbox1[tmp1[a]]^RM1[a];
833 seq_out[ind1+a+1]=Inv_Sbox1[tmp1[a+1]]^RM1[a+1];
834 seq_out[ind1+a+2]=Inv_Sbox1[tmp1[a+2]]^RM1[a+2];
835 seq_out[ind1+a+3]=Inv_Sbox1[tmp1[a+3]]^RM1[a+3];
838 for(int a=0;a<h;a+=4) {
839 IV[a]=Sbox1[IV[a]^seq_in[ind2+a]]^tmp1[a];
840 IV[a+1]=Sbox1[IV[a+1]^seq_in[ind2+a+1]]^tmp1[a+1];
841 IV[a+2]=Sbox1[IV[a+2]^seq_in[ind2+a+2]]^tmp1[a+2];
842 IV[a+3]=Sbox1[IV[a+3]^seq_in[ind2+a+3]]^tmp1[a+3];
846 for(int a=0;a<h;a+=4) {
848 MAC[a+1]=Sbox2[IV[a+1]];
849 MAC[a+2]=Sbox2[IV[a+2]];
850 MAC[a+3]=Sbox2[IV[a+3]];
855 int main(int argc, char** argv) {
864 for(int i=1; i<argc; i++){
865 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
866 if(strncmp(argv[i],"v1b",3)==0) v1b=1;
867 if(strncmp(argv[i],"v2b1",4)==0) v2b1=1;
868 if(strncmp(argv[i],"v2b2",4)==0) v2b2 = 1;
869 if(strncmp(argv[i],"v2b3",4)==0) v2b3 = 1;
870 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
871 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
872 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
875 /* printf("nb times %d\n",nb_test);
876 printf("cbcrm %d\n",cbcrm);
877 printf("cbcprng %d\n",cbcprng);
878 printf("ecbrm %d\n",ecbrm);
879 printf("ecbprng %d\n",ecbprng);
881 printf("lena %d\n",lena);
882 printf("size_buf %d\n",size_buf);
891 uchar Secretkey[key_size];
893 uchar counter[key_size];
895 for(int i=0;i<key_size;i++) {
896 Secretkey[i]=lrand48()&0xFF;
897 counter[i]=lrand48()&0xFF;
910 uchar *data_R, *data_G, *data_B;
919 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
920 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
921 imsize=width*height*3;
922 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
925 width=height=size_buf;
926 imsize=width*height*3;
927 //cout<<"imsize "<<imsize<<endl;
928 buffer=new uchar[imsize];
929 for(int i=0;i<imsize;i++) {
936 cout<<"imsize "<<imsize<<endl;
938 uchar* seq= new uchar[imsize];
939 uchar* seq2= new uchar[imsize];
941 int oneD=width*height;
943 for(int i=0;i<oneD;i++) {
945 seq[oneD+i]=data_G[i];
946 seq[2*oneD+i]=data_B[i];
950 for(int i=0;i<oneD*3;i++) {
959 int total_len=imsize;
961 int len= total_len/h;
965 uchar *mix=new uchar[256];
970 for (int i = 0; i < 256 ; i++) {
971 mix[i]=Secretkey[i]^counter[i];
976 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
977 // g_print("%s\n", sha512);
987 // cout<<"hash "<<endl;
988 for (int i = 0; i < 128 ; i++) {
995 int *Pbox=new int[len];
996 int *PboxSRM=new int[len/2];
997 int *PboxSRM2=new int[len/2];
1000 uchar Inv_Sbox1[256];
1001 uchar Inv_Sbox2[256];
1003 uchar RM[h*h*2+256];
1013 double time_encrypt=0;
1014 double time_decrypt=0;
1017 double t=TimeStart();
1019 for(int i=0;i<h;i++) {
1027 for(int i=0;i<nb_test;i++) {
1029 rc4key(DK, Sbox1, 8);
1032 rc4key(&DK[8], Sbox2, 8);
1034 rc4key(&DK[16], sc, 16);
1035 prga(sc, h*h*2+256, RM);
1041 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1044 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1046 for(int i=0;i<len/2;i++) {
1047 PboxSRM[i]=PboxSRM2[i]&(h-1);
1051 for(int i=0;i<h*2;i++) {
1052 for(int j=0;j<h;j++)
1053 cout<<(int)RM[i*h+j]<<" ";
1060 time_init+=TimeStop(t);
1061 cout<<"Time initializaton nb times "<<nb_test<<" = "<<time_init<<endl;
1066 for(int i=0;i<64;i++) {
1077 inverse_tables(Sbox1,256,Inv_Sbox1);
1078 inverse_tables(Sbox2,256,Inv_Sbox2);
1082 // lehmer64_seed(myrand);
1089 for(i=0;i<nb_test;i++)
1092 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1094 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1096 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
1098 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
1102 for(i=0;i<nb_test;i++)
1105 encrypt_authenticate_algorithm<32>(seq, seq2, len, RM, Pbox, PboxSRM, Sbox1, Sbox2, IV1, MAC, myrand);
1107 encrypt_authenticate_algorithm_2Blocks<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
1109 encrypt_authenticate_algorithm_2Blocks_V2<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
1111 encrypt_authenticate_algorithm_2Blocks_V3<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV1,MAC,myrand);
1117 time_encrypt+=TimeStop(t);
1118 cout<<"Time encrypt "<<time_encrypt<<endl;
1119 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
1121 /* for(int i=0;i<h;i++)
1122 printf("%d ",MAC[i]);
1127 for(int i=0;i<oneD;i++) {
1129 data_G[i]=seq2[oneD+i];
1130 data_B[i]=seq2[2*oneD+i];
1132 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
1135 for(int i=0;i<h;i++) {
1143 // lehmer64_seed(myrand);
1148 for(i=0;i<nb_test;i++) {
1150 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
1152 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
1154 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
1156 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
1160 for(i=0;i<nb_test;i++) {
1162 decrypt_authenticate_algorithm<32>(seq2, seq,len,RM,Pbox,PboxSRM,Inv_Sbox1,Inv_Sbox2,Sbox1,Sbox2,IV1,MAC,myrand);
1164 decrypt_authenticate_algorithm_2Blocks<32>(seq2, seq,len,RM,Pbox,PboxSRM,Inv_Sbox1,Inv_Sbox2,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
1166 decrypt_authenticate_algorithm_2Blocks_V2<32>(seq2, seq,len,RM,Pbox,PboxSRM,Inv_Sbox1,Inv_Sbox2,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
1168 decrypt_authenticate_algorithm_2Blocks_V3<32>(seq2, seq,len,RM,Pbox,PboxSRM,Inv_Sbox1,Inv_Sbox2,Sbox1,Sbox2,IV1,MAC,myrand);
1176 time_decrypt+=TimeStop(t);
1177 // cout<<"Time decrypt "<<time_decrypt<<endl;
1178 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
1179 /* for(int i=0;i<h;i++)
1180 printf("%d ",MAC[i]);
1185 for(int i=0;i<oneD;i++) {
1187 data_G[i]=seq[oneD+i];
1188 data_B[i]=seq[2*oneD+i];
1190 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
1194 for(int i=0;i<imsize;i++) {
1195 //cout<<(int)buffer[i]<<endl;
1196 if(buffer[i]!=seq[i]) {
1200 //cout<<"RESULT CORRECT: "<<equal<<endl;