spook.c 16.9 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
/*
 * 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 "spook.h"
#include "internal-spook.h"
#include "internal-util.h"
#include <string.h>

aead_cipher_t const spook_128_512_su_cipher = {
    "Spook-128-512-su",
    SPOOK_SU_KEY_SIZE,
    SPOOK_NONCE_SIZE,
    SPOOK_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    spook_128_512_su_aead_encrypt,
    spook_128_512_su_aead_decrypt
};

aead_cipher_t const spook_128_384_su_cipher = {
    "Spook-128-384-su",
    SPOOK_SU_KEY_SIZE,
    SPOOK_NONCE_SIZE,
    SPOOK_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    spook_128_384_su_aead_encrypt,
    spook_128_384_su_aead_decrypt
};

aead_cipher_t const spook_128_512_mu_cipher = {
    "Spook-128-512-mu",
    SPOOK_MU_KEY_SIZE,
    SPOOK_NONCE_SIZE,
    SPOOK_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    spook_128_512_mu_aead_encrypt,
    spook_128_512_mu_aead_decrypt
};

aead_cipher_t const spook_128_384_mu_cipher = {
    "Spook-128-384-mu",
    SPOOK_MU_KEY_SIZE,
    SPOOK_NONCE_SIZE,
    SPOOK_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    spook_128_384_mu_aead_encrypt,
    spook_128_384_mu_aead_decrypt
};

/**
 * \brief Initializes the Shadow-512 sponge state.
 *
 * \param state The sponge state.
 * \param k Points to the key.
 * \param klen Length of the key in bytes, either 16 or 32.
 * \param npub Public nonce for the state.
 */
static void spook_128_512_init
    (shadow512_state_t *state,
     const unsigned char *k, unsigned klen,
     const unsigned char *npub)
{
    memset(state->B, 0, SHADOW512_STATE_SIZE);
    if (klen == SPOOK_MU_KEY_SIZE) {
        /* The public tweak is 126 bits in size followed by a 1 bit */
        memcpy(state->B, k + CLYDE128_BLOCK_SIZE, CLYDE128_BLOCK_SIZE);
        state->B[CLYDE128_BLOCK_SIZE - 1] &= 0x7F;
        state->B[CLYDE128_BLOCK_SIZE - 1] |= 0x40;
    }
    memcpy(state->B + CLYDE128_BLOCK_SIZE, npub, CLYDE128_BLOCK_SIZE);
89
    clyde128_encrypt(k, state->W + 12, state->W + 4, state->W);
Rhys Weatherley committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    shadow512(state);
}

/**
 * \brief Initializes the Shadow-384 sponge state.
 *
 * \param state The sponge state.
 * \param k Points to the key.
 * \param klen Length of the key in bytes, either 16 or 32.
 * \param npub Public nonce for the state.
 */
static void spook_128_384_init
    (shadow384_state_t *state,
     const unsigned char *k, unsigned klen,
     const unsigned char *npub)
{
    memset(state->B, 0, SHADOW384_STATE_SIZE);
    if (klen == SPOOK_MU_KEY_SIZE) {
        /* The public tweak is 126 bits in size followed by a 1 bit */
        memcpy(state->B, k + CLYDE128_BLOCK_SIZE, CLYDE128_BLOCK_SIZE);
        state->B[CLYDE128_BLOCK_SIZE - 1] &= 0x7F;
        state->B[CLYDE128_BLOCK_SIZE - 1] |= 0x40;
    }
    memcpy(state->B + CLYDE128_BLOCK_SIZE, npub, CLYDE128_BLOCK_SIZE);
114
    clyde128_encrypt(k, state->W + 8, state->W + 4, state->W);
Rhys Weatherley committed
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
    shadow384(state);
}

/**
 * \brief Absorbs associated data into the Shadow-512 sponge state.
 *
 * \param state The sponge state.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes, must be non-zero.
 */
static void spook_128_512_absorb
    (shadow512_state_t *state,
     const unsigned char *ad, unsigned long long adlen)
{
    while (adlen >= SHADOW512_RATE) {
        lw_xor_block(state->B, ad, SHADOW512_RATE);
        shadow512(state);
        ad += SHADOW512_RATE;
        adlen -= SHADOW512_RATE;
    }
    if (adlen > 0) {
        unsigned temp = (unsigned)adlen;
        lw_xor_block(state->B, ad, temp);
        state->B[temp] ^= 0x01;
        state->B[SHADOW512_RATE] ^= 0x02;
        shadow512(state);
    }
}

/**
 * \brief Absorbs associated data into the Shadow-384 sponge state.
 *
 * \param state The sponge state.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes, must be non-zero.
 */
static void spook_128_384_absorb
    (shadow384_state_t *state,
     const unsigned char *ad, unsigned long long adlen)
{
    while (adlen >= SHADOW384_RATE) {
        lw_xor_block(state->B, ad, SHADOW384_RATE);
        shadow384(state);
        ad += SHADOW384_RATE;
        adlen -= SHADOW384_RATE;
    }
    if (adlen > 0) {
        unsigned temp = (unsigned)adlen;
        lw_xor_block(state->B, ad, temp);
        state->B[temp] ^= 0x01;
        state->B[SHADOW384_RATE] ^= 0x02;
        shadow384(state);
    }
}

/**
 * \brief Encrypts the plaintext with the Shadow-512 sponge state.
 *
 * \param state The sponge state.
 * \param c Points to the ciphertext output buffer.
 * \param m Points to the plaintext input buffer.
 * \param mlen Number of bytes of plaintext to be encrypted.
 */
static void spook_128_512_encrypt
    (shadow512_state_t *state, unsigned char *c,
     const unsigned char *m, unsigned long long mlen)
{
    state->B[SHADOW512_RATE] ^= 0x01;
    while (mlen >= SHADOW512_RATE) {
        lw_xor_block_2_dest(c, state->B, m, SHADOW512_RATE);
        shadow512(state);
        c += SHADOW512_RATE;
        m += SHADOW512_RATE;
        mlen -= SHADOW512_RATE;
    }
    if (mlen > 0) {
        unsigned temp = (unsigned)mlen;
        lw_xor_block_2_dest(c, state->B, m, temp);
        state->B[temp] ^= 0x01;
        state->B[SHADOW512_RATE] ^= 0x02;
        shadow512(state);
    }
}

/**
 * \brief Encrypts the plaintext with the Shadow-384 sponge state.
 *
 * \param state The sponge state.
 * \param c Points to the ciphertext output buffer.
 * \param m Points to the plaintext input buffer.
 * \param mlen Number of bytes of plaintext to be encrypted.
 */
static void spook_128_384_encrypt
    (shadow384_state_t *state, unsigned char *c,
     const unsigned char *m, unsigned long long mlen)
{
    state->B[SHADOW384_RATE] ^= 0x01;
    while (mlen >= SHADOW384_RATE) {
        lw_xor_block_2_dest(c, state->B, m, SHADOW384_RATE);
        shadow384(state);
        c += SHADOW384_RATE;
        m += SHADOW384_RATE;
        mlen -= SHADOW384_RATE;
    }
    if (mlen > 0) {
        unsigned temp = (unsigned)mlen;
        lw_xor_block_2_dest(c, state->B, m, temp);
        state->B[temp] ^= 0x01;
        state->B[SHADOW384_RATE] ^= 0x02;
        shadow384(state);
    }
}

/**
 * \brief Decrypts the ciphertext with the Shadow-512 sponge state.
 *
 * \param state The sponge state.
 * \param m Points to the plaintext output buffer.
 * \param c Points to the ciphertext input buffer.
 * \param clen Number of bytes of ciphertext to be decrypted.
 */
static void spook_128_512_decrypt
    (shadow512_state_t *state, unsigned char *m,
     const unsigned char *c, unsigned long long clen)
{
    state->B[SHADOW512_RATE] ^= 0x01;
    while (clen >= SHADOW512_RATE) {
        lw_xor_block_swap(m, state->B, c, SHADOW512_RATE);
        shadow512(state);
        c += SHADOW512_RATE;
        m += SHADOW512_RATE;
        clen -= SHADOW512_RATE;
    }
    if (clen > 0) {
        unsigned temp = (unsigned)clen;
        lw_xor_block_swap(m, state->B, c, temp);
        state->B[temp] ^= 0x01;
        state->B[SHADOW512_RATE] ^= 0x02;
        shadow512(state);
    }
}

/**
 * \brief Decrypts the ciphertext with the Shadow-384 sponge state.
 *
 * \param state The sponge state.
 * \param m Points to the plaintext output buffer.
 * \param c Points to the ciphertext input buffer.
 * \param clen Number of bytes of ciphertext to be decrypted.
 */
static void spook_128_384_decrypt
    (shadow384_state_t *state, unsigned char *m,
     const unsigned char *c, unsigned long long clen)
{
    state->B[SHADOW384_RATE] ^= 0x01;
    while (clen >= SHADOW384_RATE) {
        lw_xor_block_swap(m, state->B, c, SHADOW384_RATE);
        shadow384(state);
        c += SHADOW384_RATE;
        m += SHADOW384_RATE;
        clen -= SHADOW384_RATE;
    }
    if (clen > 0) {
        unsigned temp = (unsigned)clen;
        lw_xor_block_swap(m, state->B, c, temp);
        state->B[temp] ^= 0x01;
        state->B[SHADOW384_RATE] ^= 0x02;
        shadow384(state);
    }
}

int spook_128_512_su_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)
{
    shadow512_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-512 sponge state */
    spook_128_512_init(&state, k, SPOOK_SU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_512_absorb(&state, ad, adlen);

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0)
        spook_128_512_encrypt(&state, c, m, mlen);

    /* Compute the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
313
    clyde128_encrypt(k, state.W, state.W, state.W + 4);
Rhys Weatherley committed
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
    memcpy(c + mlen, state.B, SPOOK_TAG_SIZE);
    return 0;
}

int spook_128_512_su_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)
{
    shadow512_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-512 sponge state */
    spook_128_512_init(&state, k, SPOOK_SU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_512_absorb(&state, ad, adlen);

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= SPOOK_TAG_SIZE;
    if (clen > 0)
        spook_128_512_decrypt(&state, m, c, clen);

    /* Check the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
348
    clyde128_decrypt(k, state.W + 4, c + clen, state.W + 4);
Rhys Weatherley committed
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
    return aead_check_tag
        (m, clen, state.B, state.B + CLYDE128_BLOCK_SIZE, SPOOK_TAG_SIZE);
}

int spook_128_384_su_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)
{
    shadow384_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-384 sponge state */
    spook_128_384_init(&state, k, SPOOK_SU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_384_absorb(&state, ad, adlen);

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0)
        spook_128_384_encrypt(&state, c, m, mlen);

    /* Compute the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
380
    clyde128_encrypt(k, state.W, state.W, state.W + 4);
Rhys Weatherley committed
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
    memcpy(c + mlen, state.B, SPOOK_TAG_SIZE);
    return 0;
}

int spook_128_384_su_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)
{
    shadow384_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-384 sponge state */
    spook_128_384_init(&state, k, SPOOK_SU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_384_absorb(&state, ad, adlen);

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= SPOOK_TAG_SIZE;
    if (clen > 0)
        spook_128_384_decrypt(&state, m, c, clen);

    /* Check the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
415
    clyde128_decrypt(k, state.W + 4, c + clen, state.W + 4);
Rhys Weatherley committed
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
    return aead_check_tag
        (m, clen, state.B, state.B + CLYDE128_BLOCK_SIZE, SPOOK_TAG_SIZE);
}

int spook_128_512_mu_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)
{
    shadow512_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-512 sponge state */
    spook_128_512_init(&state, k, SPOOK_MU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_512_absorb(&state, ad, adlen);

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0)
        spook_128_512_encrypt(&state, c, m, mlen);

    /* Compute the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
447
    clyde128_encrypt(k, state.W, state.W, state.W + 4);
Rhys Weatherley committed
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
    memcpy(c + mlen, state.B, SPOOK_TAG_SIZE);
    return 0;
}

int spook_128_512_mu_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)
{
    shadow512_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-512 sponge state */
    spook_128_512_init(&state, k, SPOOK_MU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_512_absorb(&state, ad, adlen);

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= SPOOK_TAG_SIZE;
    if (clen > 0)
        spook_128_512_decrypt(&state, m, c, clen);

    /* Check the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
482
    clyde128_decrypt(k, state.W + 4, c + clen, state.W + 4);
Rhys Weatherley committed
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
    return aead_check_tag
        (m, clen, state.B, state.B + CLYDE128_BLOCK_SIZE, SPOOK_TAG_SIZE);
}

int spook_128_384_mu_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)
{
    shadow384_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-384 sponge state */
    spook_128_384_init(&state, k, SPOOK_MU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_384_absorb(&state, ad, adlen);

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0)
        spook_128_384_encrypt(&state, c, m, mlen);

    /* Compute the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
514
    clyde128_encrypt(k, state.W, state.W, state.W + 4);
Rhys Weatherley committed
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
    memcpy(c + mlen, state.B, SPOOK_TAG_SIZE);
    return 0;
}

int spook_128_384_mu_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)
{
    shadow384_state_t state;
    (void)nsec;

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

    /* Initialize the Shadow-384 sponge state */
    spook_128_384_init(&state, k, SPOOK_MU_KEY_SIZE, npub);

    /* Process the associated data */
    if (adlen > 0)
        spook_128_384_absorb(&state, ad, adlen);

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= SPOOK_TAG_SIZE;
    if (clen > 0)
        spook_128_384_decrypt(&state, m, c, clen);

    /* Check the authentication tag */
    state.B[CLYDE128_BLOCK_SIZE * 2 - 1] |= 0x80;
549
    clyde128_decrypt(k, state.W + 4, c + clen, state.W + 4);
Rhys Weatherley committed
550 551 552
    return aead_check_tag
        (m, clen, state.B, state.B + CLYDE128_BLOCK_SIZE, SPOOK_TAG_SIZE);
}