encrypt.c 25.3 KB
Newer Older
Alexandre Adomnicai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
/*
 * Date: 29 November 2018
 * Contact: Thomas Peyrin - thomas.peyrin@gmail.com
 * Mustafa Khairallah - mustafam001@e.ntu.edu.sg
 */

#include "crypto_aead.h"
#include "api.h"
#include "skinny.h"
#include <stdio.h>
#include <stdlib.h>

void pad (const unsigned char* m, unsigned char* mp, int len8) {

#ifdef ___ENABLE_DWORD_CAST

  if (0 == len8) {
    *(uint64_t*)(&mp[0]) = 0;
    *(uint64_t*)(&mp[8]) = 0;
  } else if (8 >  len8) {
    *(uint64_t*)(&mp[0]) = *(uint64_t*)(&m[0]) & (0xffffffffffffffff >> (64 - len8*8));
    *(uint64_t*)(&mp[8]) = 0;
    mp[15] = len8;
  } else if (8 == len8) {
    *(uint64_t*)(&mp[0]) = *(uint64_t*)(&m[0]);
    *(uint64_t*)(&mp[8]) = 0;
    mp[15] = 8;
  } else if (16 > len8) {  
    *(uint64_t*)(&mp[0]) = *(uint64_t*)(&m[0]);
    *(uint64_t*)(&mp[8]) = *(uint64_t*)(&m[8]) & (0xffffffffffffffff >> (128 - len8*8));
    mp[15] = len8;
  } else {
    *(uint64_t*)(&mp[0]) = *(uint64_t*)(&m[0]);
    *(uint64_t*)(&mp[8]) = *(uint64_t*)(&m[8]);
  }

#else

  if (0 == len8) {
    *(uint32_t*)(&mp[0])  = 0;
    *(uint32_t*)(&mp[4])  = 0;
    *(uint32_t*)(&mp[8])  = 0;
    *(uint32_t*)(&mp[12]) = 0;
  } else if (4 >   len8) {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]) & (0xffffffff >> (32 - len8*8));
    *(uint32_t*)(&mp[4])  = 0;
    *(uint32_t*)(&mp[8])  = 0;
    *(uint32_t*)(&mp[12]) = 0;
    mp[15] = len8;
  } else if (4 ==  len8) {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]);
    *(uint32_t*)(&mp[4])  = 0;
    *(uint32_t*)(&mp[8])  = 0;
    *(uint32_t*)(&mp[12]) = 0;
    mp[15] = 4;
  } else if (8 >   len8) {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]);
    *(uint32_t*)(&mp[4])  = *(uint32_t*)(&m[4]) & (0xffffffff >> (64 - len8*8));
    *(uint32_t*)(&mp[8])  = 0;
    *(uint32_t*)(&mp[12]) = 0;
    mp[15] = len8;
  } else if (8 ==  len8) {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]);
    *(uint32_t*)(&mp[4])  = *(uint32_t*)(&m[4]);
    *(uint32_t*)(&mp[8])  = 0;
    *(uint32_t*)(&mp[12]) = 0;
    mp[15] = 8;
  } else if (12 >  len8) {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]);
    *(uint32_t*)(&mp[4])  = *(uint32_t*)(&m[4]);
    *(uint32_t*)(&mp[8])  = *(uint32_t*)(&m[8]) & (0xffffffff >> (96 - len8*8));
    *(uint32_t*)(&mp[12]) = 0;
    mp[15] = len8;
  } else if (12 == len8) {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]);
    *(uint32_t*)(&mp[4])  = *(uint32_t*)(&m[4]);
    *(uint32_t*)(&mp[8])  = *(uint32_t*)(&m[8]);
    *(uint32_t*)(&mp[12]) = 0;
    mp[15] = 12;
  } else if (16 >  len8) {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]);
    *(uint32_t*)(&mp[4])  = *(uint32_t*)(&m[4]);
    *(uint32_t*)(&mp[8])  = *(uint32_t*)(&m[8]);
    *(uint32_t*)(&mp[12]) = *(uint32_t*)(&m[12]) & (0xffffffff >> (128 - len8*8));
    mp[15] = len8;
  } else {
    *(uint32_t*)(&mp[0])  = *(uint32_t*)(&m[0]);
    *(uint32_t*)(&mp[4])  = *(uint32_t*)(&m[4]);
    *(uint32_t*)(&mp[8])  = *(uint32_t*)(&m[8]);
    *(uint32_t*)(&mp[12]) = *(uint32_t*)(&m[12]);
  }

#endif

}

void g8A (unsigned char* s, unsigned char* c) {

#ifdef ___ENABLE_DWORD_CAST

  uint64_t s0 =  *(uint64_t*)(&s[0]);
  uint64_t s1 =  *(uint64_t*)(&s[8]);

  uint64_t c0, c1;

  c0 = ((s0 >> 1) & 0x7f7f7f7f7f7f7f7f) ^ ((s0 ^ (s0 << 7)) & 0x8080808080808080);
  c1 = ((s1 >> 1) & 0x7f7f7f7f7f7f7f7f) ^ ((s1 ^ (s1 << 7)) & 0x8080808080808080);

  *(uint64_t*)(&c[0])  = c0;
  *(uint64_t*)(&c[8])  = c1;

#else

  uint32_t s0 =  *(uint32_t*)(&s[0]);
  uint32_t s1 =  *(uint32_t*)(&s[4]);
  uint32_t s2 =  *(uint32_t*)(&s[8]);
  uint32_t s3 =  *(uint32_t*)(&s[12]);

  uint32_t c0, c1, c2, c3;

  c0 = ((s0 >> 1) & 0x7f7f7f7f) ^ ((s0 ^ (s0 << 7)) & 0x80808080);
  c1 = ((s1 >> 1) & 0x7f7f7f7f) ^ ((s1 ^ (s1 << 7)) & 0x80808080);
  c2 = ((s2 >> 1) & 0x7f7f7f7f) ^ ((s2 ^ (s2 << 7)) & 0x80808080);
  c3 = ((s3 >> 1) & 0x7f7f7f7f) ^ ((s3 ^ (s3 << 7)) & 0x80808080);

  *(uint32_t*)(&c[0])  = c0;
  *(uint32_t*)(&c[4])  = c1;
  *(uint32_t*)(&c[8])  = c2;
  *(uint32_t*)(&c[12]) = c3;

#endif

}

void g8A_for_Tag_Generation (unsigned char* s, unsigned char* c) {

#ifdef ___ENABLE_DWORD_CAST

  uint64_t s0 =  *(uint64_t*)(&s[0]);
  uint64_t s1 =  *(uint64_t*)(&s[8]);

  uint64_t c0, c1;

  c0 = ((s0 >> 1) & 0x7f7f7f7f7f7f7f7f) ^ ((s0 ^ (s0 << 7)) & 0x8080808080808080);
  c1 = ((s1 >> 1) & 0x7f7f7f7f7f7f7f7f) ^ ((s1 ^ (s1 << 7)) & 0x8080808080808080);

  // use byte access because of memory alignment.
  // c is not always in word(4 byte) alignment.
  c[0]  =  c0     &0xFF;
  c[1]  = (c0>>8) &0xFF;
  c[2]  = (c0>>16)&0xFF;
  c[3]  = (c0>>24)&0xFF;
  c[4]  = (c0>>32)&0xFF;
  c[5]  = (c0>>40)&0xFF;
  c[6]  = (c0>>48)&0xFF;
  c[7]  =  c0>>56;
  c[8]  =  c1     &0xFF;
  c[9]  = (c1>>8) &0xFF;
  c[10] = (c1>>16)&0xFF;
  c[11] = (c1>>24)&0xFF;
  c[12] = (c1>>32)&0xFF;
  c[13] = (c1>>40)&0xFF;
  c[14] = (c1>>48)&0xFF;
  c[15] =  c1>>56;

#else

  uint32_t s0 =  *(uint32_t*)(&s[0]);
  uint32_t s1 =  *(uint32_t*)(&s[4]);
  uint32_t s2 =  *(uint32_t*)(&s[8]);
  uint32_t s3 =  *(uint32_t*)(&s[12]);

  uint32_t c0, c1, c2, c3;

  c0 = ((s0 >> 1) & 0x7f7f7f7f) ^ ((s0 ^ (s0 << 7)) & 0x80808080);
  c1 = ((s1 >> 1) & 0x7f7f7f7f) ^ ((s1 ^ (s1 << 7)) & 0x80808080);
  c2 = ((s2 >> 1) & 0x7f7f7f7f) ^ ((s2 ^ (s2 << 7)) & 0x80808080);
  c3 = ((s3 >> 1) & 0x7f7f7f7f) ^ ((s3 ^ (s3 << 7)) & 0x80808080);

  // use byte access because of memory alignment.
  // c is not always in word(4 byte) alignment.
  c[0] =   c0     &0xFF;
  c[1] =  (c0>>8) &0xFF;
  c[2] =  (c0>>16)&0xFF;
  c[3] =   c0>>24;
  c[4] =   c1     &0xFF;
  c[5] =  (c1>>8) &0xFF;
  c[6] =  (c1>>16)&0xFF;
  c[7] =   c1>>24;
  c[8] =   c2     &0xFF;
  c[9] =  (c2>>8) &0xFF;
  c[10] = (c2>>16)&0xFF;
  c[11] =  c2>>24;
  c[12] =  c3     &0xFF;
  c[13] = (c3>>8) &0xFF;
  c[14] = (c3>>16)&0xFF;
  c[15] =  c3>>24;

#endif

}

void rho_ad_eqov16 (
    const unsigned char* m,
    unsigned char* s) {

#ifdef ___ENABLE_DWORD_CAST

  *(uint64_t*)(&s[0])  ^= *(uint64_t*)(&m[0]);
  *(uint64_t*)(&s[8])  ^= *(uint64_t*)(&m[8]);

#else

  *(uint32_t*)(&s[0])  ^= *(uint32_t*)(&m[0]);
  *(uint32_t*)(&s[4])  ^= *(uint32_t*)(&m[4]);
  *(uint32_t*)(&s[8])  ^= *(uint32_t*)(&m[8]);
  *(uint32_t*)(&s[12]) ^= *(uint32_t*)(&m[12]);

#endif

}

void rho_ad_ud16 (
    const unsigned char* m,
    unsigned char* s,
    int len8) {

  unsigned char mp [16];
  pad(m,mp,len8);

#ifdef ___ENABLE_DWORD_CAST

  *(uint64_t*)(&s[0])  ^= *(uint64_t*)(&mp[0]);
  *(uint64_t*)(&s[8])  ^= *(uint64_t*)(&mp[8]);

#else

  *(uint32_t*)(&s[0])  ^= *(uint32_t*)(&mp[0]);
  *(uint32_t*)(&s[4])  ^= *(uint32_t*)(&mp[4]);
  *(uint32_t*)(&s[8])  ^= *(uint32_t*)(&mp[8]);
  *(uint32_t*)(&s[12]) ^= *(uint32_t*)(&mp[12]);

#endif

}

void rho_eqov16 (
    const unsigned char* m,
    unsigned char* c,
    unsigned char* s) {

  g8A(s,c);

#ifdef ___ENABLE_DWORD_CAST

  uint64_t c0 = *(uint64_t*)(&c[0]);
  uint64_t c1 = *(uint64_t*)(&c[8]);

  uint64_t s0 = *(uint64_t*)(&s[0]);
  uint64_t s1 = *(uint64_t*)(&s[8]);

  uint64_t m0 = *(uint64_t*)(&m[0]);
  uint64_t m1 = *(uint64_t*)(&m[8]);

  s0 ^= m0;
  s1 ^= m1;

  c0 ^= m0;
  c1 ^= m1;

  *(uint64_t*)(&s[0])  = s0;
  *(uint64_t*)(&s[8])  = s1;

  *(uint64_t*)(&c[0])  = c0;
  *(uint64_t*)(&c[8])  = c1;

#else

  uint32_t c0 = *(uint32_t*)(&c[0]);
  uint32_t c1 = *(uint32_t*)(&c[4]);
  uint32_t c2 = *(uint32_t*)(&c[8]);
  uint32_t c3 = *(uint32_t*)(&c[12]);

  uint32_t s0 = *(uint32_t*)(&s[0]);
  uint32_t s1 = *(uint32_t*)(&s[4]);
  uint32_t s2 = *(uint32_t*)(&s[8]);
  uint32_t s3 = *(uint32_t*)(&s[12]);

  uint32_t m0 = *(uint32_t*)(&m[0]);
  uint32_t m1 = *(uint32_t*)(&m[4]);
  uint32_t m2 = *(uint32_t*)(&m[8]);
  uint32_t m3 = *(uint32_t*)(&m[12]);

  s0 ^= m0;
  s1 ^= m1;
  s2 ^= m2;
  s3 ^= m3;

  c0 ^= m0;
  c1 ^= m1;
  c2 ^= m2;
  c3 ^= m3;

  *(uint32_t*)(&s[0])  = s0;
  *(uint32_t*)(&s[4])  = s1;
  *(uint32_t*)(&s[8])  = s2;
  *(uint32_t*)(&s[12]) = s3;

  *(uint32_t*)(&c[0])  = c0;
  *(uint32_t*)(&c[4])  = c1;
  *(uint32_t*)(&c[8])  = c2;
  *(uint32_t*)(&c[12]) = c3;

#endif

}

void rho_ud16 (
    const unsigned char* m,
    unsigned char* c,
    unsigned char* s,
    int len8) {

  unsigned char mp [16];

  pad(m,mp,len8);

  g8A(s,c);

#ifdef ___ENABLE_DWORD_CAST

  uint64_t mp0 = *(uint64_t*)&mp[0];
  uint64_t mp1 = *(uint64_t*)&mp[8];
  uint64_t c0  = *(uint64_t*)&c[0];
  uint64_t c1  = *(uint64_t*)&c[8];
  
  *(uint64_t*)(&s[0])  ^= mp0;
  *(uint64_t*)(&s[8])  ^= mp1;

  if (0 == len8) {
    c0 = 0;
    c1 = 0;
  } else if (8 >  len8) {
    c0 = c0 ^ (mp0 & 0xffffffffffffffff >> (64 - (len8*8)));
    c0 = c0 ^ (c0  & 0xffffffffffffffff << (     (len8*8)));
    c1 = 0;
  } else if (8 == len8) {
    c0 = c0 ^ mp0;
    c1 = 0;
  } else if (16 > len8) {
    len8 -= 8;
    c0 = c0 ^  mp0;
    c1 = c1 ^ (mp1 & 0xffffffffffffffff >> (64 - (len8*8)));
    c1 = c1 ^ (c1  & 0xffffffffffffffff << (     (len8*8)));
  } else {
    c0 = c0 ^ mp0;
    c1 = c1 ^ mp1;
  }

  *(uint64_t*)&c[0] = c0;
  *(uint64_t*)&c[8] = c1;

#else

  uint32_t mp0 = *(uint32_t*)&mp[0];
  uint32_t mp1 = *(uint32_t*)&mp[4];
  uint32_t mp2 = *(uint32_t*)&mp[8];
  uint32_t mp3 = *(uint32_t*)&mp[12];
  uint32_t c0  = *(uint32_t*)&c[0];
  uint32_t c1  = *(uint32_t*)&c[4];
  uint32_t c2  = *(uint32_t*)&c[8];
  uint32_t c3  = *(uint32_t*)&c[12];
  
  *(uint32_t*)(&s[0])   ^= mp0;
  *(uint32_t*)(&s[4])   ^= mp1;
  *(uint32_t*)(&s[8])   ^= mp2;
  *(uint32_t*)(&s[12])  ^= mp3;

  if (0 == len8) {
    c0 = 0;
    c1 = 0;
    c2 = 0;
    c3 = 0;
  } else if (4 >  len8) {
    c0 = c0 ^ (mp0 & 0xffffffff >> (32 - (len8*8)));
    c0 = c0 ^ (c0  & 0xffffffff << (     (len8*8)));
    c1 = 0;
    c2 = 0;
    c3 = 0;
  } else if (4 == len8) {
    c0 = c0 ^  mp0;
    c1 = 0;
    c2 = 0;
    c3 = 0;
  } else if (8 >  len8) {
    len8 -= 4;
    c0 = c0 ^  mp0;
    c1 = c1 ^ (mp1 & 0xffffffff >> (32 - (len8*8)));
    c1 = c1 ^ (c1  & 0xffffffff << (     (len8*8)));
    c2 = 0;
    c3 = 0;
  } else if (8 == len8) {
    c0 = c0 ^  mp0;
    c1 = c1 ^  mp1;
    c2 = 0;
    c3 = 0;
  } else if (12 > len8) {
    len8 -= 8;
    c0 = c0 ^  mp0;
    c1 = c1 ^  mp1;
    c2 = c2 ^ (mp2 & 0xffffffff >> (32 - (len8*8)));
    c2 = c2 ^ (c2  & 0xffffffff << (     (len8*8)));
    c3 = 0;
  } else if (12 == len8) {
    c0 = c0 ^  mp0;
    c1 = c1 ^  mp1;
    c2 = c2 ^  mp2;
    c3 = 0;
  } else if (16 > len8) {
    len8 -= 12;
    c0 = c0 ^  mp0;
    c1 = c1 ^  mp1;
    c2 = c2 ^  mp2;
    c3 = c3 ^ (mp3 & 0xffffffff >> (32 - (len8*8)));
    c3 = c3 ^ (c3  & 0xffffffff << (     (len8*8)));
  } else {
    c0 = c0 ^  mp0;
    c1 = c1 ^  mp1;
    c2 = c2 ^  mp2;
    c3 = c3 ^  mp3;
  } 

  *(uint32_t*)&c[0]  = c0;
  *(uint32_t*)&c[4]  = c1;
  *(uint32_t*)&c[8]  = c2;
  *(uint32_t*)&c[12] = c3;

#endif

}

void irho_eqov16 (
    unsigned char* m,
    const unsigned char* c,
    unsigned char* s) {

  g8A(s,m);

#ifdef ___ENABLE_DWORD_CAST

  uint64_t c0 = *(uint64_t*)(&c[0]);
  uint64_t c1 = *(uint64_t*)(&c[8]);

  uint64_t s0 = *(uint64_t*)(&s[0]);
  uint64_t s1 = *(uint64_t*)(&s[8]);

  uint64_t m0 = *(uint64_t*)(&m[0]);
  uint64_t m1 = *(uint64_t*)(&m[8]);

  s0 ^= c0 ^ m0;
  s1 ^= c1 ^ m1;

  m0 ^= c0;
  m1 ^= c1;

  *(uint64_t*)(&s[0])  = s0;
  *(uint64_t*)(&s[8])  = s1;

  *(uint64_t*)(&m[0])  = m0;
  *(uint64_t*)(&m[8])  = m1;

#else

  uint32_t c0 = *(uint32_t*)(&c[0]);
  uint32_t c1 = *(uint32_t*)(&c[4]);
  uint32_t c2 = *(uint32_t*)(&c[8]);
  uint32_t c3 = *(uint32_t*)(&c[12]);

  uint32_t s0 = *(uint32_t*)(&s[0]);
  uint32_t s1 = *(uint32_t*)(&s[4]);
  uint32_t s2 = *(uint32_t*)(&s[8]);
  uint32_t s3 = *(uint32_t*)(&s[12]);

  uint32_t m0 = *(uint32_t*)(&m[0]);
  uint32_t m1 = *(uint32_t*)(&m[4]);
  uint32_t m2 = *(uint32_t*)(&m[8]);
  uint32_t m3 = *(uint32_t*)(&m[12]);

  s0 ^= c0 ^ m0;
  s1 ^= c1 ^ m1;
  s2 ^= c2 ^ m2;
  s3 ^= c3 ^ m3;

  m0 ^= c0;
  m1 ^= c1;
  m2 ^= c2;
  m3 ^= c3;

  *(uint32_t*)(&s[0])  = s0;
  *(uint32_t*)(&s[4])  = s1;
  *(uint32_t*)(&s[8])  = s2;
  *(uint32_t*)(&s[12]) = s3;

  *(uint32_t*)(&m[0])  = m0;
  *(uint32_t*)(&m[4])  = m1;
  *(uint32_t*)(&m[8])  = m2;
  *(uint32_t*)(&m[12]) = m3;

#endif

}

void irho_ud16 (
    unsigned char* m,
    const unsigned char* c,
    unsigned char* s,
    int len8) {

  unsigned char cp [16];

  pad(c,cp,len8);

  g8A(s,m);

#ifdef ___ENABLE_DWORD_CAST

  uint64_t cp0 = *(uint64_t*)&cp[0];
  uint64_t cp1 = *(uint64_t*)&cp[8];
  uint64_t m0  = *(uint64_t*)&m[0];
  uint64_t m1  = *(uint64_t*)&m[8];
  uint64_t s0  = *(uint64_t*)&s[0];
  uint64_t s1  = *(uint64_t*)&s[8];

  s0 ^= cp0;
  s1 ^= cp1;

  if (0 == len8) {
    m0 = 0;
    m1 = 0;
  } else if (8 >  len8) {
    s0 = s0 ^ (m0  & 0xffffffffffffffff >> (64 - (len8*8)));

    m0 = m0 ^ (cp0 & 0xffffffffffffffff >> (64 - (len8*8)));
    m0 = m0 ^ (m0  & 0xffffffffffffffff << (     (len8*8)));
    m1 = 0;
  } else if (8 == len8) {
    s0 = s0 ^  m0;

    m0 = m0 ^  cp0;
    m1 = 0;
  } else if (16 >  len8) {
    len8 -= 8;
    s0 = s0 ^  m0;
    s1 = s1 ^ (m1  & 0xffffffffffffffff >> (64 - (len8*8)));

    m0 = m0 ^  cp0;
    m1 = m1 ^ (cp1 & 0xffffffffffffffff >> (64 - (len8*8)));
    m1 = m1 ^ (m1  & 0xffffffffffffffff << (     (len8*8)));
  } else {
    s0 = s0 ^  m0;
    s1 = s1 ^  m1;

    m0 = m0 ^  cp0;
    m1 = m1 ^  cp1;
  }

  *(uint64_t*)&s[0] = s0;
  *(uint64_t*)&s[8] = s1;
  *(uint64_t*)&m[0] = m0;
  *(uint64_t*)&m[8] = m1;

#else

  uint32_t cp0 = *(uint32_t*)&cp[0];
  uint32_t cp1 = *(uint32_t*)&cp[4];
  uint32_t cp2 = *(uint32_t*)&cp[8];
  uint32_t cp3 = *(uint32_t*)&cp[12];
  uint32_t m0  = *(uint32_t*)&m[0];
  uint32_t m1  = *(uint32_t*)&m[4];
  uint32_t m2  = *(uint32_t*)&m[8];
  uint32_t m3  = *(uint32_t*)&m[12];
  uint32_t s0  = *(uint32_t*)&s[0];
  uint32_t s1  = *(uint32_t*)&s[4];
  uint32_t s2  = *(uint32_t*)&s[8];
  uint32_t s3  = *(uint32_t*)&s[12];

  s0 ^= cp0;
  s1 ^= cp1;
  s2 ^= cp2;
  s3 ^= cp3;

  if (0 == len8) {
    m0 = 0;
    m1 = 0;
    m2 = 0;
    m3 = 0;
  } else if (4 >  len8) {
    s0 = s0 ^ (m0  & 0xffffffff >> (32 - (len8*8)));

    m0 = m0 ^ (cp0 & 0xffffffff >> (32 - (len8*8)));
    m0 = m0 ^ (m0  & 0xffffffff << (     (len8*8)));
    m1 = 0;
    m2 = 0;
    m3 = 0;
  } else if (4 == len8) {
    s0 = s0 ^  m0;

    m0 = m0 ^  cp0;
    m1 = 0;
    m2 = 0;
    m3 = 0;
  } else if (8 >  len8) {
    len8 -= 4;
    s0 = s0 ^  m0;
    s1 = s1 ^ (m1  & 0xffffffff >> (32 - (len8*8)));

    m0 = m0 ^  cp0;
    m1 = m1 ^ (cp1 & 0xffffffff >> (32 - (len8*8)));
    m1 = m1 ^ (m1  & 0xffffffff << (     (len8*8)));
    m2 = 0;
    m3 = 0;
  } else if (8 == len8) {
    s0 = s0 ^  m0;
    s1 = s1 ^  m1;

    m0 = m0 ^  cp0;
    m1 = m1 ^  cp1;
    m2 = 0;
    m3 = 0;
  } else if (12 >  len8) {
    len8 -= 8;
    s0 = s0 ^  m0;
    s1 = s1 ^  m1;
    s2 = s2 ^ (m2  & 0xffffffff >> (32 - (len8*8)));

    m0 = m0 ^  cp0;
    m1 = m1 ^  cp1;
    m2 = m2 ^ (cp2 & 0xffffffff >> (32 - (len8*8)));
    m2 = m2 ^ (m2  & 0xffffffff << (     (len8*8)));
    m3 = 0;
  } else if (12 == len8) {
    s0 = s0 ^  m0;
    s1 = s1 ^  m1;
    s2 = s2 ^  m2;

    m0 = m0 ^  cp0;
    m1 = m1 ^  cp1;
    m2 = m2 ^  cp2;
    m3 = 0;
  } else if (16 >  len8) {
    len8 -= 12;
    s0 = s0 ^  m0;
    s1 = s1 ^  m1;
    s2 = s2 ^  m2;
    s3 = s3 ^ (m3  & 0xffffffff >> (32 - (len8*8)));

    m0 = m0 ^  cp0;
    m1 = m1 ^  cp1;
    m2 = m2 ^  cp2;
    m3 = m3 ^ (cp3 & 0xffffffff >> (32 - (len8*8)));
    m3 = m3 ^ (m3  & 0xffffffff << (     (len8*8)));
  } else {
    s0 = s0 ^  m0;
    s1 = s1 ^  m1;
    s2 = s2 ^  m2;
    s3 = s3 ^  m3;

    m0 = m0 ^  cp0;
    m1 = m1 ^  cp1;
    m2 = m2 ^  cp2;
    m3 = m3 ^  cp3;
  }

  *(uint32_t*)&s[0]  = s0;
  *(uint32_t*)&s[4]  = s1;
  *(uint32_t*)&s[8]  = s2;
  *(uint32_t*)&s[12] = s3;
  *(uint32_t*)&m[0]  = m0;
  *(uint32_t*)&m[4]  = m1;
  *(uint32_t*)&m[8]  = m2;
  *(uint32_t*)&m[12] = m3;

#endif

}

void reset_lfsr_gf56 (unsigned char* CNT) {

#ifdef ___ENABLE_DWORD_CAST

  *(uint64_t*)(&CNT[0]) = 0x0000000000000001; // CNT7 CNT6 CNT5 CNT4 CNT3 CNT2 CNT1 CNT0

#else

  *(uint32_t*)(&CNT[0]) = 0x00000001; // CNT3 CNT2 CNT1 CNT0
  *(uint32_t*)(&CNT[4]) = 0x00000000; // CNT7 CNT6 CNT5 CNT4

#endif

}

void lfsr_gf56 (unsigned char* CNT) {

#ifdef ___ENABLE_DWORD_CAST

  uint64_t C0;
  uint64_t fb0;

  C0 = *(uint64_t*)(&CNT[0]); // CNT7 CNT6 CNT5 CNT4 CNT3 CNT2 CNT1 CNT0

  fb0 = 0;
  if (CNT[6] & 0x80) {
    fb0 =  0x95;
  }

  C0 = C0 << 1 ^ fb0;

  *(uint64_t*)(&CNT[0]) = C0;

#else

  uint32_t C0;
  uint32_t C1;
  uint32_t fb0;

  C0 = *(uint32_t*)(&CNT[0]); // CNT3 CNT2 CNT1 CNT0
  C1 = *(uint32_t*)(&CNT[4]); // CNT7 CNT6 CNT5 CNT4

  fb0 = 0;
  if (CNT[6] & 0x80) {
    fb0 =  0x95;
  }

  C1 = C1 << 1 | C0 >> 31;
  C0 = C0 << 1 ^ fb0;

  *(uint32_t*)(&CNT[0]) = C0;
  *(uint32_t*)(&CNT[4]) = C1;

#endif

}

void block_cipher(
    unsigned char* s,
    const unsigned char* k, unsigned char* T,
    unsigned char* CNT, unsigned char D,
    skinny_ctrl* p_skinny_ctrl) {

  CNT[7] = D;
  p_skinny_ctrl->func_skinny_128_384_enc(s, p_skinny_ctrl, CNT, T, k);

}

void nonce_encryption (
    const unsigned char* N,
    unsigned char* CNT,
    unsigned char*s, const unsigned char* k,
    unsigned char D,
    skinny_ctrl* p_skinny_ctrl) {

  block_cipher(s,k,(unsigned char*)N,CNT,D,p_skinny_ctrl);

}

void generate_tag (
    unsigned char** c, unsigned char* s,
    unsigned long long* clen) {

  g8A_for_Tag_Generation(s, *c);

  *c = *c + 16;
  *c = *c - *clen;

}

unsigned long long msg_encryption_eqov16 (
    const unsigned char** M, unsigned char** c,
    const unsigned char* N,
    unsigned char* CNT,
    unsigned char*s, const unsigned char* k,
    unsigned char D,
    unsigned long long mlen,
    skinny_ctrl* p_skinny_ctrl) {

  rho_eqov16(*M, *c, s);
  *c = *c + 16;
  *M = *M + 16;
  lfsr_gf56(CNT);
  nonce_encryption(N,CNT,s,k,D,p_skinny_ctrl);
  return mlen - 16;

}

unsigned long long msg_encryption_ud16 (
    const unsigned char** M, unsigned char** c,
    const unsigned char* N,
    unsigned char* CNT,
    unsigned char*s, const unsigned char* k,
    unsigned char D,
    unsigned long long mlen,
    skinny_ctrl* p_skinny_ctrl) {

//  char msg[64];
//
//  unsigned int st = (unsigned int )read_cycle();

  rho_ud16(*M, *c, s, mlen);

//  unsigned int ed = (unsigned int )read_cycle();
//  sprintf(msg, "rho_ud16 %d\n", ed-st);
//  SerialPuts(msg);
//
//  fprint_bstr(NULL, "c = ", *c, 16);
  
  *c = *c + mlen;
  *M = *M + mlen;
  lfsr_gf56(CNT);
  nonce_encryption(N,CNT,s,k,D,p_skinny_ctrl);
  return 0;

}

unsigned long long msg_decryption_eqov16 (
    unsigned char** M, const unsigned char** c,
    const unsigned char* N,
    unsigned char* CNT,
    unsigned char*s, const unsigned char* k,
    unsigned char D,
    unsigned long long clen,
    skinny_ctrl* p_skinny_ctrl) {

  irho_eqov16(*M, *c, s);
  *c = *c + 16;
  *M = *M + 16;
  lfsr_gf56(CNT);
  nonce_encryption(N,CNT,s,k,D,p_skinny_ctrl);

  return clen - 16;

}

unsigned long long msg_decryption_ud16 (
    unsigned char** M, const unsigned char** c,
    const unsigned char* N,
    unsigned char* CNT,
    unsigned char*s, const unsigned char* k,
    unsigned char D,
    unsigned long long clen,
    skinny_ctrl* p_skinny_ctrl) {

  irho_ud16(*M, *c, s, clen);
  *c = *c + clen;
  *M = *M + clen;
  lfsr_gf56(CNT);
  nonce_encryption(N,CNT,s,k,D,p_skinny_ctrl);
  return 0;

}

unsigned long long ad_encryption_eqov32 (
    const unsigned char** A, unsigned char* s,
    const unsigned char* k, unsigned long long adlen,
    unsigned char* CNT,
    unsigned char D,
    skinny_ctrl* p_skinny_ctrl) {

  unsigned char T [16];

  rho_ad_eqov16(*A, s);
  *A = *A + 16;
  lfsr_gf56(CNT);

#ifdef ___ENABLE_DWORD_CAST

  *(uint64_t*)(&T[0])  = *(uint64_t*)(&(*A)[0]);
  *(uint64_t*)(&T[8])  = *(uint64_t*)(&(*A)[8]);

#else

  *(uint32_t*)(&T[0])  = *(uint32_t*)(&(*A)[0]);
  *(uint32_t*)(&T[4])  = *(uint32_t*)(&(*A)[4]);
  *(uint32_t*)(&T[8])  = *(uint32_t*)(&(*A)[8]);
  *(uint32_t*)(&T[12]) = *(uint32_t*)(&(*A)[12]);

#endif

  *A = *A + 16;
  block_cipher(s,k,T,CNT,D,p_skinny_ctrl);
  lfsr_gf56(CNT);

  return adlen - 32;

}

unsigned long long ad_encryption_ov16 (
    const unsigned char** A, unsigned char* s,
    const unsigned char* k, unsigned long long adlen,
    unsigned char* CNT,
    unsigned char D,
    skinny_ctrl* p_skinny_ctrl) {

  unsigned char T [16];

  adlen = adlen - 16;
  rho_ad_eqov16(*A, s);
  *A = *A + 16;
  lfsr_gf56(CNT);

  pad(*A, T, adlen);
  *A = *A + adlen;
  block_cipher(s,k,T,CNT,D,p_skinny_ctrl);
  lfsr_gf56(CNT);

  return 0;

}

unsigned long long ad_encryption_eq16 (
    const unsigned char** A, unsigned char* s,
    unsigned char* CNT) {

  rho_ad_eqov16(*A, s);
  *A = *A + 16;
  lfsr_gf56(CNT);

  return 0;

}

unsigned long long ad_encryption_ud16(
    const unsigned char** A, unsigned char* s,
    unsigned long long adlen,
    unsigned char* CNT) {

  rho_ad_ud16(*A, s, adlen);
  *A = *A + adlen;
  lfsr_gf56(CNT);

  return 0;

}

int crypto_aead_encrypt (
    unsigned char* c, unsigned long long* clen,
    const unsigned char* m, unsigned long long mlen,
    const unsigned char* ad, unsigned long long adlen,
    const unsigned char* nsec,
    const unsigned char* npub,
    const unsigned char* k) {

  unsigned char s[16];
  unsigned char CNT[8];
  const unsigned char* A;
  const unsigned char* M;
  const unsigned char* N;

  skinny_ctrl ctrl;
  ctrl.func_skinny_128_384_enc = skinny_128_384_enc123_12;

  (void) nsec;
  A = ad;
  M = m;
  N = npub;

#ifdef ___ENABLE_DWORD_CAST

  *(uint64_t*)(&s[0])  = 0;
  *(uint64_t*)(&s[8])  = 0;

#else

  *(uint32_t*)(&s[0])  = 0;
  *(uint32_t*)(&s[4])  = 0;
  *(uint32_t*)(&s[8])  = 0;
  *(uint32_t*)(&s[12]) = 0;

#endif

  reset_lfsr_gf56(CNT);

  if (adlen == 0) { // AD is an empty string
    lfsr_gf56(CNT);
    nonce_encryption(N,CNT,s,k,0x1a,&ctrl);
  }
  else while (adlen > 0) {
      if (adlen < 16) { // The last block of AD is odd and incomplete
        adlen = ad_encryption_ud16(&A,s,adlen,CNT);
        nonce_encryption(N,CNT,s,k,0x1a,&ctrl);
      }
      else if (adlen == 16) { // The last block of AD is odd and complete
        adlen = ad_encryption_eq16(&A,s,CNT);
        nonce_encryption(N,CNT,s,k,0x18,&ctrl);
      }
      else if (adlen < 32) { // The last block of AD is even and incomplete
        adlen = ad_encryption_ov16(&A,s,k,adlen,CNT,0x08,&ctrl);
        nonce_encryption(N,CNT,s,k,0x1a,&ctrl);
      }
      else if (adlen == 32) { // The last block of AD is even and complete
        adlen = ad_encryption_eqov32(&A,s,k,adlen,CNT,0x08,&ctrl);
        nonce_encryption(N,CNT,s,k,0x18,&ctrl);
      }
      else { // A normal full pair of blocks of AD
        adlen = ad_encryption_eqov32(&A,s,k,adlen,CNT,0x08,&ctrl);
      }
    }

  ctrl.func_skinny_128_384_enc = skinny_128_384_enc1_1;

  reset_lfsr_gf56(CNT);

  *clen = mlen + 16;

  if (mlen == 0) { // M is an empty string
    lfsr_gf56(CNT);
    nonce_encryption(N,CNT,s,k,0x15,&ctrl);
  }
  else while (mlen > 0) {
    if (mlen < 16) { // The last block of M is incomplete
      mlen = msg_encryption_ud16(&M,&c,N,CNT,s,k,0x15,mlen,&ctrl);
    }
    else if (mlen == 16) { // The last block of M is complete
      mlen = msg_encryption_eqov16(&M,&c,N,CNT,s,k,0x14,mlen,&ctrl);
    }
    else { // A normal full message block
      mlen = msg_encryption_eqov16(&M,&c,N,CNT,s,k,0x04,mlen,&ctrl);
    }
  }

  // Tag generation
  generate_tag(&c,s,clen);

  return 0;

}

int crypto_aead_decrypt(
    unsigned char *m,unsigned long long *mlen,
    unsigned char *nsec,
    const unsigned char *c,unsigned long long clen,
    const unsigned char *ad,unsigned long long adlen,
    const unsigned char *npub,
    const unsigned char *k) {

  unsigned char s[16];
  unsigned char T[16];
  unsigned char CNT[8];
  const unsigned char* A;
  unsigned char* M;
  const unsigned char* N;

  skinny_ctrl ctrl;
  ctrl.func_skinny_128_384_enc = skinny_128_384_enc123_12;

  (void) nsec;
  A = ad;
  M = m;
  N = npub;

#ifdef ___ENABLE_DWORD_CAST

  *(uint64_t*)(&s[0])  = 0;
  *(uint64_t*)(&s[8])  = 0;

#else

  *(uint32_t*)(&s[0])  = 0;
  *(uint32_t*)(&s[4])  = 0;
  *(uint32_t*)(&s[8])  = 0;
  *(uint32_t*)(&s[12]) = 0;

#endif

  reset_lfsr_gf56(CNT);

  if (adlen == 0) { // AD is an empty string
    lfsr_gf56(CNT);
    nonce_encryption(N,CNT,s,k,0x1a,&ctrl);
  }
  else while (adlen > 0) {
      if (adlen < 16) { // The last block of AD is odd and incomplete
        adlen = ad_encryption_ud16(&A,s,adlen,CNT);
        nonce_encryption(N,CNT,s,k,0x1a,&ctrl);
      }
      else if (adlen == 16) { // The last block of AD is odd and complete
        adlen = ad_encryption_eq16(&A,s,CNT);
        nonce_encryption(N,CNT,s,k,0x18,&ctrl);
      }
      else if (adlen < 32) { // The last block of AD is even and incomplete
        adlen = ad_encryption_ov16(&A,s,k,adlen,CNT,0x08,&ctrl);
        nonce_encryption(N,CNT,s,k,0x1a,&ctrl);
      }
      else if (adlen == 32) { // The last block of AD is even and complete
        adlen = ad_encryption_eqov32(&A,s,k,adlen,CNT,0x08,&ctrl);
        nonce_encryption(N,CNT,s,k,0x18,&ctrl);
      }
      else { // A normal full pair of blocks of AD
        adlen = ad_encryption_eqov32(&A,s,k,adlen,CNT,0x08,&ctrl);
      }
    }

  ctrl.func_skinny_128_384_enc = skinny_128_384_enc1_1;

  reset_lfsr_gf56(CNT);

  clen = clen -16;
  *mlen = clen;

  if (clen == 0) { // C is an empty string
    lfsr_gf56(CNT);
    nonce_encryption(N,CNT,s,k,0x15,&ctrl);
  }
  else while (clen > 0) {
    if (clen < 16) { // The last block of C is incomplete
      clen = msg_decryption_ud16(&M,&c,N,CNT,s,k,0x15,clen,&ctrl);
    }
    else if (clen == 16) { // The last block of C is complete
      clen = msg_decryption_eqov16(&M,&c,N,CNT,s,k,0x14,clen,&ctrl);
    }
    else { // A normal full message block
      clen = msg_decryption_eqov16(&M,&c,N,CNT,s,k,0x04,clen,&ctrl);
    }
  }

  // Tag generation
  g8A_for_Tag_Generation(s, T);

  for (int i = 0; i < 16; i++) {
    if (T[i] != (*(c+i))) {
      return -1;
    }
  }

  return 0;

}