hyena.c 8.85 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
/*
 * 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>

aead_cipher_t const hyena_cipher = {
    "HYENA",
    HYENA_KEY_SIZE,
    HYENA_NONCE_SIZE,
    HYENA_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    hyena_aead_encrypt,
    hyena_aead_decrypt
};

/**
 * \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);
}

/**
 * \brief Process the associated data for HYENA.
 *
 * \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_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];
    hyena_double_delta(D);
    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);
        hyena_double_delta(D);
        ad += 16;
        adlen -= 16;
    }
    if (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);
    } else {
        unsigned temp = (unsigned)adlen;
        hyena_double_delta(D);
        hyena_double_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_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 */
    if (!gift128n_init(&ks, k, HYENA_KEY_SIZE))
        return -1;
    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_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_double_delta(D);
            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;
        } else {
            unsigned temp = (unsigned)mlen;
            hyena_double_delta(D);
            hyena_double_delta(D);
            hyena_double_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_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 */
    if (!gift128n_init(&ks, k, HYENA_KEY_SIZE))
        return -1;
    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_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_double_delta(D);
            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;
        } else {
            unsigned temp = (unsigned)clen;
            hyena_double_delta(D);
            hyena_double_delta(D);
            hyena_double_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);
}