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.
|
|
|
|
*/
|
|
|
|
|
2024-06-17 23:52:07 +02:00
|
|
|
#include "internal.h"
|
2024-05-31 12:59:58 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2024-06-07 19:47:42 +02:00
|
|
|
* Implements ChaCha20-Poly1305 according to RFC 8439, XChaCha20-Poly1305
|
|
|
|
* according to draft-irtf-cfrg-xchacha-03.
|
2024-05-31 12:59:58 +02:00
|
|
|
*/
|
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
static int
|
2024-06-19 16:02:54 +02:00
|
|
|
poly1305_keysetup(struct lc_cipher_ctx *cctx,
|
2024-06-09 17:09:15 +02:00
|
|
|
uint8_t akey[LC_POLY1305_KEYLEN], void *initparams)
|
2024-06-08 01:29:29 +02:00
|
|
|
{
|
2024-06-19 16:02:54 +02:00
|
|
|
size_t akeylen;
|
2024-06-08 01:29:29 +02:00
|
|
|
|
2024-06-19 16:02:54 +02:00
|
|
|
return lc_cipher_encrypt(cctx->impl, akey, &akeylen, initparams,
|
|
|
|
zerobuf, LC_POLY1305_KEYLEN) && akeylen == LC_POLY1305_KEYLEN;
|
2024-06-08 01:29:29 +02:00
|
|
|
}
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
chacha20_poly1305_seal(uint8_t *out, size_t *outlen, void *initparams,
|
2024-06-08 01:29:29 +02:00
|
|
|
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
|
|
|
|
{
|
2024-06-09 17:09:15 +02:00
|
|
|
struct lc_chacha20_poly1305_params *params = initparams;
|
|
|
|
struct lc_cipher_ctx *cctx = NULL;
|
|
|
|
struct lc_auth_ctx *actx = NULL;
|
|
|
|
struct lc_chacha20_params cparams;
|
|
|
|
struct lc_poly1305_params aparams;
|
|
|
|
uint8_t buf[sizeof(uint64_t) * 2];
|
|
|
|
size_t i, olen;
|
|
|
|
int ret = 0;
|
2024-06-08 01:29:29 +02:00
|
|
|
|
|
|
|
*outlen = 0;
|
|
|
|
/* inlen and aadlen are capped by design; enough space of tag. */
|
2024-05-31 12:59:58 +02:00
|
|
|
if (inlen > UINT64_MAX || aadlen > UINT64_MAX ||
|
2024-06-08 01:29:29 +02:00
|
|
|
inlen > SIZE_MAX - LC_POLY1305_TAGLEN)
|
|
|
|
return 0;
|
|
|
|
/* Counter 0 is used for deriving Poly1305 key. */
|
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-08 01:29:29 +02:00
|
|
|
CHACHA20_CTRMAX - 1)
|
2024-05-31 12:59:58 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
*outlen = inlen + LC_POLY1305_TAGLEN;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cctx = lc_cipher_ctx_new(lc_cipher_impl_chacha20());
|
|
|
|
if (cctx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
actx = lc_auth_ctx_new(lc_auth_impl_poly1305());
|
|
|
|
if (actx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(params->key); i++)
|
|
|
|
cparams.key[i] = params->key[i];
|
|
|
|
for (i = 0; i < sizeof(params->nonce); i++)
|
|
|
|
cparams.nonce[i] = params->nonce[i];
|
|
|
|
|
|
|
|
cparams.counter = 0;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!poly1305_keysetup(cctx, aparams.key, &cparams))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!lc_auth_init(actx, &aparams) ||
|
|
|
|
!lc_auth_update(actx, aad, aadlen))
|
|
|
|
goto cleanup;
|
2024-05-31 12:59:58 +02:00
|
|
|
if (aadlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (aadlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cparams.counter = 1;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!lc_cipher_encrypt(cctx->impl, out, outlen, &cparams, in, inlen))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!lc_auth_update(actx, out, inlen))
|
|
|
|
goto cleanup;
|
2024-05-31 12:59:58 +02:00
|
|
|
if (inlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (inlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
store64le(&buf[0], aadlen);
|
|
|
|
store64le(&buf[sizeof(uint64_t)], inlen);
|
2024-06-08 01:29:29 +02:00
|
|
|
if (!lc_auth_update(actx, buf, sizeof(buf)) ||
|
|
|
|
!lc_auth_final(actx, out + inlen, &olen))
|
|
|
|
goto cleanup;
|
|
|
|
*outlen += olen;
|
|
|
|
if (*outlen != inlen + LC_POLY1305_TAGLEN)
|
|
|
|
goto cleanup;
|
|
|
|
ret = 1;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cleanup:
|
2024-05-31 12:59:58 +02:00
|
|
|
lc_scrub(buf, sizeof(buf));
|
2024-06-08 01:29:29 +02:00
|
|
|
lc_scrub(&aparams, sizeof(aparams));
|
|
|
|
lc_scrub(&cparams, sizeof(cparams));
|
|
|
|
lc_auth_ctx_free(actx);
|
|
|
|
lc_cipher_ctx_free(cctx);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
return ret;
|
2024-05-31 12:59:58 +02:00
|
|
|
}
|
|
|
|
|
2024-05-31 18:46:05 +02:00
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
xchacha20_poly1305_seal(uint8_t *out, size_t *outlen, void *initparams,
|
2024-06-08 01:29:29 +02:00
|
|
|
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
|
2024-05-31 18:46:05 +02:00
|
|
|
{
|
2024-06-09 17:09:15 +02:00
|
|
|
struct lc_xchacha20_poly1305_params *params = initparams;
|
|
|
|
struct lc_cipher_ctx *cctx = NULL;
|
|
|
|
struct lc_auth_ctx *actx = NULL;
|
|
|
|
struct lc_xchacha20_params cparams;
|
|
|
|
struct lc_poly1305_params aparams;
|
|
|
|
uint8_t buf[sizeof(uint64_t) * 2];
|
|
|
|
size_t i, olen;
|
|
|
|
int ret = 0;
|
2024-05-31 18:46:05 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
*outlen = 0;
|
|
|
|
/* inlen and aadlen are capped by design; enough space of tag. */
|
|
|
|
if (inlen > UINT64_MAX || aadlen > UINT64_MAX ||
|
|
|
|
inlen > SIZE_MAX - LC_POLY1305_TAGLEN)
|
|
|
|
return 0;
|
|
|
|
/* Counter 0 is used for deriving Poly1305 key. */
|
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-08 01:29:29 +02:00
|
|
|
CHACHA20_CTRMAX - 1)
|
2024-05-31 18:46:05 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
2024-06-08 01:29:29 +02:00
|
|
|
*outlen = inlen + LC_POLY1305_TAGLEN;
|
2024-05-31 18:46:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cctx = lc_cipher_ctx_new(lc_cipher_impl_xchacha20());
|
|
|
|
if (cctx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
actx = lc_auth_ctx_new(lc_auth_impl_poly1305());
|
|
|
|
if (actx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(params->key); i++)
|
|
|
|
cparams.key[i] = params->key[i];
|
|
|
|
for (i = 0; i < sizeof(params->nonce); i++)
|
|
|
|
cparams.nonce[i] = params->nonce[i];
|
|
|
|
|
|
|
|
cparams.counter = 0;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!poly1305_keysetup(cctx, aparams.key, &cparams))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!lc_auth_init(actx, &aparams) ||
|
|
|
|
!lc_auth_update(actx, aad, aadlen))
|
|
|
|
goto cleanup;
|
2024-05-31 18:46:05 +02:00
|
|
|
if (aadlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (aadlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-05-31 18:46:05 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cparams.counter = 1;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!lc_cipher_encrypt(cctx->impl, out, outlen, &cparams, in, inlen))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-05-31 18:46:05 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
if (!lc_auth_update(actx, out, inlen))
|
|
|
|
goto cleanup;
|
|
|
|
if (inlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (inlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-05-31 18:46:05 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
store64le(&buf[0], aadlen);
|
|
|
|
store64le(&buf[sizeof(uint64_t)], inlen);
|
|
|
|
if (!lc_auth_update(actx, buf, sizeof(buf)) ||
|
|
|
|
!lc_auth_final(actx, out + inlen, &olen))
|
|
|
|
goto cleanup;
|
|
|
|
*outlen += olen;
|
|
|
|
if (*outlen != inlen + LC_POLY1305_TAGLEN)
|
|
|
|
goto cleanup;
|
|
|
|
ret = 1;
|
2024-05-31 18:46:05 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cleanup:
|
2024-05-31 18:46:05 +02:00
|
|
|
lc_scrub(buf, sizeof(buf));
|
2024-06-08 01:29:29 +02:00
|
|
|
lc_scrub(&aparams, sizeof(aparams));
|
|
|
|
lc_scrub(&cparams, sizeof(cparams));
|
|
|
|
lc_auth_ctx_free(actx);
|
|
|
|
lc_cipher_ctx_free(cctx);
|
2024-05-31 18:46:05 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
return ret;
|
2024-05-31 18:46:05 +02:00
|
|
|
}
|
|
|
|
|
2024-06-07 04:18:50 +02:00
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
chacha20_poly1305_open(uint8_t *out, size_t *outlen, void *initparams,
|
2024-06-08 01:29:29 +02:00
|
|
|
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
|
2024-06-07 04:18:50 +02:00
|
|
|
{
|
2024-06-09 17:09:15 +02:00
|
|
|
struct lc_chacha20_poly1305_params *params = initparams;
|
|
|
|
struct lc_cipher_ctx *cctx = NULL;
|
|
|
|
struct lc_auth_ctx *actx = NULL;
|
|
|
|
struct lc_chacha20_params cparams;
|
|
|
|
struct lc_poly1305_params aparams;
|
|
|
|
uint8_t tag[LC_POLY1305_TAGLEN];
|
|
|
|
uint8_t buf[sizeof(uint64_t) * 2];
|
|
|
|
size_t i, olen, ctlen;
|
|
|
|
int ret = 0;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
*outlen = 0;
|
|
|
|
/* inlen includes the tag; inlen and aadlen are capped by design. */
|
|
|
|
if (inlen < LC_POLY1305_TAGLEN ||
|
|
|
|
inlen > UINT64_MAX || aadlen > UINT64_MAX)
|
|
|
|
return 0;
|
|
|
|
/* Counter 0 is used for deriving Poly1305 key. */
|
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-07 04:18:50 +02:00
|
|
|
CHACHA20_CTRMAX - 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out == NULL) {
|
2024-06-08 01:29:29 +02:00
|
|
|
*outlen = inlen - LC_POLY1305_TAGLEN;
|
2024-06-07 04:18:50 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cctx = lc_cipher_ctx_new(lc_cipher_impl_chacha20());
|
|
|
|
if (cctx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
actx = lc_auth_ctx_new(lc_auth_impl_poly1305());
|
|
|
|
if (actx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(params->key); i++)
|
|
|
|
cparams.key[i] = params->key[i];
|
|
|
|
for (i = 0; i < sizeof(params->nonce); i++)
|
|
|
|
cparams.nonce[i] = params->nonce[i];
|
|
|
|
|
|
|
|
cparams.counter = 0;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!poly1305_keysetup(cctx, aparams.key, &cparams))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!lc_auth_init(actx, &aparams) ||
|
|
|
|
!lc_auth_update(actx, aad, aadlen))
|
|
|
|
goto cleanup;
|
2024-06-07 04:18:50 +02:00
|
|
|
if (aadlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (aadlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
ctlen = inlen - LC_POLY1305_TAGLEN;
|
|
|
|
if (!lc_auth_update(actx, in, ctlen))
|
|
|
|
goto cleanup;
|
|
|
|
if (ctlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (ctlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
|
|
|
store64le(&buf[0], aadlen);
|
2024-06-08 01:29:29 +02:00
|
|
|
store64le(&buf[sizeof(uint64_t)], ctlen);
|
|
|
|
if (!lc_auth_update(actx, buf, sizeof(buf)) ||
|
|
|
|
!lc_auth_final(actx, tag, &olen))
|
|
|
|
goto cleanup;
|
|
|
|
if (olen != LC_POLY1305_TAGLEN)
|
|
|
|
goto cleanup;
|
|
|
|
if (!lc_ct_cmp(tag, in + ctlen, LC_POLY1305_TAGLEN))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
cparams.counter = 1;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!lc_cipher_decrypt(cctx->impl, out, outlen, &cparams, in, ctlen))
|
2024-06-09 04:19:51 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
cleanup:
|
2024-06-07 04:18:50 +02:00
|
|
|
lc_scrub(buf, sizeof(buf));
|
2024-06-08 01:29:29 +02:00
|
|
|
lc_scrub(&aparams, sizeof(aparams));
|
|
|
|
lc_scrub(&cparams, sizeof(cparams));
|
|
|
|
lc_scrub(tag, sizeof(tag));
|
|
|
|
lc_auth_ctx_free(actx);
|
|
|
|
lc_cipher_ctx_free(cctx);
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
return ret;
|
2024-06-07 04:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
xchacha20_poly1305_open(uint8_t *out, size_t *outlen, void *initparams,
|
2024-06-08 01:29:29 +02:00
|
|
|
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
|
2024-06-07 04:18:50 +02:00
|
|
|
{
|
2024-06-09 17:09:15 +02:00
|
|
|
struct lc_xchacha20_poly1305_params *params = initparams;
|
|
|
|
struct lc_cipher_ctx *cctx = NULL;
|
|
|
|
struct lc_auth_ctx *actx = NULL;
|
|
|
|
struct lc_xchacha20_params cparams;
|
|
|
|
struct lc_poly1305_params aparams;
|
|
|
|
uint8_t tag[LC_POLY1305_TAGLEN];
|
|
|
|
uint8_t buf[sizeof(uint64_t) * 2];
|
|
|
|
size_t i, olen, ctlen;
|
|
|
|
int ret = 0;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
*outlen = 0;
|
|
|
|
/* inlen includes the tag; inlen and aadlen are capped by design. */
|
2024-06-07 04:18:50 +02:00
|
|
|
if (inlen < LC_POLY1305_TAGLEN ||
|
2024-06-08 01:29:29 +02:00
|
|
|
inlen > UINT64_MAX || aadlen > UINT64_MAX)
|
|
|
|
return 0;
|
|
|
|
/* Counter 0 is used for deriving Poly1305 key. */
|
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-07 04:18:50 +02:00
|
|
|
CHACHA20_CTRMAX - 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
*outlen = inlen - LC_POLY1305_TAGLEN;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
cctx = lc_cipher_ctx_new(lc_cipher_impl_xchacha20());
|
|
|
|
if (cctx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
actx = lc_auth_ctx_new(lc_auth_impl_poly1305());
|
|
|
|
if (actx == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(params->key); i++)
|
|
|
|
cparams.key[i] = params->key[i];
|
|
|
|
for (i = 0; i < sizeof(params->nonce); i++)
|
|
|
|
cparams.nonce[i] = params->nonce[i];
|
|
|
|
|
|
|
|
cparams.counter = 0;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!poly1305_keysetup(cctx, aparams.key, &cparams))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!lc_auth_init(actx, &aparams) ||
|
|
|
|
!lc_auth_update(actx, aad, aadlen))
|
|
|
|
goto cleanup;
|
2024-06-07 04:18:50 +02:00
|
|
|
if (aadlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (aadlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
ctlen = inlen - LC_POLY1305_TAGLEN;
|
|
|
|
if (!lc_auth_update(actx, in, ctlen))
|
|
|
|
goto cleanup;
|
2024-06-07 04:18:50 +02:00
|
|
|
if (ctlen % 16 != 0)
|
2024-06-19 15:18:25 +02:00
|
|
|
if (!lc_auth_update(actx, zerobuf, 16 - (ctlen % 16)))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-06-07 04:18:50 +02:00
|
|
|
|
|
|
|
store64le(&buf[0], aadlen);
|
|
|
|
store64le(&buf[sizeof(uint64_t)], ctlen);
|
2024-06-08 01:29:29 +02:00
|
|
|
if (!lc_auth_update(actx, buf, sizeof(buf)) ||
|
|
|
|
!lc_auth_final(actx, tag, &olen))
|
|
|
|
goto cleanup;
|
|
|
|
if (olen != LC_POLY1305_TAGLEN)
|
|
|
|
goto cleanup;
|
|
|
|
if (!lc_ct_cmp(tag, in + ctlen, LC_POLY1305_TAGLEN))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
cparams.counter = 1;
|
2024-06-19 16:02:54 +02:00
|
|
|
if (!lc_cipher_decrypt(cctx->impl, out, outlen, &cparams, in, ctlen))
|
2024-06-08 01:29:29 +02:00
|
|
|
goto cleanup;
|
2024-06-19 16:02:54 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
cleanup:
|
2024-06-07 04:18:50 +02:00
|
|
|
lc_scrub(buf, sizeof(buf));
|
2024-06-08 01:29:29 +02:00
|
|
|
lc_scrub(&aparams, sizeof(aparams));
|
|
|
|
lc_scrub(&cparams, sizeof(cparams));
|
|
|
|
lc_scrub(tag, sizeof(tag));
|
|
|
|
lc_auth_ctx_free(actx);
|
|
|
|
lc_cipher_ctx_free(cctx);
|
2024-06-07 04:18:50 +02:00
|
|
|
|
2024-06-08 01:29:29 +02:00
|
|
|
return ret;
|
2024-06-07 04:18:50 +02:00
|
|
|
}
|
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
const struct lc_aead_impl *
|
|
|
|
lc_aead_impl_chacha20_poly1305(void)
|
|
|
|
{
|
2024-06-18 16:32:35 +02:00
|
|
|
static struct lc_aead_impl chacha20_poly1305_impl = {
|
|
|
|
.seal = &chacha20_poly1305_seal,
|
|
|
|
.open = &chacha20_poly1305_open,
|
|
|
|
|
|
|
|
.blocklen = LC_CHACHA20_BLOCKLEN,
|
|
|
|
};
|
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
return &chacha20_poly1305_impl;
|
|
|
|
}
|
2024-06-07 19:47:42 +02:00
|
|
|
|
2024-06-07 04:18:50 +02:00
|
|
|
const struct lc_aead_impl *
|
|
|
|
lc_aead_impl_xchacha20_poly1305(void)
|
|
|
|
{
|
2024-06-18 16:32:35 +02:00
|
|
|
static struct lc_aead_impl xchacha20_poly1305_impl = {
|
|
|
|
.seal = &xchacha20_poly1305_seal,
|
|
|
|
.open = &xchacha20_poly1305_open,
|
|
|
|
|
|
|
|
.blocklen = LC_XCHACHA20_BLOCKLEN,
|
|
|
|
};
|
|
|
|
|
2024-06-07 04:18:50 +02:00
|
|
|
return &xchacha20_poly1305_impl;
|
|
|
|
}
|