grain128.c 4.98 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
/*
 * 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.
 */

#include "grain128.h"
#include "internal-grain128.h"
#include <string.h>

aead_cipher_t const grain128_aead_cipher = {
    "Grain-128AEAD",
    GRAIN128_KEY_SIZE,
    GRAIN128_NONCE_SIZE,
    GRAIN128_TAG_SIZE,
    AEAD_FLAG_NONE,
    grain128_aead_encrypt,
    grain128_aead_decrypt
};

/**
 * \brief Encodes the associated data length in DER.
 *
 * \param buf The buffer to encode the length into.
 * \param adlen The length of the associated data in bytes, which must be
 * less than 2^32 to limit the length of the DER encoding to 5 bytes.
 *
 * \return The length of the DER encoding that was written to \a buf.
 */
static unsigned grain128_encode_adlen
    (unsigned char buf[5], unsigned long long adlen)
{
    if (adlen < 0x80U) {
        buf[0] = (unsigned char)adlen;
        return 1;
    } else if (adlen < 0x100U) {
        buf[0] = 0x81;
        buf[1] = (unsigned char)adlen;
        return 2;
    } else if (adlen < 0x10000U) {
        buf[0] = 0x82;
        buf[1] = (unsigned char)(adlen >> 8);
        buf[2] = (unsigned char)adlen;
        return 3;
    } else if (adlen < 0x1000000U) {
        buf[0] = 0x83;
        buf[1] = (unsigned char)(adlen >> 16);
        buf[2] = (unsigned char)(adlen >> 8);
        buf[3] = (unsigned char)adlen;
        return 4;
    } else {
        buf[0] = 0x84;
        buf[1] = (unsigned char)(adlen >> 24);
        buf[2] = (unsigned char)(adlen >> 16);
        buf[3] = (unsigned char)(adlen >> 8);
        buf[4] = (unsigned char)adlen;
        return 5;
    }
}

int grain128_aead_encrypt
    (unsigned char *c, unsigned long long *clen,
     const unsigned char *m, unsigned long long mlen,
     const unsigned char *ad, unsigned long long adlen,
     const unsigned char *nsec,
     const unsigned char *npub,
     const unsigned char *k)
{
    grain128_state_t state;
    unsigned char der[5];
    unsigned derlen;
    (void)nsec;

    /* Set the length of the returned ciphertext */
    *clen = mlen + GRAIN128_TAG_SIZE;

    /* Limit the amount of associated data to make DER encoding easier */
    if (adlen >= 0x100000000ULL)
        return -2;

    /* Initialize the Grain-128 stream cipher with the key and nonce */
    grain128_setup(&state, k, npub);

    /* Authenticate the associated data, prefixed with the DER-encoded length */
    derlen = grain128_encode_adlen(der, adlen);
    grain128_authenticate(&state, der, derlen);
    grain128_authenticate(&state, ad, adlen);

    /* Encrypt the plaintext to produce the ciphertext */
    grain128_encrypt(&state, c, m, mlen);

    /* Generate the authentication tag */
    grain128_compute_tag(&state);
    memcpy(c + mlen, state.ks, GRAIN128_TAG_SIZE);
    return 0;
}

int grain128_aead_decrypt
    (unsigned char *m, unsigned long long *mlen,
     unsigned char *nsec,
     const unsigned char *c, unsigned long long clen,
     const unsigned char *ad, unsigned long long adlen,
     const unsigned char *npub,
     const unsigned char *k)
{
    grain128_state_t state;
    unsigned char der[5];
    unsigned derlen;
    (void)nsec;

    /* Validate the ciphertext length and set the return "mlen" value */
    if (clen < GRAIN128_TAG_SIZE)
        return -1;
    *mlen = clen - GRAIN128_TAG_SIZE;

    /* Limit the amount of associated data to make DER encoding easier */
    if (adlen >= 0x100000000ULL)
        return -2;

    /* Initialize the Grain-128 stream cipher with the key and nonce */
    grain128_setup(&state, k, npub);

    /* Authenticate the associated data, prefixed with the DER-encoded length */
    derlen = grain128_encode_adlen(der, adlen);
    grain128_authenticate(&state, der, derlen);
    grain128_authenticate(&state, ad, adlen);

    /* Decrypt the ciphertext to produce the plaintext */
    clen -= GRAIN128_TAG_SIZE;
    grain128_decrypt(&state, m, c, clen);

    /* Check the authentication tag */
    grain128_compute_tag(&state);
    return aead_check_tag(m, clen, state.ks, c + clen, GRAIN128_TAG_SIZE);
}