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
/*
 * 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);
60
    gift64n_update_round_keys(ks);
Rhys Weatherley committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}

/**
 * \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)
{
80
    gift64n_init(ks, key);
Rhys Weatherley committed
81
    memset(deltaN, 0, GIFT64_BLOCK_SIZE);
82
    gift64t_encrypt(ks, deltaN, deltaN, GIFT64T_TWEAK_0);
Rhys Weatherley committed
83
    lw_xor_block_2_src(T, key, nonce, LOTUS_AEAD_KEY_SIZE);
84 85
    gift64n_init(ks, T);
    gift64t_encrypt(ks, deltaN, deltaN, GIFT64T_TWEAK_1);
Rhys Weatherley committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
}

/**
 * \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);
108
        gift64t_encrypt(ks, X, X, GIFT64T_TWEAK_2);
Rhys Weatherley committed
109 110 111 112 113 114 115 116 117 118
        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;
119
        gift64t_encrypt(ks, X, X, GIFT64T_TWEAK_3);
Rhys Weatherley committed
120 121
    } else {
        lw_xor_block_2_src(X, ad, deltaN, GIFT64_BLOCK_SIZE);
122
        gift64t_encrypt(ks, X, X, GIFT64T_TWEAK_2);
Rhys Weatherley committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    }
    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);
145
    gift64t_encrypt(ks, W, W, GIFT64T_TWEAK_6);
Rhys Weatherley committed
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
    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);
183
            gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_4);
Rhys Weatherley committed
184
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
185
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_4);
Rhys Weatherley committed
186 187 188
            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);
189
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_5);
Rhys Weatherley committed
190
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
191
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_5);
Rhys Weatherley committed
192 193 194 195 196 197 198 199 200 201
            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;
202
        gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_12);
Rhys Weatherley committed
203
        lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
204
        gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_12);
Rhys Weatherley committed
205 206 207 208 209 210 211 212 213 214
        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;
215
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
Rhys Weatherley committed
216
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
217
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
Rhys Weatherley committed
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
            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);
268
            gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_5);
Rhys Weatherley committed
269
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
270
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_5);
Rhys Weatherley committed
271 272
            lw_xor_block(X2, c + GIFT64_BLOCK_SIZE, GIFT64_BLOCK_SIZE);
            lw_xor_block_2_src(m, X2, deltaN, GIFT64_BLOCK_SIZE);
273
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_4);
Rhys Weatherley committed
274
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
275
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_4);
Rhys Weatherley committed
276 277 278 279 280 281 282 283 284 285
            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;
286
        gift64t_encrypt(&ks, X2, X1, GIFT64T_TWEAK_12);
Rhys Weatherley committed
287
        lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
288
        gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_12);
Rhys Weatherley committed
289 290 291 292 293 294 295 296 297 298 299 300
        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;
301
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
Rhys Weatherley committed
302
            lw_xor_block(WV, X2, GIFT64_BLOCK_SIZE);
303
            gift64t_encrypt(&ks, X2, X2, GIFT64T_TWEAK_13);
Rhys Weatherley committed
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
            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);
349
            gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_4);
Rhys Weatherley committed
350
            lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
351
            gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_4);
Rhys Weatherley committed
352 353 354 355 356 357 358 359 360
            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;
361
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
Rhys Weatherley committed
362 363
        lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
        lw_xor_block(WV, m, temp);
364
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
Rhys Weatherley committed
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
        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);
412
            gift64t_decrypt(&ks, X, X, GIFT64T_TWEAK_4);
Rhys Weatherley committed
413
            lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
414
            gift64t_decrypt(&ks, X, X, GIFT64T_TWEAK_4);
Rhys Weatherley committed
415 416 417 418 419 420 421 422 423
            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;
424
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
Rhys Weatherley committed
425
        lw_xor_block(WV, X, GIFT64_BLOCK_SIZE);
426
        gift64t_encrypt(&ks, X, X, GIFT64T_TWEAK_5);
Rhys Weatherley committed
427 428 429 430 431 432 433 434 435 436
        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);
}