From b793cb5b69a04501e4cbf560f043d90d5ee1a233 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Vuotto Date: Fri, 7 Jun 2024 00:29:25 +0000 Subject: [PATCH] cipher/chacha20: rename chacha20_x to chacha20_common --- aead_chacha20_poly1305.c | 24 +++++++++++------------ cipher_chacha20.c | 41 ++++++++++++++++++++-------------------- cipher_chacha20.h | 14 +++++++------- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/aead_chacha20_poly1305.c b/aead_chacha20_poly1305.c index b137ab2..ae32dea 100644 --- a/aead_chacha20_poly1305.c +++ b/aead_chacha20_poly1305.c @@ -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++) poly1305_key[i] = 0; - if (!chacha20_x_init(&cctx, key, keylen, iv, ivlen) || - !chacha20_x_update(&cctx, poly1305_key, &olen, poly1305_key, + if (!chacha20_common_init(&cctx, key, keylen, iv, ivlen) || + !chacha20_common_update(&cctx, poly1305_key, &olen, poly1305_key, LC_POLY1305_KEYLEN)) return 0; - if (!chacha20_x_final(&cctx, poly1305_key + olen, &olen)) + if (!chacha20_common_final(&cctx, poly1305_key + olen, &olen)) return 0; 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))) 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; - if (!chacha20_x_update(&cctx, out, &olen, in, inlen)) + if (!chacha20_common_update(&cctx, out, &olen, in, inlen)) return 0; *outlen = olen; - if (!chacha20_x_final(&cctx, out + olen, &olen)) + if (!chacha20_common_final(&cctx, out + olen, &olen)) return 0; if (!poly1305_update(&pctx, out, inlen)) 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++) poly1305_key[i] = 0; - if (!chacha20_x_init(&cctx, key, keylen, iv, ivlen) || - !chacha20_x_update(&cctx, poly1305_key, &olen, poly1305_key, + if (!chacha20_common_init(&cctx, key, keylen, iv, ivlen) || + !chacha20_common_update(&cctx, poly1305_key, &olen, poly1305_key, LC_POLY1305_KEYLEN)) return 0; - if (!chacha20_x_final(&cctx, poly1305_key + olen, &olen)) + if (!chacha20_common_final(&cctx, poly1305_key + olen, &olen)) return 0; 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(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; - if (!chacha20_x_update(&cctx, out, &olen, in, ctlen)) + if (!chacha20_common_update(&cctx, out, &olen, in, ctlen)) return 0; *outlen = olen; - if (!chacha20_x_final(&cctx, out + olen, &olen)) + if (!chacha20_common_final(&cctx, out + olen, &olen)) return 0; *outlen += olen; diff --git a/cipher_chacha20.c b/cipher_chacha20.c index a5539f2..13b94ec 100644 --- a/cipher_chacha20.c +++ b/cipher_chacha20.c @@ -26,7 +26,7 @@ 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) { struct chacha20_ctx *ctx = arg; @@ -48,15 +48,15 @@ chacha20_x_init_from(void *arg, const uint8_t *key, size_t keylen, } 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) { - return chacha20_x_init_from(arg, key, keylen, iv, ivlen, 0); + return chacha20_common_init_from(arg, key, keylen, iv, ivlen, 0); } int -chacha20_x_update(void *arg, uint8_t *out, size_t *outlen, const uint8_t *in, - size_t inlen) +chacha20_common_update(void *arg, uint8_t *out, size_t *outlen, + const uint8_t *in, size_t inlen) { struct chacha20_ctx *ctx = arg; 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) return 0; - *outlen = (ctx->mlen + inlen) / CHACHA20_CHUNK * CHACHA20_CHUNK; + *outlen = ctx->mlen + inlen - ((ctx->mlen + inlen) % CHACHA20_CHUNK); if (out == NULL) return 1; @@ -117,7 +117,7 @@ chacha20_x_update(void *arg, uint8_t *out, size_t *outlen, const uint8_t *in, } 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; size_t i, off; @@ -150,8 +150,9 @@ chacha20_x_final(void *arg, uint8_t *out, size_t *outlen) } int -chacha20_x(const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen, - uint8_t *out, size_t *outlen, const uint8_t *in, size_t inlen) +chacha20_common(const uint8_t *key, size_t keylen, const uint8_t *iv, + size_t ivlen, uint8_t *out, size_t *outlen, const uint8_t *in, + size_t inlen) { struct chacha20_ctx ctx; 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; } - rc = chacha20_x_init(&ctx, key, keylen, iv, ivlen) && - chacha20_x_update(&ctx, out, &l0, in, inlen) & - chacha20_x_final(&ctx, out + l0, &l1); + rc = chacha20_common_init(&ctx, key, keylen, iv, ivlen) && + chacha20_common_update(&ctx, out, &l0, in, inlen) & + chacha20_common_final(&ctx, out + l0, &l1); if (rc) *outlen = l0 + l1; @@ -186,15 +187,15 @@ chacha20_ctx_new(void) static struct lc_cipher_impl chacha20_impl = { - .encrypt_init = &chacha20_x_init, - .encrypt_update = &chacha20_x_update, - .encrypt_final = &chacha20_x_final, - .encrypt = &chacha20_x, + .encrypt_init = &chacha20_common_init, + .encrypt_update = &chacha20_common_update, + .encrypt_final = &chacha20_common_final, + .encrypt = &chacha20_common, - .decrypt_init = &chacha20_x_init, - .decrypt_update = &chacha20_x_update, - .decrypt_final = &chacha20_x_final, - .decrypt = &chacha20_x, + .decrypt_init = &chacha20_common_init, + .decrypt_update = &chacha20_common_update, + .decrypt_final = &chacha20_common_final, + .decrypt = &chacha20_common, .ctx_new = &chacha20_ctx_new, .ctx_free = NULL, diff --git a/cipher_chacha20.h b/cipher_chacha20.h index 3c8c1ac..c0ebda3 100644 --- a/cipher_chacha20.h +++ b/cipher_chacha20.h @@ -18,12 +18,12 @@ #include -int chacha20_x_init_from(void *, const uint8_t *, size_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_from(void *, const uint8_t *, size_t, + const uint8_t *, size_t, uint32_t); +int chacha20_common_init(void *, const uint8_t *, size_t, const uint8_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); -int chacha20_x_final(void *, uint8_t *, size_t *); -int chacha20_x(const uint8_t *, size_t, const uint8_t *, size_t, uint8_t *, - size_t *, const uint8_t *, size_t); +int chacha20_common_final(void *, uint8_t *, size_t *); +int chacha20_common(const uint8_t *, size_t, const uint8_t *, size_t, + uint8_t *, size_t *, const uint8_t *, size_t);