internal-grain128.h 3.64 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
/*
 * 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_GRAIN128_H
#define LW_INTERNAL_GRAIN128_H

#include "internal-util.h"

/**
 * \file internal-grain128.h
 * \brief Internal implementation of the Grain-128 stream cipher.
 */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief Representation of the state of Grain-128.
 *
 * Note: The specification numbers bits starting with the most significant,
 * so bit 0 is in the highest bit of the first word of each field below.
 */
typedef struct
{
    uint32_t lfsr[4];       /**< 128-bit LFSR state for Grain-128 */
    uint32_t nfsr[4];       /**< 128-bit NFSR state for Grain-128 */
    uint64_t accum;         /**< 64-bit accumulator for authentication */
    uint64_t sr;            /**< 64-bit shift register for authentication */
    unsigned char ks[16];   /**< Keystream block for auth or encrypt mode */
    unsigned posn;          /**< Current position within the keystream */

} grain128_state_t;

/**
 * \brief Sets up the initial Grain-128 state with the key and nonce.
 *
 * \param state Grain-128 state to be initialized.
 * \param key Points to the 128-bit key.
 * \param nonce Points to the 96-bit nonce.
 */
void grain128_setup
    (grain128_state_t *state, const unsigned char *key,
     const unsigned char *nonce);

/**
 * \brief Authenticates data with Grain-128.
 *
 * \param state Grain-128 state.
 * \param data Points to the data to be authenticated.
 * \param len Length of the data to be authenticated.
 */
void grain128_authenticate
    (grain128_state_t *state, const unsigned char *data,
     unsigned long long len);

/**
 * \brief Encrypts and authenticates data with Grain-128.
 *
 * \param state Grain-128 state.
 * \param c Points to the ciphertext output buffer.
 * \param m Points to the plaintext input buffer.
 * \param len Length of the data to be encrypted.
 */
void grain128_encrypt
    (grain128_state_t *state, unsigned char *c, const unsigned char *m,
     unsigned long long len);

/**
 * \brief Decrypts and authenticates data with Grain-128.
 *
 * \param state Grain-128 state.
 * \param m Points to the plaintext output buffer.
 * \param c Points to the ciphertext input buffer.
 * \param len Length of the data to be decrypted.
 */
void grain128_decrypt
    (grain128_state_t *state, unsigned char *m, const unsigned char *c,
     unsigned long long len);

/**
 * \brief Computes the final authentiation tag.
 *
 * \param state Grain-128 state.
 *
 * The final authentication tag is written to the first 8 bytes of state->ks.
 */
void grain128_compute_tag(grain128_state_t *state);

#ifdef __cplusplus
}
#endif

#endif