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-08 15:39:49 +02:00
|
|
|
chacha20_anycrypt_init(void *arg, const void *initparams)
|
2024-06-07 20:52:46 +02:00
|
|
|
{
|
|
|
|
const struct lc_chacha20_params *params = initparams;
|
|
|
|
struct chacha20_ctx *ctx = arg;
|
|
|
|
size_t i;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
for (i = 0; i < CHACHA20_CHUNK_WORDS; i++)
|
|
|
|
ctx->s[i] = 0;
|
|
|
|
for (i = 0; i < CHACHA20_KEY_WORDS; i++)
|
2024-06-07 20:52:46 +02:00
|
|
|
ctx->k[i] = load32le(¶ms->key[i * 4]);
|
|
|
|
ctx->n[0] = params->counter;
|
2024-06-07 02:29:25 +02:00
|
|
|
for (i = 1; i < CHACHA20_NONCE_WORDS; i++)
|
2024-06-07 20:52:46 +02:00
|
|
|
ctx->n[i] = load32le(¶ms->nonce[(i - 1) * 4]);
|
2024-06-07 02:29:17 +02:00
|
|
|
ctx->mlen = 0;
|
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
|
|
|
xchacha20_anycrypt_init(void *arg, const void *initparams)
|
2024-05-31 12:59:58 +02:00
|
|
|
{
|
2024-06-07 20:52:46 +02:00
|
|
|
const struct lc_xchacha20_params *params = initparams;
|
|
|
|
struct chacha20_ctx *ctx = arg;
|
|
|
|
size_t i;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
|
|
|
for (i = 0; i < CHACHA20_CHUNK_WORDS; i++)
|
|
|
|
ctx->s[i] = 0;
|
|
|
|
for (i = 0; i < CHACHA20_KEY_WORDS; i++)
|
2024-06-07 20:52:46 +02:00
|
|
|
ctx->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-07 20:52:46 +02:00
|
|
|
ctx->n[i] = load32le(¶ms->nonce[i * 4]);
|
2024-06-07 04:18:50 +02:00
|
|
|
ctx->mlen = 0;
|
|
|
|
|
|
|
|
hchacha20_block(ctx);
|
|
|
|
|
|
|
|
ctx->k[0] = ctx->s[0];
|
|
|
|
ctx->k[1] = ctx->s[1];
|
|
|
|
ctx->k[2] = ctx->s[2];
|
|
|
|
ctx->k[3] = ctx->s[3];
|
|
|
|
ctx->k[4] = ctx->s[12];
|
|
|
|
ctx->k[5] = ctx->s[13];
|
|
|
|
ctx->k[6] = ctx->s[14];
|
|
|
|
ctx->k[7] = ctx->s[15];
|
2024-06-07 20:52:46 +02:00
|
|
|
ctx->n[0] = params->counter;
|
|
|
|
ctx->n[1] = 0;
|
|
|
|
ctx->n[2] = load32le(¶ms->nonce[16]);
|
|
|
|
ctx->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
|
|
|
{
|
|
|
|
struct chacha20_ctx *ctx = 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-07 02:29:17 +02:00
|
|
|
if (inlen > SIZE_MAX - (CHACHA20_CHUNK - 1) - ctx->mlen)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 0;
|
2024-06-07 02:29:17 +02:00
|
|
|
blocks = (inlen + ctx->mlen + CHACHA20_CHUNK - 1) / CHACHA20_CHUNK;
|
2024-06-07 02:29:25 +02:00
|
|
|
if (blocks + ctx->n[0] > CHACHA20_CTRMAX)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 0;
|
|
|
|
|
2024-06-07 02:29:25 +02:00
|
|
|
*outlen = ctx->mlen + inlen - ((ctx->mlen + inlen) % CHACHA20_CHUNK);
|
2024-06-07 02:29:17 +02:00
|
|
|
if (out == NULL)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 1;
|
|
|
|
|
2024-06-07 02:29:17 +02:00
|
|
|
for (i = 0; i + ctx->mlen < CHACHA20_CHUNK && i < inlen; i++)
|
|
|
|
ctx->m[i + ctx->mlen] = in[i];
|
|
|
|
ctx->mlen += i;
|
|
|
|
in += i;
|
|
|
|
inlen -= i;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-07 02:29:17 +02:00
|
|
|
if (ctx->mlen == CHACHA20_CHUNK) {
|
|
|
|
chacha20_block(ctx);
|
2024-06-07 02:29:25 +02:00
|
|
|
ctx->n[0]++;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-07 02:29:17 +02:00
|
|
|
for (i = 0; i < CHACHA20_CHUNK_WORDS; i++) {
|
|
|
|
h = load32le(&ctx->m[i * 4]);
|
|
|
|
h ^= ctx->s[i];
|
|
|
|
store32le(&out[i * 4], h);
|
|
|
|
}
|
|
|
|
out += CHACHA20_CHUNK;
|
|
|
|
ctx->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
|
|
|
|
|
|
|
while (inlen >= CHACHA20_CHUNK) {
|
|
|
|
chacha20_block(ctx);
|
2024-06-07 02:29:25 +02:00
|
|
|
ctx->n[0]++;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
for (i = 0; i < CHACHA20_CHUNK_WORDS; i++) {
|
|
|
|
h = load32le(&in[i * 4]);
|
|
|
|
h ^= ctx->s[i];
|
|
|
|
store32le(&out[i * 4], h);
|
|
|
|
}
|
|
|
|
out += CHACHA20_CHUNK;
|
|
|
|
in += CHACHA20_CHUNK;
|
|
|
|
inlen -= CHACHA20_CHUNK;
|
|
|
|
}
|
|
|
|
|
2024-06-07 02:29:17 +02:00
|
|
|
for (i = 0; i < inlen; i++)
|
|
|
|
ctx->m[i] = in[i];
|
|
|
|
ctx->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
|
|
|
{
|
|
|
|
struct chacha20_ctx *ctx = arg;
|
2024-06-07 02:29:17 +02:00
|
|
|
size_t i, off;
|
|
|
|
uint32_t h;
|
|
|
|
uint8_t s[4];
|
|
|
|
|
|
|
|
*outlen = ctx->mlen;
|
|
|
|
if (out == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (ctx->mlen > 0)
|
|
|
|
chacha20_block(ctx);
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->mlen / 4; i++) {
|
|
|
|
h = load32le(&ctx->m[i * 4]);
|
|
|
|
h ^= ctx->s[i];
|
|
|
|
store32le(&out[i * 4], h);
|
|
|
|
}
|
|
|
|
off = i * 4;
|
|
|
|
ctx->mlen -= off;
|
|
|
|
out += off;
|
|
|
|
|
|
|
|
store32le(&s[0], ctx->s[i]);
|
|
|
|
for (i = 0; i < ctx->mlen; i++)
|
|
|
|
out[i] = ctx->m[i + off] ^ s[i];
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
lc_scrub(ctx, sizeof(*ctx));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:52:46 +02:00
|
|
|
static int
|
2024-06-08 15:39:49 +02:00
|
|
|
chacha20_anycrypt(uint8_t *out, size_t *outlen, const 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
|
|
|
{
|
|
|
|
struct chacha20_ctx ctx;
|
|
|
|
size_t l0, l1;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
*outlen = 0;
|
|
|
|
|
|
|
|
if (inlen > SIZE_MAX - (CHACHA20_CHUNK - 1) ||
|
|
|
|
(inlen + CHACHA20_CHUNK - 1) / CHACHA20_CHUNK > CHACHA20_CTRMAX)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
*outlen = inlen;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-08 15:39:49 +02:00
|
|
|
rc = chacha20_anycrypt_init(&ctx, initparams) &&
|
|
|
|
chacha20_anycrypt_update(&ctx, out, &l0, in, inlen) &&
|
|
|
|
chacha20_anycrypt_final(&ctx, out + l0, &l1);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
*outlen = l0 + l1;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-06-06 00:05:37 +02:00
|
|
|
static void *
|
2024-06-06 13:45:30 +02:00
|
|
|
chacha20_ctx_new(void)
|
2024-06-06 00:05:37 +02:00
|
|
|
{
|
|
|
|
return malloc(sizeof(struct chacha20_ctx));
|
|
|
|
}
|
|
|
|
|
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-06 00:05:37 +02:00
|
|
|
.ctx_new = &chacha20_ctx_new,
|
|
|
|
.ctx_free = NULL,
|
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,
|
|
|
|
.encrypt = &chacha20_anycrypt,
|
|
|
|
|
|
|
|
.decrypt_init = &xchacha20_anycrypt_init,
|
|
|
|
.decrypt_update = &chacha20_anycrypt_update,
|
|
|
|
.decrypt_final = &chacha20_anycrypt_final,
|
|
|
|
.decrypt = &chacha20_anycrypt,
|
2024-06-07 04:18:50 +02:00
|
|
|
|
|
|
|
.ctx_new = &chacha20_ctx_new,
|
|
|
|
.ctx_free = NULL,
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|