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

aead_cipher_t const gift_cofb_cipher = {
    "GIFT-COFB",
    GIFT_COFB_KEY_SIZE,
    GIFT_COFB_NONCE_SIZE,
    GIFT_COFB_TAG_SIZE,
    AEAD_FLAG_NONE,
    gift_cofb_aead_encrypt,
    gift_cofb_aead_decrypt
};

/**
 * \brief Structure of an L value.
 *
 * The value is assumed to have already been converted from big-endian
 * to host byte order.
 */
typedef struct
{
    uint32_t x;     /**< High word of the value */
    uint32_t y;     /**< Low word of the value */

} gift_cofb_l_t;

/**
 * \brief Structure of a 128-bit block in host byte order.
 *
 * The block is assumed to have already been converted from big-endian
 * to host byte order.
 */
typedef union
{
    uint32_t x[4];  /**< Words of the block */
    uint8_t y[16];  /**< Bytes of the block */

} gift_cofb_block_t;

/**
 * \brief Doubles an L value in the F(2^64) field.
 *
 * \param L The value to be doubled.
 *
 * L = L << 1 if the top-most bit is 0, or L = (L << 1) ^ 0x1B otherwise.
 */
#define gift_cofb_double_L(L) \
    do { \
        uint32_t mask = ((int32_t)((L)->x)) >> 31; \
        (L)->x = ((L)->x << 1) | ((L)->y >> 31); \
        (L)->y = ((L)->y << 1) ^ (mask & 0x1B); \
    } while (0)

/**
 * \brief Triples an L value in the F(2^64) field.
 *
 * \param L The value to be tripled.
 *
 * L = double(L) ^ L
 */
#define gift_cofb_triple_L(L) \
    do { \
        uint32_t mask = ((int32_t)((L)->x)) >> 31; \
        uint32_t tx = ((L)->x << 1) | ((L)->y >> 31); \
        uint32_t ty = ((L)->y << 1) ^ (mask & 0x1B); \
        (L)->x ^= tx; \
        (L)->y ^= ty; \
    } while (0)

/**
 * \brief Applies the GIFT-COFB feedback function to Y.
 *
 * \param Y The value to be modified with the feedback function.
 *
 * Y is divided into L and R halves and then (R, L <<< 1) is returned.
 */
#define gift_cofb_feedback(Y) \
    do { \
        uint32_t lx = (Y)->x[0]; \
        uint32_t ly = (Y)->x[1]; \
        (Y)->x[0] = (Y)->x[2]; \
        (Y)->x[1] = (Y)->x[3]; \
        (Y)->x[2] = (lx << 1) | (ly >> 31); \
        (Y)->x[3] = (ly << 1) | (lx >> 31); \
    } while (0)

/**
 * \brief Process the associated data for GIFT-COFB encryption or decryption.
 *
 * \param ks The GIFT-128 key schedule to use.
 * \param Y GIFT-COFB internal state.
 * \param L GIFT-COFB internal state.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes.
 * \param mlen Length of the plaintext in bytes.
 */
static void gift_cofb_assoc_data
    (gift128b_key_schedule_t *ks, gift_cofb_block_t *Y, gift_cofb_l_t *L,
     const unsigned char *ad, unsigned long long adlen, unsigned long long mlen)
{
    /* Deal with all associated data blocks except the last */
    while (adlen > 16) {
        gift_cofb_double_L(L);
        gift_cofb_feedback(Y);
        Y->x[0] ^= L->x ^ be_load_word32(ad);
        Y->x[1] ^= L->y ^ be_load_word32(ad + 4);
        Y->x[2] ^= be_load_word32(ad + 8);
        Y->x[3] ^= be_load_word32(ad + 12);
        gift128b_encrypt_preloaded(ks, Y->x, Y->x);
        ad += 16;
        adlen -= 16;
    }

    /* Pad and deal with the last block */
    gift_cofb_feedback(Y);
    if (adlen == 16) {
        Y->x[0] ^= be_load_word32(ad);
        Y->x[1] ^= be_load_word32(ad + 4);
        Y->x[2] ^= be_load_word32(ad + 8);
        Y->x[3] ^= be_load_word32(ad + 12);
        gift_cofb_triple_L(L);
    } else {
        unsigned temp = (unsigned)adlen;
        unsigned char padded[16];
        memcpy(padded, ad, temp);
        padded[temp] = 0x80;
        memset(padded + temp + 1, 0, 16 - temp - 1);
        Y->x[0] ^= be_load_word32(padded);
        Y->x[1] ^= be_load_word32(padded + 4);
        Y->x[2] ^= be_load_word32(padded + 8);
        Y->x[3] ^= be_load_word32(padded + 12);
        gift_cofb_triple_L(L);
        gift_cofb_triple_L(L);
    }
    if (mlen == 0) {
        gift_cofb_triple_L(L);
        gift_cofb_triple_L(L);
    }
    Y->x[0] ^= L->x;
    Y->x[1] ^= L->y;
    gift128b_encrypt_preloaded(ks, Y->x, Y->x);
}

/** @cond cofb_byte_swap */

/* Byte-swap a block if the platform is little-endian */
#if defined(LW_UTIL_LITTLE_ENDIAN)
#define gift_cofb_byte_swap_word(y) \
    (__extension__ ({ \
        uint32_t _y = (y); \
        (_y >> 24) | (_y << 24) | ((_y << 8) & 0x00FF0000U) | \
        ((_y >> 8) & 0x0000FF00U); \
    }))
#define gift_cofb_byte_swap(x) \
    do { \
        (x)[0] = gift_cofb_byte_swap_word((x)[0]); \
        (x)[1] = gift_cofb_byte_swap_word((x)[1]); \
        (x)[2] = gift_cofb_byte_swap_word((x)[2]); \
        (x)[3] = gift_cofb_byte_swap_word((x)[3]); \
    } while (0)
#else
#define gift_cofb_byte_swap(x) do { ; } while (0)
#endif

/** @endcond */

int gift_cofb_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)
{
    gift128b_key_schedule_t ks;
    gift_cofb_block_t Y;
    gift_cofb_l_t L;
    gift_cofb_block_t P;
    (void)nsec;

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

    /* Set up the key schedule and use it to encrypt the nonce */
    gift128b_init(&ks, k);
    Y.x[0] = be_load_word32(npub);
    Y.x[1] = be_load_word32(npub + 4);
    Y.x[2] = be_load_word32(npub + 8);
    Y.x[3] = be_load_word32(npub + 12);
    gift128b_encrypt_preloaded(&ks, Y.x, Y.x);
    L.x = Y.x[0];
    L.y = Y.x[1];

    /* Authenticate the associated data */
    gift_cofb_assoc_data(&ks, &Y, &L, ad, adlen, mlen);

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0) {
        /* Deal with all plaintext blocks except the last */
        while (mlen > 16) {
            P.x[0] = be_load_word32(m);
            P.x[1] = be_load_word32(m + 4);
            P.x[2] = be_load_word32(m + 8);
            P.x[3] = be_load_word32(m + 12);
            be_store_word32(c,      Y.x[0] ^ P.x[0]);
            be_store_word32(c + 4,  Y.x[1] ^ P.x[1]);
            be_store_word32(c + 8,  Y.x[2] ^ P.x[2]);
            be_store_word32(c + 12, Y.x[3] ^ P.x[3]);
            gift_cofb_double_L(&L);
            gift_cofb_feedback(&Y);
            Y.x[0] ^= L.x ^ P.x[0];
            Y.x[1] ^= L.y ^ P.x[1];
            Y.x[2] ^= P.x[2];
            Y.x[3] ^= P.x[3];
            gift128b_encrypt_preloaded(&ks, Y.x, Y.x);
            c += 16;
            m += 16;
            mlen -= 16;
        }

        /* Pad and deal with the last plaintext block */
        if (mlen == 16) {
            P.x[0] = be_load_word32(m);
            P.x[1] = be_load_word32(m + 4);
            P.x[2] = be_load_word32(m + 8);
            P.x[3] = be_load_word32(m + 12);
            be_store_word32(c,      Y.x[0] ^ P.x[0]);
            be_store_word32(c + 4,  Y.x[1] ^ P.x[1]);
            be_store_word32(c + 8,  Y.x[2] ^ P.x[2]);
            be_store_word32(c + 12, Y.x[3] ^ P.x[3]);
            gift_cofb_feedback(&Y);
            Y.x[0] ^= P.x[0];
            Y.x[1] ^= P.x[1];
            Y.x[2] ^= P.x[2];
            Y.x[3] ^= P.x[3];
            gift_cofb_triple_L(&L);
            c += 16;
        } else {
            unsigned temp = (unsigned)mlen;
            gift_cofb_block_t padded;
            memcpy(padded.y, m, temp);
            padded.y[temp] = 0x80;
            memset(padded.y + temp + 1, 0, 16 - temp - 1);
            P.x[0] = be_load_word32(padded.y);
            P.x[1] = be_load_word32(padded.y + 4);
            P.x[2] = be_load_word32(padded.y + 8);
            P.x[3] = be_load_word32(padded.y + 12);
            gift_cofb_byte_swap(padded.x);
            padded.x[0] ^= Y.x[0];
            padded.x[1] ^= Y.x[1];
            padded.x[2] ^= Y.x[2];
            padded.x[3] ^= Y.x[3];
            gift_cofb_byte_swap(padded.x);
            memcpy(c, padded.y, temp);
            gift_cofb_feedback(&Y);
            Y.x[0] ^= P.x[0];
            Y.x[1] ^= P.x[1];
            Y.x[2] ^= P.x[2];
            Y.x[3] ^= P.x[3];
            gift_cofb_triple_L(&L);
            gift_cofb_triple_L(&L);
            c += temp;
        }
        Y.x[0] ^= L.x;
        Y.x[1] ^= L.y;
        gift128b_encrypt_preloaded(&ks, Y.x, Y.x);
    }

    /* Generate the final authentication tag */
    be_store_word32(c,      Y.x[0]);
    be_store_word32(c + 4,  Y.x[1]);
    be_store_word32(c + 8,  Y.x[2]);
    be_store_word32(c + 12, Y.x[3]);
    return 0;
}

int gift_cofb_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)
{
    gift128b_key_schedule_t ks;
    gift_cofb_block_t Y;
    gift_cofb_l_t L;
    gift_cofb_block_t P;
    unsigned char *mtemp;
    (void)nsec;

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

    /* Set up the key schedule and use it to encrypt the nonce */
    gift128b_init(&ks, k);
    Y.x[0] = be_load_word32(npub);
    Y.x[1] = be_load_word32(npub + 4);
    Y.x[2] = be_load_word32(npub + 8);
    Y.x[3] = be_load_word32(npub + 12);
    gift128b_encrypt_preloaded(&ks, Y.x, Y.x);
    L.x = Y.x[0];
    L.y = Y.x[1];

    /* Authenticate the associated data */
    gift_cofb_assoc_data(&ks, &Y, &L, ad, adlen, *mlen);

    /* Decrypt the ciphertext to produce the plaintext */
    mtemp = m;
    clen -= GIFT_COFB_TAG_SIZE;
    if (clen > 0) {
        /* Deal with all ciphertext blocks except the last */
        while (clen > 16) {
            P.x[0] = Y.x[0] ^ be_load_word32(c);
            P.x[1] = Y.x[1] ^ be_load_word32(c + 4);
            P.x[2] = Y.x[2] ^ be_load_word32(c + 8);
            P.x[3] = Y.x[3] ^ be_load_word32(c + 12);
            be_store_word32(m,      P.x[0]);
            be_store_word32(m + 4,  P.x[1]);
            be_store_word32(m + 8,  P.x[2]);
            be_store_word32(m + 12, P.x[3]);
            gift_cofb_double_L(&L);
            gift_cofb_feedback(&Y);
            Y.x[0] ^= L.x ^ P.x[0];
            Y.x[1] ^= L.y ^ P.x[1];
            Y.x[2] ^= P.x[2];
            Y.x[3] ^= P.x[3];
            gift128b_encrypt_preloaded(&ks, Y.x, Y.x);
            c += 16;
            m += 16;
            clen -= 16;
        }

        /* Pad and deal with the last ciphertext block */
        if (clen == 16) {
            P.x[0] = Y.x[0] ^ be_load_word32(c);
            P.x[1] = Y.x[1] ^ be_load_word32(c + 4);
            P.x[2] = Y.x[2] ^ be_load_word32(c + 8);
            P.x[3] = Y.x[3] ^ be_load_word32(c + 12);
            be_store_word32(m,      P.x[0]);
            be_store_word32(m + 4,  P.x[1]);
            be_store_word32(m + 8,  P.x[2]);
            be_store_word32(m + 12, P.x[3]);
            gift_cofb_feedback(&Y);
            Y.x[0] ^= P.x[0];
            Y.x[1] ^= P.x[1];
            Y.x[2] ^= P.x[2];
            Y.x[3] ^= P.x[3];
            gift_cofb_triple_L(&L);
            c += 16;
        } else {
            unsigned temp = (unsigned)clen;
            P.x[0] = Y.x[0];
            P.x[1] = Y.x[1];
            P.x[2] = Y.x[2];
            P.x[3] = Y.x[3];
            gift_cofb_byte_swap(P.x);
            lw_xor_block_2_dest(m, P.y, c, temp);
            P.y[temp] = 0x80;
            memset(P.y + temp + 1, 0, 16 - temp - 1);
            gift_cofb_byte_swap(P.x);
            gift_cofb_feedback(&Y);
            Y.x[0] ^= P.x[0];
            Y.x[1] ^= P.x[1];
            Y.x[2] ^= P.x[2];
            Y.x[3] ^= P.x[3];
            gift_cofb_triple_L(&L);
            gift_cofb_triple_L(&L);
            c += temp;
        }
        Y.x[0] ^= L.x;
        Y.x[1] ^= L.y;
        gift128b_encrypt_preloaded(&ks, Y.x, Y.x);
    }

    /* Check the authentication tag at the end of the packet */
    gift_cofb_byte_swap(Y.x);
    return aead_check_tag(mtemp, *mlen, Y.y, c, GIFT_COFB_TAG_SIZE);
}