auth: replace init args with a implementation-specific params struct

This allows for more flexibility in the future.

This commit breaks ChaCha20-Poly1305. It'll be fixed in a subsequent
commit.
This commit is contained in:
Lucas Gabriel Vuotto 2024-06-07 18:40:14 +00:00
parent ccc2836fa8
commit b26a9c7274
7 changed files with 43 additions and 91 deletions

8
auth.c
View File

@ -22,9 +22,9 @@
int
lc_auth_init(struct lc_auth_ctx *ctx, const uint8_t *key, size_t keylen)
lc_auth_init(struct lc_auth_ctx *ctx, const void *initparams)
{
return ctx->impl->init(ctx->arg, key, keylen);
return ctx->impl->init(ctx->arg, initparams);
}
int
@ -41,9 +41,9 @@ lc_auth_final(struct lc_auth_ctx *ctx, uint8_t *out, size_t *outlen)
int
lc_auth(const struct lc_auth_impl *impl, uint8_t *out, size_t *outlen,
const uint8_t *key, size_t keylen, const uint8_t *in, size_t inlen)
const void *initparams, const uint8_t *in, size_t inlen)
{
return impl->auth(out, outlen, key, keylen, in, inlen);
return impl->auth(out, outlen, initparams, in, inlen);
}
struct lc_auth_ctx *

6
auth.h
View File

@ -19,11 +19,11 @@
struct lc_auth_impl {
int (*init)(void *, const uint8_t *, size_t);
int (*init)(void *, const void *);
int (*update)(void *, const uint8_t *, size_t);
int (*final)(void *, uint8_t *, size_t *);
int (*auth)(uint8_t *, size_t *, const uint8_t *, size_t,
const uint8_t *, size_t);
int (*auth)(uint8_t *, size_t *, const void *, const uint8_t *,
size_t);
void *(*ctx_new)(void);
void (*ctx_free)(void *);

View File

@ -19,7 +19,6 @@
#include "lilcrypto.h"
#include "auth.h"
#include "hash.h"
#include "auth_hmac.h"
#include "impl_hmac.h"
#include "impl_sha256.h"
#include "impl_sha512.h"
@ -58,27 +57,29 @@ hmac_common_init(void *arg, const uint8_t *key, size_t keylen)
lc_hash_update(ctx->hctx, ikeypad, ctx->blocksz);
}
int
hmac_sha224_sha256_init(void *arg, const uint8_t *key, size_t keylen)
static int
hmac_sha224_sha256_init(void *arg, const void *initparams)
{
struct hmac_ctx *ctx = arg;
const struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg;
ctx->blocksz = SHA256_CHUNK;
return hmac_common_init(ctx, key, keylen);
return hmac_common_init(ctx, params->key, params->keylen);
}
int
hmac_sha384_sha512_init(void *arg, const uint8_t *key, size_t keylen)
static int
hmac_sha384_sha512_init(void *arg, const void *initparams)
{
struct hmac_ctx *ctx = arg;
const struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg;
ctx->blocksz = SHA512_CHUNK;
return hmac_common_init(ctx, key, keylen);
return hmac_common_init(ctx, params->key, params->keylen);
}
int
static int
hmac_update(void *arg, const uint8_t *in, size_t inlen)
{
struct hmac_ctx *ctx = arg;
@ -86,14 +87,15 @@ hmac_update(void *arg, const uint8_t *in, size_t inlen)
return lc_hash_update(ctx->hctx, in, inlen);
}
int
static int
hmac_final(void *arg, uint8_t *out, size_t *outlen)
{
struct hmac_ctx *ctx = arg;
struct lc_hash_ctx *hctx;
uint8_t m[HMAC_BLOCKSZ_MAX], okeypad[HMAC_BLOCKSZ_MAX];
size_t i, olen;
int rc;
uint8_t m[HMAC_BLOCKSZ_MAX],
okeypad[HMAC_BLOCKSZ_MAX];
size_t i, olen;
int rc;
if (out == NULL) {
(void)lc_hash_final(ctx->hctx, NULL, outlen);

View File

@ -1,24 +0,0 @@
/*
* Copyright (c) 2024 Lucas Gabriel Vuotto <lucas@lgv5.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stddef.h>
#include <stdint.h>
int hmac_sha224_sha256_init(void *, const uint8_t *, size_t);
int hmac_sha384_sha512_init(void *, const uint8_t *, size_t);
int hmac_update(void *, const uint8_t *, size_t);
int hmac_final(void *, uint8_t *, size_t *);

View File

@ -18,21 +18,18 @@
#include "lilcrypto.h"
#include "auth.h"
#include "auth_poly1305.h"
#include "impl_poly1305.h"
#include "util.h"
int
poly1305_init(void *arg, const uint8_t *key, size_t keylen)
static int
poly1305_init(void *arg, const void *initparams)
{
struct poly1305_ctx *ctx = arg;
size_t i;
uint32_t t0, t1, t2, t3;
if (keylen != LC_POLY1305_KEYLEN)
return 0;
const struct lc_poly1305_params *params = initparams;
struct poly1305_ctx *ctx = arg;
size_t i;
uint32_t t0, t1, t2, t3;
ctx->h0 = 0;
ctx->h1 = 0;
@ -40,10 +37,10 @@ poly1305_init(void *arg, const uint8_t *key, size_t keylen)
ctx->h3 = 0;
ctx->h4 = 0;
t0 = load32le(&key[0]);
t1 = load32le(&key[4]);
t2 = load32le(&key[8]);
t3 = load32le(&key[12]);
t0 = load32le(&params->key[0]);
t1 = load32le(&params->key[4]);
t2 = load32le(&params->key[8]);
t3 = load32le(&params->key[12]);
ctx->r0 = t0 & 0x3ffffff;
ctx->r1 = ((t1 << 6) | (t0 >> 26)) & 0x3ffff03;
@ -56,10 +53,10 @@ poly1305_init(void *arg, const uint8_t *key, size_t keylen)
ctx->x3 = 5 * ctx->r3;
ctx->x4 = 5 * ctx->r4;
ctx->s0 = load32le(&key[16]);
ctx->s1 = load32le(&key[20]);
ctx->s2 = load32le(&key[24]);
ctx->s3 = load32le(&key[28]);
ctx->s0 = load32le(&params->key[16]);
ctx->s1 = load32le(&params->key[20]);
ctx->s2 = load32le(&params->key[24]);
ctx->s3 = load32le(&params->key[28]);
ctx->mlen = 0;
for (i = 0; i < POLY1305_CHUNK; i++)
@ -68,7 +65,7 @@ poly1305_init(void *arg, const uint8_t *key, size_t keylen)
return 1;
}
int
static int
poly1305_update(void *arg, const uint8_t *in, size_t inlen)
{
struct poly1305_ctx *ctx = arg;
@ -104,7 +101,7 @@ poly1305_update(void *arg, const uint8_t *in, size_t inlen)
return 1;
}
int
static int
poly1305_final(void *arg, uint8_t *out, size_t *outlen)
{
struct poly1305_ctx *ctx = arg;
@ -138,7 +135,7 @@ poly1305_final(void *arg, uint8_t *out, size_t *outlen)
}
static int
poly1305_auth(uint8_t *out, size_t *outlen, const uint8_t *key, size_t keylen,
poly1305_auth(uint8_t *out, size_t *outlen, const void *initparams,
const uint8_t *in, size_t inlen)
{
struct poly1305_ctx ctx;
@ -148,7 +145,7 @@ poly1305_auth(uint8_t *out, size_t *outlen, const uint8_t *key, size_t keylen,
return 1;
}
return poly1305_init(&ctx, key, keylen) &&
return poly1305_init(&ctx, initparams) &&
poly1305_update(&ctx, in, inlen) &&
poly1305_final(&ctx, out, outlen);
}

View File

@ -1,23 +0,0 @@
/*
* Copyright (c) 2024 Lucas Gabriel Vuotto <lucas@lgv5.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stddef.h>
#include <stdint.h>
int poly1305_init(void *, const uint8_t *, size_t);
int poly1305_update(void *, const uint8_t *, size_t);
int poly1305_final(void *, uint8_t *, size_t *);

View File

@ -123,11 +123,11 @@ struct lc_auth_ctx;
struct lc_auth_impl;
int lc_auth_init(struct lc_auth_ctx *, const uint8_t *, size_t);
int lc_auth_init(struct lc_auth_ctx *, const void *);
int lc_auth_update(struct lc_auth_ctx *, const uint8_t *, size_t);
int lc_auth_final(struct lc_auth_ctx *, uint8_t *, size_t *);
int lc_auth(const struct lc_auth_impl *, uint8_t *, size_t *,
const uint8_t *, size_t, const uint8_t *, size_t);
const void *, const uint8_t *, size_t);
struct lc_auth_ctx *lc_auth_ctx_new(const struct lc_auth_impl *);
void lc_auth_ctx_free(struct lc_auth_ctx *);