internal-skinny128.h 7.46 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 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
/*
 * 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_SKINNY128_H
#define LW_INTERNAL_SKINNY128_H

/**
 * \file internal-skinny128.h
 * \brief SKINNY-128 block cipher family.
 *
 * References: https://eprint.iacr.org/2016/660.pdf,
 * https://sites.google.com/site/skinnycipher/
 */

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \def SKINNY_128_SMALL_SCHEDULE
 * \brief Defined to 1 to use the small key schedule version of SKINNY-128.
 */
#if defined(__AVR__)
#define SKINNY_128_SMALL_SCHEDULE 1
#else
#define SKINNY_128_SMALL_SCHEDULE 0
#endif

/**
 * \brief Size of a block for SKINNY-128 block ciphers.
 */
#define SKINNY_128_BLOCK_SIZE 16

/**
 * \brief Number of rounds for SKINNY-128-384.
 */
#define SKINNY_128_384_ROUNDS 56

/**
 * \brief Structure of the key schedule for SKINNY-128-384.
 */
typedef struct
{
    /** TK1 for the tweakable part of the key schedule */
    uint8_t TK1[16];

#if SKINNY_128_SMALL_SCHEDULE
    /** TK2 for the small key schedule */
    uint8_t TK2[16];

    /** TK3 for the small key schedule */
    uint8_t TK3[16];
#else
    /** Words of the full key schedule */
    uint32_t k[SKINNY_128_384_ROUNDS * 2];
#endif

} skinny_128_384_key_schedule_t;

/**
 * \brief Initializes the key schedule for SKINNY-128-384.
 *
 * \param ks Points to the key schedule to initialize.
 * \param key Points to the key data.
 */
void skinny_128_384_init
    (skinny_128_384_key_schedule_t *ks, const unsigned char key[48]);

/**
 * \brief Encrypts a 128-bit block with SKINNY-128-384.
 *
 * \param ks Points to the SKINNY-128-384 key schedule.
 * \param output Output buffer which must be at least 16 bytes in length.
 * \param input Input buffer which must be at least 16 bytes in length.
 *
 * The \a input and \a output buffers can be the same buffer for
 * in-place encryption.
 */
void skinny_128_384_encrypt
    (const skinny_128_384_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input);

/**
 * \brief Decrypts a 128-bit block with SKINNY-128-384.
 *
 * \param ks Points to the SKINNY-128-384 key schedule.
 * \param output Output buffer which must be at least 16 bytes in length.
 * \param input Input buffer which must be at least 16 bytes in length.
 *
 * The \a input and \a output buffers can be the same buffer for
 * in-place encryption.
 */
void skinny_128_384_decrypt
    (const skinny_128_384_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input);

/**
 * \brief Encrypts a 128-bit block with SKINNY-128-384 and an explicitly
 * provided TK2 value.
 *
 * \param ks Points to the SKINNY-128-384 key schedule.
 * \param output Output buffer which must be at least 16 bytes in length.
 * \param input Input buffer which must be at least 16 bytes in length.
 * \param tk2 TK2 value that should be updated on the fly.
 *
 * The \a input and \a output buffers can be the same buffer for
 * in-place encryption.
 *
 * This version is useful when both TK1 and TK2 change from block to block.
 * When the key is initialized with skinny_128_384_init(), the TK2 part of
 * the key value should be set to zero.
 *
 * \note Some versions of this function may modify the key schedule to
 * copy tk2 into place.
 */
void skinny_128_384_encrypt_tk2
    (skinny_128_384_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input, const unsigned char *tk2);

/**
 * \brief Encrypts a 128-bit block with SKINNY-128-384 and a
 * fully specified tweakey value.
 *
 * \param key Points to the 384-bit tweakey value.
 * \param output Output buffer which must be at least 16 bytes in length.
 * \param input Input buffer which must be at least 16 bytes in length.
 *
 * The \a input and \a output buffers can be the same buffer for
 * in-place encryption.
 *
 * This version is useful when the entire tweakey changes from block to
 * block.  It is slower than the other versions of SKINNY-128-384 but
 * more memory-efficient.
 */
void skinny_128_384_encrypt_tk_full
    (const unsigned char key[48], unsigned char *output,
     const unsigned char *input);

/**
 * \brief Number of rounds for SKINNY-128-256.
 */
#define SKINNY_128_256_ROUNDS 48

/**
 * \brief Structure of the key schedule for SKINNY-128-256.
 */
typedef struct
{
    /** TK1 for the tweakable part of the key schedule */
    uint8_t TK1[16];

#if SKINNY_128_SMALL_SCHEDULE
    /** TK2 for the small key schedule */
    uint8_t TK2[16];
#else
    /** Words of the full key schedule */
    uint32_t k[SKINNY_128_256_ROUNDS * 2];
#endif

} skinny_128_256_key_schedule_t;

/**
 * \brief Initializes the key schedule for SKINNY-128-256.
 *
 * \param ks Points to the key schedule to initialize.
 * \param key Points to the key data.
 */
void skinny_128_256_init
    (skinny_128_256_key_schedule_t *ks, const unsigned char key[32]);

/**
 * \brief Encrypts a 128-bit block with SKINNY-128-256.
 *
 * \param ks Points to the SKINNY-128-256 key schedule.
 * \param output Output buffer which must be at least 16 bytes in length.
 * \param input Input buffer which must be at least 16 bytes in length.
 *
 * The \a input and \a output buffers can be the same buffer for
 * in-place encryption.
 */
void skinny_128_256_encrypt
    (const skinny_128_256_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input);

/**
 * \brief Decrypts a 128-bit block with SKINNY-128-256.
 *
 * \param ks Points to the SKINNY-128-256 key schedule.
 * \param output Output buffer which must be at least 16 bytes in length.
 * \param input Input buffer which must be at least 16 bytes in length.
 *
 * The \a input and \a output buffers can be the same buffer for
 * in-place encryption.
 */
void skinny_128_256_decrypt
    (const skinny_128_256_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input);

/**
 * \brief Encrypts a 128-bit block with SKINNY-128-256 and a
 * fully specified tweakey value.
 *
 * \param key Points to the 256-bit tweakey value.
 * \param output Output buffer which must be at least 16 bytes in length.
 * \param input Input buffer which must be at least 16 bytes in length.
 *
 * The \a input and \a output buffers can be the same buffer for
 * in-place encryption.
 *
 * This version is useful when the entire tweakey changes from block to
 * block.  It is slower than the other versions of SKINNY-128-256 but
 * more memory-efficient.
 */
void skinny_128_256_encrypt_tk_full
    (const unsigned char key[32], unsigned char *output,
     const unsigned char *input);

#ifdef __cplusplus
}
#endif

#endif