internal-skinny-plus.h 5.2 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
/*
 * 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.
 */

#ifndef LW_INTERNAL_SKINNY_PLUS_H
#define LW_INTERNAL_SKINNY_PLUS_H

/**
 * \file internal-skinny-plus.h
 * \brief SKINNY-128-384+ block cipher.
 *
 * References: https://eprint.iacr.org/2016/660.pdf,
 * https://romulusae.github.io/romulus/
 */

#include <stddef.h>
#include <stdint.h>
#include "internal-skinny-plus-config.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief Size of a block for SKINNY-128-384+.
 */
#define SKINNY_PLUS_BLOCK_SIZE 16

/**
 * \brief Number of rounds for SKINNY-128-384+.
 */
#define SKINNY_PLUS_ROUNDS 40

/**
 * \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_PLUS_VARIANT == SKINNY_PLUS_VARIANT_TINY
    /** TK2 for the tiny key schedule */
    uint8_t TK2[16];

    /** TK3 for the tiny key schedule */
    uint8_t TK3[16];
#elif SKINNY_PLUS_VARIANT == SKINNY_PLUS_VARIANT_FULL
    /** Words of the full key schedule */
    uint32_t k[SKINNY_PLUS_ROUNDS * 4];
#else
    /** Words of the small key schedule */
    uint32_t k[SKINNY_PLUS_ROUNDS * 2];
#endif

} skinny_plus_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_plus_init
    (skinny_plus_key_schedule_t *ks, const unsigned char key[48]);

/**
 * \brief Initializes the key schedule for SKINNY-128-384+ without TK1.
 *
 * \param ks Points to the key schedule to initialize.
 * \param tk2 Points to the 16 bytes of key data for TK2.
 * \param tk3 Points to the 16 bytes of key data for TK3.
 */
void skinny_plus_init_without_tk1
    (skinny_plus_key_schedule_t *ks, const unsigned char *tk2,
     const unsigned char *tk3);

/**
 * \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_plus_encrypt
    (const skinny_plus_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 decryption.
 */
void skinny_plus_decrypt
    (const skinny_plus_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input);

/**
 * \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_plus_encrypt_tk_full
    (const unsigned char key[48], unsigned char *output,
     const unsigned char *input);

/**
 * \brief Decrypts 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 decryption.
 *
 * 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_plus_decrypt_tk_full
    (const unsigned char key[48], unsigned char *output,
     const unsigned char *input);

#ifdef __cplusplus
}
#endif

#endif