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

3
auth.h
View File

@ -25,7 +25,8 @@ struct lc_auth_impl {
int (*auth)(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_auth_ctx {

View File

@ -14,6 +14,8 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include "lilcrypto.h"
#include "auth.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);
}
static void *
poly1305_ctx_new(const void *arg)
{
return malloc(sizeof(struct poly1305_ctx));
}
static struct lc_auth_impl poly1305_impl = {
.init = &poly1305_init,
@ -158,7 +165,8 @@ static struct lc_auth_impl poly1305_impl = {
.final = &poly1305_final,
.auth = &poly1305_auth,
.argsz = sizeof(struct poly1305_ctx),
.ctx_new = &poly1305_ctx_new,
.ctx_free = NULL,
};
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)
{
struct lc_cipher_ctx *ctx;
void *arg;
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return NULL;
if (impl->argsz > 0) {
arg = malloc(impl->argsz);
if (arg == NULL) {
if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new(NULL);
if (ctx->arg == NULL) {
free(ctx);
return NULL;
}
ctx->arg = arg;
} else
ctx->arg = NULL;
ctx->impl = impl;
@ -104,7 +102,11 @@ 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);
}
}

View File

@ -35,7 +35,8 @@ 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);
size_t argsz;
void *(*ctx_new)(const void *);
void (*ctx_free)(void *);
};
struct lc_cipher_ctx {

View File

@ -15,6 +15,7 @@
*/
#include <limits.h>
#include <stdlib.h>
#include "lilcrypto.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;
}
static void *
chacha20_ctx_new(const void *arg)
{
return malloc(sizeof(struct chacha20_ctx));
}
static struct lc_cipher_impl chacha20_impl = {
.encrypt_init = &chacha20_x_init,
@ -189,7 +196,8 @@ static struct lc_cipher_impl chacha20_impl = {
.decrypt_final = &chacha20_x_final,
.decrypt = &chacha20_x,
.argsz = sizeof(struct chacha20_ctx),
.ctx_new = &chacha20_ctx_new,
.ctx_free = NULL,
};
const struct lc_cipher_impl *