cipher/chacha20: rename chacha20_x to chacha20_common

This commit is contained in:
Lucas Gabriel Vuotto 2024-06-07 00:29:25 +00:00
parent e5215ac18e
commit b793cb5b69
3 changed files with 40 additions and 39 deletions

View File

@ -60,11 +60,11 @@ chacha20_poly1305_seal(const uint8_t *key, size_t keylen, const uint8_t *iv,
for (i = 0; i < LC_POLY1305_KEYLEN; i++) for (i = 0; i < LC_POLY1305_KEYLEN; i++)
poly1305_key[i] = 0; poly1305_key[i] = 0;
if (!chacha20_x_init(&cctx, key, keylen, iv, ivlen) || if (!chacha20_common_init(&cctx, key, keylen, iv, ivlen) ||
!chacha20_x_update(&cctx, poly1305_key, &olen, poly1305_key, !chacha20_common_update(&cctx, poly1305_key, &olen, poly1305_key,
LC_POLY1305_KEYLEN)) LC_POLY1305_KEYLEN))
return 0; return 0;
if (!chacha20_x_final(&cctx, poly1305_key + olen, &olen)) if (!chacha20_common_final(&cctx, poly1305_key + olen, &olen))
return 0; return 0;
if (!poly1305_init(&pctx, poly1305_key, LC_POLY1305_KEYLEN) || if (!poly1305_init(&pctx, poly1305_key, LC_POLY1305_KEYLEN) ||
@ -74,12 +74,12 @@ chacha20_poly1305_seal(const uint8_t *key, size_t keylen, const uint8_t *iv,
if (!poly1305_update(&pctx, zeropad, 16 - (aadlen % 16))) if (!poly1305_update(&pctx, zeropad, 16 - (aadlen % 16)))
return 0; return 0;
if (!chacha20_x_init_from(&cctx, key, keylen, iv, ivlen, 1)) if (!chacha20_common_init_from(&cctx, key, keylen, iv, ivlen, 1))
return 0; return 0;
if (!chacha20_x_update(&cctx, out, &olen, in, inlen)) if (!chacha20_common_update(&cctx, out, &olen, in, inlen))
return 0; return 0;
*outlen = olen; *outlen = olen;
if (!chacha20_x_final(&cctx, out + olen, &olen)) if (!chacha20_common_final(&cctx, out + olen, &olen))
return 0; return 0;
if (!poly1305_update(&pctx, out, inlen)) if (!poly1305_update(&pctx, out, inlen))
return 0; return 0;
@ -136,11 +136,11 @@ chacha20_poly1305_open(const uint8_t *key, size_t keylen, const uint8_t *iv,
for (i = 0; i < LC_POLY1305_KEYLEN; i++) for (i = 0; i < LC_POLY1305_KEYLEN; i++)
poly1305_key[i] = 0; poly1305_key[i] = 0;
if (!chacha20_x_init(&cctx, key, keylen, iv, ivlen) || if (!chacha20_common_init(&cctx, key, keylen, iv, ivlen) ||
!chacha20_x_update(&cctx, poly1305_key, &olen, poly1305_key, !chacha20_common_update(&cctx, poly1305_key, &olen, poly1305_key,
LC_POLY1305_KEYLEN)) LC_POLY1305_KEYLEN))
return 0; return 0;
if (!chacha20_x_final(&cctx, poly1305_key + olen, &olen)) if (!chacha20_common_final(&cctx, poly1305_key + olen, &olen))
return 0; return 0;
if (!poly1305_init(&pctx, poly1305_key, LC_POLY1305_KEYLEN) || if (!poly1305_init(&pctx, poly1305_key, LC_POLY1305_KEYLEN) ||
@ -168,12 +168,12 @@ chacha20_poly1305_open(const uint8_t *key, size_t keylen, const uint8_t *iv,
lc_scrub(buf, sizeof(buf)); lc_scrub(buf, sizeof(buf));
lc_scrub(poly1305_key, sizeof(poly1305_key)); lc_scrub(poly1305_key, sizeof(poly1305_key));
if (!chacha20_x_init_from(&cctx, key, keylen, iv, ivlen, 1)) if (!chacha20_common_init_from(&cctx, key, keylen, iv, ivlen, 1))
return 0; return 0;
if (!chacha20_x_update(&cctx, out, &olen, in, ctlen)) if (!chacha20_common_update(&cctx, out, &olen, in, ctlen))
return 0; return 0;
*outlen = olen; *outlen = olen;
if (!chacha20_x_final(&cctx, out + olen, &olen)) if (!chacha20_common_final(&cctx, out + olen, &olen))
return 0; return 0;
*outlen += olen; *outlen += olen;

View File

@ -26,7 +26,7 @@
int int
chacha20_x_init_from(void *arg, const uint8_t *key, size_t keylen, chacha20_common_init_from(void *arg, const uint8_t *key, size_t keylen,
const uint8_t *iv, size_t ivlen, uint32_t counter) const uint8_t *iv, size_t ivlen, uint32_t counter)
{ {
struct chacha20_ctx *ctx = arg; struct chacha20_ctx *ctx = arg;
@ -48,15 +48,15 @@ chacha20_x_init_from(void *arg, const uint8_t *key, size_t keylen,
} }
int int
chacha20_x_init(void *arg, const uint8_t *key, size_t keylen, chacha20_common_init(void *arg, const uint8_t *key, size_t keylen,
const uint8_t *iv, size_t ivlen) const uint8_t *iv, size_t ivlen)
{ {
return chacha20_x_init_from(arg, key, keylen, iv, ivlen, 0); return chacha20_common_init_from(arg, key, keylen, iv, ivlen, 0);
} }
int int
chacha20_x_update(void *arg, uint8_t *out, size_t *outlen, const uint8_t *in, chacha20_common_update(void *arg, uint8_t *out, size_t *outlen,
size_t inlen) const uint8_t *in, size_t inlen)
{ {
struct chacha20_ctx *ctx = arg; struct chacha20_ctx *ctx = arg;
size_t i, blocks; size_t i, blocks;
@ -69,7 +69,7 @@ chacha20_x_update(void *arg, uint8_t *out, size_t *outlen, const uint8_t *in,
if (blocks + ctx->c > CHACHA20_CTRMAX) if (blocks + ctx->c > CHACHA20_CTRMAX)
return 0; return 0;
*outlen = (ctx->mlen + inlen) / CHACHA20_CHUNK * CHACHA20_CHUNK; *outlen = ctx->mlen + inlen - ((ctx->mlen + inlen) % CHACHA20_CHUNK);
if (out == NULL) if (out == NULL)
return 1; return 1;
@ -117,7 +117,7 @@ chacha20_x_update(void *arg, uint8_t *out, size_t *outlen, const uint8_t *in,
} }
int int
chacha20_x_final(void *arg, uint8_t *out, size_t *outlen) chacha20_common_final(void *arg, uint8_t *out, size_t *outlen)
{ {
struct chacha20_ctx *ctx = arg; struct chacha20_ctx *ctx = arg;
size_t i, off; size_t i, off;
@ -150,8 +150,9 @@ chacha20_x_final(void *arg, uint8_t *out, size_t *outlen)
} }
int int
chacha20_x(const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen, chacha20_common(const uint8_t *key, size_t keylen, const uint8_t *iv,
uint8_t *out, size_t *outlen, const uint8_t *in, size_t inlen) size_t ivlen, uint8_t *out, size_t *outlen, const uint8_t *in,
size_t inlen)
{ {
struct chacha20_ctx ctx; struct chacha20_ctx ctx;
size_t l0, l1; size_t l0, l1;
@ -168,9 +169,9 @@ chacha20_x(const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
return 1; return 1;
} }
rc = chacha20_x_init(&ctx, key, keylen, iv, ivlen) && rc = chacha20_common_init(&ctx, key, keylen, iv, ivlen) &&
chacha20_x_update(&ctx, out, &l0, in, inlen) & chacha20_common_update(&ctx, out, &l0, in, inlen) &
chacha20_x_final(&ctx, out + l0, &l1); chacha20_common_final(&ctx, out + l0, &l1);
if (rc) if (rc)
*outlen = l0 + l1; *outlen = l0 + l1;
@ -186,15 +187,15 @@ chacha20_ctx_new(void)
static struct lc_cipher_impl chacha20_impl = { static struct lc_cipher_impl chacha20_impl = {
.encrypt_init = &chacha20_x_init, .encrypt_init = &chacha20_common_init,
.encrypt_update = &chacha20_x_update, .encrypt_update = &chacha20_common_update,
.encrypt_final = &chacha20_x_final, .encrypt_final = &chacha20_common_final,
.encrypt = &chacha20_x, .encrypt = &chacha20_common,
.decrypt_init = &chacha20_x_init, .decrypt_init = &chacha20_common_init,
.decrypt_update = &chacha20_x_update, .decrypt_update = &chacha20_common_update,
.decrypt_final = &chacha20_x_final, .decrypt_final = &chacha20_common_final,
.decrypt = &chacha20_x, .decrypt = &chacha20_common,
.ctx_new = &chacha20_ctx_new, .ctx_new = &chacha20_ctx_new,
.ctx_free = NULL, .ctx_free = NULL,

View File

@ -18,12 +18,12 @@
#include <stdint.h> #include <stdint.h>
int chacha20_x_init_from(void *, const uint8_t *, size_t, const uint8_t *, int chacha20_common_init_from(void *, const uint8_t *, size_t,
size_t, uint32_t); const uint8_t *, size_t, uint32_t);
int chacha20_x_init(void *, const uint8_t *, size_t, const uint8_t *, int chacha20_common_init(void *, const uint8_t *, size_t, const uint8_t *,
size_t); size_t);
int chacha20_x_update(void *, uint8_t *, size_t *, const uint8_t *, int chacha20_common_update(void *, uint8_t *, size_t *, const uint8_t *,
size_t); size_t);
int chacha20_x_final(void *, uint8_t *, size_t *); int chacha20_common_final(void *, uint8_t *, size_t *);
int chacha20_x(const uint8_t *, size_t, const uint8_t *, size_t, uint8_t *, int chacha20_common(const uint8_t *, size_t, const uint8_t *, size_t,
size_t *, const uint8_t *, size_t); uint8_t *, size_t *, const uint8_t *, size_t);