skinny-aead.c 27.3 KB
Newer Older
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
/*
 * Copyright (C) 2020 Southern Storm Software, Pty Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "skinny-aead.h"
#include "internal-skinny128.h"
#include "internal-util.h"
#include <string.h>

aead_cipher_t const skinny_aead_m1_cipher = {
    "SKINNY-AEAD-M1",
    SKINNY_AEAD_KEY_SIZE,
    SKINNY_AEAD_M1_NONCE_SIZE,
    SKINNY_AEAD_M1_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    skinny_aead_m1_encrypt,
    skinny_aead_m1_decrypt
};

aead_cipher_t const skinny_aead_m2_cipher = {
    "SKINNY-AEAD-M2",
    SKINNY_AEAD_KEY_SIZE,
    SKINNY_AEAD_M2_NONCE_SIZE,
    SKINNY_AEAD_M2_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    skinny_aead_m2_encrypt,
    skinny_aead_m2_decrypt
};

aead_cipher_t const skinny_aead_m3_cipher = {
    "SKINNY-AEAD-M3",
    SKINNY_AEAD_KEY_SIZE,
    SKINNY_AEAD_M3_NONCE_SIZE,
    SKINNY_AEAD_M3_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    skinny_aead_m3_encrypt,
    skinny_aead_m3_decrypt
};

aead_cipher_t const skinny_aead_m4_cipher = {
    "SKINNY-AEAD-M4",
    SKINNY_AEAD_KEY_SIZE,
    SKINNY_AEAD_M4_NONCE_SIZE,
    SKINNY_AEAD_M4_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    skinny_aead_m4_encrypt,
    skinny_aead_m4_decrypt
};

aead_cipher_t const skinny_aead_m5_cipher = {
    "SKINNY-AEAD-M5",
    SKINNY_AEAD_KEY_SIZE,
    SKINNY_AEAD_M5_NONCE_SIZE,
    SKINNY_AEAD_M5_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    skinny_aead_m5_encrypt,
    skinny_aead_m5_decrypt
};

aead_cipher_t const skinny_aead_m6_cipher = {
    "SKINNY-AEAD-M6",
    SKINNY_AEAD_KEY_SIZE,
    SKINNY_AEAD_M6_NONCE_SIZE,
    SKINNY_AEAD_M6_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    skinny_aead_m6_encrypt,
    skinny_aead_m6_decrypt
};

/* Domain separator prefixes for all of the SKINNY-AEAD family members */
#define DOMAIN_SEP_M1 0x00
#define DOMAIN_SEP_M2 0x10
#define DOMAIN_SEP_M3 0x08
#define DOMAIN_SEP_M4 0x18
#define DOMAIN_SEP_M5 0x10
#define DOMAIN_SEP_M6 0x18

/**
 * \brief Initialize the key and nonce for SKINNY-128-384 based AEAD schemes.
 *
 * \param ks The key schedule to initialize.
 * \param key Points to the 16 bytes of the key.
 * \param nonce Points to the nonce.
 * \param nonce_len Length of the nonce in bytes.
 */
static void skinny_aead_128_384_init
    (skinny_128_384_key_schedule_t *ks, const unsigned char *key,
     const unsigned char *nonce, unsigned nonce_len)
{
    unsigned char k[48];
    memset(k, 0, 16);
    memcpy(k + 16, nonce, nonce_len);
    memset(k + 16 + nonce_len, 0, 16 - nonce_len);
    memcpy(k + 32, key, 16);
    skinny_128_384_init(ks, k);
}

/**
 * \brief Set the domain separation value in the tweak for SKINNY-128-384.
 *
 * \param ks Key schedule for the block cipher.
 * \param d Domain separation value to write into the tweak.
 */
#define skinny_aead_128_384_set_domain(ks,d) ((ks)->TK1[15] = (d))

/**
 * \brief Sets the LFSR field in the tweak for SKINNY-128-384.
 *
 * \param ks Key schedule for the block cipher.
 * \param lfsr 64-bit LFSR value.
 */
#define skinny_aead_128_384_set_lfsr(ks,lfsr) le_store_word64((ks)->TK1, (lfsr))

/**
 * \brief Updates the LFSR value for SKINNY-128-384.
 *
 * \param lfsr 64-bit LFSR value to be updated.
 */
#define skinny_aead_128_384_update_lfsr(lfsr) \
    do { \
        uint8_t feedback = ((lfsr) & (1ULL << 63)) ? 0x1B : 0x00; \
        (lfsr) = ((lfsr) << 1) ^ feedback; \
    } while (0)

/**
 * \brief Authenticates the associated data for a SKINNY-128-384 based AEAD.
 *
 * \param ks The key schedule to use.
 * \param prefix Domain separation prefix for the family member.
 * \param tag Final tag to XOR the authentication checksum into.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes.
 */
static void skinny_aead_128_384_authenticate
    (skinny_128_384_key_schedule_t *ks, unsigned char prefix,
     unsigned char tag[SKINNY_128_BLOCK_SIZE],
     const unsigned char *ad, unsigned long long adlen)
{
    unsigned char block[SKINNY_128_BLOCK_SIZE];
    uint64_t lfsr = 1;
    skinny_aead_128_384_set_domain(ks, prefix | 2);
    while (adlen >= SKINNY_128_BLOCK_SIZE) {
        skinny_aead_128_384_set_lfsr(ks, lfsr);
        skinny_128_384_encrypt(ks, block, ad);
        lw_xor_block(tag, block, SKINNY_128_BLOCK_SIZE);
        ad += SKINNY_128_BLOCK_SIZE;
        adlen -= SKINNY_128_BLOCK_SIZE;
        skinny_aead_128_384_update_lfsr(lfsr);
    }
    if (adlen > 0) {
        unsigned temp = (unsigned)adlen;
        skinny_aead_128_384_set_lfsr(ks, lfsr);
        skinny_aead_128_384_set_domain(ks, prefix | 3);
        memcpy(block, ad, temp);
        block[temp] = 0x80;
        memset(block + temp + 1, 0, SKINNY_128_BLOCK_SIZE - temp - 1);
        skinny_128_384_encrypt(ks, block, block);
        lw_xor_block(tag, block, SKINNY_128_BLOCK_SIZE);
    }
}

/**
 * \brief Encrypts the plaintext for a SKINNY-128-384 based AEAD.
 *
 * \param ks The key schedule to use.
 * \param prefix Domain separation prefix for the family member.
 * \param sum Authenticated checksum over the plaintext.
 * \param c Points to the buffer to receive the ciphertext.
 * \param m Points to the plaintext buffer.
 * \param mlen Number of bytes of plaintext to be encrypted.
 */
static void skinny_aead_128_384_encrypt
    (skinny_128_384_key_schedule_t *ks, unsigned char prefix,
     unsigned char sum[SKINNY_128_BLOCK_SIZE], unsigned char *c,
     const unsigned char *m, unsigned long long mlen)
{
    unsigned char block[SKINNY_128_BLOCK_SIZE];
    uint64_t lfsr = 1;
    memset(sum, 0, SKINNY_128_BLOCK_SIZE);
    skinny_aead_128_384_set_domain(ks, prefix | 0);
    while (mlen >= SKINNY_128_BLOCK_SIZE) {
        skinny_aead_128_384_set_lfsr(ks, lfsr);
        lw_xor_block(sum, m, SKINNY_128_BLOCK_SIZE);
        skinny_128_384_encrypt(ks, c, m);
        c += SKINNY_128_BLOCK_SIZE;
        m += SKINNY_128_BLOCK_SIZE;
        mlen -= SKINNY_128_BLOCK_SIZE;
        skinny_aead_128_384_update_lfsr(lfsr);
    }
    skinny_aead_128_384_set_lfsr(ks, lfsr);
    if (mlen > 0) {
        unsigned temp = (unsigned)mlen;
        skinny_aead_128_384_set_domain(ks, prefix | 1);
        lw_xor_block(sum, m, temp);
        sum[temp] ^= 0x80;
        memset(block, 0, SKINNY_128_BLOCK_SIZE);
        skinny_128_384_encrypt(ks, block, block);
        lw_xor_block_2_src(c, block, m, temp);
        skinny_aead_128_384_update_lfsr(lfsr);
        skinny_aead_128_384_set_lfsr(ks, lfsr);
        skinny_aead_128_384_set_domain(ks, prefix | 5);
    } else {
        skinny_aead_128_384_set_domain(ks, prefix | 4);
    }
    skinny_128_384_encrypt(ks, sum, sum);
}

/**
 * \brief Decrypts the ciphertext for a SKINNY-128-384 based AEAD.
 *
 * \param ks The key schedule to use.
 * \param prefix Domain separation prefix for the family member.
 * \param sum Authenticated checksum over the plaintext.
 * \param m Points to the buffer to receive the plaintext.
 * \param c Points to the ciphertext buffer.
 * \param mlen Number of bytes of ciphertext to be decrypted.
 */
static void skinny_aead_128_384_decrypt
    (skinny_128_384_key_schedule_t *ks, unsigned char prefix,
     unsigned char sum[SKINNY_128_BLOCK_SIZE], unsigned char *m,
     const unsigned char *c, unsigned long long mlen)
{
    unsigned char block[SKINNY_128_BLOCK_SIZE];
    uint64_t lfsr = 1;
    memset(sum, 0, SKINNY_128_BLOCK_SIZE);
    skinny_aead_128_384_set_domain(ks, prefix | 0);
    while (mlen >= SKINNY_128_BLOCK_SIZE) {
        skinny_aead_128_384_set_lfsr(ks, lfsr);
        skinny_128_384_decrypt(ks, m, c);
        lw_xor_block(sum, m, SKINNY_128_BLOCK_SIZE);
        c += SKINNY_128_BLOCK_SIZE;
        m += SKINNY_128_BLOCK_SIZE;
        mlen -= SKINNY_128_BLOCK_SIZE;
        skinny_aead_128_384_update_lfsr(lfsr);
    }
    skinny_aead_128_384_set_lfsr(ks, lfsr);
    if (mlen > 0) {
        unsigned temp = (unsigned)mlen;
        skinny_aead_128_384_set_domain(ks, prefix | 1);
        memset(block, 0, SKINNY_128_BLOCK_SIZE);
        skinny_128_384_encrypt(ks, block, block);
        lw_xor_block_2_src(m, block, c, temp);
        lw_xor_block(sum, m, temp);
        sum[temp] ^= 0x80;
        skinny_aead_128_384_update_lfsr(lfsr);
        skinny_aead_128_384_set_lfsr(ks, lfsr);
        skinny_aead_128_384_set_domain(ks, prefix | 5);
    } else {
        skinny_aead_128_384_set_domain(ks, prefix | 4);
    }
    skinny_128_384_encrypt(ks, sum, sum);
}

int skinny_aead_m1_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Set the length of the returned ciphertext */
    *clen = mlen + SKINNY_AEAD_M1_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M1_NONCE_SIZE);

    /* Encrypt to plaintext to produce the ciphertext */
    skinny_aead_128_384_encrypt(&ks, DOMAIN_SEP_M1, sum, c, m, mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M1, sum, ad, adlen);

    /* Generate the authentication tag */
    memcpy(c + mlen, sum, SKINNY_AEAD_M1_TAG_SIZE);
    return 0;
}

int skinny_aead_m1_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Validate the ciphertext length and set the return "mlen" value */
    if (clen < SKINNY_AEAD_M1_TAG_SIZE)
        return -1;
    *mlen = clen - SKINNY_AEAD_M1_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M1_NONCE_SIZE);

    /* Decrypt to ciphertext to produce the plaintext */
    skinny_aead_128_384_decrypt(&ks, DOMAIN_SEP_M1, sum, m, c, *mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M1, sum, ad, adlen);

    /* Check the authentication tag */
    return aead_check_tag(m, *mlen, sum, c + *mlen, SKINNY_AEAD_M1_TAG_SIZE);
}

int skinny_aead_m2_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Set the length of the returned ciphertext */
    *clen = mlen + SKINNY_AEAD_M2_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M2_NONCE_SIZE);

    /* Encrypt to plaintext to produce the ciphertext */
    skinny_aead_128_384_encrypt(&ks, DOMAIN_SEP_M2, sum, c, m, mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M2, sum, ad, adlen);

    /* Generate the authentication tag */
    memcpy(c + mlen, sum, SKINNY_AEAD_M2_TAG_SIZE);
    return 0;
}

int skinny_aead_m2_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Validate the ciphertext length and set the return "mlen" value */
    if (clen < SKINNY_AEAD_M2_TAG_SIZE)
        return -1;
    *mlen = clen - SKINNY_AEAD_M2_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M2_NONCE_SIZE);

    /* Decrypt to ciphertext to produce the plaintext */
    skinny_aead_128_384_decrypt(&ks, DOMAIN_SEP_M2, sum, m, c, *mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M2, sum, ad, adlen);

    /* Check the authentication tag */
    return aead_check_tag(m, *mlen, sum, c + *mlen, SKINNY_AEAD_M2_TAG_SIZE);
}

int skinny_aead_m3_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Set the length of the returned ciphertext */
    *clen = mlen + SKINNY_AEAD_M3_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M3_NONCE_SIZE);

    /* Encrypt to plaintext to produce the ciphertext */
    skinny_aead_128_384_encrypt(&ks, DOMAIN_SEP_M3, sum, c, m, mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M3, sum, ad, adlen);

    /* Generate the authentication tag */
    memcpy(c + mlen, sum, SKINNY_AEAD_M3_TAG_SIZE);
    return 0;
}

int skinny_aead_m3_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Validate the ciphertext length and set the return "mlen" value */
    if (clen < SKINNY_AEAD_M3_TAG_SIZE)
        return -1;
    *mlen = clen - SKINNY_AEAD_M3_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M3_NONCE_SIZE);

    /* Decrypt to ciphertext to produce the plaintext */
    skinny_aead_128_384_decrypt(&ks, DOMAIN_SEP_M3, sum, m, c, *mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M3, sum, ad, adlen);

    /* Check the authentication tag */
    return aead_check_tag(m, *mlen, sum, c + *mlen, SKINNY_AEAD_M3_TAG_SIZE);
}

int skinny_aead_m4_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Set the length of the returned ciphertext */
    *clen = mlen + SKINNY_AEAD_M4_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M4_NONCE_SIZE);

    /* Encrypt to plaintext to produce the ciphertext */
    skinny_aead_128_384_encrypt(&ks, DOMAIN_SEP_M4, sum, c, m, mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M4, sum, ad, adlen);

    /* Generate the authentication tag */
    memcpy(c + mlen, sum, SKINNY_AEAD_M4_TAG_SIZE);
    return 0;
}

int skinny_aead_m4_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)
{
    skinny_128_384_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Validate the ciphertext length and set the return "mlen" value */
    if (clen < SKINNY_AEAD_M4_TAG_SIZE)
        return -1;
    *mlen = clen - SKINNY_AEAD_M4_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_384_init(&ks, k, npub, SKINNY_AEAD_M4_NONCE_SIZE);

    /* Decrypt to ciphertext to produce the plaintext */
    skinny_aead_128_384_decrypt(&ks, DOMAIN_SEP_M4, sum, m, c, *mlen);

    /* Process the associated data */
    skinny_aead_128_384_authenticate(&ks, DOMAIN_SEP_M4, sum, ad, adlen);

    /* Check the authentication tag */
    return aead_check_tag(m, *mlen, sum, c + *mlen, SKINNY_AEAD_M4_TAG_SIZE);
}

/**
 * \brief Initialize the key and nonce for SKINNY-128-256 based AEAD schemes.
 *
 * \param ks The key schedule to initialize.
 * \param key Points to the 16 bytes of the key.
 * \param nonce Points to the nonce.
 * \param nonce_len Length of the nonce in bytes.
 */
static void skinny_aead_128_256_init
    (skinny_128_256_key_schedule_t *ks, const unsigned char *key,
     const unsigned char *nonce, unsigned nonce_len)
{
    unsigned char k[32];
    memset(k, 0, 16 - nonce_len);
    memcpy(k + 16 - nonce_len, nonce, nonce_len);
    memcpy(k + 16, key, 16);
    skinny_128_256_init(ks, k);
}

/**
 * \brief Set the domain separation value in the tweak for SKINNY-128-256.
 *
 * \param ks Key schedule for the block cipher.
 * \param d Domain separation value to write into the tweak.
 */
#define skinny_aead_128_256_set_domain(ks,d) ((ks)->TK1[3] = (d))

/**
 * \brief Sets the LFSR field in the tweak for SKINNY-128-256.
 *
 * \param ks Key schedule for the block cipher.
 * \param lfsr 24-bit LFSR value.
 */
#define skinny_aead_128_256_set_lfsr(ks,lfsr) \
    do { \
        (ks)->TK1[0] = (uint8_t)(lfsr); \
        (ks)->TK1[1] = (uint8_t)((lfsr) >> 8); \
        (ks)->TK1[2] = (uint8_t)((lfsr) >> 16); \
    } while (0)

/**
 * \brief Updates the LFSR value for SKINNY-128-256.
 *
 * \param lfsr 24-bit LFSR value to be updated.
 */
#define skinny_aead_128_256_update_lfsr(lfsr) \
    do { \
        uint32_t feedback = ((lfsr) & (((uint32_t)1) << 23)) ? 0x1B : 0x00; \
        (lfsr) = ((lfsr) << 1) ^ (feedback); \
    } while (0)

/**
 * \brief Authenticates the associated data for a SKINNY-128-256 based AEAD.
 *
 * \param ks The key schedule to use.
 * \param prefix Domain separation prefix for the family member.
 * \param tag Final tag to XOR the authentication checksum into.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes.
 */
static void skinny_aead_128_256_authenticate
    (skinny_128_256_key_schedule_t *ks, unsigned char prefix,
     unsigned char tag[SKINNY_128_BLOCK_SIZE],
     const unsigned char *ad, unsigned long long adlen)
{
    unsigned char block[SKINNY_128_BLOCK_SIZE];
    uint32_t lfsr = 1;
    skinny_aead_128_256_set_domain(ks, prefix | 2);
    while (adlen >= SKINNY_128_BLOCK_SIZE) {
        skinny_aead_128_256_set_lfsr(ks, lfsr);
        skinny_128_256_encrypt(ks, block, ad);
        lw_xor_block(tag, block, SKINNY_128_BLOCK_SIZE);
        ad += SKINNY_128_BLOCK_SIZE;
        adlen -= SKINNY_128_BLOCK_SIZE;
        skinny_aead_128_256_update_lfsr(lfsr);
    }
    if (adlen > 0) {
        unsigned temp = (unsigned)adlen;
        skinny_aead_128_256_set_lfsr(ks, lfsr);
        skinny_aead_128_256_set_domain(ks, prefix | 3);
        memcpy(block, ad, temp);
        block[temp] = 0x80;
        memset(block + temp + 1, 0, SKINNY_128_BLOCK_SIZE - temp - 1);
        skinny_128_256_encrypt(ks, block, block);
        lw_xor_block(tag, block, SKINNY_128_BLOCK_SIZE);
    }
}

/**
 * \brief Encrypts the plaintext for a SKINNY-128-256 based AEAD.
 *
 * \param ks The key schedule to use.
 * \param prefix Domain separation prefix for the family member.
 * \param sum Authenticated checksum over the plaintext.
 * \param c Points to the buffer to receive the ciphertext.
 * \param m Points to the plaintext buffer.
 * \param mlen Number of bytes of plaintext to be encrypted.
 */
static void skinny_aead_128_256_encrypt
    (skinny_128_256_key_schedule_t *ks, unsigned char prefix,
     unsigned char sum[SKINNY_128_BLOCK_SIZE], unsigned char *c,
     const unsigned char *m, unsigned long long mlen)
{
    unsigned char block[SKINNY_128_BLOCK_SIZE];
    uint32_t lfsr = 1;
    memset(sum, 0, SKINNY_128_BLOCK_SIZE);
    skinny_aead_128_256_set_domain(ks, prefix | 0);
    while (mlen >= SKINNY_128_BLOCK_SIZE) {
        skinny_aead_128_256_set_lfsr(ks, lfsr);
        lw_xor_block(sum, m, SKINNY_128_BLOCK_SIZE);
        skinny_128_256_encrypt(ks, c, m);
        c += SKINNY_128_BLOCK_SIZE;
        m += SKINNY_128_BLOCK_SIZE;
        mlen -= SKINNY_128_BLOCK_SIZE;
        skinny_aead_128_256_update_lfsr(lfsr);
    }
    skinny_aead_128_256_set_lfsr(ks, lfsr);
    if (mlen > 0) {
        unsigned temp = (unsigned)mlen;
        skinny_aead_128_256_set_domain(ks, prefix | 1);
        lw_xor_block(sum, m, temp);
        sum[temp] ^= 0x80;
        memset(block, 0, SKINNY_128_BLOCK_SIZE);
        skinny_128_256_encrypt(ks, block, block);
        lw_xor_block_2_src(c, block, m, temp);
        skinny_aead_128_256_update_lfsr(lfsr);
        skinny_aead_128_256_set_lfsr(ks, lfsr);
        skinny_aead_128_256_set_domain(ks, prefix | 5);
    } else {
        skinny_aead_128_256_set_domain(ks, prefix | 4);
    }
    skinny_128_256_encrypt(ks, sum, sum);
}

/**
 * \brief Decrypts the ciphertext for a SKINNY-128-256 based AEAD.
 *
 * \param ks The key schedule to use.
 * \param prefix Domain separation prefix for the family member.
 * \param sum Authenticated checksum over the plaintext.
 * \param m Points to the buffer to receive the plaintext.
 * \param c Points to the ciphertext buffer.
 * \param mlen Number of bytes of ciphertext to be decrypted.
 */
static void skinny_aead_128_256_decrypt
    (skinny_128_256_key_schedule_t *ks, unsigned char prefix,
     unsigned char sum[SKINNY_128_BLOCK_SIZE], unsigned char *m,
     const unsigned char *c, unsigned long long mlen)
{
    unsigned char block[SKINNY_128_BLOCK_SIZE];
    uint32_t lfsr = 1;
    memset(sum, 0, SKINNY_128_BLOCK_SIZE);
    skinny_aead_128_256_set_domain(ks, prefix | 0);
    while (mlen >= SKINNY_128_BLOCK_SIZE) {
        skinny_aead_128_256_set_lfsr(ks, lfsr);
        skinny_128_256_decrypt(ks, m, c);
        lw_xor_block(sum, m, SKINNY_128_BLOCK_SIZE);
        c += SKINNY_128_BLOCK_SIZE;
        m += SKINNY_128_BLOCK_SIZE;
        mlen -= SKINNY_128_BLOCK_SIZE;
        skinny_aead_128_256_update_lfsr(lfsr);
    }
    skinny_aead_128_256_set_lfsr(ks, lfsr);
    if (mlen > 0) {
        unsigned temp = (unsigned)mlen;
        skinny_aead_128_256_set_domain(ks, prefix | 1);
        memset(block, 0, SKINNY_128_BLOCK_SIZE);
        skinny_128_256_encrypt(ks, block, block);
        lw_xor_block_2_src(m, block, c, temp);
        lw_xor_block(sum, m, temp);
        sum[temp] ^= 0x80;
        skinny_aead_128_256_update_lfsr(lfsr);
        skinny_aead_128_256_set_lfsr(ks, lfsr);
        skinny_aead_128_256_set_domain(ks, prefix | 5);
    } else {
        skinny_aead_128_256_set_domain(ks, prefix | 4);
    }
    skinny_128_256_encrypt(ks, sum, sum);
}

int skinny_aead_m5_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)
{
    skinny_128_256_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Set the length of the returned ciphertext */
    *clen = mlen + SKINNY_AEAD_M5_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_256_init(&ks, k, npub, SKINNY_AEAD_M5_NONCE_SIZE);

    /* Encrypt to plaintext to produce the ciphertext */
    skinny_aead_128_256_encrypt(&ks, DOMAIN_SEP_M5, sum, c, m, mlen);

    /* Process the associated data */
    skinny_aead_128_256_authenticate(&ks, DOMAIN_SEP_M5, sum, ad, adlen);

    /* Generate the authentication tag */
    memcpy(c + mlen, sum, SKINNY_AEAD_M5_TAG_SIZE);
    return 0;
}

int skinny_aead_m5_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)
{
    skinny_128_256_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Validate the ciphertext length and set the return "mlen" value */
    if (clen < SKINNY_AEAD_M5_TAG_SIZE)
        return -1;
    *mlen = clen - SKINNY_AEAD_M5_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_256_init(&ks, k, npub, SKINNY_AEAD_M5_NONCE_SIZE);

    /* Decrypt to ciphertext to produce the plaintext */
    skinny_aead_128_256_decrypt(&ks, DOMAIN_SEP_M5, sum, m, c, *mlen);

    /* Process the associated data */
    skinny_aead_128_256_authenticate(&ks, DOMAIN_SEP_M5, sum, ad, adlen);

    /* Check the authentication tag */
    return aead_check_tag(m, *mlen, sum, c + *mlen, SKINNY_AEAD_M5_TAG_SIZE);
}

int skinny_aead_m6_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)
{
    skinny_128_256_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Set the length of the returned ciphertext */
    *clen = mlen + SKINNY_AEAD_M6_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_256_init(&ks, k, npub, SKINNY_AEAD_M6_NONCE_SIZE);

    /* Encrypt to plaintext to produce the ciphertext */
    skinny_aead_128_256_encrypt(&ks, DOMAIN_SEP_M6, sum, c, m, mlen);

    /* Process the associated data */
    skinny_aead_128_256_authenticate(&ks, DOMAIN_SEP_M6, sum, ad, adlen);

    /* Generate the authentication tag */
    memcpy(c + mlen, sum, SKINNY_AEAD_M6_TAG_SIZE);
    return 0;
}

int skinny_aead_m6_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)
{
    skinny_128_256_key_schedule_t ks;
    unsigned char sum[SKINNY_128_BLOCK_SIZE];
    (void)nsec;

    /* Validate the ciphertext length and set the return "mlen" value */
    if (clen < SKINNY_AEAD_M6_TAG_SIZE)
        return -1;
    *mlen = clen - SKINNY_AEAD_M6_TAG_SIZE;

    /* Set up the key schedule with the key and the nonce */
    skinny_aead_128_256_init(&ks, k, npub, SKINNY_AEAD_M6_NONCE_SIZE);

    /* Decrypt to ciphertext to produce the plaintext */
    skinny_aead_128_256_decrypt(&ks, DOMAIN_SEP_M6, sum, m, c, *mlen);

    /* Process the associated data */
    skinny_aead_128_256_authenticate(&ks, DOMAIN_SEP_M6, sum, ad, adlen);

    /* Check the authentication tag */
    return aead_check_tag(m, *mlen, sum, c + *mlen, SKINNY_AEAD_M6_TAG_SIZE);
}