skinny-hash.c 5.28 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
/*
 * 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 "skinny-hash.h"
#include "internal-skinny128.h"
#include "internal-util.h"
#include <string.h>

aead_hash_algorithm_t const skinny_tk3_hash_algorithm = {
    "SKINNY-tk3-HASH",
    sizeof(int),
    SKINNY_HASH_SIZE,
    AEAD_FLAG_NONE,
    skinny_tk3_hash,
    (aead_hash_init_t)0,
    (aead_hash_update_t)0,
    (aead_hash_finalize_t)0,
    (aead_xof_absorb_t)0,
    (aead_xof_squeeze_t)0
};

aead_hash_algorithm_t const skinny_tk2_hash_algorithm = {
    "SKINNY-tk2-HASH",
    sizeof(int),
    SKINNY_HASH_SIZE,
    AEAD_FLAG_NONE,
    skinny_tk2_hash,
    (aead_hash_init_t)0,
    (aead_hash_update_t)0,
    (aead_hash_finalize_t)0,
    (aead_xof_absorb_t)0,
    (aead_xof_squeeze_t)0
};

/**
 * \brief Size of the permutation state for SKINNY-tk3-HASH.
 */
#define SKINNY_TK3_STATE_SIZE 48

/**
 * \brief Size of the permutation state for SKINNY-tk2-HASH.
 */
#define SKINNY_TK2_STATE_SIZE 32

/**
 * \brief Rate of absorbing data for SKINNY-tk3-HASH.
 */
#define SKINNY_TK3_HASH_RATE 16

/**
 * \brief Rate of absorbing data for SKINNY-tk2-HASH.
 */
#define SKINNY_TK2_HASH_RATE 4

/**
 * \brief Input block that is encrypted with the state for each
 * block permutation of SKINNY-tk3-HASH or SKINNY-tk2-HASH.
 */
static unsigned char const skinny_hash_block[48] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

/**
 * \brief Permutes the internal state for SKINNY-tk3-HASH.
 *
 * \param state The state to be permuted.
 */
static void skinny_tk3_permute(unsigned char state[SKINNY_TK3_STATE_SIZE])
{
    unsigned char temp[SKINNY_TK3_STATE_SIZE];
    skinny_128_384_encrypt_tk_full(state, temp, skinny_hash_block);
    skinny_128_384_encrypt_tk_full(state, temp + 16, skinny_hash_block + 16);
    skinny_128_384_encrypt_tk_full(state, temp + 32, skinny_hash_block + 32);
    memcpy(state, temp, SKINNY_TK3_STATE_SIZE);
}

/**
 * \brief Permutes the internal state for SKINNY-tk2-HASH.
 *
 * \param state The state to be permuted.
 */
static void skinny_tk2_permute(unsigned char state[SKINNY_TK2_STATE_SIZE])
{
    unsigned char temp[SKINNY_TK2_STATE_SIZE];
    skinny_128_256_encrypt_tk_full(state, temp, skinny_hash_block);
    skinny_128_256_encrypt_tk_full(state, temp + 16, skinny_hash_block + 16);
    memcpy(state, temp, SKINNY_TK2_STATE_SIZE);
}

int skinny_tk3_hash
    (unsigned char *out, const unsigned char *in, unsigned long long inlen)
{
    unsigned char state[SKINNY_TK3_STATE_SIZE];
    unsigned temp;

    /* Initialize the hash state */
    memset(state, 0, sizeof(state));
    state[SKINNY_TK3_HASH_RATE] = 0x80;

    /* Process as many full blocks as possible */
    while (inlen >= SKINNY_TK3_HASH_RATE) {
        lw_xor_block(state, in, SKINNY_TK3_HASH_RATE);
        skinny_tk3_permute(state);
        in += SKINNY_TK3_HASH_RATE;
        inlen -= SKINNY_TK3_HASH_RATE;
    }

    /* Pad and process the last block */
    temp = (unsigned)inlen;
    lw_xor_block(state, in, temp);
    state[temp] ^= 0x80; /* padding */
    skinny_tk3_permute(state);

    /* Generate the hash output */
    memcpy(out, state, 16);
    skinny_tk3_permute(state);
    memcpy(out + 16, state, 16);
    return 0;
}

int skinny_tk2_hash
    (unsigned char *out, const unsigned char *in, unsigned long long inlen)
{
    unsigned char state[SKINNY_TK2_STATE_SIZE];
    unsigned temp;

    /* Initialize the hash state */
    memset(state, 0, sizeof(state));
    state[SKINNY_TK2_HASH_RATE] = 0x80;

    /* Process as many full blocks as possible */
    while (inlen >= SKINNY_TK2_HASH_RATE) {
        lw_xor_block(state, in, SKINNY_TK2_HASH_RATE);
        skinny_tk2_permute(state);
        in += SKINNY_TK2_HASH_RATE;
        inlen -= SKINNY_TK2_HASH_RATE;
    }

    /* Pad and process the last block */
    temp = (unsigned)inlen;
    lw_xor_block(state, in, temp);
    state[temp] ^= 0x80; /* padding */
    skinny_tk2_permute(state);

    /* Generate the hash output */
    memcpy(out, state, 16);
    skinny_tk2_permute(state);
    memcpy(out + 16, state, 16);
    return 0;
}