Make all *_impl provide ctx_new and ctx_free functions

This commit is contained in:
Lucas Gabriel Vuotto 2024-06-05 22:05:37 +00:00
parent fa489d2b4b
commit e9bcc64e62
6 changed files with 54 additions and 32 deletions

12
auth.c
View File

@ -50,18 +50,16 @@ struct lc_auth_ctx *
lc_auth_ctx_new(const struct lc_auth_impl *impl) lc_auth_ctx_new(const struct lc_auth_impl *impl)
{ {
struct lc_auth_ctx *ctx; struct lc_auth_ctx *ctx;
void *arg;
ctx = malloc(sizeof(*ctx)); ctx = malloc(sizeof(*ctx));
if (ctx == NULL) if (ctx == NULL)
return NULL; return NULL;
if (impl->argsz > 0) { if (impl->ctx_new != NULL) {
arg = malloc(impl->argsz); ctx->arg = impl->ctx_new(NULL);
if (arg == NULL) { if (ctx->arg == NULL) {
free(ctx); free(ctx);
return NULL; return NULL;
} }
ctx->arg = arg;
} else } else
ctx->arg = NULL; ctx->arg = NULL;
ctx->impl = impl; ctx->impl = impl;
@ -72,7 +70,11 @@ lc_auth_ctx_new(const struct lc_auth_impl *impl)
void void
lc_auth_ctx_free(struct lc_auth_ctx *ctx) 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) if (ctx != NULL)
free(ctx->arg); free(ctx->arg);
free(ctx); free(ctx);
} }
}

3
auth.h
View File

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

View File

@ -14,6 +14,8 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <stdlib.h>
#include "lilcrypto.h" #include "lilcrypto.h"
#include "auth.h" #include "auth.h"
#include "auth_poly1305.h" #include "auth_poly1305.h"
@ -151,6 +153,11 @@ poly1305_auth(const uint8_t *key, size_t keylen, uint8_t *out, size_t *outlen,
poly1305_final(&ctx, out, outlen); poly1305_final(&ctx, out, outlen);
} }
static void *
poly1305_ctx_new(const void *arg)
{
return malloc(sizeof(struct poly1305_ctx));
}
static struct lc_auth_impl poly1305_impl = { static struct lc_auth_impl poly1305_impl = {
.init = &poly1305_init, .init = &poly1305_init,
@ -158,7 +165,8 @@ static struct lc_auth_impl poly1305_impl = {
.final = &poly1305_final, .final = &poly1305_final,
.auth = &poly1305_auth, .auth = &poly1305_auth,
.argsz = sizeof(struct poly1305_ctx), .ctx_new = &poly1305_ctx_new,
.ctx_free = NULL,
}; };
const struct lc_auth_impl * const struct lc_auth_impl *

View File

@ -82,18 +82,16 @@ struct lc_cipher_ctx *
lc_cipher_ctx_new(const struct lc_cipher_impl *impl) lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
{ {
struct lc_cipher_ctx *ctx; struct lc_cipher_ctx *ctx;
void *arg;
ctx = malloc(sizeof(*ctx)); ctx = malloc(sizeof(*ctx));
if (ctx == NULL) if (ctx == NULL)
return NULL; return NULL;
if (impl->argsz > 0) { if (impl->ctx_new != NULL) {
arg = malloc(impl->argsz); ctx->arg = impl->ctx_new(NULL);
if (arg == NULL) { if (ctx->arg == NULL) {
free(ctx); free(ctx);
return NULL; return NULL;
} }
ctx->arg = arg;
} else } else
ctx->arg = NULL; ctx->arg = NULL;
ctx->impl = impl; ctx->impl = impl;
@ -104,7 +102,11 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
void void
lc_cipher_ctx_free(struct lc_cipher_ctx *ctx) 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) if (ctx != NULL)
free(ctx->arg); free(ctx->arg);
free(ctx); free(ctx);
} }
}

View File

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

View File

@ -15,6 +15,7 @@
*/ */
#include <limits.h> #include <limits.h>
#include <stdlib.h>
#include "lilcrypto.h" #include "lilcrypto.h"
#include "cipher.h" #include "cipher.h"
@ -177,6 +178,12 @@ chacha20_x(const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
return rc; return rc;
} }
static void *
chacha20_ctx_new(const void *arg)
{
return malloc(sizeof(struct chacha20_ctx));
}
static struct lc_cipher_impl chacha20_impl = { static struct lc_cipher_impl chacha20_impl = {
.encrypt_init = &chacha20_x_init, .encrypt_init = &chacha20_x_init,
@ -189,7 +196,8 @@ static struct lc_cipher_impl chacha20_impl = {
.decrypt_final = &chacha20_x_final, .decrypt_final = &chacha20_x_final,
.decrypt = &chacha20_x, .decrypt = &chacha20_x,
.argsz = sizeof(struct chacha20_ctx), .ctx_new = &chacha20_ctx_new,
.ctx_free = NULL,
}; };
const struct lc_cipher_impl * const struct lc_cipher_impl *