2024-05-31 12:59:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024 Lucas Gabriel Vuotto <lucas@lgv5.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
2024-06-06 00:05:37 +02:00
|
|
|
#include <stdlib.h>
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
#include "lilcrypto.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
#include "impl_chacha20.h"
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
2024-06-07 20:52:46 +02:00
|
|
|
/*
|
|
|
|
* Implements ChaCha20 according to RFC 8439, XChaCha20 according to
|
|
|
|
* draft-irtf-cfrg-xchacha-03.
|
|
|
|
*/
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-07 20:52:46 +02:00
|
|
|
|
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
chacha20_anycrypt_init(void *arg, void *initparams)
|
2024-06-07 20:52:46 +02:00
|
|
|
{
|
2024-06-09 17:09:15 +02:00
|
|
|
struct lc_chacha20_params *params = initparams;
|
2024-06-15 23:13:31 +02:00
|
|
|
struct chacha20_state *state = arg;
|
2024-06-07 20:52:46 +02:00
|
|
|
size_t i;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-09 16:05:52 +02:00
|
|
|
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->s[i] = 0;
|
2024-05-31 12:59:58 +02:00
|
|
|
for (i = 0; i < CHACHA20_KEY_WORDS; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->k[i] = load32le(¶ms->key[i * 4]);
|
|
|
|
state->n[0] = params->counter;
|
2024-06-07 02:29:25 +02:00
|
|
|
for (i = 1; i < CHACHA20_NONCE_WORDS; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->n[i] = load32le(¶ms->nonce[(i - 1) * 4]);
|
|
|
|
state->mlen = 0;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:52:46 +02:00
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
xchacha20_anycrypt_init(void *arg, void *initparams)
|
2024-05-31 12:59:58 +02:00
|
|
|
{
|
2024-06-09 17:09:15 +02:00
|
|
|
struct lc_xchacha20_params *params = initparams;
|
2024-06-15 23:13:31 +02:00
|
|
|
struct chacha20_state *state = arg;
|
2024-06-09 17:09:15 +02:00
|
|
|
size_t i;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-09 16:05:52 +02:00
|
|
|
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->s[i] = 0;
|
2024-06-07 04:18:50 +02:00
|
|
|
for (i = 0; i < CHACHA20_KEY_WORDS; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->k[i] = load32le(¶ms->key[i * 4]);
|
2024-06-07 04:18:50 +02:00
|
|
|
for (i = 0; i < CHACHA20_NONCE_WORDS; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->n[i] = load32le(¶ms->nonce[i * 4]);
|
|
|
|
state->mlen = 0;
|
|
|
|
|
|
|
|
hchacha20_block(state);
|
|
|
|
|
|
|
|
state->k[0] = state->s[0];
|
|
|
|
state->k[1] = state->s[1];
|
|
|
|
state->k[2] = state->s[2];
|
|
|
|
state->k[3] = state->s[3];
|
|
|
|
state->k[4] = state->s[12];
|
|
|
|
state->k[5] = state->s[13];
|
|
|
|
state->k[6] = state->s[14];
|
|
|
|
state->k[7] = state->s[15];
|
|
|
|
state->n[0] = params->counter;
|
|
|
|
state->n[1] = 0;
|
|
|
|
state->n[2] = load32le(¶ms->nonce[16]);
|
|
|
|
state->n[3] = load32le(¶ms->nonce[20]);
|
2024-06-07 04:18:50 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:52:46 +02:00
|
|
|
static int
|
2024-06-08 15:39:49 +02:00
|
|
|
chacha20_anycrypt_update(void *arg, uint8_t *out, size_t *outlen,
|
2024-06-07 02:29:25 +02:00
|
|
|
const uint8_t *in, size_t inlen)
|
2024-05-31 12:59:58 +02:00
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct chacha20_state *state = arg;
|
2024-06-07 02:29:17 +02:00
|
|
|
size_t i, blocks;
|
2024-05-31 12:59:58 +02:00
|
|
|
uint32_t h;
|
|
|
|
|
|
|
|
*outlen = 0;
|
2024-06-15 23:13:31 +02:00
|
|
|
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) - state->mlen)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 0;
|
2024-06-15 23:13:31 +02:00
|
|
|
blocks = (inlen + state->mlen + LC_CHACHA20_BLOCKLEN - 1) /
|
2024-06-09 16:16:18 +02:00
|
|
|
LC_CHACHA20_BLOCKLEN;
|
2024-06-15 23:13:31 +02:00
|
|
|
if (blocks + state->n[0] > CHACHA20_CTRMAX)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 0;
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
*outlen = state->mlen + inlen -
|
|
|
|
((state->mlen + inlen) % LC_CHACHA20_BLOCKLEN);
|
2024-06-07 02:29:17 +02:00
|
|
|
if (out == NULL)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 1;
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
for (i = 0; i + state->mlen < LC_CHACHA20_BLOCKLEN && i < inlen; i++)
|
|
|
|
state->m[i + state->mlen] = in[i];
|
|
|
|
state->mlen += i;
|
2024-06-07 02:29:17 +02:00
|
|
|
in += i;
|
|
|
|
inlen -= i;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
if (state->mlen == LC_CHACHA20_BLOCKLEN) {
|
|
|
|
chacha20_block(state);
|
|
|
|
state->n[0]++;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-09 16:05:52 +02:00
|
|
|
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++) {
|
2024-06-15 23:13:31 +02:00
|
|
|
h = load32le(&state->m[i * 4]);
|
|
|
|
h ^= state->s[i];
|
2024-06-07 02:29:17 +02:00
|
|
|
store32le(&out[i * 4], h);
|
|
|
|
}
|
2024-06-09 16:16:18 +02:00
|
|
|
out += LC_CHACHA20_BLOCKLEN;
|
2024-06-15 23:13:31 +02:00
|
|
|
state->mlen = 0;
|
2024-05-31 12:59:58 +02:00
|
|
|
}
|
|
|
|
|
2024-06-07 02:29:17 +02:00
|
|
|
if (inlen == 0)
|
|
|
|
return 1;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-09 16:16:18 +02:00
|
|
|
while (inlen >= LC_CHACHA20_BLOCKLEN) {
|
2024-06-15 23:13:31 +02:00
|
|
|
chacha20_block(state);
|
|
|
|
state->n[0]++;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-09 16:05:52 +02:00
|
|
|
for (i = 0; i < CHACHA20_BLOCKLEN_WORDS; i++) {
|
2024-05-31 12:59:58 +02:00
|
|
|
h = load32le(&in[i * 4]);
|
2024-06-15 23:13:31 +02:00
|
|
|
h ^= state->s[i];
|
2024-05-31 12:59:58 +02:00
|
|
|
store32le(&out[i * 4], h);
|
|
|
|
}
|
2024-06-09 16:16:18 +02:00
|
|
|
out += LC_CHACHA20_BLOCKLEN;
|
|
|
|
in += LC_CHACHA20_BLOCKLEN;
|
|
|
|
inlen -= LC_CHACHA20_BLOCKLEN;
|
2024-05-31 12:59:58 +02:00
|
|
|
}
|
|
|
|
|
2024-06-07 02:29:17 +02:00
|
|
|
for (i = 0; i < inlen; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->m[i] = in[i];
|
|
|
|
state->mlen = inlen;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:52:46 +02:00
|
|
|
static int
|
2024-06-08 15:39:49 +02:00
|
|
|
chacha20_anycrypt_final(void *arg, uint8_t *out, size_t *outlen)
|
2024-05-31 12:59:58 +02:00
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct chacha20_state *state = arg;
|
2024-06-07 02:29:17 +02:00
|
|
|
size_t i, off;
|
|
|
|
uint32_t h;
|
|
|
|
uint8_t s[4];
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
*outlen = state->mlen;
|
2024-06-07 02:29:17 +02:00
|
|
|
if (out == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
if (state->mlen > 0)
|
|
|
|
chacha20_block(state);
|
2024-06-07 02:29:17 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
for (i = 0; i < state->mlen / 4; i++) {
|
|
|
|
h = load32le(&state->m[i * 4]);
|
|
|
|
h ^= state->s[i];
|
2024-06-07 02:29:17 +02:00
|
|
|
store32le(&out[i * 4], h);
|
|
|
|
}
|
|
|
|
off = i * 4;
|
2024-06-15 23:13:31 +02:00
|
|
|
state->mlen -= off;
|
2024-06-07 02:29:17 +02:00
|
|
|
out += off;
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
store32le(&s[0], state->s[i]);
|
|
|
|
for (i = 0; i < state->mlen; i++)
|
|
|
|
out[i] = state->m[i + off] ^ s[i];
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
lc_scrub(state, sizeof(*state));
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:52:46 +02:00
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
chacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
|
2024-06-07 20:52:46 +02:00
|
|
|
const uint8_t *in, size_t inlen)
|
2024-05-31 12:59:58 +02:00
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct chacha20_state state;
|
2024-05-31 12:59:58 +02:00
|
|
|
size_t l0, l1;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
*outlen = 0;
|
|
|
|
|
2024-06-09 16:16:18 +02:00
|
|
|
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) ||
|
|
|
|
(inlen + LC_CHACHA20_BLOCKLEN - 1) / LC_CHACHA20_BLOCKLEN >
|
2024-06-09 16:05:52 +02:00
|
|
|
CHACHA20_CTRMAX)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
*outlen = inlen;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
rc = chacha20_anycrypt_init(&state, initparams) &&
|
|
|
|
chacha20_anycrypt_update(&state, out, &l0, in, inlen) &&
|
|
|
|
chacha20_anycrypt_final(&state, out + l0, &l1);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
*outlen = l0 + l1;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-06-11 05:07:53 +02:00
|
|
|
static int
|
|
|
|
xchacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
|
|
|
|
const uint8_t *in, size_t inlen)
|
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct chacha20_state state;
|
2024-06-11 05:07:53 +02:00
|
|
|
size_t l0, l1;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
*outlen = 0;
|
|
|
|
|
|
|
|
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) ||
|
|
|
|
(inlen + LC_CHACHA20_BLOCKLEN - 1) / LC_CHACHA20_BLOCKLEN >
|
|
|
|
CHACHA20_CTRMAX)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
*outlen = inlen;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
rc = xchacha20_anycrypt_init(&state, initparams) &&
|
|
|
|
chacha20_anycrypt_update(&state, out, &l0, in, inlen) &&
|
|
|
|
chacha20_anycrypt_final(&state, out + l0, &l1);
|
2024-06-11 05:07:53 +02:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
*outlen = l0 + l1;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
static struct lc_cipher_impl chacha20_impl = {
|
2024-06-08 15:39:49 +02:00
|
|
|
.encrypt_init = &chacha20_anycrypt_init,
|
|
|
|
.encrypt_update = &chacha20_anycrypt_update,
|
|
|
|
.encrypt_final = &chacha20_anycrypt_final,
|
|
|
|
.encrypt = &chacha20_anycrypt,
|
2024-06-07 02:29:25 +02:00
|
|
|
|
2024-06-08 15:39:49 +02:00
|
|
|
.decrypt_init = &chacha20_anycrypt_init,
|
|
|
|
.decrypt_update = &chacha20_anycrypt_update,
|
|
|
|
.decrypt_final = &chacha20_anycrypt_final,
|
|
|
|
.decrypt = &chacha20_anycrypt,
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
.argsz = sizeof(struct chacha20_state),
|
2024-06-10 19:23:48 +02:00
|
|
|
.blocklen = LC_CHACHA20_BLOCKLEN,
|
2024-05-31 12:59:58 +02:00
|
|
|
};
|
|
|
|
|
2024-06-07 04:18:50 +02:00
|
|
|
static struct lc_cipher_impl xchacha20_impl = {
|
2024-06-08 15:39:49 +02:00
|
|
|
.encrypt_init = &xchacha20_anycrypt_init,
|
|
|
|
.encrypt_update = &chacha20_anycrypt_update,
|
|
|
|
.encrypt_final = &chacha20_anycrypt_final,
|
2024-06-11 05:07:53 +02:00
|
|
|
.encrypt = &xchacha20_anycrypt,
|
2024-06-08 15:39:49 +02:00
|
|
|
|
|
|
|
.decrypt_init = &xchacha20_anycrypt_init,
|
|
|
|
.decrypt_update = &chacha20_anycrypt_update,
|
|
|
|
.decrypt_final = &chacha20_anycrypt_final,
|
2024-06-11 05:07:53 +02:00
|
|
|
.decrypt = &xchacha20_anycrypt,
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
.argsz = sizeof(struct chacha20_state),
|
2024-06-10 19:23:48 +02:00
|
|
|
.blocklen = LC_XCHACHA20_BLOCKLEN,
|
2024-06-07 04:18:50 +02:00
|
|
|
};
|
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
const struct lc_cipher_impl *
|
|
|
|
lc_cipher_impl_chacha20(void)
|
|
|
|
{
|
|
|
|
return &chacha20_impl;
|
|
|
|
}
|
2024-06-07 04:18:50 +02:00
|
|
|
|
|
|
|
const struct lc_cipher_impl *
|
|
|
|
lc_cipher_impl_xchacha20(void)
|
|
|
|
{
|
|
|
|
return &xchacha20_impl;
|
|
|
|
}
|