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];
192 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) {
198 mylong *rm1=(mylong*)RM1;
199 mylong *rm2=(mylong*)RM2;
201 for(int it=0;it<len;it++) {
205 for(int a=0;a<(h>>3);a++) {
212 for(int a=0;a<h;a+=4) {
214 X[a+1]=seq_in[ind2+a+1];
215 X[a+2]=seq_in[ind2+a+2];
216 X[a+3]=seq_in[ind2+a+3];
219 for(int a=0;a<h;a+=4) {
220 tmp[a]=Sbox1[X[a]^RM1[a]];
221 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
222 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
223 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
226 for(int a=0;a<h;a+=4) {
227 X[a]=Sbox2[tmp[a]^RM2[a]];
228 X[a+1]=Sbox2[tmp[a+1]^RM2[a+1]];
229 X[a+2]=Sbox2[tmp[a+2]^RM2[a+2]];
230 X[a+3]=Sbox2[tmp[a+3]^RM2[a+3]];
233 for(int a=0;a<h;a+=4) {
234 seq_out[ind1+a]=X[a];
235 seq_out[ind1+a+1]=X[a+1];
236 seq_out[ind1+a+2]=X[a+2];
237 seq_out[ind1+a+3]=X[a+3];
240 for(int a=0;a<h;a+=4) {
241 IV[a]=Sbox2[IV[a]^tmp[a]];
242 IV[a+1]=Sbox2[IV[a+1]^tmp[a+1]];
243 IV[a+2]=Sbox2[IV[a+2]^tmp[a+2]];
244 IV[a+3]=Sbox2[IV[a+3]^tmp[a+3]];
249 for(int a=0;a<h;a+=4) {
251 MAC[a+1]=Sbox1[IV[a+1]];
252 MAC[a+2]=Sbox1[IV[a+2]];
253 MAC[a+3]=Sbox1[IV[a+3]];
259 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) {
266 mylong *rm1=(mylong*)RM1;
267 mylong *rm2=(mylong*)RM2;
269 for(int it=0;it<len;it++) {
274 for(int a=0;a<(h>>3);a++) {
281 /* for(int a=0;a<h;a+=4) {
283 X[a+1]=seq_in[ind2+a+1];
284 X[a+2]=seq_in[ind2+a+2];
285 X[a+3]=seq_in[ind2+a+3];
288 for(int a=0;a<h;a+=4) {
289 tmp[a]=Inv_Sbox2[seq_in[ind2+a]]^RM2[a];
290 tmp[a+1]=Inv_Sbox2[seq_in[ind2+a+1]]^RM2[a+1];
291 tmp[a+2]=Inv_Sbox2[seq_in[ind2+a+2]]^RM2[a+2];
292 tmp[a+3]=Inv_Sbox2[seq_in[ind2+a+3]]^RM2[a+3];
295 /* for(int a=0;a<h;a+=4) {
296 X[a]=Inv_Sbox1[tmp[a]]^RM1[a];
297 X[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
298 X[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
299 X[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
302 for(int a=0;a<h;a+=4) {
303 seq_out[ind1+a]=Inv_Sbox1[tmp[a]]^RM1[a];
304 seq_out[ind1+a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
305 seq_out[ind1+a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
306 seq_out[ind1+a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
309 for(int a=0;a<h;a+=4) {
310 IV[a]=Sbox2[IV[a]^tmp[a]];
311 IV[a+1]=Sbox2[IV[a+1]^tmp[a+1]];
312 IV[a+2]=Sbox2[IV[a+2]^tmp[a+2]];
313 IV[a+3]=Sbox2[IV[a+3]^tmp[a+3]];
318 for(int a=0;a<h;a+=4) {
320 MAC[a+1]=Sbox1[IV[a+1]];
321 MAC[a+2]=Sbox1[IV[a+2]];
322 MAC[a+3]=Sbox1[IV[a+3]];
331 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) {
338 mylong *rm1=(mylong*)RM1;
339 mylong *rm2=(mylong*)RM2;
341 for(int it=0;it<len/2;it++) {
343 int ind2=Pbox[it+len/2]*h;
345 for(int a=0;a<(h>>3);a++) {
352 for(int a=0;a<h;a+=4) {
354 X[a+1]=seq_in[ind1+a+1];
355 X[a+2]=seq_in[ind1+a+2];
356 X[a+3]=seq_in[ind1+a+3];
359 for(int a=0;a<h;a+=4) {
361 Y[a+1]=seq_in[ind2+a+1];
362 Y[a+2]=seq_in[ind2+a+2];
363 Y[a+3]=seq_in[ind2+a+3];
366 for(int a=0;a<h;a+=4) {
367 tmp1[a]=Sbox1[X[a]^RM1[a]];
368 tmp1[a+1]=Sbox1[X[a+1]^RM1[a+1]];
369 tmp1[a+2]=Sbox1[X[a+2]^RM1[a+2]];
370 tmp1[a+3]=Sbox1[X[a+3]^RM1[a+3]];
373 for(int a=0;a<h;a+=4) {
374 tmp2[a]=Sbox2[Y[a]^RM2[a]];
375 tmp2[a+1]=Sbox2[Y[a+1]^RM2[a+1]];
376 tmp2[a+2]=Sbox2[Y[a+2]^RM2[a+2]];
377 tmp2[a+3]=Sbox2[Y[a+3]^RM2[a+3]];
380 for(int a=0;a<h;a+=4) {
381 X[a]=Sbox2[tmp1[a]^RM2[a]];
382 X[a+1]=Sbox2[tmp1[a+1]^RM2[a+1]];
383 X[a+2]=Sbox2[tmp1[a+2]^RM2[a+2]];
384 X[a+3]=Sbox2[tmp1[a+3]^RM2[a+3]];
387 for(int a=0;a<h;a+=4) {
388 Y[a]=Sbox1[tmp2[a]^RM1[a]];
389 Y[a+1]=Sbox1[tmp2[a+1]^RM1[a+1]];
390 Y[a+2]=Sbox1[tmp2[a+2]^RM1[a+2]];
391 Y[a+3]=Sbox1[tmp2[a+3]^RM1[a+3]];
395 for(int a=0;a<h;a+=4) {
396 seq_out[ind2+a]=Y[a];
397 seq_out[ind2+a+1]=Y[a+1];
398 seq_out[ind2+a+2]=Y[a+2];
399 seq_out[ind2+a+3]=Y[a+3];
402 for(int a=0;a<h;a+=4) {
403 seq_out[ind1+a]=X[a];
404 seq_out[ind1+a+1]=X[a+1];
405 seq_out[ind1+a+2]=X[a+2];
406 seq_out[ind1+a+3]=X[a+3];
410 for(int a=0;a<h;a+=4) {
411 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
412 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
413 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
414 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
417 for(int a=0;a<h;a+=4) {
418 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
419 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
420 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
421 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
425 for(int a=0;a<h;a+=4) {
426 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
427 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
428 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
429 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
437 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) {
444 mylong *rm1=(mylong*)RM1;
445 mylong *rm2=(mylong*)RM2;
447 for(int it=0;it<len/2;it++) {
449 int ind2=Pbox[it+len/2]*h;
451 for(int a=0;a<(h>>3);a++) {
458 for(int a=0;a<h;a+=4) {
460 X[a+1]=seq_in[ind1+a+1];
461 X[a+2]=seq_in[ind1+a+2];
462 X[a+3]=seq_in[ind1+a+3];
465 for(int a=0;a<h;a+=4) {
467 Y[a+1]=seq_in[ind2+a+1];
468 Y[a+2]=seq_in[ind2+a+2];
469 Y[a+3]=seq_in[ind2+a+3];
472 for(int a=0;a<h;a+=4) {
473 tmp1[a]=Inv_Sbox2[X[a]]^RM2[a];
474 tmp1[a+1]=Inv_Sbox2[X[a+1]]^RM2[a+1];
475 tmp1[a+2]=Inv_Sbox2[X[a+2]]^RM2[a+2];
476 tmp1[a+3]=Inv_Sbox2[X[a+3]]^RM2[a+3];
479 for(int a=0;a<h;a+=4) {
480 tmp2[a]=Inv_Sbox1[Y[a]]^RM1[a];
481 tmp2[a+1]=Inv_Sbox1[Y[a+1]]^RM1[a+1];
482 tmp2[a+2]=Inv_Sbox1[Y[a+2]]^RM1[a+2];
483 tmp2[a+3]=Inv_Sbox1[Y[a+3]]^RM1[a+3];
487 for(int a=0;a<h;a+=4) {
488 X[a]=Inv_Sbox1[tmp1[a]]^RM1[a];
489 X[a+1]=Inv_Sbox1[tmp1[a+1]]^RM1[a+1];
490 X[a+2]=Inv_Sbox1[tmp1[a+2]]^RM1[a+2];
491 X[a+3]=Inv_Sbox1[tmp1[a+3]]^RM1[a+3];
494 for(int a=0;a<h;a+=4) {
495 Y[a]=Inv_Sbox2[tmp2[a]]^RM2[a];
496 Y[a+1]=Inv_Sbox2[tmp2[a+1]]^RM2[a+1];
497 Y[a+2]=Inv_Sbox2[tmp2[a+2]]^RM2[a+2];
498 Y[a+3]=Inv_Sbox2[tmp2[a+3]]^RM2[a+3];
501 for(int a=0;a<h;a+=4) {
502 seq_out[ind2+a]=Y[a];
503 seq_out[ind2+a+1]=Y[a+1];
504 seq_out[ind2+a+2]=Y[a+2];
505 seq_out[ind2+a+3]=Y[a+3];
508 for(int a=0;a<h;a+=4) {
509 seq_out[ind1+a]=X[a];
510 seq_out[ind1+a+1]=X[a+1];
511 seq_out[ind1+a+2]=X[a+2];
512 seq_out[ind1+a+3]=X[a+3];
516 for(int a=0;a<h;a+=4) {
517 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
518 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
519 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
520 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
523 for(int a=0;a<h;a+=4) {
524 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
525 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
526 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
527 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
531 for(int a=0;a<h;a+=4) {
532 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
533 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
534 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
535 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
543 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) {
550 mylong *rm1=(mylong*)RM1;
551 mylong *rm2=(mylong*)RM2;
553 for(int it=0;it<len/2;it++) {
555 int ind2=Pbox[it+len/2]*h;
557 for(int a=0;a<(h>>3);a++) {
564 for(int a=0;a<h;a+=4) {
566 X[a+1]=seq_in[ind1+a+1];
567 X[a+2]=seq_in[ind1+a+2];
568 X[a+3]=seq_in[ind1+a+3];
571 for(int a=0;a<h;a+=4) {
573 Y[a+1]=seq_in[ind2+a+1];
574 Y[a+2]=seq_in[ind2+a+2];
575 Y[a+3]=seq_in[ind2+a+3];
578 for(int a=0;a<h;a+=4) {
579 tmp1[a]=Sbox1[X[a]^RM1[a]]^Y[a];
580 tmp1[a+1]=Sbox1[X[a+1]^RM1[a+1]]^Y[a+1];
581 tmp1[a+2]=Sbox1[X[a+2]^RM1[a+2]]^Y[a+2];
582 tmp1[a+3]=Sbox1[X[a+3]^RM1[a+3]]^Y[a+3];
585 for(int a=0;a<h;a+=4) {
586 tmp2[a]=Sbox2[Y[a]^RM2[a]];
587 tmp2[a+1]=Sbox2[Y[a+1]^RM2[a+1]];
588 tmp2[a+2]=Sbox2[Y[a+2]^RM2[a+2]];
589 tmp2[a+3]=Sbox2[Y[a+3]^RM2[a+3]];
592 for(int a=0;a<h;a+=4) {
593 X[a]=Sbox2[tmp1[a]^RM2[a]];
594 X[a+1]=Sbox2[tmp1[a+1]^RM2[a+1]];
595 X[a+2]=Sbox2[tmp1[a+2]^RM2[a+2]];
596 X[a+3]=Sbox2[tmp1[a+3]^RM2[a+3]];
599 for(int a=0;a<h;a+=4) {
600 Y[a]=Sbox1[tmp2[a]^RM1[a]];
601 Y[a+1]=Sbox1[tmp2[a+1]^RM1[a+1]];
602 Y[a+2]=Sbox1[tmp2[a+2]^RM1[a+2]];
603 Y[a+3]=Sbox1[tmp2[a+3]^RM1[a+3]];
607 for(int a=0;a<h;a+=4) {
608 seq_out[ind2+a]=Y[a];
609 seq_out[ind2+a+1]=Y[a+1];
610 seq_out[ind2+a+2]=Y[a+2];
611 seq_out[ind2+a+3]=Y[a+3];
614 for(int a=0;a<h;a+=4) {
615 seq_out[ind1+a]=X[a];
616 seq_out[ind1+a+1]=X[a+1];
617 seq_out[ind1+a+2]=X[a+2];
618 seq_out[ind1+a+3]=X[a+3];
622 for(int a=0;a<h;a+=4) {
623 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
624 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
625 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
626 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
629 for(int a=0;a<h;a+=4) {
630 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
631 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
632 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
633 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
637 for(int a=0;a<h;a+=4) {
638 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
639 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
640 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
641 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
650 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) {
657 mylong *rm1=(mylong*)RM1;
658 mylong *rm2=(mylong*)RM2;
660 for(int it=0;it<len/2;it++) {
662 int ind2=Pbox[it+len/2]*h;
664 for(int a=0;a<(h>>3);a++) {
671 for(int a=0;a<h;a+=4) {
673 X[a+1]=seq_in[ind1+a+1];
674 X[a+2]=seq_in[ind1+a+2];
675 X[a+3]=seq_in[ind1+a+3];
678 for(int a=0;a<h;a+=4) {
680 Y[a+1]=seq_in[ind2+a+1];
681 Y[a+2]=seq_in[ind2+a+2];
682 Y[a+3]=seq_in[ind2+a+3];
686 for(int a=0;a<h;a+=4) {
687 tmp2[a]=Inv_Sbox1[Y[a]]^RM1[a];
688 tmp2[a+1]=Inv_Sbox1[Y[a+1]]^RM1[a+1];
689 tmp2[a+2]=Inv_Sbox1[Y[a+2]]^RM1[a+2];
690 tmp2[a+3]=Inv_Sbox1[Y[a+3]]^RM1[a+3];
693 for(int a=0;a<h;a+=4) {
694 tmp1[a]=Inv_Sbox2[X[a]]^RM2[a];
695 tmp1[a+1]=Inv_Sbox2[X[a+1]]^RM2[a+1];
696 tmp1[a+2]=Inv_Sbox2[X[a+2]]^RM2[a+2];
697 tmp1[a+3]=Inv_Sbox2[X[a+3]]^RM2[a+3];
700 for(int a=0;a<h;a+=4) {
701 Y[a]=Inv_Sbox2[tmp2[a]]^RM2[a];
702 Y[a+1]=Inv_Sbox2[tmp2[a+1]]^RM2[a+1];
703 Y[a+2]=Inv_Sbox2[tmp2[a+2]]^RM2[a+2];
704 Y[a+3]=Inv_Sbox2[tmp2[a+3]]^RM2[a+3];
707 for(int a=0;a<h;a+=4) {
708 X[a] =Inv_Sbox1[tmp1[a]^Y[a]]^RM1[a];
709 X[a+1] =Inv_Sbox1[tmp1[a+1]^Y[a+1]]^RM1[a+1];
710 X[a+2] =Inv_Sbox1[tmp1[a+2]^Y[a+2]]^RM1[a+2];
711 X[a+3] =Inv_Sbox1[tmp1[a+3]^Y[a+3]]^RM1[a+3];
715 for(int a=0;a<h;a+=4) {
716 seq_out[ind2+a]=Y[a];
717 seq_out[ind2+a+1]=Y[a+1];
718 seq_out[ind2+a+2]=Y[a+2];
719 seq_out[ind2+a+3]=Y[a+3];
722 for(int a=0;a<h;a+=4) {
723 seq_out[ind1+a]=X[a];
724 seq_out[ind1+a+1]=X[a+1];
725 seq_out[ind1+a+2]=X[a+2];
726 seq_out[ind1+a+3]=X[a+3];
729 for(int a=0;a<h;a+=4) {
730 IV2[a]=Sbox2[IV1[a]^tmp1[a]];
731 IV2[a+1]=Sbox2[IV1[a+1]^tmp1[a+1]];
732 IV2[a+2]=Sbox2[IV1[a+2]^tmp1[a+2]];
733 IV2[a+3]=Sbox2[IV1[a+3]^tmp1[a+3]];
736 for(int a=0;a<h;a+=4) {
737 IV1[a]=Sbox1[IV2[a]^tmp2[a]];
738 IV1[a+1]=Sbox1[IV2[a+1]^tmp2[a+1]];
739 IV1[a+2]=Sbox1[IV2[a+2]^tmp2[a+2]];
740 IV1[a+3]=Sbox1[IV2[a+3]^tmp2[a+3]];
744 for(int a=0;a<h;a+=4) {
745 MAC[a]=Sbox1[IV2[a]]^Sbox2[IV1[a]];
746 MAC[a+1]=Sbox1[IV2[a+1]]^Sbox2[IV1[a+1]];
747 MAC[a+2]=Sbox1[IV2[a+2]]^Sbox2[IV1[a+2]];
748 MAC[a+3]=Sbox1[IV2[a+3]]^Sbox2[IV1[a+3]];
755 int main(int argc, char** argv) {
764 for(int i=1; i<argc; i++){
765 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
766 if(strncmp(argv[i],"v1b",3)==0) v1b=1;
767 if(strncmp(argv[i],"v2b1",4)==0) v2b1=1;
768 if(strncmp(argv[i],"v2b2",4)==0) v2b2 = 1;
769 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
770 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
771 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
774 /* printf("nb times %d\n",nb_test);
775 printf("cbcrm %d\n",cbcrm);
776 printf("cbcprng %d\n",cbcprng);
777 printf("ecbrm %d\n",ecbrm);
778 printf("ecbprng %d\n",ecbprng);
780 printf("lena %d\n",lena);
781 printf("size_buf %d\n",size_buf);
790 uchar Secretkey[key_size];
792 uchar counter[key_size];
794 for(int i=0;i<key_size;i++) {
795 Secretkey[i]=lrand48()&0xFF;
796 counter[i]=lrand48()&0xFF;
809 uchar *data_R, *data_G, *data_B;
818 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
819 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
820 imsize=width*height*3;
821 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
824 width=height=size_buf;
825 imsize=width*height*3;
826 //cout<<"imsize "<<imsize<<endl;
827 buffer=new uchar[imsize];
828 for(int i=0;i<imsize;i++) {
835 cout<<"imsize "<<imsize<<endl;
837 uchar* seq= new uchar[imsize];
838 uchar* seq2= new uchar[imsize];
840 int oneD=width*height;
842 for(int i=0;i<oneD;i++) {
844 seq[oneD+i]=data_G[i];
845 seq[2*oneD+i]=data_B[i];
849 for(int i=0;i<oneD*3;i++) {
858 int total_len=imsize;
860 int len= total_len/h;
864 uchar *mix=new uchar[256];
869 for (int i = 0; i < 256 ; i++) {
870 mix[i]=Secretkey[i]^counter[i];
875 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
876 // g_print("%s\n", sha512);
886 // cout<<"hash "<<endl;
887 for (int i = 0; i < 128 ; i++) {
894 int *Pbox=new int[len];
895 int *PboxSRM=new int[len/2];
896 int *PboxSRM2=new int[len/2];
899 uchar Inv_Sbox1[256];
900 uchar Inv_Sbox2[256];
910 double time_encrypt=0;
911 double time_decrypt=0;
914 double t=TimeStart();
916 for(int i=0;i<nb_test;i++) {
918 rc4key(DK, Sbox1, 8);
921 rc4key(&DK[8], Sbox2, 8);
923 rc4key(&DK[16], sc, 16);
924 prga(sc, h*h*2+256, RM);
930 rc4keyperm(&DK[72], len, rp, Pbox, 16);
933 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
935 for(int i=0;i<len/2;i++) {
936 PboxSRM[i]=PboxSRM2[i]&(h-1);
940 for(int i=0;i<h*2;i++) {
942 cout<<(int)RM[i*h+j]<<" ";
949 time_init+=TimeStop(t);
950 cout<<"Time initializaton nb times "<<nb_test<<" = "<<time_init<<endl;
955 for(int i=0;i<64;i++) {
966 inverse_tables(Sbox1,256,Inv_Sbox1);
967 inverse_tables(Sbox2,256,Inv_Sbox2);
971 // lehmer64_seed(myrand);
978 for(i=0;i<nb_test;i++)
981 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
983 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
985 encrypt_ecb_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,0);
987 encrypt_ecb_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV,myrand,0);
991 for(i=0;i<nb_test;i++)
994 encrypt_authenticate_algorithm<32>(seq, seq2, len, RM, Pbox, PboxSRM, Sbox1, Sbox2, IV1, MAC, myrand);
996 encrypt_authenticate_algorithm_2Blocks<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
998 encrypt_authenticate_algorithm_2Blocks_V2<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
1004 time_encrypt+=TimeStop(t);
1005 cout<<"Time encrypt "<<time_encrypt<<endl;
1006 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
1010 for(int i=0;i<oneD;i++) {
1012 data_G[i]=seq2[oneD+i];
1013 data_B[i]=seq2[2*oneD+i];
1015 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
1020 // lehmer64_seed(myrand);
1025 for(i=0;i<nb_test;i++) {
1027 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
1029 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
1031 decrypt_ecb_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,0);
1033 decrypt_ecb_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,IV,myrand,0);
1037 for(i=0;i<nb_test;i++) {
1039 decrypt_authenticate_algorithm<32>(seq2, seq,len,RM,Pbox,PboxSRM,Inv_Sbox1,Inv_Sbox2,Sbox1,Sbox2,IV1,MAC,myrand);
1041 decrypt_authenticate_algorithm_2Blocks<32>(seq2, seq,len,RM,Pbox,PboxSRM,Inv_Sbox1,Inv_Sbox2,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
1043 decrypt_authenticate_algorithm_2Blocks_V2<32>(seq2, seq,len,RM,Pbox,PboxSRM,Inv_Sbox1,Inv_Sbox2,Sbox1,Sbox2,IV1,IV2,MAC,myrand);
1050 time_decrypt+=TimeStop(t);
1051 // cout<<"Time decrypt "<<time_decrypt<<endl;
1052 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
1055 for(int i=0;i<oneD;i++) {
1057 data_G[i]=seq[oneD+i];
1058 data_B[i]=seq[2*oneD+i];
1060 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
1064 for(int i=0;i<imsize;i++) {
1065 //cout<<(int)buffer[i]<<endl;
1066 if(buffer[i]!=seq[i]) {
1070 //cout<<"RESULT CORRECT: "<<equal<<endl;