spix.c 6.87 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
/*
 * 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 "spix.h"
#include "internal-sliscp-light.h"
#include "internal-util.h"
#include <string.h>

/**
 * \brief Size of the state for the internal sLiSCP-light permutation.
 */
#define SPIX_STATE_SIZE SLISCP_LIGHT256_STATE_SIZE

/**
 * \brief Rate for absorbing data into the sLiSCP-light state and for
 * squeezing data out again.
 */
#define SPIX_RATE 8

aead_cipher_t const spix_cipher = {
    "SPIX",
    SPIX_KEY_SIZE,
    SPIX_NONCE_SIZE,
    SPIX_TAG_SIZE,
    AEAD_FLAG_NONE,
    spix_aead_encrypt,
    spix_aead_decrypt
};

/* Indices of where a rate byte is located in the state.  We don't
 * need this array any more because sliscp_light256_permute_spix()
 * operates on byte-swapped states where the rate bytes are contiguous
 * in the bytes 8 to 15 */
/*
static unsigned char const spix_rate_posn[8] = {
    8, 9, 10, 11, 24, 25, 26, 27
};
*/

/**
 * \brief Initializes the SPIX state.
 *
 * \param state sLiSCP-light-256 permutation state.
 * \param k Points to the 128-bit key.
 * \param npub Points to the 128-bit nonce.
 * \param ad Points to the associated data.
 * \param adlen Length of the associated data in bytes.
 */
static void spix_init
    (unsigned char state[SPIX_STATE_SIZE],
     const unsigned char *k, const unsigned char *npub,
     const unsigned char *ad, unsigned long long adlen)
{
    unsigned temp;

    /* Initialize the state by interleaving the key and nonce */
    memcpy(state, npub, 8);
    memcpy(state + 8, k, 8);
    memcpy(state + 16, npub + 8, 8);
    memcpy(state + 24, k + 8, 8);
    sliscp_light256_swap_spix(state);

    /* Run the permutation to scramble the initial state */
    sliscp_light256_permute_spix(state, 18);

    /* Absorb the key in two further permutation operations */
    lw_xor_block(state + 8, k, 8);
    sliscp_light256_permute_spix(state, 18);
    lw_xor_block(state + 8, k + 8, 8);
    sliscp_light256_permute_spix(state, 18);

    /* Absorb the associated data into the state */
    if (adlen != 0) {
        while (adlen >= SPIX_RATE) {
            lw_xor_block(state + 8, ad, SPIX_RATE);
            state[SPIX_STATE_SIZE - 1] ^= 0x01; /* domain separation */
            sliscp_light256_permute_spix(state, 9);
            ad += SPIX_RATE;
            adlen -= SPIX_RATE;
        }
        temp = (unsigned)adlen;
        lw_xor_block(state + 8, ad, temp);
        state[temp + 8] ^= 0x80; /* padding */
        state[SPIX_STATE_SIZE - 1] ^= 0x01; /* domain separation */
        sliscp_light256_permute_spix(state, 9);
    }
}

/**
 * \brief Finalizes the SPIX encryption or decryption operation.
 *
 * \param state sLiSCP-light-256 permutation state.
 * \param k Points to the 128-bit key.
 * \param tag Points to the 16 byte buffer to receive the computed tag.
 */
static void spix_finalize
    (unsigned char state[SPIX_STATE_SIZE], const unsigned char *k,
     unsigned char *tag)
{
    /* Absorb the key into the state again */
    lw_xor_block(state + 8, k, 8);
    sliscp_light256_permute_spix(state, 18);
    lw_xor_block(state + 8, k + 8, 8);
    sliscp_light256_permute_spix(state, 18);

    /* Copy out the authentication tag */
    sliscp_light256_swap_spix(state);
    memcpy(tag, state + 8, 8);
    memcpy(tag + 8, state + 24, 8);
}

int spix_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)
{
    unsigned char state[SPIX_STATE_SIZE];
    unsigned temp;
    (void)nsec;

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

    /* Initialize the SPIX state and absorb the associated data */
    spix_init(state, k, npub, ad, adlen);

    /* Encrypt the plaintext to produce the ciphertext */
    while (mlen >= SPIX_RATE) {
        lw_xor_block_2_dest(c, state + 8, m, SPIX_RATE);
        state[SPIX_STATE_SIZE - 1] ^= 0x02; /* domain separation */
        sliscp_light256_permute_spix(state, 9);
        c += SPIX_RATE;
        m += SPIX_RATE;
        mlen -= SPIX_RATE;
    }
    temp = (unsigned)mlen;
    lw_xor_block_2_dest(c, state + 8, m, temp);
    state[temp + 8] ^= 0x80; /* padding */
    state[SPIX_STATE_SIZE - 1] ^= 0x02; /* domain separation */
    sliscp_light256_permute_spix(state, 9);
    c += mlen;

    /* Generate the authentication tag */
    spix_finalize(state, k, c);
    return 0;
}

int spix_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)
{
    unsigned char state[SPIX_STATE_SIZE];
    unsigned char *mtemp = m;
    unsigned temp;
    (void)nsec;

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

    /* Initialize the SPIX state and absorb the associated data */
    spix_init(state, k, npub, ad, adlen);

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= SPIX_TAG_SIZE;
    while (clen >= SPIX_RATE) {
        lw_xor_block_swap(m, state + 8, c, SPIX_RATE);
        state[SPIX_STATE_SIZE - 1] ^= 0x02; /* domain separation */
        sliscp_light256_permute_spix(state, 9);
        c += SPIX_RATE;
        m += SPIX_RATE;
        clen -= SPIX_RATE;
    }
    temp = (unsigned)clen;
    lw_xor_block_swap(m, state + 8, c, temp);
    state[temp + 8] ^= 0x80; /* padding */
    state[SPIX_STATE_SIZE - 1] ^= 0x02; /* domain separation */
    sliscp_light256_permute_spix(state, 9);
    c += clen;

    /* Finalize the SPIX state and compare against the authentication tag */
    spix_finalize(state, k, state);
    return aead_check_tag(mtemp, *mlen, state, c, SPIX_TAG_SIZE);
}