internal-gift128.h 4.48 KB
Newer Older
Rhys Weatherley committed
1
/*
2
 * Copyright (C) 2021 Southern Storm Software, Pty Ltd.
Rhys Weatherley committed
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
 *
 * 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_GIFT128_H
#define LW_INTERNAL_GIFT128_H

/**
 * \file internal-gift128.h
 * \brief GIFT-128 block cipher.
 *
30 31
 * This version of GIFT-128 implements a cutdown form of the bit-sliced
 * representation of GIFT-128 that is used by GIFT-COFB.
Rhys Weatherley committed
32
 *
33 34 35
 * Only initialization and block encryption are supported, and the
 * encryption function assumes that the input data was already converted
 * from big endian to host byte order by the caller.
Rhys Weatherley committed
36 37
 *
 * References: https://eprint.iacr.org/2017/622.pdf,
38
 * https://eprint.iacr.org/2020/412.pdf,
Rhys Weatherley committed
39 40 41 42 43
 * https://giftcipher.github.io/gift/
 */

#include <stddef.h>
#include <stdint.h>
44
#include "internal-gift128-config.h"
Rhys Weatherley committed
45 46 47 48 49 50 51 52

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief Size of a GIFT-128 block in bytes.
 */
53
#define GIFT128_BLK_SIZE 16
Rhys Weatherley committed
54 55

/**
56 57
 * \var GIFT128_ROUND_KEYS
 * \brief Number of round keys for the GIFT-128 key schedule.
Rhys Weatherley committed
58
 */
59 60 61 62 63
#if GIFT128_VARIANT == GIFT128_VARIANT_TINY
#define GIFT128_ROUND_KEYS 4
#elif GIFT128_VARIANT == GIFT128_VARIANT_SMALL
#define GIFT128_ROUND_KEYS 20
#else
Rhys Weatherley committed
64
#define GIFT128_ROUND_KEYS 80
65
#endif
Rhys Weatherley committed
66 67 68 69 70 71

/**
 * \brief Structure of the key schedule for GIFT-128 (bit-sliced).
 */
typedef struct
{
72
    /** Pre-computed round keys for bit-sliced GIFT-128 */
Rhys Weatherley committed
73 74 75 76 77 78 79 80
    uint32_t k[GIFT128_ROUND_KEYS];

} gift128b_key_schedule_t;

/**
 * \brief Initializes the key schedule for GIFT-128 (bit-sliced).
 *
 * \param ks Points to the key schedule to initialize.
81
 * \param key Points to the 16 bytes of the key data.
Rhys Weatherley committed
82
 */
83
void gift128b_init(gift128b_key_schedule_t *ks, const unsigned char *key);
Rhys Weatherley committed
84 85 86 87 88 89 90 91 92 93 94

/**
 * \brief Encrypts a 128-bit block with GIFT-128 (bit-sliced and pre-loaded).
 *
 * \param ks Points to the GIFT-128 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.
 *
95
 * This function assumes that the input has already been pre-loaded from
Rhys Weatherley committed
96 97 98 99 100 101 102 103
 * big-endian into host byte order in the supplied word array.  The output
 * is delivered in the same way.
 */
void gift128b_encrypt_preloaded
    (const gift128b_key_schedule_t *ks, uint32_t output[4],
     const uint32_t input[4]);

/**
104
 * \brief Decrypts a 128-bit block with GIFT-128 (bit-sliced and pre-loaded).
Rhys Weatherley committed
105 106 107 108 109 110 111 112
 *
 * \param ks Points to the GIFT-128 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.
 *
113 114 115
 * This function assumes that the input has already been pre-loaded from
 * big-endian into host byte order in the supplied word array.  The output
 * is delivered in the same way.
Rhys Weatherley committed
116
 */
117 118 119
void gift128b_decrypt_preloaded
    (const gift128b_key_schedule_t *ks, uint32_t output[4],
     const uint32_t input[4]);
120

Rhys Weatherley committed
121
/**
122
 * \brief Converts the GIFT-128 nibble-based representation into word-based.
Rhys Weatherley committed
123
 *
124
 * \param block Block to convert from nibble-based to word-based.
Rhys Weatherley committed
125
 *
126
 * \sa gift128_words_to_nibbles()
Rhys Weatherley committed
127
 */
128
void gift128_nibbles_to_words(uint32_t block[4]);
Rhys Weatherley committed
129 130

/**
131
 * \brief Converts the GIFT-128 word-based representation into nibble-based.
Rhys Weatherley committed
132
 *
133
 * \param block Block to convert from word-based to nibble-based.
Rhys Weatherley committed
134
 *
135
 * \sa gift128_nibbles_to_words()
Rhys Weatherley committed
136
 */
137
void gift128_words_to_nibbles(uint32_t block[4]);
Rhys Weatherley committed
138 139 140 141 142 143

#ifdef __cplusplus
}
#endif

#endif