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, int (*open)(uint8_t *, size_t *, void *, const uint8_t *, size_t,
const uint8_t *, size_t); const uint8_t *, size_t);
size_t argsz;
size_t blocklen; 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)); ctx = malloc(sizeof(*ctx));
if (ctx == NULL) if (ctx == NULL)
return NULL; return NULL;
if (impl->ctx_new != NULL) { if (impl->argsz > 0) {
ctx->arg = impl->ctx_new(); ctx->arg = malloc(impl->argsz);
if (ctx->arg == NULL) { if (ctx->arg == NULL) {
free(ctx); free(ctx);
return NULL; return NULL;
@ -70,10 +70,7 @@ 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) { if (ctx != NULL)
if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx->arg);
free(ctx->arg); free(ctx->arg);
}
free(ctx); free(ctx);
} }

4
auth.h
View File

@ -24,9 +24,7 @@ struct lc_auth_impl {
int (*final)(void *, uint8_t *, size_t *); int (*final)(void *, uint8_t *, size_t *);
int (*auth)(uint8_t *, size_t *, void *, const uint8_t *, size_t); int (*auth)(uint8_t *, size_t *, void *, const uint8_t *, size_t);
void *(*ctx_new)(void); size_t argsz;
void (*ctx_free)(void *);
size_t blocklen; size_t blocklen;
size_t taglen; size_t taglen;
}; };

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); hmac_final(&ctx, out, outlen);
} }
static void *
hmac_ctx_new(void)
{
return malloc(sizeof(struct hmac_ctx));
}
static struct lc_auth_impl hmac_impl = { static struct lc_auth_impl hmac_impl = {
.init = &hmac_init, .init = &hmac_init,
@ -127,9 +121,7 @@ static struct lc_auth_impl hmac_impl = {
.final = &hmac_final, .final = &hmac_final,
.auth = &hmac_auth, .auth = &hmac_auth,
.ctx_new = &hmac_ctx_new, .argsz = sizeof(struct hmac_ctx),
.ctx_free = NULL,
.blocklen = 0, .blocklen = 0,
.taglen = 0, .taglen = 0,
}; };

View File

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

View File

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

View File

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

View File

@ -209,12 +209,6 @@ chacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
return rc; return rc;
} }
static void *
chacha20_ctx_new(void)
{
return malloc(sizeof(struct chacha20_ctx));
}
static struct lc_cipher_impl chacha20_impl = { static struct lc_cipher_impl chacha20_impl = {
.encrypt_init = &chacha20_anycrypt_init, .encrypt_init = &chacha20_anycrypt_init,
@ -227,9 +221,7 @@ static struct lc_cipher_impl chacha20_impl = {
.decrypt_final = &chacha20_anycrypt_final, .decrypt_final = &chacha20_anycrypt_final,
.decrypt = &chacha20_anycrypt, .decrypt = &chacha20_anycrypt,
.ctx_new = &chacha20_ctx_new, .argsz = sizeof(struct chacha20_ctx),
.ctx_free = NULL,
.blocklen = LC_CHACHA20_BLOCKLEN, .blocklen = LC_CHACHA20_BLOCKLEN,
}; };
@ -244,9 +236,7 @@ static struct lc_cipher_impl xchacha20_impl = {
.decrypt_final = &chacha20_anycrypt_final, .decrypt_final = &chacha20_anycrypt_final,
.decrypt = &chacha20_anycrypt, .decrypt = &chacha20_anycrypt,
.ctx_new = &chacha20_ctx_new, .argsz = sizeof(struct chacha20_ctx),
.ctx_free = NULL,
.blocklen = LC_XCHACHA20_BLOCKLEN, .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)); ctx = malloc(sizeof(*ctx));
if (ctx == NULL) if (ctx == NULL)
return NULL; return NULL;
if (impl->ctx_new != NULL) { if (impl->argsz > 0) {
ctx->arg = impl->ctx_new(); ctx->arg = malloc(impl->argsz);
if (ctx->arg == NULL) { if (ctx->arg == NULL) {
free(ctx); free(ctx);
return NULL; return NULL;
@ -70,10 +70,7 @@ lc_hash_ctx_new(const struct lc_hash_impl *impl)
void void
lc_hash_ctx_free(struct lc_hash_ctx *ctx) lc_hash_ctx_free(struct lc_hash_ctx *ctx)
{ {
if (ctx != NULL) { if (ctx != NULL)
if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
ctx->impl->ctx_free(ctx->arg);
free(ctx->arg); free(ctx->arg);
}
free(ctx); free(ctx);
} }

4
hash.h
View File

@ -24,9 +24,7 @@ struct lc_hash_impl {
int (*final)(void *, uint8_t *, size_t *); int (*final)(void *, uint8_t *, size_t *);
int (*hash)(uint8_t *, size_t *, const uint8_t *, size_t); int (*hash)(uint8_t *, size_t *, const uint8_t *, size_t);
void *(*ctx_new)(void); size_t argsz;
void (*ctx_free)(void *);
size_t blocklen; size_t blocklen;
size_t hashlen; size_t hashlen;
}; };

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); 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 = { static struct lc_hash_impl sha224_impl = {
.init = &sha224_init, .init = &sha224_init,
@ -264,9 +258,7 @@ static struct lc_hash_impl sha224_impl = {
.final = &sha224_final, .final = &sha224_final,
.hash = &sha224_hash, .hash = &sha224_hash,
.ctx_new = &sha224_sha256_ctx_new, .argsz = sizeof(struct sha256_ctx),
.ctx_free = NULL,
.blocklen = LC_SHA224_BLOCKLEN, .blocklen = LC_SHA224_BLOCKLEN,
.hashlen = LC_SHA224_HASHLEN, .hashlen = LC_SHA224_HASHLEN,
}; };
@ -277,9 +269,7 @@ static struct lc_hash_impl sha256_impl = {
.final = &sha256_final, .final = &sha256_final,
.hash = &sha256_hash, .hash = &sha256_hash,
.ctx_new = &sha224_sha256_ctx_new, .argsz = sizeof(struct sha256_ctx),
.ctx_free = NULL,
.blocklen = LC_SHA256_BLOCKLEN, .blocklen = LC_SHA256_BLOCKLEN,
.hashlen = LC_SHA256_HASHLEN, .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); 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 = { static struct lc_hash_impl sha384_impl = {
.init = &sha384_init, .init = &sha384_init,
@ -269,9 +263,7 @@ static struct lc_hash_impl sha384_impl = {
.final = &sha384_final, .final = &sha384_final,
.hash = &sha384_hash, .hash = &sha384_hash,
.ctx_new = &sha384_sha512_ctx_new, .argsz = sizeof(struct sha512_ctx),
.ctx_free = NULL,
.blocklen = LC_SHA384_BLOCKLEN, .blocklen = LC_SHA384_BLOCKLEN,
.hashlen = LC_SHA384_HASHLEN, .hashlen = LC_SHA384_HASHLEN,
}; };
@ -282,9 +274,7 @@ static struct lc_hash_impl sha512_impl = {
.final = &sha512_final, .final = &sha512_final,
.hash = &sha512_hash, .hash = &sha512_hash,
.ctx_new = &sha384_sha512_ctx_new, .argsz = sizeof(struct sha512_ctx),
.ctx_free = NULL,
.blocklen = LC_SHA512_BLOCKLEN, .blocklen = LC_SHA512_BLOCKLEN,
.hashlen = LC_SHA512_HASHLEN, .hashlen = LC_SHA512_HASHLEN,
}; };