internal-subterranean.h 5.24 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
/*
 * 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.
 */

#ifndef LW_INTERNAL_SUBTERRANEAN_H
#define LW_INTERNAL_SUBTERRANEAN_H

#include "internal-util.h"

/**
 * \file internal-subterranean.h
 * \brief Internal implementation of the Subterranean block operation.
31 32
 *
 * References: https://cs.ru.nl/~joan/subterranean.html
Rhys Weatherley committed
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
 */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief Representation of the 257-bit state of Subterranean.
 *
 * The 257-bit state is represented as nine 32-bit words with only a single
 * bit in the last word.
 */
typedef struct
{
    uint32_t x[9];      /**< State words */

} subterranean_state_t;

/**
 * \brief Performs a single Subterranean round.
 *
 * \param state Subterranean state to be transformed.
 */
void subterranean_round(subterranean_state_t *state);

/**
 * \brief Performs 8 Subterranean rounds with no absorption or squeezing
 * of data; i.e. data input and output is "blanked".
 *
 * \param state Subterranean state to be transformed.
 */
void subterranean_blank(subterranean_state_t *state);

/**
 * \brief Performs a single Subterranean round and absorbs 0 bytes.
 *
 * \param state Subterranean state to be transformed.
 */
71 72 73 74 75 76 77 78 79 80 81 82 83
#define subterranean_duplex_0(state) \
    do { \
        subterranean_round((state)); \
        (state)->x[0] ^= 2; /* padding for an empty block */ \
    } while (0)

/**
 * \brief Absorbs a single byte into the Subterranean state.
 *
 * \param state Subterranean state to be transformed.
 * \param data The single byte to be absorbed.
 */
void subterranean_absorb_1(subterranean_state_t *state, unsigned char data);
Rhys Weatherley committed
84 85 86 87 88 89 90

/**
 * \brief Performs a single Subterranean round and absorbs one byte.
 *
 * \param state Subterranean state to be transformed.
 * \param data The single byte to be absorbed.
 */
91 92 93 94 95
#define subterranean_duplex_1(state, data) \
    do { \
        subterranean_round((state)); \
        subterranean_absorb_1((state), (data)); \
    } while (0)
Rhys Weatherley committed
96 97 98 99 100 101 102

/**
 * \brief Absorbs a 32-bit word into the Subterranean state.
 *
 * \param state Subterranean state to be transformed.
 * \param x The word to absorb into the state.
 */
103 104 105 106 107 108 109 110 111 112 113 114 115 116
void subterranean_absorb_word(subterranean_state_t *state, uint32_t x);

/**
 * \brief Absorbs a 32-bit word into the Subterranean state after performing
 * the round function.
 *
 * \param state Subterranean state to be transformed.
 * \param x The word to absorb into the state.
 */
#define subterranean_duplex_word(state, x) \
    do { \
        subterranean_round((state)); \
        subterranean_absorb_word((state), (x)); \
    } while (0)
Rhys Weatherley committed
117 118 119 120 121

/**
 * \brief Performs a single Subterranean round and absorbs four bytes.
 *
 * \param state Subterranean state to be transformed.
122
 * \param data 32-bit word containing the four data bytes to be absorbed.
Rhys Weatherley committed
123 124 125
 */
#define subterranean_duplex_4(state, data) \
    do { \
126
        subterranean_duplex_word((state), (data)); \
Rhys Weatherley committed
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
        (state)->x[8] ^= 1; \
    } while (0)

/**
 * \brief Performs a single Subterranean round and absorbs between
 * zero and four bytes.
 *
 * \param state Subterranean state to be transformed.
 * \param data Points to the data bytes to be absorbed.
 * \param len Length of the data to be absorbed.
 */
void subterranean_duplex_n
    (subterranean_state_t *state, const unsigned char *data, unsigned len);

/**
 * \brief Extracts 32 bits of output from the Subterranean state.
 *
 * \param state Subterranean state to extract the output from.
 *
 * \return Returns the 32-bit word that was extracted.
 */
uint32_t subterranean_extract(subterranean_state_t *state);

/**
 * \brief Absorbs an arbitrary amount of data, four bytes at a time.
 *
 * \param state Subterranean state to be transformed.
 * \param data Points to the bytes to be absorbed.
 * \param len Number of bytes to absorb.
 */
void subterranean_absorb
    (subterranean_state_t *state, const unsigned char *data,
     unsigned long long len);

/**
 * \brief Squeezes an arbitrary amount of data out of a Subterranean state.
 *
 * \param state Subterranean state to extract the output from.
 * \param data Points to the data buffer to receive the output.
 * \param len Number of bytes to be extracted.
 */
void subterranean_squeeze
    (subterranean_state_t *state, unsigned char *data, unsigned len);

#ifdef __cplusplus
}
#endif

#endif