From patchwork Tue Nov 10 06:25:05 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arun Iyengar via B4 Relay X-Patchwork-Id: 4028 Return-Path: X-Original-To: noreply@patchwork.local Delivered-To: noreply@patchwork.local Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mxe881.netcup.net (Postfix) with ESMTPS id 543821C7814 for ; Tue, 10 Nov 2026 07:25:31 +0100 (CET) Authentication-Results: mxe881; dkim=pass header.d=kernel.org; spf=pass (sender IP is 172.105.105.114) smtp.mailfrom=linux-sunxi+bounces-27482-noreply=patchwork.local@lists.linux.dev smtp.helo=tor.lore.kernel.org Received-SPF: pass (mxe881: domain of lists.linux.dev designates 172.105.105.114 as permitted sender) client-ip=172.105.105.114; envelope-from=linux-sunxi+bounces-27482-noreply=patchwork.local@lists.linux.dev; helo=tor.lore.kernel.org; Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 7DD12E4F5A for ; Tue, 10 Nov 2026 06:25:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794291909; b=0naoaLOtpnmqIj4x/IylzdQPAfz6WuJmIJ7ApmVFSgDBNw8OXLo2oguSIml1heTa/PZN rvUVXEZd9FUtR834F5RSBezlnSlOSMA9F6AWh+NW9u96U2gn5Xniye/GcJ/b72TdKfm0f EPq5STsMtVHJJlfAWww73HopbZf88DtNS/DKD05QpeQ37otQ4XwN+OIwQARhNOcHojK/E TGhOGOyv3XXYQg8EXbB+yqXZf+vLwn/aBhukaZQ8pFZDZmjo6ovLa7qi+m6U+NGj7er4/ UirkilI/lE0jJmqDbPg/GsXsCcHlj7o2OizJyxHgLRBTrar2/uaoSL4aWgoBg81ofjw== ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794291909; h=X-Original-To:Received:Received:DKIM-Signature:Received:From:Date: Subject:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qVa7im9JyNQecrk3rUYJg0fizwf9uJh68ADezBOAxBo=; b=Cz+h3SfNOGwzWS16bYRxd3rXyaUqN5HbjRqUYzZjizRM2li8NkRqfSNryzRD9dx3HYz4 nJNvYLBCnG7crHI5a+az/DrAZQK4ghocYYUT58pGPGgrVdDxOKdbwmeHXn726nSgzJ0hg fM8eHfkRHcZcGGRqzuUZUJkuVq4N1uTzqZXyhxRpKjxE2rWnsaKmi222J6+WeMswsxSkv 7XHcGeyClqOt3rI3+7vxpZimVZfOk20qhxSUvPLTk7WhHsmmhITfMWiwpe4K/V1Wsn3lB IH9GXfIDX/UAN2SeDjTeso+pNKYYPtmLogxrzDUbZ/DLlkQcJXwgySgLwpqP/z0RLXQ== ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kifDjBR8; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 141F8606A33F; Tue, 10 Nov 2026 06:25:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kifDjBR8 X-Original-To: linux-sunxi@lists.linux.dev Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794291909; b=DpIBw+xxq2WV1QLhMFC1lyKbWl7gaM/gSmOoFVWSEh85zNzHJ+cZRIsyQ1phCtkFE9bS ZSr3PwniFfQ+o6HGd/yKYrZPKxFtq0u7z7oOmv9oDKesYY03M7n5dj5yfE2KvjGwoeqVo J/KLk+VDQOUU4gUpDlyk2sW4GtsmyKzTKfgEub6I1DivNl/3CpZMgEd7hsphRFlNY08db NkjFq0xm3ghiGfikds0gQQYZQgd4iHuS1MFSCIcpu1NdGgFJt66aLksYGkmZQ2+2/VMmi dDGAquRp4X6EeTNbvPHVybFn3K/fESpzvyfrwmhXToRj3qcPNw2J6DlzbZnjUv7hqwQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794291909; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qVa7im9JyNQecrk3rUYJg0fizwf9uJh68ADezBOAxBo=; b=SWqRiLR2D/dFEg0uOwH7AvU2gUCmxVEbhHQSfiom9Ke21Db6PXVJXInIiA2jcTX4Qomu s74CPlBnUX37hev6/J2Y3l/yHMsn9Wf09VRIDGIBn7LraLNtEV3+/YSRCtWTAWN5CEwJo Y6FOufGOcGnJaUPURLY9R5W5bUvL+7FnIvWgaKPrn9A3tLl+96Y6I8qWXev8+2ItFW7gf TyHPWetUnF7rzsh7/901tVGJEktqv+gCwZsHkIq6QrN619Z5vChS4h4ij5I02U7EazKZB u0Nd2nURTY0HkIYCuBgKZDalKsVCe4iWLEixUWCsF7zzb9QDgswGMQQv0LlqtoFuvGA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kifDjBR8; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6ECBF606A31C; Tue, 10 Nov 2026 06:25:08 +0000 (UTC) Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1794291907; b=UtUq8U+S225whiMCdxuz6b3ADzNz3FOxwu0klBb0YTPosu/0xw0X+YL8PBJu8npmMLfn 9MFNCWaUoIR3wQJCzcO4+x0NezEIvi9hAR1CL3hvexC6gWavq0wxa8csZ0BJmalaQiMrP VZiVF2V+NdVfWTMJy+OWeYR/GZ5yZDJJvMFTBWWjL9e+h376vl4wO3oJb2caZ9M7JLbPn KHBy0evfsUmH0pZdAKtdN7qZO3mU3ybEDynjGXWXtJ7r2IxlH0W1BN6/dB7DLiA2bTdVO XcUTr0tIrXVLsEl30T+2rPePXF4CwKAR4ndlH6zLA10FFjR0CBE/Q0jIAPTkGct+GRA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1794291907; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qVa7im9JyNQecrk3rUYJg0fizwf9uJh68ADezBOAxBo=; b=UsHuU0pZ4d9mwGQ4vpg7KZVMij2Cm9ZA/GevlYeAD9DgXZswXf/wBBgTUK89n1wGHISY ONlrXAuYRD4rm7mbsilOL8ssy+OW1uOTwgxOtFGnCvT5XbSOVS3ey4MxH5HLYmCPkYOkx 3AL71V6dAhPJdLWjQzraYz8+B6pfCcRXT+mr/wdZeCrnCZFpRENLOCqiEDgOVBODbVpgz 9XhicHavf+ooDrk/sNyvBVj8Q324HY9CXmYD9bgNlQpMgYzoCCxjB9m18I/iWixkrxbHc afmiy5cHEeI4KRjLIvVb1d3T0q59WHp5tEu4Rv7wAoPHnOlza9PcTrMbciRewo9utUw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (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.kernel.org (Postfix) with ESMTPS id 642571F008A1; Tue, 10 Nov 2026 06:25:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794291907; bh=qVa7im9JyNQecrk3rUYJg0fizwf9uJh68ADezBOAxBo=; h=From:Date:Subject:To:Cc:Reply-To; b=kifDjBR8lxEDTBqgYx4AFYPitH8iDRtPUujwcdyxlNCwsTd49L+Ms7pRoyFeFVbV4 LaMgH8HViY25YM1ruyawnyWDbNbzicsw1Tv1L769Lpyq6Bf2siVpYlH4rgM27WUfDy JLv7erGZ8QtTGAGn2JMSDfbwVYwtd5kq6fyu/t47Cxq6ZQBMcnSrJO+IaUW9kuV9Kk v8gsEMTZGSJsrr8yClb9Zts2Q1Wp4oPEtgBkZSriQunITG3lX/+8/g64WybPqSKZ8G DhEMGX7ijbwu1HFFtuueLrm30roMfdh0p9k/cpiDJwx7bGOJ3T2ZKktH6/yk8I4x3a kYnMtIqcMkE1w== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id B4CF548906; Tue, 10 Nov 2026 06:25:05 +0000 (UTC) From: Arun Iyengar via B4 Relay Date: Tue, 10 Nov 2026 06:25:05 +0000 Subject: [PATCH v2] crypto: sun8i-ce - Add AES-XTS for the H616 X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Message-Id: <20261110-sun8i-ce-h616-xts-v2-1-0b99efabc1d8@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/32NQQ6CMBBFr0Jm7Zi2xEJceQ/DgtYpjBEwnUIwh LsLuHf5kvffX0AoMglcswUiTSw89BuYUwa+rfuGkB8bg1HGaq0VytiXjJ6wtdrinARLuhjncl+ 43MK2e0cKPB/Ne/VjGd2TfNpDu9GypCF+jtNJ796//qRRowkh5ORUqYr61nQ1v85+6KBa1/ULF de5BcQAAAA= X-Change-ID: 20261110-sun8i-ce-h616-xts-8e52bb3c7b36 To: Corentin Labbe , Herbert Xu , "David S. Miller" , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland Cc: linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1794291905; l=17220; i=aiyengar1990@gmail.com; s=20261110; h=from:subject:message-id; bh=2nQWpNUcBW76Dz7uIz3wV2AA9flhPGO+JjUz//TRszY=; b=7XtX2fH6fEJvY5KBEabHfZvwnq0+KzVN2CKQFw1SCd7uulfPPzJToEoPfzDQ9yAl4mSsCkghC hOHrsMiL/EwBwy5f8GY2Y2Hs0WvlhNmTniDS6FuhhwJZmOcFOFc0pL1 X-Developer-Key: i=aiyengar1990@gmail.com; a=ed25519; pk=zXpRgUTrhaFe6jLyPqbBy3ccfkj04w0g2690Wv3mg+0= X-Endpoint-Received: by B4 Relay for aiyengar1990@gmail.com/20261110 with auth_id=1480 X-Original-From: Arun Iyengar Reply-To: aiyengar1990@gmail.com X-Rspamd-Server: rspamd-worker-8404 X-Spamd-Result: default: False [4.31 / 15.00]; RBL_SENDERSCORE(2.00)[172.105.105.114:from]; SUSPICIOUS_RECIPS(1.50)[]; MID_RHS_MATCH_TO(1.00)[]; MAILLIST(-0.18)[generic]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; HAS_LIST_UNSUB(-0.01)[]; FROM_HAS_DN(0.00)[]; TAGGED_RCPT(0.00)[]; RCVD_COUNT_FIVE(0.00)[6]; FREEMAIL_REPLYTO(0.00)[gmail.com]; HAS_REPLYTO(0.00)[aiyengar1990@gmail.com]; DBL_BLOCKED_OPENRESOLVER(0.00)[tor.lore.kernel.org:helo,tor.lore.kernel.org:rdns]; REPLYTO_DOM_NEQ_FROM_DOM(0.00)[]; FORGED_SENDER_MAILLIST(0.00)[]; R_DKIM_ALLOW(0.00)[kernel.org:s=k20260515]; FROM_NEQ_ENVFROM(0.00)[devnull@kernel.org,linux-sunxi@lists.linux.dev]; TAGGED_FROM(0.00)[bounces-27482-noreply=patchwork.local,aiyengar1990.gmail.com]; DKIM_TRACE(0.00)[kernel.org:+]; DMARC_POLICY_ALLOW(0.00)[kernel.org,quarantine]; R_SPF_ALLOW(0.00)[+ip4:172.105.105.114]; TO_DN_SOME(0.00)[]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc20261015:i=3]; RCPT_COUNT_SEVEN(0.00)[10]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; ASN(0.00)[asn:63949, ipnet:172.105.96.0/20, country:SG]; FREEMAIL_TO(0.00)[gmail.com,gondor.apana.org.au,davemloft.net,kernel.org,sholland.org]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[172.105.105.114:from] X-Rspamd-Queue-Id: 543821C7814 X-MORS-Enabled: yes X-MORS-DOMAIN: patchwork.local X-MORS-HOSTING: hosting172546 X-MORS-USER: hosting172546 X-getmail-retrieved-from-mailbox: INBOX From: Arun Iyengar The H616 crypto engine has a separate RAES unit (algorithm type 0x30) that does AES-ECB, AES-CBC and AES-XTS. Its XTS mode (symmetric mode 0b1001) takes the two keys back to back at the key address, the tweak at the IV address, and an xts_first/xts_last pair of flags that mark the first and the last block of a data unit. The key size field gives the size of one of the two keys: 128 or 256 bits, there is no 192-bit XTS. Register xts(aes) for the H616: each request is one data unit, so both flags are set and the request IV is the tweak of its first block. XTS returns no IV, so the CBC write-back of the last block is skipped. Ciphertext stealing (lengths that are not a multiple of the block size) and two 192-bit keys go to the fallback, as do the requests that the existing checks already send there. The engine is much slower at XTS than the CPU's ARMv8 AES instructions: 4 times slower for 64 KiB requests (213 against 861 MB/s), 8 times for 4 KiB and 17 times for 512 bytes. Register it below every CPU implementation of xts(aes) (priority 90), so that the CPU stays the default, for dm-crypt too, and the engine is only used when it is asked for by driver name. The other variants get CE_ID_NOTSUPP for the new mode, so nothing changes for them. Signed-off-by: Arun Iyengar --- The H616 crypto engine can do AES-XTS in its separate RAES unit. This patch registers xts(aes) for the H616, below the CPU implementations because the engine is slower than the ARMv8 Crypto Extensions; it stays available by driver name (xts-aes-sun8i-ce) for users who want to offload the CPU. Testing on an Orange Pi Zero2 (H616), v7.3-based kernel with this patch (sun8i-ce is unchanged between v7.3 and cryptodev-2.6): - the boot-time self-test of xts-aes-sun8i-ce passes, and the testmgr vectors run on the engine (debugfs request counters); - 21 known-answer vectors (IEEE 1619 and generated, including ciphertext stealing lengths, which go to the fallback) pass, and 3000 random requests (10% with misaligned buffers) give the same result as xts-aes-ce; - dm-crypt with capi:xts-aes-sun8i-ce-plain64, 512 and 4096-byte sectors: 48 MiB written and read back with matching sha256, and the raw sectors decrypt with xts-aes-ce; with this priority, xts(aes) and plain aes-xts-plain64 resolve to the CPU's xts-aes-ce; - the existing ECB/CBC ciphers, hashes and the TRNG still match their software implementations. Throughput, engine against xts-aes-ce: 20/352 MB/s for 512-byte requests, 98/759 MB/s for 4 KiB, 213/861 MB/s for 64 KiB. This patch was developed with Claude (Anthropic) as a coding assistant: it investigated the problem, wrote the code and the commit messages. I reviewed it, and it was tested on an Orange Pi Zero2 (H616) as described above. --- Changes in v2: - Select CRYPTO_XTS: the xts(aes) transform allocates an xts(aes) fallback in sun8i_ce_cipher_init(), which fails without it (found by the Sashiko AI review). - Link to v1: https://patch.msgid.link/20261110-sun8i-ce-h616-xts-v1-1-2fff3eb0807a@gmail.com To: Corentin Labbe To: Herbert Xu To: "David S. Miller" To: Chen-Yu Tsai To: Jernej Skrabec To: Samuel Holland Cc: linux-crypto@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org Cc: linux-sunxi@lists.linux.dev Cc: linux-kernel@vger.kernel.org --- drivers/crypto/allwinner/Kconfig | 1 + .../crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c | 102 +++++++++++++++++---- drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c | 44 +++++++-- drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h | 21 ++++- 4 files changed, 142 insertions(+), 26 deletions(-) --- base-commit: 82e91fe19fb838f235f7c8829afc1eb389c93bdf change-id: 20261110-sun8i-ce-h616-xts-8e52bb3c7b36 Best regards, -- Arun Iyengar diff --git a/drivers/crypto/allwinner/Kconfig b/drivers/crypto/allwinner/Kconfig index d86ae005fbe2..c07212ea2fba 100644 --- a/drivers/crypto/allwinner/Kconfig +++ b/drivers/crypto/allwinner/Kconfig @@ -39,6 +39,7 @@ config CRYPTO_DEV_SUN8I_CE select CRYPTO_ENGINE select CRYPTO_ECB select CRYPTO_CBC + select CRYPTO_XTS select CRYPTO_AES select CRYPTO_DES depends on CRYPTO_DEV_ALLWINNER diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c index 021614b65e39..b87be3123b12 100644 --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c @@ -6,7 +6,7 @@ * Copyright (C) 2016-2019 Corentin LABBE * * This file add support for AES cipher with 128,192,256 bits keysize in - * CBC and ECB mode. + * CBC and ECB mode, and for AES-XTS with 256 and 512 bits keysize. * * You could find a link for the datasheet in Documentation/arch/arm/sunxi.rst */ @@ -19,18 +19,34 @@ #include #include #include +#include #include "sun8i-ce.h" +/* + * XTS uses the request IV as the tweak of its first block and returns no IV, + * CBC returns the last ciphertext block as the IV of the next request. + */ +static bool sun8i_ce_cipher_iv_out(struct sun8i_ce_alg_template *algt) +{ + return algt->ce_blockmode != CE_ID_OP_XTS; +} + static int sun8i_ce_cipher_need_fallback(struct skcipher_request *areq) { struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); struct scatterlist *sg; + struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); struct skcipher_alg *alg = crypto_skcipher_alg(tfm); struct sun8i_ce_alg_template *algt; unsigned int todo, len; algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher.base); + /* The XTS unit takes two 128-bit or two 256-bit keys only */ + if (algt->ce_blockmode == CE_ID_OP_XTS && + op->keylen == 2 * AES_KEYSIZE_192) + return true; + if (sg_nents_for_len(areq->src, areq->cryptlen) > MAX_SG || sg_nents_for_len(areq->dst, areq->cryptlen) > MAX_SG) { if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG)) @@ -164,7 +180,10 @@ static int sun8i_ce_cipher_prepare(struct skcipher_request *areq, memset(cet, 0, sizeof(struct ce_task)); cet->t_id = cpu_to_le32(rctx->flow); - common = ce->variant->alg_cipher[algt->ce_algo_id]; + if (algt->ce_blockmode == CE_ID_OP_XTS) + common = CE_ALG_RAES; + else + common = ce->variant->alg_cipher[algt->ce_algo_id]; common |= rctx->op_dir | CE_COMM_INT; cet->t_common_ctl = cpu_to_le32(common); /* CTS and recent CE (H6) need length in bytes, in word otherwise */ @@ -175,6 +194,15 @@ static int sun8i_ce_cipher_prepare(struct skcipher_request *areq, sym = ce->variant->op_mode[algt->ce_blockmode]; len = op->keylen; + if (algt->ce_blockmode == CE_ID_OP_XTS) { + /* + * One request is one data unit: its first block uses the + * tweak from the IV, its last block ends the data unit. The + * key size field gives the size of each of the two keys. + */ + sym |= CE_SYM_XTS_FIRST | CE_SYM_XTS_LAST; + len /= 2; + } switch (len) { case 128 / 8: sym |= CE_AES_128BITS; @@ -200,7 +228,8 @@ static int sun8i_ce_cipher_prepare(struct skcipher_request *areq, ivsize = crypto_skcipher_ivsize(tfm); if (areq->iv && ivsize > 0) { - if (rctx->op_dir & CE_DECRYPTION) { + if (sun8i_ce_cipher_iv_out(algt) && + (rctx->op_dir & CE_DECRYPTION)) { offset = areq->cryptlen - ivsize; scatterwalk_map_and_copy(rctx->backup_iv, areq->src, offset, ivsize, 0); @@ -290,13 +319,15 @@ static int sun8i_ce_cipher_prepare(struct skcipher_request *areq, dma_unmap_single(ce->dev, rctx->addr_iv, ivsize, DMA_TO_DEVICE); - offset = areq->cryptlen - ivsize; - if (rctx->op_dir & CE_DECRYPTION) { - memcpy(areq->iv, rctx->backup_iv, ivsize); - memzero_explicit(rctx->backup_iv, ivsize); - } else { - scatterwalk_map_and_copy(areq->iv, areq->dst, offset, - ivsize, 0); + if (sun8i_ce_cipher_iv_out(algt)) { + offset = areq->cryptlen - ivsize; + if (rctx->op_dir & CE_DECRYPTION) { + memcpy(areq->iv, rctx->backup_iv, ivsize); + memzero_explicit(rctx->backup_iv, ivsize); + } else { + scatterwalk_map_and_copy(areq->iv, areq->dst, + offset, ivsize, 0); + } } memzero_explicit(rctx->bounce_iv, ivsize); } @@ -314,10 +345,13 @@ static void sun8i_ce_cipher_unprepare(struct skcipher_request *areq, struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); struct sun8i_ce_dev *ce = op->ce; struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq); + struct skcipher_alg *alg = crypto_skcipher_alg(tfm); + struct sun8i_ce_alg_template *algt; unsigned int ivsize, offset; int nr_sgs = rctx->nr_sgs; int nr_sgd = rctx->nr_sgd; + algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher.base); ivsize = crypto_skcipher_ivsize(tfm); if (areq->src == areq->dst) { @@ -332,13 +366,15 @@ static void sun8i_ce_cipher_unprepare(struct skcipher_request *areq, if (cet->t_iv) dma_unmap_single(ce->dev, rctx->addr_iv, ivsize, DMA_TO_DEVICE); - offset = areq->cryptlen - ivsize; - if (rctx->op_dir & CE_DECRYPTION) { - memcpy(areq->iv, rctx->backup_iv, ivsize); - memzero_explicit(rctx->backup_iv, ivsize); - } else { - scatterwalk_map_and_copy(areq->iv, areq->dst, offset, - ivsize, 0); + if (sun8i_ce_cipher_iv_out(algt)) { + offset = areq->cryptlen - ivsize; + if (rctx->op_dir & CE_DECRYPTION) { + memcpy(areq->iv, rctx->backup_iv, ivsize); + memzero_explicit(rctx->backup_iv, ivsize); + } else { + scatterwalk_map_and_copy(areq->iv, areq->dst, + offset, ivsize, 0); + } } memzero_explicit(rctx->bounce_iv, ivsize); } @@ -489,6 +525,38 @@ int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, return crypto_skcipher_setkey(op->fallback_tfm, key, keylen); } +int sun8i_ce_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, + unsigned int keylen) +{ + struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm); + struct sun8i_ce_dev *ce = op->ce; + int err; + + err = xts_verify_key(tfm, key, keylen); + if (err) + return err; + + switch (keylen) { + case 2 * AES_KEYSIZE_128: + case 2 * AES_KEYSIZE_192: + case 2 * AES_KEYSIZE_256: + break; + default: + dev_dbg(ce->dev, "ERROR: Invalid keylen %u\n", keylen); + return -EINVAL; + } + kfree_sensitive(op->key); + op->keylen = keylen; + op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA); + if (!op->key) + return -ENOMEM; + + crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); + crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); + + return crypto_skcipher_setkey(op->fallback_tfm, key, keylen); +} + int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen) { diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c index 21762bd408e5..a7d771927b7a 100644 --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c @@ -41,7 +41,7 @@ static const struct ce_variant ce_h3_variant = { .alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256, CE_ALG_SHA384, CE_ALG_SHA512 }, - .op_mode = { CE_OP_ECB, CE_OP_CBC + .op_mode = { CE_OP_ECB, CE_OP_CBC, CE_ID_NOTSUPP }, .ce_clks = { { "bus", 0, 200000000 }, @@ -57,7 +57,7 @@ static const struct ce_variant ce_h5_variant = { .alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256, CE_ID_NOTSUPP, CE_ID_NOTSUPP }, - .op_mode = { CE_OP_ECB, CE_OP_CBC + .op_mode = { CE_OP_ECB, CE_OP_CBC, CE_ID_NOTSUPP }, .ce_clks = { { "bus", 0, 200000000 }, @@ -73,7 +73,7 @@ static const struct ce_variant ce_h6_variant = { .alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256, CE_ALG_SHA384, CE_ALG_SHA512 }, - .op_mode = { CE_OP_ECB, CE_OP_CBC + .op_mode = { CE_OP_ECB, CE_OP_CBC, CE_ID_NOTSUPP }, .cipher_t_dlen_in_bytes = true, .hash_t_dlen_in_bits = true, @@ -93,7 +93,7 @@ static const struct ce_variant ce_h616_variant = { .alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256, CE_ALG_SHA384, CE_ALG_SHA512 }, - .op_mode = { CE_OP_ECB, CE_OP_CBC + .op_mode = { CE_OP_ECB, CE_OP_CBC, CE_OP_XTS }, .cipher_t_dlen_in_bytes = true, .hash_t_dlen_in_bits = true, @@ -115,7 +115,7 @@ static const struct ce_variant ce_a64_variant = { .alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256, CE_ID_NOTSUPP, CE_ID_NOTSUPP }, - .op_mode = { CE_OP_ECB, CE_OP_CBC + .op_mode = { CE_OP_ECB, CE_OP_CBC, CE_ID_NOTSUPP }, .ce_clks = { { "bus", 0, 200000000 }, @@ -131,7 +131,7 @@ static const struct ce_variant ce_d1_variant = { .alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256, CE_ALG_SHA384, CE_ALG_SHA512 }, - .op_mode = { CE_OP_ECB, CE_OP_CBC + .op_mode = { CE_OP_ECB, CE_OP_CBC, CE_ID_NOTSUPP }, .ce_clks = { { "bus", 0, 200000000 }, @@ -149,7 +149,7 @@ static const struct ce_variant ce_r40_variant = { .alg_hash = { CE_ALG_MD5, CE_ALG_SHA1, CE_ALG_SHA224, CE_ALG_SHA256, CE_ID_NOTSUPP, CE_ID_NOTSUPP }, - .op_mode = { CE_OP_ECB, CE_OP_CBC + .op_mode = { CE_OP_ECB, CE_OP_CBC, CE_ID_NOTSUPP }, .ce_clks = { { "bus", 0, 200000000 }, @@ -350,6 +350,36 @@ static struct sun8i_ce_alg_template ce_algs[] = { .do_one_request = sun8i_ce_cipher_do_one, }, }, +{ + .type = CRYPTO_ALG_TYPE_SKCIPHER, + .ce_algo_id = CE_ID_CIPHER_AES, + .ce_blockmode = CE_ID_OP_XTS, + .alg.skcipher.base = { + .base = { + .cra_name = "xts(aes)", + .cra_driver_name = "xts-aes-sun8i-ce", + .cra_priority = CE_PRIO_BELOW_CPU, + .cra_blocksize = AES_BLOCK_SIZE, + .cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | + CRYPTO_ALG_ASYNC | + CRYPTO_ALG_NEED_FALLBACK, + .cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx), + .cra_module = THIS_MODULE, + .cra_alignmask = 0xf, + .cra_init = sun8i_ce_cipher_init, + .cra_exit = sun8i_ce_cipher_exit, + }, + .min_keysize = 2 * AES_MIN_KEY_SIZE, + .max_keysize = 2 * AES_MAX_KEY_SIZE, + .ivsize = AES_BLOCK_SIZE, + .setkey = sun8i_ce_xts_setkey, + .encrypt = sun8i_ce_skencrypt, + .decrypt = sun8i_ce_skdecrypt, + }, + .alg.skcipher.op = { + .do_one_request = sun8i_ce_cipher_do_one, + }, +}, { .type = CRYPTO_ALG_TYPE_SKCIPHER, .ce_algo_id = CE_ID_CIPHER_DES3, diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h index 468d99bf5bf6..89689e54e5fd 100644 --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h @@ -46,6 +46,10 @@ #define CE_OP_ECB 0 #define CE_OP_CBC (1 << 8) +#define CE_OP_XTS (9 << 8) + +#define CE_SYM_XTS_FIRST BIT(12) +#define CE_SYM_XTS_LAST BIT(13) #define CE_ALG_AES 0 #define CE_ALG_DES 1 @@ -58,6 +62,7 @@ #define CE_ALG_SHA512 21 #define CE_ALG_TRNG 48 #define CE_ALG_TRNG_V2 0x1c +#define CE_ALG_RAES 0x30 /* Used in ce_variant */ #define CE_ID_NOTSUPP 0xFF @@ -77,7 +82,8 @@ #define CE_ID_OP_ECB 0 #define CE_ID_OP_CBC 1 -#define CE_ID_OP_MAX 2 +#define CE_ID_OP_XTS 2 +#define CE_ID_OP_MAX 3 /* Used in CE registers */ #define CE_ERR_ALGO_NOTSUP BIT(0) @@ -98,6 +104,14 @@ #define MAX_SG 8 +/* + * Priority of the modes that the CPU's own AES instructions run faster than + * the engine: below every CPU implementation of them (arm64 CE 300, NEON 250 + * and 200, AES library 110, generic templates 100), so the CPU stays the + * default and the engine is used when it is asked for by driver name. + */ +#define CE_PRIO_BELOW_CPU 90 + #define CE_MAX_CLOCKS 4 #define CE_DMA_TIMEOUT_MS 3000 @@ -124,7 +138,8 @@ struct ce_clock { * coresponding CE_ALG_XXX value * @alg_hash: list of supported hashes. for each CE_ID_ this will give the * corresponding CE_ALG_XXX value - * @op_mode: list of supported block modes + * @op_mode: list of supported block modes, CE_ID_NOTSUPP if not supported. + * XTS runs on the RAES unit (algorithm CE_ALG_RAES) * @cipher_t_dlen_in_bytes: Does the request size for cipher is in * bytes or words * @hash_t_dlen_in_bytes: Does the request size for hash is in @@ -352,6 +367,8 @@ struct sun8i_ce_alg_template { int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen); +int sun8i_ce_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, + unsigned int keylen); int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen); int sun8i_ce_cipher_init(struct crypto_tfm *tfm);