Make all *_impl provide ctx_new and ctx_free functions
This commit is contained in:
parent
fa489d2b4b
commit
e9bcc64e62
12
auth.c
12
auth.c
@ -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
3
auth.h
@ -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 {
|
||||
|
@ -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 *
|
||||
|
12
cipher.c
12
cipher.c
@ -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);
|
||||
}
|
||||
}
|
||||
|
3
cipher.h
3
cipher.h
@ -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 {
|
||||
|
@ -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 *
|
||||
|
Loading…
Reference in New Issue
Block a user