From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.16 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796194268; b=WoX1tO5X3GenqOUtT1Imt+imkorzdKXjcP2GPhO/VFTwBy1blVW5FMC6DshpB1vsk4+9 pAUAMWXRl1xM3s9MS6nQzzlgvkG5LNbonOQ47L7xslJZ8vM3JgWsKZaZiT3BkmqoBYYQ7 20Ar2F4+mldtlx6KnNuWcry4rZdOYK9NIJouy4NVvv+g0cjanlJPaGkDPYF01cLb3YF0X v8SG3GE73IEvIeXGmpG0p67tqAZeal5YOXnz/dCgz0rtsByPoHsz+LnN9lj1yMdJ372PK wF86nPq5Pjniuy1/ZKZdu8jKDrieuIZbGs2NYOdwgjHJT5ImLI39cQ/40VrbaV1vDtw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796194268; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-ZohoMailClient; bh=7Pyz85vhC7iI8VbhXWk8w3EU5g6Bti84QOpvtCWIgl8=; b=hjmsovxfPwnme3uSNk2hURUsqk7Sy86V6C0jV/05nzOUvEqhZyqQmZ8LzqcW0PhGKBnb pJ1Z3mbehT6D1csQasQKco/BM/aOCOP1jEMt45H2CcvKNEHNBZD2r3HvfbRrb5hjuEj2d 56/wGCK035HJIg9/V7RZkBF7f1EvGq65sWhuDWA6TEOrA+KsCJ2W2Q3LPFfPYqcKPga2T KjEacGuJrnIdk4YduRpSJ1gY1ONs6fvjlREsHUxqU9yTF7OVNCeUH4IweiXzzAdhxLaFJ 53/yKMOJHuSql4FAtqLbD93pNRGSZaNrJZi1KhGkacXrarYhL0IU0G6MAeN1M67kvGw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=laveeshbansal.com; spf=pass smtp.mailfrom=laveeshbansal.com; dkim=pass header.d=laveeshbansal.com header.i=@laveeshbansal.com header.a=rsa-sha256 header.s=zoho header.b=WS4mvLSI; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.16 Received: from sender6-op-o16.zoho.com (sender6-op-o16.zoho.com [165.173.180.16]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1026E606A31A; Wed, 2 Dec 2026 06:51:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1026E606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=laveeshbansal.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=laveeshbansal.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=laveeshbansal.com header.i=@laveeshbansal.com header.a=rsa-sha256 header.s=zoho header.b=WS4mvLSI ARC-Seal: i=1; a=rsa-sha256; t=1796194231; cv=none; d=zohomail.com; s=zohoarc; b=ZZAX4GLJfidHz2yVAPNeuLial9gey83ab3vfN7oQfi/8VQJAOD08yDs2cc+Ttqk09GLqQe0DKvhC3ZcpojkaLpENI/TMrSZDFzx3yt+Vmg0uInRyBOw1keM3+ErF6efr+IGSGsnZeT0FDr6OwCxA2C5rAZuHHS04vuZ8Vah2ej4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796194231; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=7Pyz85vhC7iI8VbhXWk8w3EU5g6Bti84QOpvtCWIgl8=; b=ezWebHSnw24TbkOZSi7I4zBYpMtqWgdaR0PWKioknuYGo0DFomYAgfpe2A8OEmrglANQGNpN/REr3FI7PppGLbh9hV/s0T1buepbm8/NSqsGXEVUB1AaFKPojoQil+9+G+dog2TloaBd093TSkUvCv59IenYsJZ3wri4VoNoMGc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=laveeshbansal.com; spf=pass smtp.mailfrom=laveeshb@laveeshbansal.com; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=WS4mvLSIBsbEvOpiiuh6kPEPQMvwmr3BHAIU6dZdNAFabwi8/8PGBV9OiHb+p2DZWU2wvRIErtCyUx9RH5mLrPRIKHKXsHBK1VZCzPQYlcN7fREQ/hkHpcChP01D23PXzTm5d0u4iPUQyw2Vkjdu9XG1nO5RT5rTcVLR66yayVk=; c=relaxed/relaxed; s=zoho; d=laveeshbansal.com; v=1; bh=7Pyz85vhC7iI8VbhXWk8w3EU5g6Bti84QOpvtCWIgl8=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796194226652153.7996040952968; Tue, 1 Dec 2026 22:50:26 -0800 (PST) From: Laveesh Bansal To: Eric Biggers Cc: Herbert Xu , "David S. Miller" , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/2] crypto: adiantum - Use the ChaCha library Date: Tue, 1 Dec 2026 22:50:22 -0800 Message-ID: <20261202065022.46893-3-laveeshb@laveeshbansal.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261202065022.46893-1-laveeshb@laveeshbansal.com> References: <20261202065022.46893-1-laveeshb@laveeshbansal.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External adiantum runs the XChaCha step through a child crypto_skcipher. That needs a spawn, a child tfm and a request sized for the child; adiantum_setkey() puts a request on the stack for it. SYNC_SKCIPHER_REQUEST_ON_STACK() reserves much more stack than it needs. Call the ChaCha library directly and remove the child. The library works on plain buffers, so adiantum walks the src and dst scatterlists itself. adiantum_setkey() only needs a small chacha state on the stack. adiantum is no longer a template; only adiantum(xchacha12,aes) and adiantum(xchacha20,aes) are accepted. Suggested-by: Eric Biggers Signed-off-by: Laveesh Bansal --- Notes: 1. A side effect of this patch is that an odd combination such as adiantum(xchacha12-lib,aes) now fails with "not found" instead of "already exists". 2. There are already two places where we have similar code to do xchacha initialization -- xchacha_init() in lib/crypto/chacha20poly1305.c and crypto_xchacha_crypt() in crypto/chacha.c. To keep the patch focused, I have added another helper that does similar logic -- adiantum_xchacha_init() in crypto/adiantum.c. We should consolidate these into chacha library as a follow up. 3. I did the following tests to verify this patch after applying setkey, aeslib and patch 1 to the base first: Selftests: I ran these on arm64, x86_64 and big-endian s390x (all QEMU). In my setup, I had enabled CRYPTO_SELFTESTS on all 3; KASAN and lockdep only on arm64 and x86_64. They passed for both xchacha12 and xchacha20. I did not see any KASAN or lockdep reports on arm64 or x86_64. Random key test: I ran 400 random inputs per algorithm on the same setups as the selftests. I got the same digest for each algorithm with and without the patch. Split tests: I ran a test module on arm64 and x86_64 that splits src and dst at random offsets - in place and out of place - and compares the result with a plain buffer. They match. HIGHMEM test: I tested on a 32-bit x86 kernel with CONFIG_HIGHMEM=y. I used a test module that puts the data in highmem pages. For in-place requests, chacha_crypt() now gets the same address for src and dst. The results match the run without this patch. Speed test: I ran the tcrypt adiantum speed test on arm64 in a KVM guest with debug options off. I did not find any slowness in performance when compared with results on base + setkey + aeslib. I can share specific test results if anybody is interested. crypto/Kconfig | 4 +- crypto/adiantum.c | 415 ++++++++++++++++++++-------------------------- 2 files changed, 180 insertions(+), 239 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index 7cac0141a73cb..6f647f8fba9cf 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -578,12 +578,12 @@ menu "Length-preserving ciphers and modes" config CRYPTO_ADIANTUM tristate "Adiantum" - select CRYPTO_CHACHA20 select CRYPTO_LIB_AES + select CRYPTO_LIB_CHACHA select CRYPTO_LIB_NH select CRYPTO_LIB_POLY1305 select CRYPTO_LIB_POLY1305_GENERIC - select CRYPTO_MANAGER + select CRYPTO_SKCIPHER help Adiantum tweakable, length-preserving encryption mode diff --git a/crypto/adiantum.c b/crypto/adiantum.c index 5e16d2c4699b6..5ee65c8ae1fdb 100644 --- a/crypto/adiantum.c +++ b/crypto/adiantum.c @@ -27,6 +27,7 @@ #include #include #include +#include /* Size of the hash key (K_H) in bytes */ #define HASH_KEY_SIZE (2 * POLY1305_BLOCK_SIZE + NH_KEY_BYTES) @@ -39,14 +40,12 @@ * needing to derive per-file keys. So instead we use two blocks, or 32 bytes. */ #define TWEAK_SIZE 32 - -struct adiantum_instance_ctx { - struct crypto_skcipher_spawn streamcipher_spawn; -}; +#define XCHACHA_NONCE_SIZE 24 struct adiantum_tfm_ctx { struct aes_key aes; - struct crypto_skcipher *streamcipher; + u32 stream_key[CHACHA_KEY_WORDS]; + int nrounds; union { struct { struct poly1305_core_key header_hash_key; @@ -75,16 +74,23 @@ struct nhpoly1305_ctx { __le64 nh_hash[NH_NUM_PASSES]; }; -struct adiantum_request_ctx { - /* - * skcipher sub-request size is unknown at compile-time, so it needs to - * go after the members with known sizes. - */ - union { - struct nhpoly1305_ctx hash_ctx; - struct skcipher_request streamcipher_req; - } u; -}; +/* Initialize @state for XChaCha, starting at stream position 0. */ +static void adiantum_xchacha_init(struct chacha_state *state, + const u32 key[CHACHA_KEY_WORDS], int nrounds, + const u8 nonce[XCHACHA_NONCE_SIZE]) +{ + u32 subkey[HCHACHA_OUT_WORDS]; + u8 real_iv[CHACHA_IV_SIZE] = {}; + + chacha_init(state, key, nonce); + hchacha_block(state, subkey, nrounds); + + memcpy(&real_iv[8], nonce + 16, 8); + + chacha_init(state, subkey, real_iv); + + memzero_explicit(subkey, sizeof(subkey)); +} /* * Given the XChaCha stream key K_S, derive the block cipher key K_E and the @@ -95,35 +101,27 @@ struct adiantum_request_ctx { * Note that this denotes using bits from the XChaCha keystream, which here we * get indirectly by encrypting a buffer containing all 0's. */ -static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key, - unsigned int keylen) +static int adiantum_setkey(struct crypto_skcipher *tfm, + const u8 key[CHACHA_KEY_SIZE], int nrounds) { struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); - SYNC_SKCIPHER_REQUEST_ON_STACK( - req, (struct crypto_sync_skcipher *)tctx->streamcipher); u8 poly_keys[2 * POLY1305_BLOCK_SIZE]; - u8 iv[XCHACHA_IV_SIZE] = { 1 }; - struct scatterlist sg; + static const u8 nonce[XCHACHA_NONCE_SIZE] = { 1 }; + struct chacha_state state; int err; + tctx->nrounds = nrounds; + /* Set the stream cipher key (K_S) */ - crypto_skcipher_clear_flags(tctx->streamcipher, CRYPTO_TFM_REQ_MASK); - crypto_skcipher_set_flags(tctx->streamcipher, - crypto_skcipher_get_flags(tfm) & - CRYPTO_TFM_REQ_MASK); - err = crypto_skcipher_setkey(tctx->streamcipher, key, keylen); - if (err) - return err; + for (int i = 0; i < CHACHA_KEY_WORDS; i++) + tctx->stream_key[i] = get_unaligned_le32(key + 4 * i); /* Derive the subkeys */ memset(tctx->derived_keys, 0, sizeof(tctx->derived_keys)); - sg_init_one(&sg, tctx->derived_keys, sizeof(tctx->derived_keys)); - skcipher_request_set_callback(req, 0, NULL, NULL); - skcipher_request_set_crypt(req, &sg, &sg, sizeof(tctx->derived_keys), - iv); - err = crypto_skcipher_encrypt(req); - if (err) - goto out; + adiantum_xchacha_init(&state, tctx->stream_key, nrounds, nonce); + chacha_crypt(&state, tctx->derived_keys, tctx->derived_keys, + sizeof(tctx->derived_keys), nrounds); + chacha_zeroize_state(&state); /* Set the block cipher key (K_E) */ err = aes_preparekey(&tctx->aes, tctx->derived_keys, AES_KEYSIZE_256); @@ -145,10 +143,21 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key, if (err) memzero_explicit(tctx->derived_keys, sizeof(tctx->derived_keys)); - skcipher_request_zero(req); return err; } +static int adiantum_setkey_xchacha12(struct crypto_skcipher *tfm, const u8 *key, + unsigned int keylen) +{ + return adiantum_setkey(tfm, key, 12); +} + +static int adiantum_setkey_xchacha20(struct crypto_skcipher *tfm, const u8 *key, + unsigned int keylen) +{ + return adiantum_setkey(tfm, key, 20); +} + /* Addition in Z/(2^{128}Z) */ static inline void le128_add(le128 *r, const le128 *v1, const le128 *v2) { @@ -329,27 +338,86 @@ static void adiantum_hash_message(struct skcipher_request *req, { struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); - struct adiantum_request_ctx *rctx = skcipher_request_ctx(req); + struct nhpoly1305_ctx *hash_ctx = skcipher_request_ctx(req); unsigned int len = req->cryptlen - AES_BLOCK_SIZE; struct scatter_walk walk; - nhpoly1305_init(&rctx->u.hash_ctx); + nhpoly1305_init(hash_ctx); scatterwalk_start(&walk, sgl); while (len) { unsigned int n = scatterwalk_next(&walk, len); - nhpoly1305_update(&rctx->u.hash_ctx, tctx, walk.addr, n); + nhpoly1305_update(hash_ctx, tctx, walk.addr, n); scatterwalk_done_src(&walk, n); len -= n; } - nhpoly1305_final(&rctx->u.hash_ctx, tctx, out); + nhpoly1305_final(hash_ctx, tctx, out); +} + +static void adiantum_stream_xor(struct chacha_state *state, int nrounds, + struct scatterlist *src, + struct scatterlist *dst, unsigned int len) +{ + struct scatter_walk swalk, dwalk; + + scatterwalk_start(&swalk, src); + scatterwalk_start(&dwalk, dst); + while (len) { + unsigned int n = min(scatterwalk_clamp(&swalk, len), + scatterwalk_clamp(&dwalk, len)); + + /* + * The ChaCha counter advances in whole blocks, so every + * step except the last must be a whole number of blocks. + */ + if (n < len) + n = round_down(n, CHACHA_BLOCK_SIZE); + if (n) { + const u8 *src_virt; + u8 *dst_virt; + + scatterwalk_map(&dwalk); + dst_virt = dwalk.addr; + + /* In-place: map the memory only once. */ + if (IS_ENABLED(CONFIG_HIGHMEM) && + offset_in_page(swalk.offset) == + offset_in_page(dwalk.offset) && + sg_page(swalk.sg) + swalk.offset / PAGE_SIZE == + sg_page(dwalk.sg) + dwalk.offset / PAGE_SIZE) { + src_virt = dst_virt; + } else { + scatterwalk_map(&swalk); + src_virt = swalk.addr; + } + + chacha_crypt(state, dst_virt, src_virt, n, nrounds); + + if (src_virt != dst_virt) + scatterwalk_unmap(&swalk); + + scatterwalk_advance(&swalk, n); + scatterwalk_done_dst(&dwalk, n); + } else { + u8 buf[CHACHA_BLOCK_SIZE] __aligned(sizeof(long)); + + n = min(len, CHACHA_BLOCK_SIZE); + + memcpy_from_scatterwalk(buf, &swalk, n); + chacha_crypt(state, buf, buf, n, nrounds); + memcpy_to_scatterwalk(&dwalk, buf, n); + + memzero_explicit(buf, sizeof(buf)); + } + len -= n; + } } static int adiantum_crypt(struct skcipher_request *req, bool enc) { struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); - struct adiantum_request_ctx *rctx = skcipher_request_ctx(req); + struct nhpoly1305_ctx *hash_ctx = skcipher_request_ctx(req); const unsigned int bulk_len = req->cryptlen - AES_BLOCK_SIZE; struct scatterlist *src = req->src, *dst = req->dst; /* @@ -358,16 +426,14 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc) * P_L => P_M => C_M => C_R when encrypting, or * C_R => C_M => P_M => P_L when decrypting. * - * Also used to build the IV for the stream cipher. + * Also used to build the nonce for the stream cipher. */ union { - u8 bytes[XCHACHA_IV_SIZE]; - __le32 words[XCHACHA_IV_SIZE / sizeof(__le32)]; + u8 bytes[XCHACHA_NONCE_SIZE]; + __le32 words[XCHACHA_NONCE_SIZE / sizeof(__le32)]; le128 bignum; /* interpret as element of Z/(2^{128}Z) */ } rbuf; le128 header_hash, msg_hash; - unsigned int stream_len; - int err; if (req->cryptlen < AES_BLOCK_SIZE) return -EINVAL; @@ -383,9 +449,9 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc) /* Fast path for single-page source */ void *virt = kmap_local_page(sg_page(src)) + src->offset; - nhpoly1305_init(&rctx->u.hash_ctx); - nhpoly1305_update(&rctx->u.hash_ctx, tctx, virt, bulk_len); - nhpoly1305_final(&rctx->u.hash_ctx, tctx, &msg_hash); + nhpoly1305_init(hash_ctx); + nhpoly1305_update(hash_ctx, tctx, virt, bulk_len); + nhpoly1305_final(hash_ctx, tctx, &msg_hash); memcpy(&rbuf.bignum, virt + bulk_len, sizeof(le128)); kunmap_local(virt); } else { @@ -400,35 +466,34 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc) if (enc) aes_encrypt(&tctx->aes, rbuf.bytes, rbuf.bytes); - /* Initialize the rest of the XChaCha IV (first part is C_M) */ + /* Initialize the rest of the XChaCha nonce (first part is C_M) */ BUILD_BUG_ON(AES_BLOCK_SIZE != 16); - BUILD_BUG_ON(XCHACHA_IV_SIZE != 32); /* nonce || stream position */ rbuf.words[4] = cpu_to_le32(1); rbuf.words[5] = 0; - rbuf.words[6] = 0; - rbuf.words[7] = 0; - /* - * XChaCha needs to be done on all the data except the last 16 bytes; - * for disk encryption that usually means 4080 or 496 bytes. But ChaCha - * implementations tend to be most efficient when passed a whole number - * of 64-byte ChaCha blocks, or sometimes even a multiple of 256 bytes. - * And here it doesn't matter whether the last 16 bytes are written to, - * as the second hash step will overwrite them. Thus, round the XChaCha - * length up to the next 64-byte boundary if possible. - */ - stream_len = bulk_len; - if (round_up(stream_len, CHACHA_BLOCK_SIZE) <= req->cryptlen) - stream_len = round_up(stream_len, CHACHA_BLOCK_SIZE); - - skcipher_request_set_tfm(&rctx->u.streamcipher_req, tctx->streamcipher); - skcipher_request_set_crypt(&rctx->u.streamcipher_req, req->src, - req->dst, stream_len, &rbuf); - skcipher_request_set_callback(&rctx->u.streamcipher_req, - req->base.flags, NULL, NULL); - err = crypto_skcipher_encrypt(&rctx->u.streamcipher_req); - if (err) - return err; + if (bulk_len) { + struct chacha_state state; + unsigned int stream_len = bulk_len; + + /* + * XChaCha needs to be done on all the data except the last + * 16 bytes; for disk encryption that usually means 4080 or 496 + * bytes. But ChaCha implementations tend to be most efficient + * when passed a whole number of 64-byte ChaCha blocks, or + * sometimes even a multiple of 256 bytes. And here it doesn't + * matter whether the last 16 bytes are written to, as the + * second hash step will overwrite them. Thus, round the + * XChaCha length up to the next 64-byte boundary if possible. + */ + if (round_up(stream_len, CHACHA_BLOCK_SIZE) <= req->cryptlen) + stream_len = round_up(stream_len, CHACHA_BLOCK_SIZE); + + adiantum_xchacha_init(&state, tctx->stream_key, tctx->nrounds, + rbuf.bytes); + adiantum_stream_xor(&state, tctx->nrounds, src, dst, + stream_len); + chacha_zeroize_state(&state); + } /* If decrypting, decrypt C_M with the block cipher to get P_M */ if (!enc) @@ -446,9 +511,9 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc) struct page *page = sg_page(dst); void *virt = kmap_local_page(page) + dst->offset; - nhpoly1305_init(&rctx->u.hash_ctx); - nhpoly1305_update(&rctx->u.hash_ctx, tctx, virt, bulk_len); - nhpoly1305_final(&rctx->u.hash_ctx, tctx, &msg_hash); + nhpoly1305_init(hash_ctx); + nhpoly1305_update(hash_ctx, tctx, virt, bulk_len); + nhpoly1305_final(hash_ctx, tctx, &msg_hash); le128_sub(&rbuf.bignum, &rbuf.bignum, &msg_hash); memcpy(virt + bulk_len, &rbuf.bignum, sizeof(le128)); flush_dcache_page(page); @@ -472,173 +537,47 @@ static int adiantum_decrypt(struct skcipher_request *req) return adiantum_crypt(req, false); } -static int adiantum_init_tfm(struct crypto_skcipher *tfm) -{ - struct skcipher_instance *inst = skcipher_alg_instance(tfm); - struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst); - struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); - struct crypto_skcipher *streamcipher; - - streamcipher = crypto_spawn_skcipher(&ictx->streamcipher_spawn); - if (IS_ERR(streamcipher)) - return PTR_ERR(streamcipher); - if (WARN_ON(crypto_skcipher_reqsize(streamcipher) > - MAX_SYNC_SKCIPHER_REQSIZE)) { - crypto_free_skcipher(streamcipher); - return -EINVAL; - } - - tctx->streamcipher = streamcipher; - - BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) != - sizeof(struct adiantum_request_ctx)); - crypto_skcipher_set_reqsize( - tfm, max(sizeof(struct adiantum_request_ctx), - offsetofend(struct adiantum_request_ctx, - u.streamcipher_req) + - crypto_skcipher_reqsize(streamcipher))); - return 0; -} - -static void adiantum_exit_tfm(struct crypto_skcipher *tfm) -{ - struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); - - crypto_free_skcipher(tctx->streamcipher); -} - -static void adiantum_free_instance(struct skcipher_instance *inst) -{ - struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst); - - crypto_drop_skcipher(&ictx->streamcipher_spawn); - kfree(inst); -} - -/* - * Check for a supported set of inner algorithms. - * See the comment at the beginning of this file. - */ -static bool -adiantum_supported_algorithms(struct skcipher_alg_common *streamcipher_alg, - const char *blockcipher_name) -{ - if (strcmp(streamcipher_alg->base.cra_name, "xchacha12") != 0 && - strcmp(streamcipher_alg->base.cra_name, "xchacha20") != 0) - return false; - - if (strcmp(blockcipher_name, "aes") != 0 && - /* This is to support the usage by testmgr. */ - strcmp(blockcipher_name, "aes-lib") != 0) - return false; - - return true; -} - -static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) -{ - u32 mask; - struct skcipher_instance *inst; - struct adiantum_instance_ctx *ictx; - struct skcipher_alg_common *streamcipher_alg; - const char *blockcipher_name; - int err; - - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask); - if (err) - return err; - - inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); - if (!inst) - return -ENOMEM; - ictx = skcipher_instance_ctx(inst); - - /* Stream cipher: "xchacha12" or "xchacha20" */ - err = crypto_grab_skcipher(&ictx->streamcipher_spawn, - skcipher_crypto_instance(inst), - crypto_attr_alg_name(tb[1]), 0, - mask | CRYPTO_ALG_ASYNC /* sync only */ | - CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE); - if (err) - goto err_free_inst; - streamcipher_alg = crypto_spawn_skcipher_alg_common(&ictx->streamcipher_spawn); - - /* Block cipher: "aes" */ - blockcipher_name = crypto_attr_alg_name(tb[2]); - if (IS_ERR(blockcipher_name)) { - err = PTR_ERR(blockcipher_name); - goto err_free_inst; - } - - /* - * Originally there was an optional third parameter, for requesting a - * specific implementation of "nhpoly1305" for message hashing. This is - * no longer supported. The best implementation is just always used. - */ - if (crypto_attr_alg_name(tb[3]) != ERR_PTR(-ENOENT)) { - err = -ENOENT; - goto err_free_inst; - } - - /* Check the set of algorithms */ - if (!adiantum_supported_algorithms(streamcipher_alg, - blockcipher_name)) { - err = -ENOENT; - goto err_free_inst; +static struct skcipher_alg adiantum_algs[] = { + { + .base.cra_name = "adiantum(xchacha12,aes)", + .base.cra_driver_name = "adiantum(xchacha12-lib,aes-lib)", + .base.cra_priority = 300, + .base.cra_blocksize = AES_BLOCK_SIZE, + .base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx), + .base.cra_reqsize = sizeof(struct nhpoly1305_ctx), + .base.cra_module = THIS_MODULE, + .setkey = adiantum_setkey_xchacha12, + .encrypt = adiantum_encrypt, + .decrypt = adiantum_decrypt, + .min_keysize = CHACHA_KEY_SIZE, + .max_keysize = CHACHA_KEY_SIZE, + .ivsize = TWEAK_SIZE, + }, { + .base.cra_name = "adiantum(xchacha20,aes)", + .base.cra_driver_name = "adiantum(xchacha20-lib,aes-lib)", + .base.cra_priority = 300, + .base.cra_blocksize = AES_BLOCK_SIZE, + .base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx), + .base.cra_reqsize = sizeof(struct nhpoly1305_ctx), + .base.cra_module = THIS_MODULE, + .setkey = adiantum_setkey_xchacha20, + .encrypt = adiantum_encrypt, + .decrypt = adiantum_decrypt, + .min_keysize = CHACHA_KEY_SIZE, + .max_keysize = CHACHA_KEY_SIZE, + .ivsize = TWEAK_SIZE, } - - /* Instance fields */ - - err = -ENAMETOOLONG; - if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, - "adiantum(%s,aes)", - streamcipher_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME) - goto err_free_inst; - if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, - "adiantum(%s,aes-lib)", - streamcipher_alg->base.cra_driver_name) >= - CRYPTO_MAX_ALG_NAME) - goto err_free_inst; - - inst->alg.base.cra_blocksize = AES_BLOCK_SIZE; - inst->alg.base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx); - inst->alg.base.cra_alignmask = streamcipher_alg->base.cra_alignmask; - inst->alg.base.cra_priority = streamcipher_alg->base.cra_priority; - - inst->alg.setkey = adiantum_setkey; - inst->alg.encrypt = adiantum_encrypt; - inst->alg.decrypt = adiantum_decrypt; - inst->alg.init = adiantum_init_tfm; - inst->alg.exit = adiantum_exit_tfm; - inst->alg.min_keysize = streamcipher_alg->min_keysize; - inst->alg.max_keysize = streamcipher_alg->max_keysize; - inst->alg.ivsize = TWEAK_SIZE; - - inst->free = adiantum_free_instance; - - err = skcipher_register_instance(tmpl, inst); - if (err) { -err_free_inst: - adiantum_free_instance(inst); - } - return err; -} - -/* adiantum(xchacha12,aes) or adiantum(xchacha20,aes) */ -static struct crypto_template adiantum_tmpl = { - .name = "adiantum", - .create = adiantum_create, - .module = THIS_MODULE, }; static int __init adiantum_module_init(void) { - return crypto_register_template(&adiantum_tmpl); + return crypto_register_skciphers(adiantum_algs, + ARRAY_SIZE(adiantum_algs)); } static void __exit adiantum_module_exit(void) { - crypto_unregister_template(&adiantum_tmpl); + crypto_unregister_skciphers(adiantum_algs, ARRAY_SIZE(adiantum_algs)); } module_init(adiantum_module_init); @@ -647,5 +586,7 @@ module_exit(adiantum_module_exit); MODULE_DESCRIPTION("Adiantum length-preserving encryption mode"); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("Eric Biggers "); -MODULE_ALIAS_CRYPTO("adiantum"); -MODULE_IMPORT_NS("CRYPTO_INTERNAL"); +MODULE_ALIAS_CRYPTO("adiantum(xchacha12,aes)"); +MODULE_ALIAS_CRYPTO("adiantum(xchacha12-lib,aes-lib)"); +MODULE_ALIAS_CRYPTO("adiantum(xchacha20,aes)"); +MODULE_ALIAS_CRYPTO("adiantum(xchacha20-lib,aes-lib)"); -- 2.43.0