lilcrypto/aead.c
Lucas Gabriel Vuotto 5c1325ed11 aead: rewrite api
Decouple the authentication tag from the ciphertext and add streaming
interfaces.

Tests will be fixed in the next commit.
2024-06-20 15:52:58 +00:00

110 lines
2.9 KiB
C

/*
* 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 <stdlib.h>
#include "internal.h"
int
lc_aead_seal_init(struct lc_aead_ctx *ctx, void *initparams)
{
return ctx->impl->seal_init(ctx->arg, initparams);
}
int
lc_aead_seal_update(struct lc_aead_ctx *ctx, uint8_t *out, size_t *outlen,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
{
return ctx->impl->seal_update(ctx->arg, out, outlen, aad, aadlen, in,
inlen);
}
int
lc_aead_seal_final(struct lc_aead_ctx *ctx, uint8_t *out, size_t *outlen,
uint8_t *tag, size_t *taglen)
{
return ctx->impl->seal_final(ctx->arg, out, outlen, tag, taglen);
}
int
lc_aead_seal(const struct lc_aead_impl *impl, uint8_t *out, size_t *outlen,
uint8_t *tag, size_t *taglen, void *initparams, const uint8_t *aad,
size_t aadlen, const uint8_t *in, size_t inlen)
{
return impl->seal(out, outlen, tag, taglen, initparams, aad, aadlen,
in, inlen);
}
int
lc_aead_open_init(struct lc_aead_ctx *ctx, void *initparams)
{
return ctx->impl->open_init(ctx->arg, initparams);
}
int
lc_aead_open_update(struct lc_aead_ctx *ctx, uint8_t *out, size_t *outlen,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
{
return ctx->impl->open_update(ctx->arg, out, outlen, aad, aadlen, in,
inlen);
}
int
lc_aead_open_final(struct lc_aead_ctx *ctx, uint8_t *out, size_t *outlen,
const uint8_t *tag, size_t taglen)
{
return ctx->impl->open_final(ctx->arg, out, outlen, tag, taglen);
}
int
lc_aead_open(const struct lc_aead_impl *impl, uint8_t *out, size_t *outlen,
void *initparams, const uint8_t *tag, size_t taglen, const uint8_t *aad,
size_t aadlen, const uint8_t *in, size_t inlen)
{
return impl->open(out, outlen, initparams, tag, taglen, aad, aadlen,
in, inlen);
}
struct lc_aead_ctx *
lc_aead_ctx_new(const struct lc_aead_impl *impl)
{
struct lc_aead_ctx *ctx;
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return NULL;
if (impl->argsz > 0) {
ctx->arg = malloc(impl->argsz);
if (ctx->arg == NULL) {
free(ctx);
return NULL;
}
} else
ctx->arg = NULL;
ctx->impl = impl;
return ctx;
}
void
lc_aead_ctx_free(struct lc_aead_ctx *ctx)
{
if (ctx != NULL)
free(ctx->arg);
free(ctx);
}