2021-02-18 17:21:56 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Netflix Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-02-18 17:22:18 +00:00
|
|
|
#include <opencrypto/xform_auth.h>
|
2021-02-18 17:21:56 +00:00
|
|
|
#include <opencrypto/xform_enc.h>
|
|
|
|
|
2021-02-18 17:22:18 +00:00
|
|
|
#include <sodium/crypto_onetimeauth_poly1305.h>
|
|
|
|
#include <sodium/crypto_stream_chacha20.h>
|
|
|
|
|
2021-12-09 19:52:42 +00:00
|
|
|
struct chacha20_poly1305_ctx {
|
|
|
|
struct crypto_onetimeauth_poly1305_state auth;
|
2021-02-18 17:22:18 +00:00
|
|
|
const void *key;
|
|
|
|
uint32_t ic;
|
2021-10-06 21:08:49 +00:00
|
|
|
bool ietf;
|
2021-02-18 17:22:18 +00:00
|
|
|
char nonce[CHACHA20_POLY1305_IV_LEN];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
chacha20_poly1305_setkey(void *vctx, const uint8_t *key, int len)
|
|
|
|
{
|
2021-12-09 19:52:42 +00:00
|
|
|
struct chacha20_poly1305_ctx *ctx = vctx;
|
2021-02-18 17:22:18 +00:00
|
|
|
|
|
|
|
if (len != CHACHA20_POLY1305_KEY)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
ctx->key = key;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-06 21:08:46 +00:00
|
|
|
chacha20_poly1305_reinit(void *vctx, const uint8_t *iv, size_t ivlen)
|
2021-02-18 17:22:18 +00:00
|
|
|
{
|
2021-12-09 19:52:42 +00:00
|
|
|
struct chacha20_poly1305_ctx *ctx = vctx;
|
|
|
|
char block[CHACHA20_NATIVE_BLOCK_LEN];
|
2021-02-18 17:22:18 +00:00
|
|
|
|
2021-11-09 18:52:30 +00:00
|
|
|
KASSERT(ivlen == 8 || ivlen == sizeof(ctx->nonce),
|
2021-10-06 21:08:46 +00:00
|
|
|
("%s: invalid nonce length", __func__));
|
|
|
|
|
2021-10-06 21:08:49 +00:00
|
|
|
memcpy(ctx->nonce, iv, ivlen);
|
|
|
|
ctx->ietf = (ivlen == CHACHA20_POLY1305_IV_LEN);
|
2021-12-09 19:52:42 +00:00
|
|
|
|
|
|
|
/* Block 0 is used for the poly1305 key. */
|
|
|
|
if (ctx->ietf)
|
|
|
|
crypto_stream_chacha20_ietf(block, sizeof(block), iv, ctx->key);
|
|
|
|
else
|
|
|
|
crypto_stream_chacha20(block, sizeof(block), iv, ctx->key);
|
|
|
|
crypto_onetimeauth_poly1305_init(&ctx->auth, block);
|
|
|
|
explicit_bzero(block, sizeof(block));
|
|
|
|
|
|
|
|
/* Start with block 1 for ciphertext. */
|
2021-02-18 17:22:18 +00:00
|
|
|
ctx->ic = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chacha20_poly1305_crypt(void *vctx, const uint8_t *in, uint8_t *out)
|
|
|
|
{
|
2021-12-09 19:52:42 +00:00
|
|
|
struct chacha20_poly1305_ctx *ctx = vctx;
|
2021-12-05 22:23:47 +00:00
|
|
|
int error __diagused;
|
2021-02-18 17:22:18 +00:00
|
|
|
|
2021-10-06 21:08:49 +00:00
|
|
|
if (ctx->ietf)
|
|
|
|
error = crypto_stream_chacha20_ietf_xor_ic(out, in,
|
|
|
|
CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key);
|
|
|
|
else
|
|
|
|
error = crypto_stream_chacha20_xor_ic(out, in,
|
|
|
|
CHACHA20_NATIVE_BLOCK_LEN, ctx->nonce, ctx->ic, ctx->key);
|
2021-02-18 17:22:18 +00:00
|
|
|
KASSERT(error == 0, ("%s failed: %d", __func__, error));
|
|
|
|
ctx->ic++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chacha20_poly1305_crypt_last(void *vctx, const uint8_t *in, uint8_t *out,
|
|
|
|
size_t len)
|
|
|
|
{
|
2021-12-09 19:52:42 +00:00
|
|
|
struct chacha20_poly1305_ctx *ctx = vctx;
|
2021-02-18 17:22:18 +00:00
|
|
|
|
2021-12-05 22:23:47 +00:00
|
|
|
int error __diagused;
|
2021-02-18 17:22:18 +00:00
|
|
|
|
2021-10-06 21:08:49 +00:00
|
|
|
if (ctx->ietf)
|
|
|
|
error = crypto_stream_chacha20_ietf_xor_ic(out, in, len,
|
|
|
|
ctx->nonce, ctx->ic, ctx->key);
|
|
|
|
else
|
|
|
|
error = crypto_stream_chacha20_xor_ic(out, in, len, ctx->nonce,
|
|
|
|
ctx->ic, ctx->key);
|
2021-02-18 17:22:18 +00:00
|
|
|
KASSERT(error == 0, ("%s failed: %d", __func__, error));
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:52:42 +00:00
|
|
|
static int
|
|
|
|
chacha20_poly1305_update(void *vctx, const void *data, u_int len)
|
|
|
|
{
|
|
|
|
struct chacha20_poly1305_ctx *ctx = vctx;
|
|
|
|
|
|
|
|
crypto_onetimeauth_poly1305_update(&ctx->auth, data, len);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chacha20_poly1305_final(uint8_t *digest, void *vctx)
|
|
|
|
{
|
|
|
|
struct chacha20_poly1305_ctx *ctx = vctx;
|
|
|
|
|
|
|
|
crypto_onetimeauth_poly1305_final(&ctx->auth, digest);
|
|
|
|
}
|
|
|
|
|
2021-07-26 20:41:05 +00:00
|
|
|
const struct enc_xform enc_xform_chacha20_poly1305 = {
|
2021-02-18 17:21:56 +00:00
|
|
|
.type = CRYPTO_CHACHA20_POLY1305,
|
|
|
|
.name = "ChaCha20-Poly1305",
|
2021-12-09 19:52:42 +00:00
|
|
|
.ctxsize = sizeof(struct chacha20_poly1305_ctx),
|
2021-02-18 17:21:56 +00:00
|
|
|
.blocksize = 1,
|
2021-02-18 17:22:18 +00:00
|
|
|
.native_blocksize = CHACHA20_NATIVE_BLOCK_LEN,
|
2021-02-18 17:21:56 +00:00
|
|
|
.ivsize = CHACHA20_POLY1305_IV_LEN,
|
|
|
|
.minkey = CHACHA20_POLY1305_KEY,
|
|
|
|
.maxkey = CHACHA20_POLY1305_KEY,
|
2021-12-09 19:52:42 +00:00
|
|
|
.macsize = POLY1305_HASH_LEN,
|
2021-02-18 17:22:18 +00:00
|
|
|
.encrypt = chacha20_poly1305_crypt,
|
|
|
|
.decrypt = chacha20_poly1305_crypt,
|
|
|
|
.setkey = chacha20_poly1305_setkey,
|
|
|
|
.reinit = chacha20_poly1305_reinit,
|
|
|
|
.encrypt_last = chacha20_poly1305_crypt_last,
|
|
|
|
.decrypt_last = chacha20_poly1305_crypt_last,
|
2021-12-09 19:52:42 +00:00
|
|
|
.update = chacha20_poly1305_update,
|
|
|
|
.final = chacha20_poly1305_final,
|
2021-02-18 17:22:18 +00:00
|
|
|
};
|