Make params struct non-const

Upcoming changes will push lc_*_ctx into the params struct for higher
order constructions like HMAC or HKDF.
This commit is contained in:
Lucas Gabriel Vuotto 2024-06-09 15:09:15 +00:00
parent af3fe8a67a
commit 2b3e390bbf
12 changed files with 88 additions and 89 deletions

8
aead.c
View File

@ -20,16 +20,16 @@
int int
lc_aead_seal(const struct lc_aead_impl *impl, uint8_t *out, size_t *outlen, lc_aead_seal(const struct lc_aead_impl *impl, uint8_t *out, size_t *outlen,
const void *initparams, const uint8_t *aad, size_t aadlen, void *initparams, const uint8_t *aad, size_t aadlen, const uint8_t *in,
const uint8_t *in, size_t inlen) size_t inlen)
{ {
return impl->seal(out, outlen, initparams, aad, aadlen, in, inlen); return impl->seal(out, outlen, initparams, aad, aadlen, in, inlen);
} }
int int
lc_aead_open(const struct lc_aead_impl *impl, uint8_t *out, size_t *outlen, lc_aead_open(const struct lc_aead_impl *impl, uint8_t *out, size_t *outlen,
const void *initparams, const uint8_t *aad, size_t aadlen, void *initparams, const uint8_t *aad, size_t aadlen, const uint8_t *in,
const uint8_t *in, size_t inlen) size_t inlen)
{ {
return impl->open(out, outlen, initparams, aad, aadlen, in, inlen); return impl->open(out, outlen, initparams, aad, aadlen, in, inlen);
} }

8
aead.h
View File

@ -19,8 +19,8 @@
struct lc_aead_impl { struct lc_aead_impl {
int (*seal)(uint8_t *, size_t *, const void *, const uint8_t *, int (*seal)(uint8_t *, size_t *, void *, const uint8_t *, size_t,
size_t, const uint8_t *, size_t); const uint8_t *, size_t);
int (*open)(uint8_t *, size_t *, const void *, const uint8_t *, int (*open)(uint8_t *, size_t *, void *, const uint8_t *, size_t,
size_t, const uint8_t *, size_t); const uint8_t *, size_t);
}; };

View File

@ -30,7 +30,7 @@ static const uint8_t zeropad[16];
static int static int
chacha20_xchacha20_keysetup(struct lc_cipher_ctx *cctx, chacha20_xchacha20_keysetup(struct lc_cipher_ctx *cctx,
uint8_t akey[LC_POLY1305_KEYLEN], const void *initparams) uint8_t akey[LC_POLY1305_KEYLEN], void *initparams)
{ {
size_t i, olen, akeylen; size_t i, olen, akeylen;
@ -49,10 +49,10 @@ chacha20_xchacha20_keysetup(struct lc_cipher_ctx *cctx,
} }
static int static int
chacha20_poly1305_seal(uint8_t *out, size_t *outlen, const void *initparams, chacha20_poly1305_seal(uint8_t *out, size_t *outlen, void *initparams,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen) const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
{ {
const struct lc_chacha20_poly1305_params *params = initparams; struct lc_chacha20_poly1305_params *params = initparams;
struct lc_cipher_ctx *cctx = NULL; struct lc_cipher_ctx *cctx = NULL;
struct lc_auth_ctx *actx = NULL; struct lc_auth_ctx *actx = NULL;
struct lc_chacha20_params cparams; struct lc_chacha20_params cparams;
@ -138,10 +138,10 @@ chacha20_poly1305_seal(uint8_t *out, size_t *outlen, const void *initparams,
} }
static int static int
xchacha20_poly1305_seal(uint8_t *out, size_t *outlen, const void *initparams, xchacha20_poly1305_seal(uint8_t *out, size_t *outlen, void *initparams,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen) const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
{ {
const struct lc_xchacha20_poly1305_params *params = initparams; struct lc_xchacha20_poly1305_params *params = initparams;
struct lc_cipher_ctx *cctx = NULL; struct lc_cipher_ctx *cctx = NULL;
struct lc_auth_ctx *actx = NULL; struct lc_auth_ctx *actx = NULL;
struct lc_xchacha20_params cparams; struct lc_xchacha20_params cparams;
@ -227,10 +227,10 @@ xchacha20_poly1305_seal(uint8_t *out, size_t *outlen, const void *initparams,
} }
static int static int
chacha20_poly1305_open(uint8_t *out, size_t *outlen, const void *initparams, chacha20_poly1305_open(uint8_t *out, size_t *outlen, void *initparams,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen) const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
{ {
const struct lc_chacha20_poly1305_params *params = initparams; struct lc_chacha20_poly1305_params *params = initparams;
struct lc_cipher_ctx *cctx = NULL; struct lc_cipher_ctx *cctx = NULL;
struct lc_auth_ctx *actx = NULL; struct lc_auth_ctx *actx = NULL;
struct lc_chacha20_params cparams; struct lc_chacha20_params cparams;
@ -322,10 +322,10 @@ chacha20_poly1305_open(uint8_t *out, size_t *outlen, const void *initparams,
} }
static int static int
xchacha20_poly1305_open(uint8_t *out, size_t *outlen, const void *initparams, xchacha20_poly1305_open(uint8_t *out, size_t *outlen, void *initparams,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen) const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen)
{ {
const struct lc_xchacha20_poly1305_params *params = initparams; struct lc_xchacha20_poly1305_params *params = initparams;
struct lc_cipher_ctx *cctx = NULL; struct lc_cipher_ctx *cctx = NULL;
struct lc_auth_ctx *actx = NULL; struct lc_auth_ctx *actx = NULL;
struct lc_xchacha20_params cparams; struct lc_xchacha20_params cparams;

4
auth.c
View File

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

5
auth.h
View File

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

View File

@ -58,9 +58,9 @@ hmac_common_init(void *arg, const uint8_t *key, size_t keylen)
} }
static int static int
hmac_sha224_sha256_init(void *arg, const void *initparams) hmac_sha224_sha256_init(void *arg, void *initparams)
{ {
const struct lc_hmac_params *params = initparams; struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg; struct hmac_ctx *ctx = arg;
ctx->blocksz = LC_SHA256_BLOCKLEN; ctx->blocksz = LC_SHA256_BLOCKLEN;
@ -69,9 +69,9 @@ hmac_sha224_sha256_init(void *arg, const void *initparams)
} }
static int static int
hmac_sha384_sha512_init(void *arg, const void *initparams) hmac_sha384_sha512_init(void *arg, void *initparams)
{ {
const struct lc_hmac_params *params = initparams; struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg; struct hmac_ctx *ctx = arg;
ctx->blocksz = LC_SHA512_BLOCKLEN; ctx->blocksz = LC_SHA512_BLOCKLEN;

View File

@ -24,9 +24,9 @@
static int static int
poly1305_init(void *arg, const void *initparams) poly1305_init(void *arg, void *initparams)
{ {
const struct lc_poly1305_params *params = initparams; struct lc_poly1305_params *params = initparams;
struct poly1305_ctx *ctx = arg; struct poly1305_ctx *ctx = arg;
size_t i; size_t i;
uint32_t t0, t1, t2, t3; uint32_t t0, t1, t2, t3;
@ -135,7 +135,7 @@ poly1305_final(void *arg, uint8_t *out, size_t *outlen)
} }
static int static int
poly1305_auth(uint8_t *out, size_t *outlen, const void *initparams, poly1305_auth(uint8_t *out, size_t *outlen, void *initparams,
const uint8_t *in, size_t inlen) const uint8_t *in, size_t inlen)
{ {
struct poly1305_ctx ctx; struct poly1305_ctx ctx;

View File

@ -21,7 +21,7 @@
int int
lc_cipher_encrypt_init(struct lc_cipher_ctx *ctx, const void *initparams) lc_cipher_encrypt_init(struct lc_cipher_ctx *ctx, void *initparams)
{ {
return ctx->impl->encrypt_init(ctx->arg, initparams); return ctx->impl->encrypt_init(ctx->arg, initparams);
} }
@ -42,13 +42,13 @@ lc_cipher_encrypt_final(struct lc_cipher_ctx *ctx, uint8_t *out,
int int
lc_cipher_encrypt(const struct lc_cipher_impl *impl, uint8_t *out, lc_cipher_encrypt(const struct lc_cipher_impl *impl, uint8_t *out,
size_t *outlen, const void *initparams, const uint8_t *in, size_t inlen) size_t *outlen, void *initparams, const uint8_t *in, size_t inlen)
{ {
return impl->encrypt(out, outlen, initparams, in, inlen); return impl->encrypt(out, outlen, initparams, in, inlen);
} }
int int
lc_cipher_decrypt_init(struct lc_cipher_ctx *ctx, const void *initparams) lc_cipher_decrypt_init(struct lc_cipher_ctx *ctx, void *initparams)
{ {
return ctx->impl->decrypt_init(ctx->arg, initparams); return ctx->impl->decrypt_init(ctx->arg, initparams);
} }
@ -69,7 +69,7 @@ lc_cipher_decrypt_final(struct lc_cipher_ctx *ctx, uint8_t *out,
int int
lc_cipher_decrypt(const struct lc_cipher_impl *impl, uint8_t *out, lc_cipher_decrypt(const struct lc_cipher_impl *impl, uint8_t *out,
size_t *outlen, const void *initparams, const uint8_t *in, size_t inlen) size_t *outlen, void *initparams, const uint8_t *in, size_t inlen)
{ {
return impl->decrypt(out, outlen, initparams, in, inlen); return impl->decrypt(out, outlen, initparams, in, inlen);
} }

View File

@ -19,18 +19,18 @@
struct lc_cipher_impl { struct lc_cipher_impl {
int (*encrypt_init)(void *, const 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 *, size_t); const uint8_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 *, const void *, const uint8_t *, int (*encrypt)(uint8_t *, size_t *, void *, const uint8_t *,
size_t); size_t);
int (*decrypt_init)(void *, const 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 *, size_t); const uint8_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 *, const void *, const uint8_t *, int (*decrypt)(uint8_t *, size_t *, void *, const uint8_t *,
size_t); size_t);
void *(*ctx_new)(void); void *(*ctx_new)(void);

View File

@ -31,9 +31,9 @@
static int static int
chacha20_anycrypt_init(void *arg, const void *initparams) chacha20_anycrypt_init(void *arg, void *initparams)
{ {
const struct lc_chacha20_params *params = initparams; struct lc_chacha20_params *params = initparams;
struct chacha20_ctx *ctx = arg; struct chacha20_ctx *ctx = arg;
size_t i; size_t i;
@ -50,9 +50,9 @@ chacha20_anycrypt_init(void *arg, const void *initparams)
} }
static int static int
xchacha20_anycrypt_init(void *arg, const void *initparams) xchacha20_anycrypt_init(void *arg, void *initparams)
{ {
const struct lc_xchacha20_params *params = initparams; struct lc_xchacha20_params *params = initparams;
struct chacha20_ctx *ctx = arg; struct chacha20_ctx *ctx = arg;
size_t i; size_t i;
@ -180,7 +180,7 @@ chacha20_anycrypt_final(void *arg, uint8_t *out, size_t *outlen)
} }
static int static int
chacha20_anycrypt(uint8_t *out, size_t *outlen, const void *initparams, chacha20_anycrypt(uint8_t *out, size_t *outlen, void *initparams,
const uint8_t *in, size_t inlen) const uint8_t *in, size_t inlen)
{ {
struct chacha20_ctx ctx; struct chacha20_ctx ctx;

View File

@ -137,11 +137,11 @@ const struct lc_hash_impl *lc_hash_impl_sha512(void);
* Authentication. * Authentication.
*/ */
int lc_auth_init(struct lc_auth_ctx *, const void *); int lc_auth_init(struct lc_auth_ctx *, void *);
int lc_auth_update(struct lc_auth_ctx *, const uint8_t *, size_t); 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_final(struct lc_auth_ctx *, uint8_t *, size_t *);
int lc_auth(const struct lc_auth_impl *, uint8_t *, size_t *, int lc_auth(const struct lc_auth_impl *, uint8_t *, size_t *, void *,
const void *, const uint8_t *, size_t); const uint8_t *, size_t);
struct lc_auth_ctx *lc_auth_ctx_new(const struct lc_auth_impl *); struct lc_auth_ctx *lc_auth_ctx_new(const struct lc_auth_impl *);
void lc_auth_ctx_free(struct lc_auth_ctx *); void lc_auth_ctx_free(struct lc_auth_ctx *);
@ -157,18 +157,18 @@ const struct lc_auth_impl *lc_auth_impl_hmac_sha512(void);
* Ciphers. * Ciphers.
*/ */
int lc_cipher_encrypt_init(struct lc_cipher_ctx *, const void *); int lc_cipher_encrypt_init(struct lc_cipher_ctx *, void *);
int lc_cipher_encrypt_update(struct lc_cipher_ctx *, uint8_t *, size_t *, int lc_cipher_encrypt_update(struct lc_cipher_ctx *, uint8_t *, size_t *,
const uint8_t *, size_t); const uint8_t *, size_t);
int lc_cipher_encrypt_final(struct lc_cipher_ctx *, uint8_t *, size_t *); int lc_cipher_encrypt_final(struct lc_cipher_ctx *, uint8_t *, size_t *);
int lc_cipher_encrypt(const struct lc_cipher_impl *, uint8_t *, size_t *, int lc_cipher_encrypt(const struct lc_cipher_impl *, uint8_t *, size_t *,
const void *, const uint8_t *, size_t); void *, const uint8_t *, size_t);
int lc_cipher_decrypt_init(struct lc_cipher_ctx *, const void *); int lc_cipher_decrypt_init(struct lc_cipher_ctx *, void *);
int lc_cipher_decrypt_update(struct lc_cipher_ctx *, uint8_t *, size_t *, int lc_cipher_decrypt_update(struct lc_cipher_ctx *, uint8_t *, size_t *,
const uint8_t *, size_t); const uint8_t *, size_t);
int lc_cipher_decrypt_final(struct lc_cipher_ctx *, uint8_t *, size_t *); int lc_cipher_decrypt_final(struct lc_cipher_ctx *, uint8_t *, size_t *);
int lc_cipher_decrypt(const struct lc_cipher_impl *, uint8_t *, size_t *, int lc_cipher_decrypt(const struct lc_cipher_impl *, uint8_t *, size_t *,
const void *, const uint8_t *, size_t); void *, const uint8_t *, size_t);
struct lc_cipher_ctx *lc_cipher_ctx_new(const struct lc_cipher_impl *); struct lc_cipher_ctx *lc_cipher_ctx_new(const struct lc_cipher_impl *);
void lc_cipher_ctx_free(struct lc_cipher_ctx *); void lc_cipher_ctx_free(struct lc_cipher_ctx *);
@ -181,10 +181,10 @@ const struct lc_cipher_impl *lc_cipher_impl_xchacha20(void);
* Authenticated encryption with additional data. * Authenticated encryption with additional data.
*/ */
int lc_aead_seal(const struct lc_aead_impl *, uint8_t *, size_t *, int lc_aead_seal(const struct lc_aead_impl *, uint8_t *, size_t *, void *,
const void *, const uint8_t *, size_t, const uint8_t *, size_t); const uint8_t *, size_t, const uint8_t *, size_t);
int lc_aead_open(const struct lc_aead_impl *, uint8_t *, size_t *, int lc_aead_open(const struct lc_aead_impl *, uint8_t *, size_t *, void *,
const void *, const uint8_t *, size_t, const uint8_t *, size_t); const uint8_t *, size_t, const uint8_t *, size_t);
const struct lc_aead_impl *lc_aead_impl_chacha20_poly1305(void); const struct lc_aead_impl *lc_aead_impl_chacha20_poly1305(void);
const struct lc_aead_impl *lc_aead_impl_xchacha20_poly1305(void); const struct lc_aead_impl *lc_aead_impl_xchacha20_poly1305(void);

View File

@ -53,7 +53,7 @@ struct kwrunner {
static int aead_poly1305_runner(const struct lc_aead_impl *, static int aead_poly1305_runner(const struct lc_aead_impl *,
const struct testcase *, const void *, int); const struct testcase *, void *, int);
static int chacha20_poly1305_runner(const struct testcase *, int); static int chacha20_poly1305_runner(const struct testcase *, int);
static int xchacha20_poly1305_runner(const struct testcase *, int); static int xchacha20_poly1305_runner(const struct testcase *, int);
@ -266,7 +266,7 @@ main(int argc, char *argv[])
static int static int
aead_poly1305_runner(const struct lc_aead_impl *impl, const struct testcase *c, aead_poly1305_runner(const struct lc_aead_impl *impl, const struct testcase *c,
const void *params, int verbose) void *params, int verbose)
{ {
uint8_t *buf, *encout, *decout; uint8_t *buf, *encout, *decout;
size_t aeadlen, encoutlen, decoutlen; size_t aeadlen, encoutlen, decoutlen;