From f511cddf0d0aef9b043d42b34b4024593538a373 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Vuotto Date: Thu, 6 Jun 2024 11:45:30 +0000 Subject: [PATCH] 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. --- auth.c | 13 ++++++------- auth.h | 2 +- auth_poly1305.c | 3 ++- cipher.c | 13 ++++++------- cipher.h | 2 +- cipher_chacha20.c | 2 +- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/auth.c b/auth.c index ab5db90..23178c1 100644 --- a/auth.c +++ b/auth.c @@ -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); } diff --git a/auth.h b/auth.h index c5e6a98..c60c42a 100644 --- a/auth.h +++ b/auth.h @@ -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 *); }; diff --git a/auth_poly1305.c b/auth_poly1305.c index 9c394fc..6ee842c 100644 --- a/auth_poly1305.c +++ b/auth_poly1305.c @@ -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, diff --git a/cipher.c b/cipher.c index a2c4a25..80cb149 100644 --- a/cipher.c +++ b/cipher.c @@ -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); } diff --git a/cipher.h b/cipher.h index d20c4cb..949426a 100644 --- a/cipher.h +++ b/cipher.h @@ -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 *); }; diff --git a/cipher_chacha20.c b/cipher_chacha20.c index 5e09375..937a090 100644 --- a/cipher_chacha20.c +++ b/cipher_chacha20.c @@ -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)); }