elephant.c 32.1 KB
Newer Older
Rhys Weatherley 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
/*
 * 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 "elephant.h"
#include "internal-keccak.h"
#include "internal-spongent.h"
#include <string.h>

aead_cipher_t const dumbo_cipher = {
    "Dumbo",
    DUMBO_KEY_SIZE,
    DUMBO_NONCE_SIZE,
    DUMBO_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    dumbo_aead_encrypt,
    dumbo_aead_decrypt
};

aead_cipher_t const jumbo_cipher = {
    "Jumbo",
    JUMBO_KEY_SIZE,
    JUMBO_NONCE_SIZE,
    JUMBO_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    jumbo_aead_encrypt,
    jumbo_aead_decrypt
};

aead_cipher_t const delirium_cipher = {
    "Delirium",
    DELIRIUM_KEY_SIZE,
    DELIRIUM_NONCE_SIZE,
    DELIRIUM_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    delirium_aead_encrypt,
    delirium_aead_decrypt
};

/**
 * \brief Applies the Dumbo LFSR to the mask.
 *
 * \param out The output mask.
 * \param in The input mask.
 */
static void dumbo_lfsr
    (unsigned char out[SPONGENT160_STATE_SIZE],
     const unsigned char in[SPONGENT160_STATE_SIZE])
{
    unsigned char temp = 
        leftRotate3_8(in[0]) ^ (in[3] << 7) ^ (in[13] >> 7);
    unsigned index;
    for (index = 0; index < SPONGENT160_STATE_SIZE - 1; ++index)
        out[index] = in[index + 1];
    out[SPONGENT160_STATE_SIZE - 1] = temp;
}

/**
 * \brief Processes the nonce and associated data for Dumbo.
 *
 * \param state Points to the Spongent-pi[160] state.
 * \param mask Points to the initial mask value.
 * \param next Points to the next mask value.
 * \param tag Points to the ongoing tag that is being computed.
 * \param npub Points to the nonce.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data.
 */
static void dumbo_process_ad
    (spongent160_state_t *state,
     unsigned char mask[SPONGENT160_STATE_SIZE],
     unsigned char next[SPONGENT160_STATE_SIZE],
     unsigned char tag[DUMBO_TAG_SIZE],
     const unsigned char *npub,
     const unsigned char *ad, unsigned long long adlen)
{
    unsigned posn, size;

    /* We need the "previous" and "next" masks in each step.
     * Compare the first such values */
    dumbo_lfsr(next, mask);
    dumbo_lfsr(next, next);

    /* Absorb the nonce into the state */
    lw_xor_block_2_src(state->B, mask, next, SPONGENT160_STATE_SIZE);
    lw_xor_block(state->B, npub, DUMBO_NONCE_SIZE);

    /* Absorb the rest of the associated data */
    posn = DUMBO_NONCE_SIZE;
    while (adlen > 0) {
        size = SPONGENT160_STATE_SIZE - posn;
        if (size <= adlen) {
            /* Process a complete block */
            lw_xor_block(state->B + posn, ad, size);
            spongent160_permute(state);
            lw_xor_block(state->B, mask, DUMBO_TAG_SIZE);
            lw_xor_block(state->B, next, DUMBO_TAG_SIZE);
            lw_xor_block(tag, state->B, DUMBO_TAG_SIZE);
            dumbo_lfsr(mask, mask);
            dumbo_lfsr(next, next);
            lw_xor_block_2_src(state->B, mask, next, SPONGENT160_STATE_SIZE);
            posn = 0;
        } else {
            /* Process the partial block at the end of the associated data */
            size = (unsigned)adlen;
            lw_xor_block(state->B + posn, ad, size);
            posn += size;
        }
        ad += size;
        adlen -= size;
    }

    /* Pad and absorb the final block */
    state->B[posn] ^= 0x01;
    spongent160_permute(state);
    lw_xor_block(state->B, mask, DUMBO_TAG_SIZE);
    lw_xor_block(state->B, next, DUMBO_TAG_SIZE);
    lw_xor_block(tag, state->B, DUMBO_TAG_SIZE);
}

int dumbo_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)
{
    spongent160_state_t state;
    unsigned char start[SPONGENT160_STATE_SIZE];
    unsigned char mask[SPONGENT160_STATE_SIZE];
    unsigned char next[SPONGENT160_STATE_SIZE];
    unsigned char tag[DUMBO_TAG_SIZE];
    (void)nsec;

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

    /* Hash the key and generate the initial mask */
    memcpy(state.B, k, DUMBO_KEY_SIZE);
    memset(state.B + DUMBO_KEY_SIZE, 0, sizeof(state.B) - DUMBO_KEY_SIZE);
    spongent160_permute(&state);
    memcpy(mask, state.B, DUMBO_KEY_SIZE);
    memset(mask + DUMBO_KEY_SIZE, 0, sizeof(mask) - DUMBO_KEY_SIZE);
    memcpy(start, mask, sizeof(mask));

    /* Tag starts at zero */
    memset(tag, 0, sizeof(tag));

    /* Authenticate the nonce and the associated data */
    dumbo_process_ad(&state, mask, next, tag, npub, ad, adlen);

    /* Reset back to the starting mask for the encryption phase */
    memcpy(mask, start, sizeof(mask));

    /* Encrypt and authenticate the payload */
    while (mlen >= SPONGENT160_STATE_SIZE) {
        /* Encrypt using the current mask */
        memcpy(state.B, mask, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, npub, DUMBO_NONCE_SIZE);
        spongent160_permute(&state);
        lw_xor_block(state.B, m, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, mask, SPONGENT160_STATE_SIZE);
        memcpy(c, state.B, SPONGENT160_STATE_SIZE);

        /* Authenticate using the next mask */
        dumbo_lfsr(next, mask);
        lw_xor_block(state.B, mask, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, next, SPONGENT160_STATE_SIZE);
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, DUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, DUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, DUMBO_TAG_SIZE);

        /* Advance to the next block */
        memcpy(mask, next, SPONGENT160_STATE_SIZE);
        c += SPONGENT160_STATE_SIZE;
        m += SPONGENT160_STATE_SIZE;
        mlen -= SPONGENT160_STATE_SIZE;
    }
    if (mlen > 0) {
        /* Encrypt the last block using the current mask */
        unsigned temp = (unsigned)mlen;
        memcpy(state.B, mask, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, npub, DUMBO_NONCE_SIZE);
        spongent160_permute(&state);
        lw_xor_block(state.B, m, temp);
        lw_xor_block(state.B, mask, SPONGENT160_STATE_SIZE);
        memcpy(c, state.B, temp);

        /* Authenticate the last block using the next mask */
        dumbo_lfsr(next, mask);
        state.B[temp] = 0x01;
        memset(state.B + temp + 1, 0, SPONGENT160_STATE_SIZE - temp - 1);
        lw_xor_block(state.B, mask, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, next, SPONGENT160_STATE_SIZE);
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, DUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, DUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, DUMBO_TAG_SIZE);
        c += temp;
    } else if (*clen != DUMBO_TAG_SIZE) {
        /* Pad and authenticate when the last block is aligned */
        dumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT160_STATE_SIZE);
        state.B[0] ^= 0x01;
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, DUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, DUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, DUMBO_TAG_SIZE);
    }

    /* Generate the authentication tag */
    memcpy(c, tag, DUMBO_TAG_SIZE);
    return 0;
}

int dumbo_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)
{
    spongent160_state_t state;
    unsigned char *mtemp = m;
    unsigned char start[SPONGENT160_STATE_SIZE];
    unsigned char mask[SPONGENT160_STATE_SIZE];
    unsigned char next[SPONGENT160_STATE_SIZE];
    unsigned char tag[DUMBO_TAG_SIZE];
    (void)nsec;

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

    /* Hash the key and generate the initial mask */
    memcpy(state.B, k, DUMBO_KEY_SIZE);
    memset(state.B + DUMBO_KEY_SIZE, 0, sizeof(state.B) - DUMBO_KEY_SIZE);
    spongent160_permute(&state);
    memcpy(mask, state.B, DUMBO_KEY_SIZE);
    memset(mask + DUMBO_KEY_SIZE, 0, sizeof(mask) - DUMBO_KEY_SIZE);
    memcpy(start, mask, sizeof(mask));

    /* Tag starts at zero */
    memset(tag, 0, sizeof(tag));

    /* Authenticate the nonce and the associated data */
    dumbo_process_ad(&state, mask, next, tag, npub, ad, adlen);

    /* Reset back to the starting mask for the encryption phase */
    memcpy(mask, start, sizeof(mask));

    /* Decrypt and authenticate the payload */
    clen -= DUMBO_TAG_SIZE;
    while (clen >= SPONGENT160_STATE_SIZE) {
        /* Authenticate using the next mask */
        dumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, c, SPONGENT160_STATE_SIZE);
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, DUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, DUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, DUMBO_TAG_SIZE);

        /* Decrypt using the current mask */
        memcpy(state.B, mask, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, npub, DUMBO_NONCE_SIZE);
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, SPONGENT160_STATE_SIZE);
        lw_xor_block_2_src(m, state.B, c, SPONGENT160_STATE_SIZE);

        /* Advance to the next block */
        memcpy(mask, next, SPONGENT160_STATE_SIZE);
        c += SPONGENT160_STATE_SIZE;
        m += SPONGENT160_STATE_SIZE;
        clen -= SPONGENT160_STATE_SIZE;
    }
    if (clen > 0) {
        /* Authenticate the last block using the next mask */
        unsigned temp = (unsigned)clen;
        dumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, c, temp);
        state.B[temp] ^= 0x01;
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, DUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, DUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, DUMBO_TAG_SIZE);

        /* Decrypt the last block using the current mask */
        memcpy(state.B, mask, SPONGENT160_STATE_SIZE);
        lw_xor_block(state.B, npub, DUMBO_NONCE_SIZE);
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, temp);
        lw_xor_block_2_src(m, state.B, c, temp);
        c += temp;
    } else if (*mlen != 0) {
        /* Pad and authenticate when the last block is aligned */
        dumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT160_STATE_SIZE);
        state.B[0] ^= 0x01;
        spongent160_permute(&state);
        lw_xor_block(state.B, mask, DUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, DUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, DUMBO_TAG_SIZE);
    }

    /* Check the authentication tag */
    return aead_check_tag(mtemp, *mlen, tag, c, DUMBO_TAG_SIZE);
}

/**
 * \brief Applies the Jumbo LFSR to the mask.
 *
 * \param out The output mask.
 * \param in The input mask.
 */
static void jumbo_lfsr
    (unsigned char out[SPONGENT176_STATE_SIZE],
     const unsigned char in[SPONGENT176_STATE_SIZE])
{
    unsigned char temp = 
        leftRotate1_8(in[0]) ^ (in[3] << 7) ^ (in[19] >> 7);
    unsigned index;
    for (index = 0; index < SPONGENT176_STATE_SIZE - 1; ++index)
        out[index] = in[index + 1];
    out[SPONGENT176_STATE_SIZE - 1] = temp;
}

/**
 * \brief Processes the nonce and associated data for Jumbo.
 *
 * \param state Points to the Spongent-pi[170] state.
 * \param mask Points to the initial mask value.
 * \param next Points to the next mask value.
 * \param tag Points to the ongoing tag that is being computed.
 * \param npub Points to the nonce.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data.
 */
static void jumbo_process_ad
    (spongent176_state_t *state,
     unsigned char mask[SPONGENT176_STATE_SIZE],
     unsigned char next[SPONGENT176_STATE_SIZE],
     unsigned char tag[JUMBO_TAG_SIZE],
     const unsigned char *npub,
     const unsigned char *ad, unsigned long long adlen)
{
    unsigned posn, size;

    /* We need the "previous" and "next" masks in each step.
     * Compare the first such values */
    jumbo_lfsr(next, mask);
    jumbo_lfsr(next, next);

    /* Absorb the nonce into the state */
    lw_xor_block_2_src(state->B, mask, next, SPONGENT176_STATE_SIZE);
    lw_xor_block(state->B, npub, JUMBO_NONCE_SIZE);

    /* Absorb the rest of the associated data */
    posn = JUMBO_NONCE_SIZE;
    while (adlen > 0) {
        size = SPONGENT176_STATE_SIZE - posn;
        if (size <= adlen) {
            /* Process a complete block */
            lw_xor_block(state->B + posn, ad, size);
            spongent176_permute(state);
            lw_xor_block(state->B, mask, JUMBO_TAG_SIZE);
            lw_xor_block(state->B, next, JUMBO_TAG_SIZE);
            lw_xor_block(tag, state->B, JUMBO_TAG_SIZE);
            jumbo_lfsr(mask, mask);
            jumbo_lfsr(next, next);
            lw_xor_block_2_src(state->B, mask, next, SPONGENT176_STATE_SIZE);
            posn = 0;
        } else {
            /* Process the partial block at the end of the associated data */
            size = (unsigned)adlen;
            lw_xor_block(state->B + posn, ad, size);
            posn += size;
        }
        ad += size;
        adlen -= size;
    }

    /* Pad and absorb the final block */
    state->B[posn] ^= 0x01;
    spongent176_permute(state);
    lw_xor_block(state->B, mask, JUMBO_TAG_SIZE);
    lw_xor_block(state->B, next, JUMBO_TAG_SIZE);
    lw_xor_block(tag, state->B, JUMBO_TAG_SIZE);
}

int jumbo_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)
{
    spongent176_state_t state;
    unsigned char start[SPONGENT176_STATE_SIZE];
    unsigned char mask[SPONGENT176_STATE_SIZE];
    unsigned char next[SPONGENT176_STATE_SIZE];
    unsigned char tag[JUMBO_TAG_SIZE];
    (void)nsec;

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

    /* Hash the key and generate the initial mask */
    memcpy(state.B, k, JUMBO_KEY_SIZE);
    memset(state.B + JUMBO_KEY_SIZE, 0, sizeof(state.B) - JUMBO_KEY_SIZE);
    spongent176_permute(&state);
    memcpy(mask, state.B, JUMBO_KEY_SIZE);
    memset(mask + JUMBO_KEY_SIZE, 0, sizeof(mask) - JUMBO_KEY_SIZE);
    memcpy(start, mask, sizeof(mask));

    /* Tag starts at zero */
    memset(tag, 0, sizeof(tag));

    /* Authenticate the nonce and the associated data */
    jumbo_process_ad(&state, mask, next, tag, npub, ad, adlen);

    /* Reset back to the starting mask for the encryption phase */
    memcpy(mask, start, sizeof(mask));

    /* Encrypt and authenticate the payload */
    while (mlen >= SPONGENT176_STATE_SIZE) {
        /* Encrypt using the current mask */
        memcpy(state.B, mask, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, npub, JUMBO_NONCE_SIZE);
        spongent176_permute(&state);
        lw_xor_block(state.B, m, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, mask, SPONGENT176_STATE_SIZE);
        memcpy(c, state.B, SPONGENT176_STATE_SIZE);

        /* Authenticate using the next mask */
        jumbo_lfsr(next, mask);
        lw_xor_block(state.B, mask, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, next, SPONGENT176_STATE_SIZE);
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, JUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, JUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, JUMBO_TAG_SIZE);

        /* Advance to the next block */
        memcpy(mask, next, SPONGENT176_STATE_SIZE);
        c += SPONGENT176_STATE_SIZE;
        m += SPONGENT176_STATE_SIZE;
        mlen -= SPONGENT176_STATE_SIZE;
    }
    if (mlen > 0) {
        /* Encrypt the last block using the current mask */
        unsigned temp = (unsigned)mlen;
        memcpy(state.B, mask, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, npub, JUMBO_NONCE_SIZE);
        spongent176_permute(&state);
        lw_xor_block(state.B, m, temp);
        lw_xor_block(state.B, mask, SPONGENT176_STATE_SIZE);
        memcpy(c, state.B, temp);

        /* Authenticate the last block using the next mask */
        jumbo_lfsr(next, mask);
        state.B[temp] = 0x01;
        memset(state.B + temp + 1, 0, SPONGENT176_STATE_SIZE - temp - 1);
        lw_xor_block(state.B, mask, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, next, SPONGENT176_STATE_SIZE);
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, JUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, JUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, JUMBO_TAG_SIZE);
        c += temp;
    } else if (*clen != JUMBO_TAG_SIZE) {
        /* Pad and authenticate when the last block is aligned */
        jumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT176_STATE_SIZE);
        state.B[0] ^= 0x01;
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, JUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, JUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, JUMBO_TAG_SIZE);
    }

    /* Generate the authentication tag */
    memcpy(c, tag, JUMBO_TAG_SIZE);
    return 0;
}

int jumbo_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)
{
    spongent176_state_t state;
    unsigned char *mtemp = m;
    unsigned char start[SPONGENT176_STATE_SIZE];
    unsigned char mask[SPONGENT176_STATE_SIZE];
    unsigned char next[SPONGENT176_STATE_SIZE];
    unsigned char tag[JUMBO_TAG_SIZE];
    (void)nsec;

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

    /* Hash the key and generate the initial mask */
    memcpy(state.B, k, JUMBO_KEY_SIZE);
    memset(state.B + JUMBO_KEY_SIZE, 0, sizeof(state.B) - JUMBO_KEY_SIZE);
    spongent176_permute(&state);
    memcpy(mask, state.B, JUMBO_KEY_SIZE);
    memset(mask + JUMBO_KEY_SIZE, 0, sizeof(mask) - JUMBO_KEY_SIZE);
    memcpy(start, mask, sizeof(mask));

    /* Tag starts at zero */
    memset(tag, 0, sizeof(tag));

    /* Authenticate the nonce and the associated data */
    jumbo_process_ad(&state, mask, next, tag, npub, ad, adlen);

    /* Reset back to the starting mask for the encryption phase */
    memcpy(mask, start, sizeof(mask));

    /* Decrypt and authenticate the payload */
    clen -= JUMBO_TAG_SIZE;
    while (clen >= SPONGENT176_STATE_SIZE) {
        /* Authenticate using the next mask */
        jumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, c, SPONGENT176_STATE_SIZE);
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, JUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, JUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, JUMBO_TAG_SIZE);

        /* Decrypt using the current mask */
        memcpy(state.B, mask, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, npub, JUMBO_NONCE_SIZE);
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, SPONGENT176_STATE_SIZE);
        lw_xor_block_2_src(m, state.B, c, SPONGENT176_STATE_SIZE);

        /* Advance to the next block */
        memcpy(mask, next, SPONGENT176_STATE_SIZE);
        c += SPONGENT176_STATE_SIZE;
        m += SPONGENT176_STATE_SIZE;
        clen -= SPONGENT176_STATE_SIZE;
    }
    if (clen > 0) {
        /* Authenticate the last block using the next mask */
        unsigned temp = (unsigned)clen;
        jumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, c, temp);
        state.B[temp] ^= 0x01;
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, JUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, JUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, JUMBO_TAG_SIZE);

        /* Decrypt the last block using the current mask */
        memcpy(state.B, mask, SPONGENT176_STATE_SIZE);
        lw_xor_block(state.B, npub, JUMBO_NONCE_SIZE);
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, temp);
        lw_xor_block_2_src(m, state.B, c, temp);
        c += temp;
    } else if (*mlen != 0) {
        /* Pad and authenticate when the last block is aligned */
        jumbo_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, SPONGENT176_STATE_SIZE);
        state.B[0] ^= 0x01;
        spongent176_permute(&state);
        lw_xor_block(state.B, mask, JUMBO_TAG_SIZE);
        lw_xor_block(state.B, next, JUMBO_TAG_SIZE);
        lw_xor_block(tag, state.B, JUMBO_TAG_SIZE);
    }

    /* Check the authentication tag */
    return aead_check_tag(mtemp, *mlen, tag, c, JUMBO_TAG_SIZE);
}

/**
 * \brief Applies the Delirium LFSR to the mask.
 *
 * \param out The output mask.
 * \param in The input mask.
 */
static void delirium_lfsr
    (unsigned char out[KECCAKP_200_STATE_SIZE],
     const unsigned char in[KECCAKP_200_STATE_SIZE])
{
    unsigned char temp = 
        leftRotate1_8(in[0]) ^ leftRotate1_8(in[2]) ^ (in[13] << 1);
    unsigned index;
    for (index = 0; index < KECCAKP_200_STATE_SIZE - 1; ++index)
        out[index] = in[index + 1];
    out[KECCAKP_200_STATE_SIZE - 1] = temp;
}

/**
 * \brief Processes the nonce and associated data for Delirium.
 *
 * \param state Points to the Keccak[200] state.
 * \param mask Points to the initial mask value.
 * \param next Points to the next mask value.
 * \param tag Points to the ongoing tag that is being computed.
 * \param npub Points to the nonce.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data.
 */
static void delirium_process_ad
    (keccakp_200_state_t *state,
     unsigned char mask[KECCAKP_200_STATE_SIZE],
     unsigned char next[KECCAKP_200_STATE_SIZE],
     unsigned char tag[DELIRIUM_TAG_SIZE],
     const unsigned char *npub,
     const unsigned char *ad, unsigned long long adlen)
{
    unsigned posn, size;

    /* We need the "previous" and "next" masks in each step.
     * Compare the first such values */
    delirium_lfsr(next, mask);
    delirium_lfsr(next, next);

    /* Absorb the nonce into the state */
    lw_xor_block_2_src(state->B, mask, next, KECCAKP_200_STATE_SIZE);
    lw_xor_block(state->B, npub, DELIRIUM_NONCE_SIZE);

    /* Absorb the rest of the associated data */
    posn = DELIRIUM_NONCE_SIZE;
    while (adlen > 0) {
        size = KECCAKP_200_STATE_SIZE - posn;
        if (size <= adlen) {
            /* Process a complete block */
            lw_xor_block(state->B + posn, ad, size);
663
            keccakp_200_permute(state);
Rhys Weatherley committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
            lw_xor_block(state->B, mask, DELIRIUM_TAG_SIZE);
            lw_xor_block(state->B, next, DELIRIUM_TAG_SIZE);
            lw_xor_block(tag, state->B, DELIRIUM_TAG_SIZE);
            delirium_lfsr(mask, mask);
            delirium_lfsr(next, next);
            lw_xor_block_2_src(state->B, mask, next, KECCAKP_200_STATE_SIZE);
            posn = 0;
        } else {
            /* Process the partial block at the end of the associated data */
            size = (unsigned)adlen;
            lw_xor_block(state->B + posn, ad, size);
            posn += size;
        }
        ad += size;
        adlen -= size;
    }

    /* Pad and absorb the final block */
    state->B[posn] ^= 0x01;
683
    keccakp_200_permute(state);
Rhys Weatherley committed
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
    lw_xor_block(state->B, mask, DELIRIUM_TAG_SIZE);
    lw_xor_block(state->B, next, DELIRIUM_TAG_SIZE);
    lw_xor_block(tag, state->B, DELIRIUM_TAG_SIZE);
}

int delirium_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)
{
    keccakp_200_state_t state;
    unsigned char start[KECCAKP_200_STATE_SIZE];
    unsigned char mask[KECCAKP_200_STATE_SIZE];
    unsigned char next[KECCAKP_200_STATE_SIZE];
    unsigned char tag[DELIRIUM_TAG_SIZE];
    (void)nsec;

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

    /* Hash the key and generate the initial mask */
    memcpy(state.B, k, DELIRIUM_KEY_SIZE);
    memset(state.B + DELIRIUM_KEY_SIZE, 0, sizeof(state.B) - DELIRIUM_KEY_SIZE);
710
    keccakp_200_permute(&state);
Rhys Weatherley committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    memcpy(mask, state.B, DELIRIUM_KEY_SIZE);
    memset(mask + DELIRIUM_KEY_SIZE, 0, sizeof(mask) - DELIRIUM_KEY_SIZE);
    memcpy(start, mask, sizeof(mask));

    /* Tag starts at zero */
    memset(tag, 0, sizeof(tag));

    /* Authenticate the nonce and the associated data */
    delirium_process_ad(&state, mask, next, tag, npub, ad, adlen);

    /* Reset back to the starting mask for the encryption phase */
    memcpy(mask, start, sizeof(mask));

    /* Encrypt and authenticate the payload */
    while (mlen >= KECCAKP_200_STATE_SIZE) {
        /* Encrypt using the current mask */
        memcpy(state.B, mask, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, npub, DELIRIUM_NONCE_SIZE);
729
        keccakp_200_permute(&state);
Rhys Weatherley committed
730 731 732 733 734 735 736 737
        lw_xor_block(state.B, m, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, mask, KECCAKP_200_STATE_SIZE);
        memcpy(c, state.B, KECCAKP_200_STATE_SIZE);

        /* Authenticate using the next mask */
        delirium_lfsr(next, mask);
        lw_xor_block(state.B, mask, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, next, KECCAKP_200_STATE_SIZE);
738
        keccakp_200_permute(&state);
Rhys Weatherley committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
        lw_xor_block(state.B, mask, DELIRIUM_TAG_SIZE);
        lw_xor_block(state.B, next, DELIRIUM_TAG_SIZE);
        lw_xor_block(tag, state.B, DELIRIUM_TAG_SIZE);

        /* Advance to the next block */
        memcpy(mask, next, KECCAKP_200_STATE_SIZE);
        c += KECCAKP_200_STATE_SIZE;
        m += KECCAKP_200_STATE_SIZE;
        mlen -= KECCAKP_200_STATE_SIZE;
    }
    if (mlen > 0) {
        /* Encrypt the last block using the current mask */
        unsigned temp = (unsigned)mlen;
        memcpy(state.B, mask, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, npub, DELIRIUM_NONCE_SIZE);
754
        keccakp_200_permute(&state);
Rhys Weatherley committed
755 756 757 758 759 760 761 762 763 764
        lw_xor_block(state.B, m, temp);
        lw_xor_block(state.B, mask, KECCAKP_200_STATE_SIZE);
        memcpy(c, state.B, temp);

        /* Authenticate the last block using the next mask */
        delirium_lfsr(next, mask);
        state.B[temp] = 0x01;
        memset(state.B + temp + 1, 0, KECCAKP_200_STATE_SIZE - temp - 1);
        lw_xor_block(state.B, mask, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, next, KECCAKP_200_STATE_SIZE);
765
        keccakp_200_permute(&state);
Rhys Weatherley committed
766 767 768 769 770 771 772 773 774
        lw_xor_block(state.B, mask, DELIRIUM_TAG_SIZE);
        lw_xor_block(state.B, next, DELIRIUM_TAG_SIZE);
        lw_xor_block(tag, state.B, DELIRIUM_TAG_SIZE);
        c += temp;
    } else if (*clen != DELIRIUM_TAG_SIZE) {
        /* Pad and authenticate when the last block is aligned */
        delirium_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, KECCAKP_200_STATE_SIZE);
        state.B[0] ^= 0x01;
775
        keccakp_200_permute(&state);
Rhys Weatherley committed
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
        lw_xor_block(state.B, mask, DELIRIUM_TAG_SIZE);
        lw_xor_block(state.B, next, DELIRIUM_TAG_SIZE);
        lw_xor_block(tag, state.B, DELIRIUM_TAG_SIZE);
    }

    /* Generate the authentication tag */
    memcpy(c, tag, DELIRIUM_TAG_SIZE);
    return 0;
}

int delirium_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)
{
    keccakp_200_state_t state;
    unsigned char *mtemp = m;
    unsigned char start[KECCAKP_200_STATE_SIZE];
    unsigned char mask[KECCAKP_200_STATE_SIZE];
    unsigned char next[KECCAKP_200_STATE_SIZE];
    unsigned char tag[DELIRIUM_TAG_SIZE];
    (void)nsec;

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

    /* Hash the key and generate the initial mask */
    memcpy(state.B, k, DELIRIUM_KEY_SIZE);
    memset(state.B + DELIRIUM_KEY_SIZE, 0, sizeof(state.B) - DELIRIUM_KEY_SIZE);
810
    keccakp_200_permute(&state);
Rhys Weatherley committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
    memcpy(mask, state.B, DELIRIUM_KEY_SIZE);
    memset(mask + DELIRIUM_KEY_SIZE, 0, sizeof(mask) - DELIRIUM_KEY_SIZE);
    memcpy(start, mask, sizeof(mask));

    /* Tag starts at zero */
    memset(tag, 0, sizeof(tag));

    /* Authenticate the nonce and the associated data */
    delirium_process_ad(&state, mask, next, tag, npub, ad, adlen);

    /* Reset back to the starting mask for the encryption phase */
    memcpy(mask, start, sizeof(mask));

    /* Decrypt and authenticate the payload */
    clen -= DELIRIUM_TAG_SIZE;
    while (clen >= KECCAKP_200_STATE_SIZE) {
        /* Authenticate using the next mask */
        delirium_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, c, KECCAKP_200_STATE_SIZE);
831
        keccakp_200_permute(&state);
Rhys Weatherley committed
832 833 834 835 836 837 838
        lw_xor_block(state.B, mask, DELIRIUM_TAG_SIZE);
        lw_xor_block(state.B, next, DELIRIUM_TAG_SIZE);
        lw_xor_block(tag, state.B, DELIRIUM_TAG_SIZE);

        /* Decrypt using the current mask */
        memcpy(state.B, mask, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, npub, DELIRIUM_NONCE_SIZE);
839
        keccakp_200_permute(&state);
Rhys Weatherley committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
        lw_xor_block(state.B, mask, KECCAKP_200_STATE_SIZE);
        lw_xor_block_2_src(m, state.B, c, KECCAKP_200_STATE_SIZE);

        /* Advance to the next block */
        memcpy(mask, next, KECCAKP_200_STATE_SIZE);
        c += KECCAKP_200_STATE_SIZE;
        m += KECCAKP_200_STATE_SIZE;
        clen -= KECCAKP_200_STATE_SIZE;
    }
    if (clen > 0) {
        /* Authenticate the last block using the next mask */
        unsigned temp = (unsigned)clen;
        delirium_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, c, temp);
        state.B[temp] ^= 0x01;
856
        keccakp_200_permute(&state);
Rhys Weatherley committed
857 858 859 860 861 862 863
        lw_xor_block(state.B, mask, DELIRIUM_TAG_SIZE);
        lw_xor_block(state.B, next, DELIRIUM_TAG_SIZE);
        lw_xor_block(tag, state.B, DELIRIUM_TAG_SIZE);

        /* Decrypt the last block using the current mask */
        memcpy(state.B, mask, KECCAKP_200_STATE_SIZE);
        lw_xor_block(state.B, npub, DELIRIUM_NONCE_SIZE);
864
        keccakp_200_permute(&state);
Rhys Weatherley committed
865 866 867 868 869 870 871 872
        lw_xor_block(state.B, mask, temp);
        lw_xor_block_2_src(m, state.B, c, temp);
        c += temp;
    } else if (*mlen != 0) {
        /* Pad and authenticate when the last block is aligned */
        delirium_lfsr(next, mask);
        lw_xor_block_2_src(state.B, mask, next, KECCAKP_200_STATE_SIZE);
        state.B[0] ^= 0x01;
873
        keccakp_200_permute(&state);
Rhys Weatherley committed
874 875 876 877 878 879 880 881
        lw_xor_block(state.B, mask, DELIRIUM_TAG_SIZE);
        lw_xor_block(state.B, next, DELIRIUM_TAG_SIZE);
        lw_xor_block(tag, state.B, DELIRIUM_TAG_SIZE);
    }

    /* Check the authentication tag */
    return aead_check_tag(mtemp, *mlen, tag, c, DELIRIUM_TAG_SIZE);
}