lotus-locus.c 15.5 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
/*
 * 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 "lotus-locus.h"
#include "internal-gift64.h"
#include "internal-util.h"
#include <string.h>

aead_cipher_t const lotus_aead_cipher = {
    "LOTUS-AEAD",
    LOTUS_AEAD_KEY_SIZE,
    LOTUS_AEAD_NONCE_SIZE,
    LOTUS_AEAD_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    lotus_aead_encrypt,
    lotus_aead_decrypt
};

aead_cipher_t const locus_aead_cipher = {
    "LOCUS-AEAD",
    LOCUS_AEAD_KEY_SIZE,
    LOCUS_AEAD_NONCE_SIZE,
    LOCUS_AEAD_TAG_SIZE,
    AEAD_FLAG_LITTLE_ENDIAN,
    locus_aead_encrypt,
    locus_aead_decrypt
};

/**
 * \brief Multiplies a key by 2 in the GF(128) field.
 *
 * \param ks The key schedule structure containing the key in host byte order.
 */
STATIC_INLINE void lotus_or_locus_mul_2(gift64n_key_schedule_t *ks)
{
    uint32_t mask = (uint32_t)(((int32_t)(ks->k[0])) >> 31);
    ks->k[0] = (ks->k[0] << 1) | (ks->k[1] >> 31);
    ks->k[1] = (ks->k[1] << 1) | (ks->k[2] >> 31);
    ks->k[2] = (ks->k[2] << 1) | (ks->k[3] >> 31);
    ks->k[3] = (ks->k[3] << 1) ^ (mask & 0x87);
    gift64n_update_round_keys(ks);
}

/**
 * \brief Initializes a LOTUS-AEAD or LOCUS-AEAD cipher instance.
 *
 * \param ks Key schedule to initialize.
 * \param deltaN Delta-N value for the cipher state.
 * \param key Points to the 16-byte key for the cipher instance.
 * \param nonce Points to the 16-byte key for the cipher instance.
 * \param T Points to a temporary buffer of LOTUS_AEAD_KEY_SIZE bytes
 * that will be destroyed during this function.
 */
static void lotus_or_locus_init
    (gift64n_key_schedule_t *ks,
     unsigned char deltaN[GIFT64_BLOCK_SIZE],
     const unsigned char *key,
     const unsigned char *nonce,
     unsigned char *T)
{
    gift64n_init(ks, key);
    memset(deltaN, 0, GIFT64_BLOCK_SIZE);
    gift64t_encrypt(ks, deltaN, deltaN, GIFT64T_TWEAK_0);
    lw_xor_block_2_src(T, key, nonce, LOTUS_AEAD_KEY_SIZE);
    gift64n_init(ks, T);
    gift64t_encrypt(ks, deltaN, deltaN, GIFT64T_TWEAK_1);
}

/**
 * \brief Processes associated data for LOTUS-AEAD or LOCUS-AEAD.
 *
 * \param ks Points to the key schedule.
 * \param deltaN Points to the Delta-N value from the state.
 * \param V Points to the V value from the state.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes, must be non-zero.
 */
static void lotus_or_locus_process_ad
    (gift64n_key_schedule_t *ks,
     const unsigned char deltaN[GIFT64_BLOCK_SIZE],
     unsigned char V[GIFT64_BLOCK_SIZE],
     const unsigned char *ad, unsigned long long adlen)
{
    unsigned char X[GIFT64_BLOCK_SIZE];
    unsigned char temp;
    while (adlen > GIFT64_BLOCK_SIZE) {
        lotus_or_locus_mul_2(ks);
        lw_xor_block_2_src(X, ad, deltaN, GIFT64_BLOCK_SIZE);
        gift64t_encrypt(ks, X, X, GIFT64T_TWEAK_2);
        lw_xor_block(V, X, GIFT64_BLOCK_SIZE);
        ad += GIFT64_BLOCK_SIZE;
        adlen -= GIFT64_BLOCK_SIZE;
    }
    lotus_or_locus_mul_2(ks);
    temp = (unsigned)adlen;
    if (temp < GIFT64_BLOCK_SIZE) {
        memcpy(X, deltaN, GIFT64_BLOCK_SIZE);
        lw_xor_block(X, ad, temp);
        X[temp] ^= 0x01;
        gift64t_encrypt(ks, X, X, GIFT64T_TWEAK_3);
    } else {
        lw_xor_block_2_src(X, ad, deltaN, GIFT64_BLOCK_SIZE);
        gift64t_encrypt(ks, X, X, GIFT64T_TWEAK_2);
    }
    lw_xor_block(V, X, GIFT64_BLOCK_SIZE);
}

/**
 * \brief Generates the authentication tag for LOTUS-AEAD or LOCUS-AEAD.
 *
 * \param ks Points to the key schedule.
 * \param tag Points to the buffer to receive the authentication tag.
 * \param deltaN Points to the Delta-N value from the state.
 * \param W Points to the W value from the state.
 * \param V Points to the V value from the state.
 */
static void lotus_or_locus_gen_tag
    (gift64n_key_schedule_t *ks, unsigned char *tag,
     unsigned char deltaN[GIFT64_BLOCK_SIZE],
     unsigned char W[GIFT64_BLOCK_SIZE],
     unsigned char V[GIFT64_BLOCK_SIZE])
{
    lotus_or_locus_mul_2(ks);
    lw_xor_block(W, deltaN, GIFT64_BLOCK_SIZE);
    lw_xor_block(W, V, GIFT64_BLOCK_SIZE);
    gift64t_encrypt(ks, W, W, GIFT64T_TWEAK_6);
    lw_xor_block_2_src(tag, W, deltaN, GIFT64_BLOCK_SIZE);
}

int lotus_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)
{
    gift64n_key_schedule_t ks;
    unsigned char WV[GIFT64_BLOCK_SIZE * 2];
    unsigned char deltaN[GIFT64_BLOCK_SIZE];
    unsigned char X1[GIFT64_BLOCK_SIZE];
    unsigned char X2[GIFT64_BLOCK_SIZE];
    unsigned temp;
    (void)nsec;

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

    /* Initialize the state with the key and the nonce */
    lotus_or_locus_init(&ks, deltaN, k, npub, WV);
    memset(WV, 0, sizeof(WV));

    /* Process the associated data */
    if (adlen > 0) {
        lotus_or_locus_process_ad
            (&ks, deltaN, WV + GIFT64_BLOCK_SIZE, ad, adlen);
    }

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0) {
        while (mlen > (GIFT64_BLOCK_SIZE * 2)) {
            lotus_or_locus_mul_2(&ks);
            lw_xor_block_2_src(X1, m, deltaN, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_4);
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_4);
            lw_xor_block_2_src
                (X2, m + GIFT64_BLOCK_SIZE, X2, GIFT64_BLOCK_SIZE);
            lw_xor_block_2_src(c, X2, deltaN, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_5);
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_5);
            lw_xor_block_2_src
                (c + GIFT64_BLOCK_SIZE, X1, X2, GIFT64_BLOCK_SIZE);
            c += GIFT64_BLOCK_SIZE * 2;
            m += GIFT64_BLOCK_SIZE * 2;
            mlen -= GIFT64_BLOCK_SIZE * 2;
        }
        temp = (unsigned)mlen;
        lotus_or_locus_mul_2(&ks);
        memcpy(X1, deltaN, GIFT64_BLOCK_SIZE);
        X1[0] ^= (unsigned char)temp;
        gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_12);
        lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
        gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_12);
        if (temp <= GIFT64_BLOCK_SIZE) {
            lw_xor_block(WV, m, temp);
            lw_xor_block(X2, m, temp);
            lw_xor_block_2_src(c, X2, deltaN, temp);
        } else {
            lw_xor_block(X2, m, GIFT64_BLOCK_SIZE);
            lw_xor_block_2_src(c, X2, deltaN, GIFT64_BLOCK_SIZE);
            c += GIFT64_BLOCK_SIZE;
            m += GIFT64_BLOCK_SIZE;
            temp -= GIFT64_BLOCK_SIZE;
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
            lw_xor_block(WV, m, temp);
            lw_xor_block(X1, X2, temp);
            lw_xor_block_2_src(c, X1, m, temp);
        }
        c += temp;
    }

    /* Generate the authentication tag */
    lotus_or_locus_gen_tag(&ks, c, deltaN, WV, WV + GIFT64_BLOCK_SIZE);
    return 0;
}

int lotus_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)
{
    gift64n_key_schedule_t ks;
    unsigned char WV[GIFT64_BLOCK_SIZE * 2];
    unsigned char deltaN[GIFT64_BLOCK_SIZE];
    unsigned char X1[GIFT64_BLOCK_SIZE];
    unsigned char X2[GIFT64_BLOCK_SIZE];
    unsigned char *mtemp = m;
    unsigned temp;
    (void)nsec;

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

    /* Initialize the state with the key and the nonce */
    lotus_or_locus_init(&ks, deltaN, k, npub, WV);
    memset(WV, 0, sizeof(WV));

    /* Process the associated data */
    if (adlen > 0) {
        lotus_or_locus_process_ad
            (&ks, deltaN, WV + GIFT64_BLOCK_SIZE, ad, adlen);
    }

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= LOTUS_AEAD_TAG_SIZE;
    if (clen > 0) {
        while (clen > (GIFT64_BLOCK_SIZE * 2)) {
            lotus_or_locus_mul_2(&ks);
            lw_xor_block_2_src(X1, c, deltaN, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_5);
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_5);
            lw_xor_block(X2, c + GIFT64_BLOCK_SIZE, GIFT64_BLOCK_SIZE);
            lw_xor_block_2_src(m, X2, deltaN, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_4);
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_4);
            lw_xor_block_2_src
                (m + GIFT64_BLOCK_SIZE, X1, X2, GIFT64_BLOCK_SIZE);
            c += GIFT64_BLOCK_SIZE * 2;
            m += GIFT64_BLOCK_SIZE * 2;
            clen -= GIFT64_BLOCK_SIZE * 2;
        }
        temp = (unsigned)clen;
        lotus_or_locus_mul_2(&ks);
        memcpy(X1, deltaN, GIFT64_BLOCK_SIZE);
        X1[0] ^= (unsigned char)temp;
        gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_12);
        lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
        gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_12);
        if (temp <= GIFT64_BLOCK_SIZE) {
            lw_xor_block_2_src(m, X2, c, temp);
            lw_xor_block(m, deltaN, temp);
            lw_xor_block(X2, m, temp);
            lw_xor_block(WV, m, temp);
        } else {
            lw_xor_block_2_src(m, X2, c, GIFT64_BLOCK_SIZE);
            lw_xor_block(m, deltaN, GIFT64_BLOCK_SIZE);
            lw_xor_block(X2, m, GIFT64_BLOCK_SIZE);
            c += GIFT64_BLOCK_SIZE;
            m += GIFT64_BLOCK_SIZE;
            temp -= GIFT64_BLOCK_SIZE;
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
            lw_xor_block(X1, X2, temp);
            lw_xor_block_2_src(m, X1, c, temp);
            lw_xor_block(WV, m, temp);
        }
        c += temp;
    }

    /* Check the authentication tag */
    lotus_or_locus_gen_tag(&ks, WV, deltaN, WV, WV + GIFT64_BLOCK_SIZE);
    return aead_check_tag(mtemp, *mlen, WV, c, LOTUS_AEAD_TAG_SIZE);
}

int locus_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)
{
    gift64n_key_schedule_t ks;
    unsigned char WV[GIFT64_BLOCK_SIZE * 2];
    unsigned char deltaN[GIFT64_BLOCK_SIZE];
    unsigned char X[GIFT64_BLOCK_SIZE];
    unsigned temp;
    (void)nsec;

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

    /* Initialize the state with the key and the nonce */
    lotus_or_locus_init(&ks, deltaN, k, npub, WV);
    memset(WV, 0, sizeof(WV));

    /* Process the associated data */
    if (adlen > 0) {
        lotus_or_locus_process_ad
            (&ks, deltaN, WV + GIFT64_BLOCK_SIZE, ad, adlen);
    }

    /* Encrypt the plaintext to produce the ciphertext */
    if (mlen > 0) {
        while (mlen > GIFT64_BLOCK_SIZE) {
            lotus_or_locus_mul_2(&ks);
            lw_xor_block_2_src(X, m, deltaN, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_4);
            lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
            gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_4);
            lw_xor_block_2_src(c, X, deltaN, GIFT64_BLOCK_SIZE);
            c += GIFT64_BLOCK_SIZE;
            m += GIFT64_BLOCK_SIZE;
            mlen -= GIFT64_BLOCK_SIZE;
        }
        temp = (unsigned)mlen;
        lotus_or_locus_mul_2(&ks);
        memcpy(X, deltaN, GIFT64_BLOCK_SIZE);
        X[0] ^= (unsigned char)temp;
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
        lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
        lw_xor_block(WV, m, temp);
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
        lw_xor_block(X, deltaN, temp);
        lw_xor_block_2_src(c, m, X, temp);
        c += temp;
    }

    /* Generate the authentication tag */
    lotus_or_locus_gen_tag(&ks, c, deltaN, WV, WV + GIFT64_BLOCK_SIZE);
    return 0;
}

int locus_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)
{
    gift64n_key_schedule_t ks;
    unsigned char WV[GIFT64_BLOCK_SIZE * 2];
    unsigned char deltaN[GIFT64_BLOCK_SIZE];
    unsigned char X[GIFT64_BLOCK_SIZE];
    unsigned char *mtemp = m;
    unsigned temp;
    (void)nsec;

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

    /* Initialize the state with the key and the nonce */
    lotus_or_locus_init(&ks, deltaN, k, npub, WV);
    memset(WV, 0, sizeof(WV));

    /* Process the associated data */
    if (adlen > 0) {
        lotus_or_locus_process_ad
            (&ks, deltaN, WV + GIFT64_BLOCK_SIZE, ad, adlen);
    }

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= LOCUS_AEAD_TAG_SIZE;
    if (clen > 0) {
        while (clen > GIFT64_BLOCK_SIZE) {
            lotus_or_locus_mul_2(&ks);
            lw_xor_block_2_src(X, c, deltaN, GIFT64_BLOCK_SIZE);
            gift64t_decrypt(&ks, X, X, GIFT64T_TWEAK_4);
            lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
            gift64t_decrypt(&ks, X, X, GIFT64T_TWEAK_4);
            lw_xor_block_2_src(m, X, deltaN, GIFT64_BLOCK_SIZE);
            c += GIFT64_BLOCK_SIZE;
            m += GIFT64_BLOCK_SIZE;
            clen -= GIFT64_BLOCK_SIZE;
        }
        temp = (unsigned)clen;
        lotus_or_locus_mul_2(&ks);
        memcpy(X, deltaN, GIFT64_BLOCK_SIZE);
        X[0] ^= (unsigned char)temp;
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
        lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
        lw_xor_block(X, deltaN, temp);
        lw_xor_block_2_src(m, c, X, temp);
        lw_xor_block(WV, m, temp);
        c += temp;
    }

    /* Check the authentication tag */
    lotus_or_locus_gen_tag(&ks, WV, deltaN, WV, WV + GIFT64_BLOCK_SIZE);
    return aead_check_tag(mtemp, *mlen, WV, c, LOCUS_AEAD_TAG_SIZE);
}