From patchwork Tue Nov 10 05:39:00 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: 4019 Return-Path: X-Original-To: noreply@patchwork.local Delivered-To: noreply@patchwork.local Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mxe881.netcup.net (Postfix) with ESMTPS id 666741C7366 for ; Tue, 10 Nov 2026 06:41:02 +0100 (CET) Authentication-Results: mxe881; dkim=pass header.d=kernel.org; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-27469-noreply=patchwork.local@lists.linux.dev smtp.helo=sea.lore.kernel.org Received-SPF: pass (mxe881: domain of lists.linux.dev designates 172.234.253.10 as permitted sender) client-ip=172.234.253.10; envelope-from=linux-sunxi+bounces-27469-noreply=patchwork.local@lists.linux.dev; helo=sea.lore.kernel.org; Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 05B082B6CB for ; Tue, 10 Nov 2026 05:39:05 +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=1794289143; b=kBK660o1sr/Qpqx0Hc6lCq+a06V/pQBZwM151YcpCrcvaSJtuc8u5lG9b+XkMx+JeNpt TI4g6d76nCNIfcXOqgWMIgbdedMXNSEJD4eTY9b5+gR5Edj7BI3GortIH/SlY+fl4UMlf RUP7HLU9LRjjIeAHowUMsHR8YzSTy8QfppI1Z2XURDOL3ufN1SMGOzfIPZdZGi/1zFVUt +cyFTk/9BYRiszymV9VcKUuo1iZFP0NWraiPNqxLOBO+gaMKQXCv4d/u61uRRMdDX5XsJ /LPA6iPPIbdqfvEB6Z2mMiKVip/XLNBG+BJtDW1PQuEacx1xxFVtw3h2rCygfmXHKIA== ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794289143; 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=/J6Cc8Pic3gnrFFnoEmm7ri6Lh5JoY2EXIMtxFmFiT4=; b=EyEgLORLyt2/Hb6tU1J9JWrvvtGvBxgn8/lN6tHnBem/MKfKyfrvtxpyOlCqTYf+07A4 c7KNI2m0vYcgm+7CHYMPijax2250v2A+9x3z5g8uytZ0F5MySvOGuItEOx6h5Lv1ysOi2 MNU18tFNsvbxLu/zIxYYEvzD3nAFOQMvIfVd7OssGogGFcS/EymfkcXQcZktS7bMBeeeB N2+5sXps+Xsou2WXYTIB7mHx2UUn96hd+xahwU0ViFkI+yFcI4BdTFb6gE9YmqOcFuYQF SNTXXwNMVkC/bl6qg8YLOVkk9HMzQr2TB+Uhj0ajDyic8qHtoaOSNOxdpF8mXQ+/o/Q== 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=MX8e7NuE; 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 B04BD606A326; Tue, 10 Nov 2026 05:39:03 +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=MX8e7NuE 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=1794289143; b=mzyBzFBaOEqPNf1X0JuZNXls05YouuJKQr0LzAoDLpVeJ1EdfcCgRrIIGnJxFNd//2NC 0U8f1htl85UE6O2M9MdqpHjVIznGN2FiTTujELjOdzGSY3fBSqcpiA/r1DGhfNQNCD/ee DHqvgbwut3i/HOzzp0xdoo8oIaTO8Mkv6dIda65Ada9rTojo6TmXvzs31o6hSDbYKzfYs 3ys56gKMlIzHoWZViB87Q+lOLTMSZ6B1odFG/P4Lm3MWAZ7yB2zwFIkQOmImwF/lpmv5m 491k+JEzyQY1hPVnfU8JIqTAtduQMQxHbmA6iPmydadj4TPQoV6/CykZZTiffExrINQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794289143; 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=/J6Cc8Pic3gnrFFnoEmm7ri6Lh5JoY2EXIMtxFmFiT4=; b=wCK7wiu9sKETqq+2WyhUJlLQDxVfz2xyD7cBraQcj3tyXXOroRIaHB1fMlvWUL2dkCE3 l8sjdtthe0nLAHUORcLSl4sPFxteCyqiFQcF9c92KsHTLBxPQmVxfPwNAqMfVL+dzLw6P Jap2NY+7o7rwRLeey7tgE8zZ42r8B2zI3gxGnK6xBF0bWPp0e9dNaRkpuLKXOT9vSMvAG xpSyFr61bmmVXalJ4ptl76xaYONZb7PdL4lSKkZ9OOjNeKOY83HX6wkgvsyThoiYPpIYC okrCqSiqsmEoDOq6bM7AVVZY2ek0RomFq+9BByTnFYSFsekn1pvKDVLCYOtriIs366A== 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=MX8e7NuE; 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 2135C606A31C; Tue, 10 Nov 2026 05:39:03 +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=1794289143; b=XVDPoeuJXtiIozINDU4AQHcx30Z2klCNgfiq8TKpW7Q/ijDk0MYG7k/u0MVdDE08AIPY 0o7u/SYv7zbuIuN+ZknraOfRJjGpNx7gUisnbBMvnc1vGlBJGKuOwvadjlN/wj2JE+bel o8mFw3tY5cxiu3XzedxRF2lpqdwbC7/9w9MkEUWOale5vb0HYj0f9WNCvTjpc+ayztCdj rtEhWG95XrVzRS/dG9/m7R5IY1JVPWG6NTlTI6OSSBAtIDc0XWVWft9wP8Dv2lACtQ4yl UtDr/iWkWNUX9zcUL/uAA8u6siG0tiD1xwQBWwnWbgN0vQSjD2ErBmZfnmpKnfqCWuA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1794289143; 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=/J6Cc8Pic3gnrFFnoEmm7ri6Lh5JoY2EXIMtxFmFiT4=; b=frM4vUwTQdjsxrRa+NbEIujimqKecHERHTsFXNCjCy6Xz19MXAqnMnwpvykq+gkWtOHL ogwX1feZLXRe9vM5LTnn5ynWXSowW/VDbpFI84eafKWLUCLAITWUJaAIIDouNB1Zcwomz DoCX/BD1qqPfI9Wb1zwpPU8eelZBatV9jSmsyVMypCy6MQVcOixgVkN1apl5GFL5oZ3cE g9Kb6wQ3Rq3vRsxzF58ZVvZpLFbQAIT0MTbXJ/xsRlVFKC1SQ0emm7uc5K8PnlL5pc6Tl XTngACKhqPag2VHo8JHWUxmWczCLAYOrc8I8yhnnyVPvOjbmLYOQbZMhzwIoR9hKhuw== 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 C77C11F0089F; Tue, 10 Nov 2026 05:39:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794289142; bh=/J6Cc8Pic3gnrFFnoEmm7ri6Lh5JoY2EXIMtxFmFiT4=; h=From:Date:Subject:To:Cc:Reply-To; b=MX8e7NuEriTupoaLbdF2xvyG3OFyLgGs8l86f+M1gXfyjuetYJKczFiuyvgJ2RngA 8DgqUcky5kXyI9n0h/VEMTEEHB6PiLArSoslwl1VqFyBRmdTGAB58XoPFGw9DUiE/Y q7c9HbHpDh+MYW9tqqLgA6UFhSBrfT1D+XrtLENC6EhjEtbXwwjwDVoZRCOxL/JC03 hGHklukfYh3cacugfJpmoOncATdXZ/oJm0bZRg1c+PlEP3xfk61f/9vADWiEALX+Wp v3BnAP/cMP3vokiPV+cGORDK1r3Z3aTlwfRxN6SPCfOHPHjdgp9W3T5Di8yXCUGYra 26u7SmimnbfOw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 41D794889F; Tue, 10 Nov 2026 05:39:01 +0000 (UTC) From: Arun Iyengar via B4 Relay Date: Tue, 10 Nov 2026 05:39:00 +0000 Subject: [PATCH] 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-v1-1-2fff3eb0807a@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/yXMMQ6DMAxA0asgz1iKgwioV6kYSDDEHdIqBoSEu HvTMr7h/xOUs7DCozoh8y4q71RAdQUhjmlhlKkYrLGOiAzqlnrBwBgdOTxWxZ5b630TOt84KN0 n8yzH//kcbuvmXxzW3wiu6wsBOPb0dQAAAA== 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, Arun Iyengar X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1794289140; l=16055; i=aiyengar1990@gmail.com; s=20261110; h=from:subject:message-id; bh=TMArJD25MvyT3zJCh5VJk/Y3KQLwee2FG4Ai1cMLXRc=; b=zDh5IOuf0haJENbWnm+R4Kpwc33DrFxM0cCnz2jCpDOIYfYpTioKCGK3ZTfZ01ql2WvJs+8sy x+JWUEaWz5DCNOl1L5kbnSuxe+wEak939ULR5q8Y01+VgF9z0UNwVna 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 [-1.19 / 15.00]; BAYES_HAM(-5.50)[100.00%]; RBL_SENDERSCORE(2.00)[172.234.253.10: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)[]; RCVD_COUNT_FIVE(0.00)[6]; FORGED_SENDER_MAILLIST(0.00)[]; FROM_HAS_DN(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[sea.lore.kernel.org:helo,sea.lore.kernel.org:rdns,skcipher.base:url]; TAGGED_RCPT(0.00)[]; REPLYTO_DOM_NEQ_FROM_DOM(0.00)[]; FREEMAIL_REPLYTO(0.00)[gmail.com]; FREEMAIL_CC(0.00)[vger.kernel.org,lists.infradead.org,lists.linux.dev,gmail.com]; HAS_REPLYTO(0.00)[aiyengar1990@gmail.com]; R_DKIM_ALLOW(0.00)[kernel.org:s=k20260515]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc20261015:i=3]; DKIM_TRACE(0.00)[kernel.org:+]; DMARC_POLICY_ALLOW(0.00)[kernel.org,quarantine]; R_SPF_ALLOW(0.00)[+ip4:172.234.253.10:c]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[100.103.45.18:received,100.77.114.92:received,100.80.115.77:received]; TO_DN_SOME(0.00)[]; FROM_NEQ_ENVFROM(0.00)[devnull@kernel.org,linux-sunxi@lists.linux.dev]; RCPT_COUNT_SEVEN(0.00)[11]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; TAGGED_FROM(0.00)[bounces-27469-noreply=patchwork.local,aiyengar1990.gmail.com]; FREEMAIL_TO(0.00)[gmail.com,gondor.apana.org.au,davemloft.net,kernel.org,sholland.org]; ASN(0.00)[asn:63949, ipnet:172.234.224.0/19, country:SG]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_LAST(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[172.234.253.10:from] X-Rspamd-Queue-Id: 666741C7366 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. --- .../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 ++++- 3 files changed, 141 insertions(+), 26 deletions(-) --- base-commit: 82e91fe19fb838f235f7c8829afc1eb389c93bdf change-id: 20261110-sun8i-ce-h616-xts-8e52bb3c7b36 Best regards, -- Arun Iyengar 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);