Fix *_ctx_new and *_ctx_free implementations

They both will only alloc and free the internal *_ctx structs. Get rid
of the void * argument for new and only pass arg to *_free instead of
the whole lc_*_ctx struct.
This commit is contained in:
Lucas Gabriel Vuotto 2024-06-06 11:45:30 +00:00
parent 0a47025c19
commit f511cddf0d
6 changed files with 17 additions and 18 deletions

13
auth.c
View File

@ -55,7 +55,7 @@ lc_auth_ctx_new(const struct lc_auth_impl *impl)
if (ctx == NULL)
return NULL;
if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new(NULL);
ctx->arg = impl->ctx_new();
if (ctx->arg == NULL) {
free(ctx);
return NULL;
@ -70,11 +70,10 @@ lc_auth_ctx_new(const struct lc_auth_impl *impl)
void
lc_auth_ctx_free(struct lc_auth_ctx *ctx)
{
if (ctx != NULL && ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx);
else {
if (ctx != NULL)
free(ctx->arg);
free(ctx);
if (ctx != NULL) {
if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx->arg);
free(ctx->arg);
}
free(ctx);
}

2
auth.h
View File

@ -25,7 +25,7 @@ struct lc_auth_impl {
int (*auth)(const uint8_t *, size_t, uint8_t *, size_t *,
const uint8_t *, size_t);
void *(*ctx_new)(const void *);
void *(*ctx_new)(void);
void (*ctx_free)(void *);
};

View File

@ -154,11 +154,12 @@ poly1305_auth(const uint8_t *key, size_t keylen, uint8_t *out, size_t *outlen,
}
static void *
poly1305_ctx_new(const void *arg)
poly1305_ctx_new(void)
{
return malloc(sizeof(struct poly1305_ctx));
}
static struct lc_auth_impl poly1305_impl = {
.init = &poly1305_init,
.update = &poly1305_update,

View File

@ -87,7 +87,7 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
if (ctx == NULL)
return NULL;
if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new(NULL);
ctx->arg = impl->ctx_new();
if (ctx->arg == NULL) {
free(ctx);
return NULL;
@ -102,11 +102,10 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
void
lc_cipher_ctx_free(struct lc_cipher_ctx *ctx)
{
if (ctx != NULL && ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx);
else {
if (ctx != NULL)
free(ctx->arg);
free(ctx);
if (ctx != NULL) {
if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx->arg);
free(ctx->arg);
}
free(ctx);
}

View File

@ -35,7 +35,7 @@ struct lc_cipher_impl {
int (*decrypt)(const uint8_t *, size_t, const uint8_t *, size_t,
uint8_t *, size_t *, const uint8_t *, size_t);
void *(*ctx_new)(const void *);
void *(*ctx_new)(void);
void (*ctx_free)(void *);
};

View File

@ -179,7 +179,7 @@ chacha20_x(const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
}
static void *
chacha20_ctx_new(const void *arg)
chacha20_ctx_new(void)
{
return malloc(sizeof(struct chacha20_ctx));
}