From 6677c6cab33bfc9807da1a0e6ba0a78a529f6fe9 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Vuotto Date: Mon, 10 Jun 2024 17:44:05 +0000 Subject: [PATCH] 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 e9bcc64e623f16e0d9ae8e34cb568e3c37fdc6ec . --- aead.h | 1 + auth.c | 9 +++------ auth.h | 16 +++++++--------- auth_hmac.c | 10 +--------- auth_poly1305.c | 10 +--------- cipher.c | 9 +++------ cipher.h | 26 ++++++++++++-------------- cipher_chacha20.c | 14 ++------------ hash.c | 9 +++------ hash.h | 16 +++++++--------- hash_sha224_sha256.c | 14 ++------------ hash_sha384_sha512.c | 14 ++------------ 12 files changed, 44 insertions(+), 104 deletions(-) diff --git a/aead.h b/aead.h index aa5b51c..06d51aa 100644 --- a/aead.h +++ b/aead.h @@ -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; }; diff --git a/auth.c b/auth.c index e653976..4d28657 100644 --- a/auth.c +++ b/auth.c @@ -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); } diff --git a/auth.h b/auth.h index 35924b7..480daa9 100644 --- a/auth.h +++ b/auth.h @@ -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 { diff --git a/auth_hmac.c b/auth_hmac.c index de65912..3c0c817 100644 --- a/auth_hmac.c +++ b/auth_hmac.c @@ -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, }; diff --git a/auth_poly1305.c b/auth_poly1305.c index ce4c4c8..9175bc6 100644 --- a/auth_poly1305.c +++ b/auth_poly1305.c @@ -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, }; diff --git a/cipher.c b/cipher.c index 182c4e7..826dc08 100644 --- a/cipher.c +++ b/cipher.c @@ -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); } diff --git a/cipher.h b/cipher.h index 497de0a..3846fc7 100644 --- a/cipher.h +++ b/cipher.h @@ -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 { diff --git a/cipher_chacha20.c b/cipher_chacha20.c index d8e9b83..18c7188 100644 --- a/cipher_chacha20.c +++ b/cipher_chacha20.c @@ -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, }; diff --git a/hash.c b/hash.c index 5226069..22aee66 100644 --- a/hash.c +++ b/hash.c @@ -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); } diff --git a/hash.h b/hash.h index 16989fc..7ef72de 100644 --- a/hash.h +++ b/hash.h @@ -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 { diff --git a/hash_sha224_sha256.c b/hash_sha224_sha256.c index ea7760d..8ddb377 100644 --- a/hash_sha224_sha256.c +++ b/hash_sha224_sha256.c @@ -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, }; diff --git a/hash_sha384_sha512.c b/hash_sha384_sha512.c index f63f657..66bc6be 100644 --- a/hash_sha384_sha512.c +++ b/hash_sha384_sha512.c @@ -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, };