Remove ctx_new and ctx_free function pointers

Now that HMAC isn't special anymore, and with the help of init params,
this isn't needed anymore as we only allocate memory for the state.

This effectively reverts e9bcc64e62 .
This commit is contained in:
Lucas Gabriel Vuotto 2024-06-10 17:44:05 +00:00
parent 666b833b98
commit 6677c6cab3
12 changed files with 44 additions and 104 deletions

1
aead.h
View File

@ -24,5 +24,6 @@ struct lc_aead_impl {
int (*open)(uint8_t *, size_t *, void *, const uint8_t *, size_t,
const uint8_t *, size_t);
size_t argsz;
size_t blocklen;
};

9
auth.c
View File

@ -54,8 +54,8 @@ lc_auth_ctx_new(const struct lc_auth_impl *impl)
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return NULL;
if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new();
if (impl->argsz > 0) {
ctx->arg = malloc(impl->argsz);
if (ctx->arg == NULL) {
free(ctx);
return NULL;
@ -70,10 +70,7 @@ lc_auth_ctx_new(const struct lc_auth_impl *impl)
void
lc_auth_ctx_free(struct lc_auth_ctx *ctx)
{
if (ctx != NULL) {
if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx->arg);
if (ctx != NULL)
free(ctx->arg);
}
free(ctx);
}

16
auth.h
View File

@ -19,16 +19,14 @@
struct lc_auth_impl {
int (*init)(void *, void *);
int (*update)(void *, const uint8_t *, size_t);
int (*final)(void *, uint8_t *, size_t *);
int (*auth)(uint8_t *, size_t *, void *, const uint8_t *, size_t);
int (*init)(void *, void *);
int (*update)(void *, const uint8_t *, size_t);
int (*final)(void *, uint8_t *, size_t *);
int (*auth)(uint8_t *, size_t *, void *, const uint8_t *, size_t);
void *(*ctx_new)(void);
void (*ctx_free)(void *);
size_t blocklen;
size_t taglen;
size_t argsz;
size_t blocklen;
size_t taglen;
};
struct lc_auth_ctx {

View File

@ -114,12 +114,6 @@ hmac_auth(uint8_t *out, size_t *outlen, void *initparams, const uint8_t *in,
hmac_final(&ctx, out, outlen);
}
static void *
hmac_ctx_new(void)
{
return malloc(sizeof(struct hmac_ctx));
}
static struct lc_auth_impl hmac_impl = {
.init = &hmac_init,
@ -127,9 +121,7 @@ static struct lc_auth_impl hmac_impl = {
.final = &hmac_final,
.auth = &hmac_auth,
.ctx_new = &hmac_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct hmac_ctx),
.blocklen = 0,
.taglen = 0,
};

View File

@ -150,12 +150,6 @@ poly1305_auth(uint8_t *out, size_t *outlen, void *initparams,
poly1305_final(&ctx, out, outlen);
}
static void *
poly1305_ctx_new(void)
{
return malloc(sizeof(struct poly1305_ctx));
}
static struct lc_auth_impl poly1305_impl = {
.init = &poly1305_init,
@ -163,9 +157,7 @@ static struct lc_auth_impl poly1305_impl = {
.final = &poly1305_final,
.auth = &poly1305_auth,
.ctx_new = &poly1305_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct poly1305_ctx),
.blocklen = LC_POLY1305_BLOCKLEN,
.taglen = LC_POLY1305_TAGLEN,
};

View File

@ -82,8 +82,8 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return NULL;
if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new();
if (impl->argsz > 0) {
ctx->arg = malloc(impl->argsz);
if (ctx->arg == NULL) {
free(ctx);
return NULL;
@ -98,10 +98,7 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
void
lc_cipher_ctx_free(struct lc_cipher_ctx *ctx)
{
if (ctx != NULL) {
if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx->arg);
if (ctx != NULL)
free(ctx->arg);
}
free(ctx);
}

View File

@ -19,24 +19,22 @@
struct lc_cipher_impl {
int (*encrypt_init)(void *, void *);
int (*encrypt_update)(void *, uint8_t *, size_t *,
const uint8_t *, size_t);
int (*encrypt_final)(void *, uint8_t *, size_t *);
int (*encrypt)(uint8_t *, size_t *, void *, const uint8_t *,
int (*encrypt_init)(void *, void *);
int (*encrypt_update)(void *, uint8_t *, size_t *, const uint8_t *,
size_t);
int (*encrypt_final)(void *, uint8_t *, size_t *);
int (*encrypt)(uint8_t *, size_t *, void *, const uint8_t *,
size_t);
int (*decrypt_init)(void *, void *);
int (*decrypt_update)(void *, uint8_t *, size_t *,
const uint8_t *, size_t);
int (*decrypt_final)(void *, uint8_t *, size_t *);
int (*decrypt)(uint8_t *, size_t *, void *, const uint8_t *,
int (*decrypt_init)(void *, void *);
int (*decrypt_update)(void *, uint8_t *, size_t *, const uint8_t *,
size_t);
int (*decrypt_final)(void *, uint8_t *, size_t *);
int (*decrypt)(uint8_t *, size_t *, void *, const uint8_t *,
size_t);
void *(*ctx_new)(void);
void (*ctx_free)(void *);
size_t blocklen;
size_t argsz;
size_t blocklen;
};
struct lc_cipher_ctx {

View File

@ -209,12 +209,6 @@ chacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
return rc;
}
static void *
chacha20_ctx_new(void)
{
return malloc(sizeof(struct chacha20_ctx));
}
static struct lc_cipher_impl chacha20_impl = {
.encrypt_init = &chacha20_anycrypt_init,
@ -227,9 +221,7 @@ static struct lc_cipher_impl chacha20_impl = {
.decrypt_final = &chacha20_anycrypt_final,
.decrypt = &chacha20_anycrypt,
.ctx_new = &chacha20_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct chacha20_ctx),
.blocklen = LC_CHACHA20_BLOCKLEN,
};
@ -244,9 +236,7 @@ static struct lc_cipher_impl xchacha20_impl = {
.decrypt_final = &chacha20_anycrypt_final,
.decrypt = &chacha20_anycrypt,
.ctx_new = &chacha20_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct chacha20_ctx),
.blocklen = LC_XCHACHA20_BLOCKLEN,
};

9
hash.c
View File

@ -54,8 +54,8 @@ lc_hash_ctx_new(const struct lc_hash_impl *impl)
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return NULL;
if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new();
if (impl->argsz > 0) {
ctx->arg = malloc(impl->argsz);
if (ctx->arg == NULL) {
free(ctx);
return NULL;
@ -70,10 +70,7 @@ lc_hash_ctx_new(const struct lc_hash_impl *impl)
void
lc_hash_ctx_free(struct lc_hash_ctx *ctx)
{
if (ctx != NULL) {
if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx->arg);
if (ctx != NULL)
free(ctx->arg);
}
free(ctx);
}

16
hash.h
View File

@ -19,16 +19,14 @@
struct lc_hash_impl {
int (*init)(void *);
int (*update)(void *, const uint8_t *, size_t);
int (*final)(void *, uint8_t *, size_t *);
int (*hash)(uint8_t *, size_t *, const uint8_t *, size_t);
int (*init)(void *);
int (*update)(void *, const uint8_t *, size_t);
int (*final)(void *, uint8_t *, size_t *);
int (*hash)(uint8_t *, size_t *, const uint8_t *, size_t);
void *(*ctx_new)(void);
void (*ctx_free)(void *);
size_t blocklen;
size_t hashlen;
size_t argsz;
size_t blocklen;
size_t hashlen;
};
struct lc_hash_ctx {

View File

@ -251,12 +251,6 @@ sha256_hash(uint8_t *out, size_t *outlen, const uint8_t *in, size_t inlen)
sha256_final(&ctx, out, outlen);
}
static void *
sha224_sha256_ctx_new(void)
{
return malloc(sizeof(struct sha256_ctx));
}
static struct lc_hash_impl sha224_impl = {
.init = &sha224_init,
@ -264,9 +258,7 @@ static struct lc_hash_impl sha224_impl = {
.final = &sha224_final,
.hash = &sha224_hash,
.ctx_new = &sha224_sha256_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct sha256_ctx),
.blocklen = LC_SHA224_BLOCKLEN,
.hashlen = LC_SHA224_HASHLEN,
};
@ -277,9 +269,7 @@ static struct lc_hash_impl sha256_impl = {
.final = &sha256_final,
.hash = &sha256_hash,
.ctx_new = &sha224_sha256_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct sha256_ctx),
.blocklen = LC_SHA256_BLOCKLEN,
.hashlen = LC_SHA256_HASHLEN,
};

View File

@ -256,12 +256,6 @@ sha512_hash(uint8_t *out, size_t *outlen, const uint8_t *in, size_t inlen)
sha512_final(&ctx, out, outlen);
}
static void *
sha384_sha512_ctx_new(void)
{
return malloc(sizeof(struct sha512_ctx));
}
static struct lc_hash_impl sha384_impl = {
.init = &sha384_init,
@ -269,9 +263,7 @@ static struct lc_hash_impl sha384_impl = {
.final = &sha384_final,
.hash = &sha384_hash,
.ctx_new = &sha384_sha512_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct sha512_ctx),
.blocklen = LC_SHA384_BLOCKLEN,
.hashlen = LC_SHA384_HASHLEN,
};
@ -282,9 +274,7 @@ static struct lc_hash_impl sha512_impl = {
.final = &sha512_final,
.hash = &sha512_hash,
.ctx_new = &sha384_sha512_ctx_new,
.ctx_free = NULL,
.argsz = sizeof(struct sha512_ctx),
.blocklen = LC_SHA512_BLOCKLEN,
.hashlen = LC_SHA512_HASHLEN,
};