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

aead_cipher_t const estate_twegift_cipher = {
    "ESTATE_TweGIFT-128",
    ESTATE_TWEGIFT_KEY_SIZE,
    ESTATE_TWEGIFT_NONCE_SIZE,
    ESTATE_TWEGIFT_TAG_SIZE,
    AEAD_FLAG_NONE,
    estate_twegift_aead_encrypt,
    estate_twegift_aead_decrypt
};

/**
 * \brief Generates the FCBC MAC for a packet using ESTATE_TweGIFT-128.
 *
 * \param ks The key schedule for TweGIFT-128.
 * \param tag Rolling state of the authentication tag.
 * \param m Message to be authenticated.
 * \param mlen Length of the message to be authenticated; must be >= 1.
 * \param tweak1 Tweak value to use when the last block is full.
 * \param tweak2 Tweak value to use when the last block is partial.
 */
static void estate_twegift_fcbc
    (const gift128n_key_schedule_t *ks, unsigned char tag[16],
     const unsigned char *m, unsigned long long mlen,
51
     uint32_t tweak1, uint32_t tweak2)
Rhys Weatherley committed
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
{
    while (mlen > 16) {
        lw_xor_block(tag, m, 16);
        gift128n_encrypt(ks, tag, tag);
        m += 16;
        mlen -= 16;
    }
    if (mlen == 16) {
        lw_xor_block(tag, m, 16);
        gift128t_encrypt(ks, tag, tag, tweak1);
    } else {
        unsigned temp = (unsigned)mlen;
        lw_xor_block(tag, m, temp);
        tag[temp] ^= 0x01;
        gift128t_encrypt(ks, tag, tag, tweak2);
    }
}

/**
 * \brief Generates the MAC for a packet using ESTATE_TweGIFT-128.
 *
 * \param ks The key schedule for TweGIFT-128.
 * \param tag Rolling state of the authentication tag.
 * \param m Message to be authenticated.
 * \param mlen Length of the message to be authenticated.
 * \param ad Associated data to be authenticated.
 * \param adlen Length of the associated data to be authenticated.
 */
static void estate_twegift_authenticate
    (const gift128n_key_schedule_t *ks, unsigned char tag[16],
     const unsigned char *m, unsigned long long mlen,
     const unsigned char *ad, unsigned long long adlen)
{
    /* Handle the case where both the message and associated data are empty */
    if (mlen == 0 && adlen == 0) {
87
        gift128t_encrypt(ks, tag, tag, GIFT128T_TWEAK_8);
Rhys Weatherley committed
88 89 90 91
        return;
    }

    /* Encrypt the nonce */
92
    gift128t_encrypt(ks, tag, tag, GIFT128T_TWEAK_1);
Rhys Weatherley committed
93 94 95

    /* Compute the FCBC MAC over the associated data */
    if (adlen != 0) {
96 97 98 99 100 101 102
        if (mlen != 0) {
            estate_twegift_fcbc
                (ks, tag, ad, adlen, GIFT128T_TWEAK_2, GIFT128T_TWEAK_3);
        } else {
            estate_twegift_fcbc
                (ks, tag, ad, adlen, GIFT128T_TWEAK_6, GIFT128T_TWEAK_7);
        }
Rhys Weatherley committed
103 104 105
    }

    /* Compute the FCBC MAC over the message data */
106 107 108 109
    if (mlen != 0) {
        estate_twegift_fcbc
            (ks, tag, m, mlen, GIFT128T_TWEAK_4, GIFT128T_TWEAK_5);
    }
Rhys Weatherley committed
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
}

/**
 * \brief Encrypts (or decrypts) a payload using ESTATE_TweGIFT-128.
 *
 * \param ks The key schedule for TweGIFT-128.
 * \param tag Pre-computed authentication tag for the packet.
 * \param c Ciphertext after encryption.
 * \param m Plaintext to be encrypted.
 * \param mlen Length of the plaintext to be encrypted.
 */
static void estate_twegift_encrypt
    (const gift128n_key_schedule_t *ks, const unsigned char tag[16],
     unsigned char *c, const unsigned char *m, unsigned long long mlen)
{
    unsigned char block[16];
    memcpy(block, tag, 16);
    while (mlen >= 16) {
        gift128n_encrypt(ks, block, block);
        lw_xor_block_2_src(c, block, m, 16);
        c += 16;
        m += 16;
        mlen -= 16;
    }
    if (mlen > 0) {
        gift128n_encrypt(ks, block, block);
        lw_xor_block_2_src(c, block, m, (unsigned)mlen);
    }
}

int estate_twegift_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 tag[16];
    (void)nsec;

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

    /* Set up the key schedule and copy the nonce into the tag */
156
    gift128n_init(&ks, k);
Rhys Weatherley committed
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
    memcpy(tag, npub, 16);

    /* Authenticate the associated data and plaintext */
    estate_twegift_authenticate(&ks, tag, m, mlen, ad, adlen);

    /* Encrypt the plaintext to generate the ciphertext */
    estate_twegift_encrypt(&ks, tag, c, m, mlen);

    /* Generate the authentication tag */
    memcpy(c + mlen, tag, 16);
    return 0;
}

int estate_twegift_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 tag[16];
    (void)nsec;

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

    /* Set up the key schedule and copy the nonce into the tag */
188
    gift128n_init(&ks, k);
Rhys Weatherley committed
189 190 191 192 193 194 195 196 197 198 199
    memcpy(tag, npub, 16);

    /* Decrypt the ciphertext to generate the plaintext */
    estate_twegift_encrypt(&ks, c + *mlen, m, c, *mlen);

    /* Authenticate the associated data and plaintext */
    estate_twegift_authenticate(&ks, tag, m, *mlen, ad, adlen);

    /* Check the authentication tag */
    return aead_check_tag(m, *mlen, tag, c + *mlen, 16);
}