internal-isap.h 12.5 KB
Newer Older
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
/*
 * Copyright (C) 2021 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.
 */

/* We expect a number of macros to be defined before this file
 * is included to configure the underlying ISAP variant.
 *
 * ISAP_ALG_NAME        Name of the ISAP algorithm; e.g. isap_keccak_128
 * ISAP_RATE            Number of bytes in the rate for hashing and encryption.
 * ISAP_sH              Number of rounds for hashing.
 * ISAP_sE              Number of rounds for encryption.
 * ISAP_sB              Number of rounds for key bit absorption.
 * ISAP_sK              Number of rounds for keying.
 * ISAP_STATE           Type for the permuation state; e.g. ascon_state_t
 * ISAP_PERMUTE(s,r)    Permutes the state "s" with number of rounds "r".
 * ISAP_PERMUTE_SLICED(s,r) Defined if using the sliced version of ASCON.
 */
#if defined(ISAP_ALG_NAME)

#if !defined(ISAP_KEY_SIZE)
#define ISAP_KEY_SIZE 16
#define ISAP_NONCE_SIZE 16
#define ISAP_TAG_SIZE 16
#endif

#define ISAP_CONCAT_INNER(name,suffix) name##suffix
#define ISAP_CONCAT(name,suffix) ISAP_CONCAT_INNER(name,suffix)

/* IV string for initialising the associated data */
static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_A)
        [sizeof(ISAP_STATE) - ISAP_NONCE_SIZE] = {
    0x01, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
    ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
};

/* IV string for authenticating associated data */
static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA)
        [sizeof(ISAP_STATE) - ISAP_KEY_SIZE] = {
    0x02, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
    ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
};

/* IV string for encrypting payload data */
static unsigned char const ISAP_CONCAT(ISAP_ALG_NAME,_IV_KE)
        [sizeof(ISAP_STATE) - ISAP_KEY_SIZE] = {
    0x03, ISAP_KEY_SIZE * 8, ISAP_RATE * 8, 1,
    ISAP_sH, ISAP_sB, ISAP_sE, ISAP_sK
};

/**
 * \brief Re-keys the ISAP permutation state.
 *
 * \param state The permutation state to be re-keyed.
 * \param k Points to the 128-bit key for the ISAP cipher.
 * \param iv Points to the initialization vector for this re-keying operation.
 * \param data Points to the data to be absorbed to perform the re-keying.
 * \param data_len Length of the data to be absorbed.
 *
 * The output key will be left in the leading bytes of \a state.
 */
static void ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
    (ISAP_STATE *state, const unsigned char *k, const unsigned char *iv,
     const unsigned char *data, unsigned data_len)
{
#if defined(ISAP_PERMUTE_SLICED)
    unsigned bit, num_bits;

    /* Initialize the state with the key and IV */
    memcpy(state->B, k, ISAP_KEY_SIZE);
    memcpy(state->B + ISAP_KEY_SIZE, iv, sizeof(state->B) - ISAP_KEY_SIZE);
    ascon_to_sliced(state);
    ISAP_PERMUTE_SLICED(state, ISAP_sK);

    /* Absorb all of the bits of the data buffer one by one */
    num_bits = data_len * 8 - 1;
    for (bit = 0; bit < num_bits; ++bit) {
        state->W[1] ^=
            (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
        ISAP_PERMUTE_SLICED(state, ISAP_sB);
    }
    state->W[1] ^=
        (((uint32_t)(data[bit / 8])) << (24 + bit % 8)) & 0x80000000U;
    ISAP_PERMUTE_SLICED(state, ISAP_sK);
#else
    unsigned bit, num_bits;

    /* Initialize the state with the key and IV */
    memcpy(state->B, k, ISAP_KEY_SIZE);
    memcpy(state->B + ISAP_KEY_SIZE, iv, sizeof(state->B) - ISAP_KEY_SIZE);
    ISAP_PERMUTE(state, ISAP_sK);

    /* Absorb all of the bits of the data buffer one by one */
    num_bits = data_len * 8 - 1;
    for (bit = 0; bit < num_bits; ++bit) {
        state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
        ISAP_PERMUTE(state, ISAP_sB);
    }
    state->B[0] ^= (data[bit / 8] << (bit % 8)) & 0x80;
    ISAP_PERMUTE(state, ISAP_sK);
#endif
}

/**
 * \brief Encrypts (or decrypts) a message payload with ISAP.
 *
 * \param state ISAP permutation state.
 * \param k Points to the 128-bit key for the ISAP cipher.
 * \param npub Points to the 128-bit nonce for the ISAP cipher.
 * \param c Buffer to receive the output ciphertext.
 * \param m Buffer to receive the input plaintext.
 * \param mlen Length of the input plaintext.
 */
static void ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)
    (ISAP_STATE *state, const unsigned char *k, const unsigned char *npub,
     unsigned char *c, const unsigned char *m, size_t mlen)
{
#if defined(ISAP_PERMUTE_SLICED)
    unsigned char block[ISAP_RATE];

    /* Set up the re-keyed encryption key and nonce in the state */
    ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
        (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KE), npub, ISAP_NONCE_SIZE);
    ascon_set_sliced(state, npub, 3);
    ascon_set_sliced(state, npub + 8, 4);

    /* Encrypt the plaintext to produce the ciphertext */
    while (mlen >= ISAP_RATE) {
        ISAP_PERMUTE_SLICED(state, ISAP_sE);
        ascon_squeeze_sliced(state, block, 0);
        lw_xor_block_2_src(c, block, m, ISAP_RATE);
        c += ISAP_RATE;
        m += ISAP_RATE;
        mlen -= ISAP_RATE;
    }
    if (mlen > 0) {
        ISAP_PERMUTE_SLICED(state, ISAP_sE);
        ascon_squeeze_sliced(state, block, 0);
        lw_xor_block_2_src(c, block, m, (unsigned)mlen);
    }
#else
    /* Set up the re-keyed encryption key and nonce in the state */
    ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
        (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KE), npub, ISAP_NONCE_SIZE);
    memcpy(state->B + sizeof(ISAP_STATE) - ISAP_NONCE_SIZE,
           npub, ISAP_NONCE_SIZE);

    /* Encrypt the plaintext to produce the ciphertext */
    while (mlen >= ISAP_RATE) {
        ISAP_PERMUTE(state, ISAP_sE);
        lw_xor_block_2_src(c, state->B, m, ISAP_RATE);
        c += ISAP_RATE;
        m += ISAP_RATE;
        mlen -= ISAP_RATE;
    }
    if (mlen > 0) {
        ISAP_PERMUTE(state, ISAP_sE);
        lw_xor_block_2_src(c, state->B, m, (unsigned)mlen);
    }
#endif
}

/**
 * \brief Authenticates the associated data and ciphertext using ISAP.
 *
 * \param state ISAP permutation state.
 * \param k Points to the 128-bit key for the ISAP cipher.
 * \param npub Points to the 128-bit nonce for the ISAP cipher.
 * \param ad Buffer containing the associated data.
 * \param adlen Length of the associated data.
 * \param c Buffer containing the ciphertext.
 * \param clen Length of the ciphertext.
 */
static void ISAP_CONCAT(ISAP_ALG_NAME,_mac)
    (ISAP_STATE *state, const unsigned char *k, const unsigned char *npub,
     const unsigned char *ad, size_t adlen,
     const unsigned char *c, size_t clen,
     unsigned char *tag)
{
#if defined(ISAP_PERMUTE_SLICED)
    unsigned char preserve[sizeof(ISAP_STATE) - ISAP_TAG_SIZE];
    unsigned char padded[ISAP_RATE];
    unsigned temp;

    /* Absorb the associated data */
    memcpy(state->B, npub, ISAP_NONCE_SIZE);
    memcpy(state->B + ISAP_NONCE_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_IV_A),
           sizeof(state->B) - ISAP_NONCE_SIZE);
    ascon_to_sliced(state);
    ISAP_PERMUTE_SLICED(state, ISAP_sH);
    while (adlen >= ISAP_RATE) {
        ascon_absorb_sliced(state, ad, 0);
        ISAP_PERMUTE_SLICED(state, ISAP_sH);
        ad += ISAP_RATE;
        adlen -= ISAP_RATE;
    }
    temp = (unsigned)adlen;
    memcpy(padded, ad, temp);
    padded[temp] = 0x80; /* padding */
    memset(padded + temp + 1, 0, sizeof(padded) - (temp + 1));
    ascon_absorb_sliced(state, padded, 0);
    ISAP_PERMUTE_SLICED(state, ISAP_sH);
    state->W[8] ^= 0x01; /* domain separation */

    /* Absorb the ciphertext */
    while (clen >= ISAP_RATE) {
        ascon_absorb_sliced(state, c, 0);
        ISAP_PERMUTE_SLICED(state, ISAP_sH);
        c += ISAP_RATE;
        clen -= ISAP_RATE;
    }
    temp = (unsigned)clen;
    memcpy(padded, c, temp);
    padded[temp] = 0x80; /* padding */
    memset(padded + temp + 1, 0, sizeof(padded) - (temp + 1));
    ascon_absorb_sliced(state, padded, 0);
    ISAP_PERMUTE_SLICED(state, ISAP_sH);

    /* Re-key the state and generate the authentication tag */
    ascon_from_sliced(state);
    memcpy(tag, state->B, ISAP_TAG_SIZE);
    memcpy(preserve, state->B + ISAP_TAG_SIZE, sizeof(preserve));
    ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
        (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA), tag, ISAP_TAG_SIZE);
    ascon_from_sliced(state);
    memcpy(state->B + ISAP_TAG_SIZE, preserve, sizeof(preserve));
    ascon_to_sliced(state);
    ISAP_PERMUTE_SLICED(state, ISAP_sH);
    ascon_squeeze_sliced(state, tag, 0);
    ascon_squeeze_sliced(state, tag + 8, 1);
#else
    unsigned char preserve[sizeof(ISAP_STATE) - ISAP_TAG_SIZE];
    unsigned temp;

    /* Absorb the associated data */
    memcpy(state->B, npub, ISAP_NONCE_SIZE);
    memcpy(state->B + ISAP_NONCE_SIZE, ISAP_CONCAT(ISAP_ALG_NAME,_IV_A),
           sizeof(state->B) - ISAP_NONCE_SIZE);
    ISAP_PERMUTE(state, ISAP_sH);
    while (adlen >= ISAP_RATE) {
        lw_xor_block(state->B, ad, ISAP_RATE);
        ISAP_PERMUTE(state, ISAP_sH);
        ad += ISAP_RATE;
        adlen -= ISAP_RATE;
    }
    temp = (unsigned)adlen;
    lw_xor_block(state->B, ad, temp);
    state->B[temp] ^= 0x80; /* padding */
    ISAP_PERMUTE(state, ISAP_sH);
    state->B[sizeof(state->B) - 1] ^= 0x01; /* domain separation */

    /* Absorb the ciphertext */
    while (clen >= ISAP_RATE) {
        lw_xor_block(state->B, c, ISAP_RATE);
        ISAP_PERMUTE(state, ISAP_sH);
        c += ISAP_RATE;
        clen -= ISAP_RATE;
    }
    temp = (unsigned)clen;
    lw_xor_block(state->B, c, temp);
    state->B[temp] ^= 0x80; /* padding */
    ISAP_PERMUTE(state, ISAP_sH);

    /* Re-key the state and generate the authentication tag */
    memcpy(tag, state->B, ISAP_TAG_SIZE);
    memcpy(preserve, state->B + ISAP_TAG_SIZE, sizeof(preserve));
    ISAP_CONCAT(ISAP_ALG_NAME,_rekey)
        (state, k, ISAP_CONCAT(ISAP_ALG_NAME,_IV_KA), tag, ISAP_TAG_SIZE);
    memcpy(state->B + ISAP_TAG_SIZE, preserve, sizeof(preserve));
    ISAP_PERMUTE(state, ISAP_sH);
    memcpy(tag, state->B, ISAP_TAG_SIZE);
#endif
}

int ISAP_CONCAT(ISAP_ALG_NAME,_aead_encrypt)
    (unsigned char *c, size_t *clen,
     const unsigned char *m, size_t mlen,
     const unsigned char *ad, size_t adlen,
     const unsigned char *npub,
     const unsigned char *k)
{
    ISAP_STATE state;

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

    /* Encrypt the plaintext to produce the ciphertext */
    ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)(&state, k, npub, c, m, mlen);

    /* Authenticate the associated data and ciphertext to generate the tag */
    ISAP_CONCAT(ISAP_ALG_NAME,_mac)
        (&state, k, npub, ad, adlen, c, mlen, c + mlen);
    return 0;
}

int ISAP_CONCAT(ISAP_ALG_NAME,_aead_decrypt)
    (unsigned char *m, size_t *mlen,
     const unsigned char *c, size_t clen,
     const unsigned char *ad, size_t adlen,
     const unsigned char *npub,
     const unsigned char *k)
{
    ISAP_STATE state;
    unsigned char tag[ISAP_TAG_SIZE];

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

    /* Authenticate the associated data and ciphertext to generate the tag */
    ISAP_CONCAT(ISAP_ALG_NAME,_mac)(&state, k, npub, ad, adlen, c, *mlen, tag);

    /* Decrypt the ciphertext to produce the plaintext */
    ISAP_CONCAT(ISAP_ALG_NAME,_encrypt)(&state, k, npub, m, c, *mlen);

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

#endif /* ISAP_ALG_NAME */

/* Now undefine everything so that we can include this file again for
 * another variant on the ISAP algorithm */
#undef ISAP_ALG_NAME
#undef ISAP_RATE
#undef ISAP_sH
#undef ISAP_sE
#undef ISAP_sB
#undef ISAP_sK
#undef ISAP_STATE
#undef ISAP_PERMUTE
#undef ISAP_PERMUTE_SLICED
#undef ISAP_CONCAT_INNER
#undef ISAP_CONCAT