hyena.c 16.2 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
/*
 * 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 "hyena.h"
#include "internal-gift128.h"
#include "internal-util.h"
#include <string.h>

28 29
aead_cipher_t const hyena_v1_cipher = {
    "HYENA-v1",
Rhys Weatherley committed
30 31 32 33
    HYENA_KEY_SIZE,
    HYENA_NONCE_SIZE,
    HYENA_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
34 35 36 37 38 39 40 41 42 43 44 45
    hyena_v1_aead_encrypt,
    hyena_v1_aead_decrypt
};

aead_cipher_t const hyena_v2_cipher = {
    "HYENA-v2",
    HYENA_KEY_SIZE,
    HYENA_NONCE_SIZE,
    HYENA_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    hyena_v2_aead_encrypt,
    hyena_v2_aead_decrypt
Rhys Weatherley committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
};

/**
 * \brief Doubles a delta value in the F(2^64) field.
 *
 * \param D The delta value to be doubled.
 *
 * D = D << 1 if the top-most bit is 0, or D = (D << 1) ^ 0x1B otherwise.
 */
static void hyena_double_delta(unsigned char D[8])
{
    unsigned index;
    unsigned char mask = (unsigned char)(((signed char)(D[0])) >> 7);
    for (index = 0; index < 7; ++index)
        D[index] = (D[index] << 1) | (D[index + 1] >> 7);
    D[7] = (D[7] << 1) ^ (mask & 0x1B);
}

/**
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 * \brief Triples a delta value in the F(2^64) field.
 *
 * \param D The delta value to be tripled.
 *
 * D' = D ^ (D << 1) if the top-most bit is 0, or D' = D ^ (D << 1) ^ 0x1B
 * otherwise.
 */
static void hyena_triple_delta(unsigned char D[8])
{
    unsigned index;
    unsigned char mask = (unsigned char)(((signed char)(D[0])) >> 7);
    for (index = 0; index < 7; ++index)
        D[index] ^= (D[index] << 1) | (D[index + 1] >> 7);
    D[7] ^= (D[7] << 1) ^ (mask & 0x1B);
}

/**
 * \brief Process the associated data for HYENA-v1.
Rhys Weatherley committed
83 84 85 86 87 88 89
 *
 * \param ks Key schedule for the GIFT-128 cipher.
 * \param Y Internal hash state of HYENA.
 * \param D Internal hash state of HYENA.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes.
 */
90
static void hyena_v1_process_ad
Rhys Weatherley committed
91 92 93 94 95
    (const gift128n_key_schedule_t *ks, unsigned char Y[16],
     unsigned char D[8], const unsigned char *ad,
     unsigned long long adlen)
{
    unsigned char feedback[16];
Enrico Pozzobon committed
96
    hyena_double_delta(D);
Rhys Weatherley committed
97 98 99 100 101 102
    while (adlen > 16) {
        memcpy(feedback, ad, 16);
        lw_xor_block(feedback + 8, Y + 8, 8);
        lw_xor_block(feedback + 8, D, 8);
        lw_xor_block(Y, feedback, 16);
        gift128n_encrypt(ks, Y, Y);
Enrico Pozzobon committed
103
        hyena_double_delta(D);
Rhys Weatherley committed
104 105 106 107
        ad += 16;
        adlen -= 16;
    }
    if (adlen == 16) {
Enrico Pozzobon committed
108
        hyena_double_delta(D);
Rhys Weatherley committed
109 110 111 112 113 114
        memcpy(feedback, ad, 16);
        lw_xor_block(feedback + 8, Y + 8, 8);
        lw_xor_block(feedback + 8, D, 8);
        lw_xor_block(Y, feedback, 16);
    } else {
        unsigned temp = (unsigned)adlen;
Enrico Pozzobon committed
115 116
        hyena_double_delta(D);
        hyena_double_delta(D);
Rhys Weatherley committed
117 118 119 120 121 122 123 124 125 126
        memcpy(feedback, ad, temp);
        feedback[temp] = 0x01;
        memset(feedback + temp + 1, 0, 15 - temp);
        if (temp > 8)
            lw_xor_block(feedback + 8, Y + 8, temp - 8);
        lw_xor_block(feedback + 8, D, 8);
        lw_xor_block(Y, feedback, 16);
    }
}

127
int hyena_v1_aead_encrypt
Rhys Weatherley committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    (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)
{
    gift128n_key_schedule_t ks;
    unsigned char Y[16];
    unsigned char D[8];
    unsigned char feedback[16];
    unsigned index;
    (void)nsec;

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

    /* Set up the key schedule and use it to encrypt the nonce */
146
    gift128n_init(&ks, k);
Rhys Weatherley committed
147 148 149 150 151 152 153 154 155 156 157 158 159
    Y[0] = 0;
    if (adlen == 0)
        Y[0] |= 0x01;
    if (adlen == 0 && mlen == 0)
        Y[0] |= 0x02;
    Y[1] = 0;
    Y[2] = 0;
    Y[3] = 0;
    memcpy(Y + 4, npub, HYENA_NONCE_SIZE);
    gift128n_encrypt(&ks, Y, Y);
    memcpy(D, Y + 8, 8);

    /* Process the associated data */
160
    hyena_v1_process_ad(&ks, Y, D, ad, adlen);
Rhys Weatherley committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0) {
        while (mlen > 16) {
            gift128n_encrypt(&ks, Y, Y);
            hyena_double_delta(D);
            memcpy(feedback, m, 16);
            lw_xor_block(feedback + 8, Y + 8, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block_2_src(c, m, Y, 16);
            lw_xor_block(Y, feedback, 16);
            c += 16;
            m += 16;
            mlen -= 16;
        }
        gift128n_encrypt(&ks, Y, Y);
        if (mlen == 16) {
Enrico Pozzobon committed
178 179
            hyena_double_delta(D);
            hyena_double_delta(D);
Rhys Weatherley committed
180 181 182 183 184 185 186 187
            memcpy(feedback, m, 16);
            lw_xor_block(feedback + 8, Y + 8, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block_2_src(c, m, Y, 16);
            lw_xor_block(Y, feedback, 16);
            c += 16;
        } else {
            unsigned temp = (unsigned)mlen;
Enrico Pozzobon committed
188 189 190
            hyena_double_delta(D);
            hyena_double_delta(D);
            hyena_double_delta(D);
Rhys Weatherley committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            memcpy(feedback, m, temp);
            feedback[temp] = 0x01;
            memset(feedback + temp + 1, 0, 15 - temp);
            if (temp > 8)
                lw_xor_block(feedback + 8, Y + 8, temp - 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block_2_src(c, m, Y, temp);
            lw_xor_block(Y, feedback, 16);
            c += temp;
        }
    }

    /* Swap the two halves of Y and generate the authentication tag */
    for (index = 0; index < 8; ++index) {
        unsigned char temp1 = Y[index];
        unsigned char temp2 = Y[index + 8];
        Y[index] = temp2;
        Y[index + 8] = temp1;
    }
    gift128n_encrypt(&ks, c, Y);
    return 0;
}

214
int hyena_v1_aead_decrypt
Rhys Weatherley committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    (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)
{
    gift128n_key_schedule_t ks;
    unsigned char Y[16];
    unsigned char D[8];
    unsigned char feedback[16];
    unsigned char *mtemp;
    unsigned index;
    (void)nsec;

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

    /* Set up the key schedule and use it to encrypt the nonce */
236
    gift128n_init(&ks, k);
Rhys Weatherley committed
237 238 239 240 241 242 243 244 245 246 247 248 249
    Y[0] = 0;
    if (adlen == 0)
        Y[0] |= 0x01;
    if (adlen == 0 && clen == HYENA_TAG_SIZE)
        Y[0] |= 0x02;
    Y[1] = 0;
    Y[2] = 0;
    Y[3] = 0;
    memcpy(Y + 4, npub, HYENA_NONCE_SIZE);
    gift128n_encrypt(&ks, Y, Y);
    memcpy(D, Y + 8, 8);

    /* Process the associated data */
250
    hyena_v1_process_ad(&ks, Y, D, ad, adlen);
Rhys Weatherley committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= HYENA_TAG_SIZE;
    mtemp = m;
    if (clen > 0) {
        while (clen > 16) {
            gift128n_encrypt(&ks, Y, Y);
            hyena_double_delta(D);
            memcpy(feedback + 8, c + 8, 8);
            lw_xor_block_2_src(m, c, Y, 16);
            memcpy(feedback, m, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block(Y, feedback, 16);
            c += 16;
            m += 16;
            clen -= 16;
        }
        gift128n_encrypt(&ks, Y, Y);
        if (clen == 16) {
Enrico Pozzobon committed
270 271
            hyena_double_delta(D);
            hyena_double_delta(D);
Rhys Weatherley committed
272 273 274 275 276 277 278 279
            memcpy(feedback + 8, c + 8, 8);
            lw_xor_block_2_src(m, c, Y, 16);
            memcpy(feedback, m, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block(Y, feedback, 16);
            c += 16;
        } else {
            unsigned temp = (unsigned)clen;
Enrico Pozzobon committed
280 281 282
            hyena_double_delta(D);
            hyena_double_delta(D);
            hyena_double_delta(D);
Rhys Weatherley committed
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
            if (temp > 8) {
                memcpy(feedback + 8, c + 8, temp - 8);
                lw_xor_block_2_src(m, c, Y, temp);
                memcpy(feedback, m, 8);
            } else {
                lw_xor_block_2_src(m, c, Y, temp);
                memcpy(feedback, m, temp);
            }
            feedback[temp] = 0x01;
            memset(feedback + temp + 1, 0, 15 - temp);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block(Y, feedback, 16);
            c += temp;
        }
    }

    /* Swap the two halves of Y and check the authentication tag */
    for (index = 0; index < 8; ++index) {
        unsigned char temp1 = Y[index];
        unsigned char temp2 = Y[index + 8];
        Y[index] = temp2;
        Y[index + 8] = temp1;
    }
    gift128n_encrypt(&ks, Y, Y);
    return aead_check_tag(mtemp, *mlen, Y, c, HYENA_TAG_SIZE);
}
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

/**
 * \brief Process the associated data for HYENA-v2.
 *
 * \param ks Key schedule for the GIFT-128 cipher.
 * \param Y Internal hash state of HYENA.
 * \param D Internal hash state of HYENA.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes.
 */
static void hyena_v2_process_ad
    (const gift128n_key_schedule_t *ks, unsigned char Y[16],
     unsigned char D[8], const unsigned char *ad,
     unsigned long long adlen)
{
    unsigned char feedback[16];
    while (adlen > 16) {
        hyena_double_delta(D);
        memcpy(feedback, ad, 16);
        lw_xor_block(feedback + 8, Y + 8, 8);
        lw_xor_block(feedback + 8, D, 8);
        lw_xor_block(Y, feedback, 16);
        gift128n_encrypt(ks, Y, Y);
        ad += 16;
        adlen -= 16;
    }
    if (adlen == 16) {
        hyena_triple_delta(D);
        memcpy(feedback, ad, 16);
        lw_xor_block(feedback + 8, Y + 8, 8);
        lw_xor_block(feedback + 8, D, 8);
        lw_xor_block(Y, feedback, 16);
    } else {
        unsigned temp = (unsigned)adlen;
        hyena_triple_delta(D);
        hyena_triple_delta(D);
        memcpy(feedback, ad, temp);
        feedback[temp] = 0x01;
        memset(feedback + temp + 1, 0, 15 - temp);
        if (temp > 8)
            lw_xor_block(feedback + 8, Y + 8, temp - 8);
        lw_xor_block(feedback + 8, D, 8);
        lw_xor_block(Y, feedback, 16);
    }
}

int hyena_v2_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)
{
    gift128n_key_schedule_t ks;
    unsigned char Y[16];
    unsigned char D[8];
    unsigned char feedback[16];
    unsigned index;
    (void)nsec;

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

    /* Set up the key schedule and use it to encrypt the nonce */
    gift128n_init(&ks, k);
    Y[0] = 0;
    if (adlen == 0)
        Y[0] |= 0x01;
    if (adlen == 0 && mlen == 0)
        Y[0] |= 0x02;
    Y[1] = 0;
    Y[2] = 0;
    Y[3] = 0;
    memcpy(Y + 4, npub, HYENA_NONCE_SIZE);
    gift128n_encrypt(&ks, Y, Y);
    memcpy(D, Y + 8, 8);

    /* Process the associated data */
    hyena_v2_process_ad(&ks, Y, D, ad, adlen);

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0) {
        while (mlen > 16) {
            gift128n_encrypt(&ks, Y, Y);
            hyena_double_delta(D);
            memcpy(feedback, m, 16);
            lw_xor_block(feedback + 8, Y + 8, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block_2_src(c, m, Y, 16);
            lw_xor_block(Y, feedback, 16);
            c += 16;
            m += 16;
            mlen -= 16;
        }
        gift128n_encrypt(&ks, Y, Y);
        if (mlen == 16) {
            hyena_triple_delta(D);
            memcpy(feedback, m, 16);
            lw_xor_block(feedback + 8, Y + 8, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block_2_src(c, m, Y, 16);
            lw_xor_block(Y, feedback, 16);
            c += 16;
        } else {
            unsigned temp = (unsigned)mlen;
            hyena_triple_delta(D);
            hyena_triple_delta(D);
            memcpy(feedback, m, temp);
            feedback[temp] = 0x01;
            memset(feedback + temp + 1, 0, 15 - temp);
            if (temp > 8)
                lw_xor_block(feedback + 8, Y + 8, temp - 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block_2_src(c, m, Y, temp);
            lw_xor_block(Y, feedback, 16);
            c += temp;
        }
    }

    /* Swap the two halves of Y and generate the authentication tag */
    for (index = 0; index < 8; ++index) {
        unsigned char temp1 = Y[index];
        unsigned char temp2 = Y[index + 8];
        Y[index] = temp2;
        Y[index + 8] = temp1;
    }
    gift128n_encrypt(&ks, c, Y);
    return 0;
}

int hyena_v2_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)
{
    gift128n_key_schedule_t ks;
    unsigned char Y[16];
    unsigned char D[8];
    unsigned char feedback[16];
    unsigned char *mtemp;
    unsigned index;
    (void)nsec;

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

    /* Set up the key schedule and use it to encrypt the nonce */
    gift128n_init(&ks, k);
    Y[0] = 0;
    if (adlen == 0)
        Y[0] |= 0x01;
    if (adlen == 0 && clen == HYENA_TAG_SIZE)
        Y[0] |= 0x02;
    Y[1] = 0;
    Y[2] = 0;
    Y[3] = 0;
    memcpy(Y + 4, npub, HYENA_NONCE_SIZE);
    gift128n_encrypt(&ks, Y, Y);
    memcpy(D, Y + 8, 8);

    /* Process the associated data */
    hyena_v2_process_ad(&ks, Y, D, ad, adlen);

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= HYENA_TAG_SIZE;
    mtemp = m;
    if (clen > 0) {
        while (clen > 16) {
            gift128n_encrypt(&ks, Y, Y);
            hyena_double_delta(D);
            memcpy(feedback + 8, c + 8, 8);
            lw_xor_block_2_src(m, c, Y, 16);
            memcpy(feedback, m, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block(Y, feedback, 16);
            c += 16;
            m += 16;
            clen -= 16;
        }
        gift128n_encrypt(&ks, Y, Y);
        if (clen == 16) {
            hyena_triple_delta(D);
            memcpy(feedback + 8, c + 8, 8);
            lw_xor_block_2_src(m, c, Y, 16);
            memcpy(feedback, m, 8);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block(Y, feedback, 16);
            c += 16;
        } else {
            unsigned temp = (unsigned)clen;
            hyena_triple_delta(D);
            hyena_triple_delta(D);
            if (temp > 8) {
                memcpy(feedback + 8, c + 8, temp - 8);
                lw_xor_block_2_src(m, c, Y, temp);
                memcpy(feedback, m, 8);
            } else {
                lw_xor_block_2_src(m, c, Y, temp);
                memcpy(feedback, m, temp);
            }
            feedback[temp] = 0x01;
            memset(feedback + temp + 1, 0, 15 - temp);
            lw_xor_block(feedback + 8, D, 8);
            lw_xor_block(Y, feedback, 16);
            c += temp;
        }
    }

    /* Swap the two halves of Y and check the authentication tag */
    for (index = 0; index < 8; ++index) {
        unsigned char temp1 = Y[index];
        unsigned char temp2 = Y[index + 8];
        Y[index] = temp2;
        Y[index + 8] = temp1;
    }
    gift128n_encrypt(&ks, Y, Y);
    return aead_check_tag(mtemp, *mlen, Y, c, HYENA_TAG_SIZE);
}