Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4844152rdh; Wed, 29 Nov 2023 12:16:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IEIuN1u4MlGESDZqFitI7WukA/LtDs0//lwldfCYoUFBOOKXcihRe7P4hzmNNK2oDRMMPQX X-Received: by 2002:a17:902:c408:b0:1cf:ad33:61b with SMTP id k8-20020a170902c40800b001cfad33061bmr19716714plk.39.1701288993900; Wed, 29 Nov 2023 12:16:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701288993; cv=none; d=google.com; s=arc-20160816; b=Zf6Wv1z6g/xGTa9CfFuc+9IASVr28mHzSnKRtcEx4p+pxKe+sGojaNEJG52dDkguuQ Gq+TU4uWrzNJPgfRcCInDT5kgIKfoPlB1olYZbrjUv4RVEDIoKcXBpgWP+eXNEAmqsRF 3FIQzfGISjI4ixCKRP9RpW30aIK88wc+Z3RPdc+RLBgEc3DyV7/ycigJRioVYIdUNYE1 I4o5w5Vc4DBOE63UacQJLiiTRAV+hIwdjkMIHCBSQ0AVI5PpuNX5sHLkWnytzjyCitD3 cm6incTGsBvbsdF5gL+0Moc6Rpt4v4Ux9jS/xo6emyFGf1SGyteJUVvr8JJThXkbxXzz gMqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=m1ddjssTdEJJiFikJjkVuSSra6f8gTqBrl6V7K8h95I=; fh=QgWW13MBtDRhdLW5KXWKdqDi7d/dVjBrrVl8ytQ/Irs=; b=IhCr45IGehLoJaHW3XIitQkwieeZamFereLP3cGiUH5/es7M5j45kfFMOw6CInNq/7 MiumesNuV+QnLkyHmmwaXuejm9pRLUYJdPNHxTuyqcphEdwE9Rv+oeK7D+YiBkTgDlIK jOE/adPbO4g3UYFx6gTLtBTFVEdadB7WUKxU5YiLwJBzjbxYlat6hHpg9ZBRN8jxEpbn cIuhpAnrok7mpXV/NZB/e2s4xGfroK/+Y6q8A2lDDSpCy2LPQYwxfw9rhClCVngBa+TN HZiLJvLXQfLPOUb0D2ceDah35SFeh3PdXzmOIvQWaxpt0yT5mdy6RkO/v9VXM9Scqxwv LxRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=A58xGnKF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id h4-20020a170902f54400b001cfd057098dsi7502249plf.418.2023.11.29.12.16.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 12:16:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=A58xGnKF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 438C6807CB4D; Wed, 29 Nov 2023 12:16:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231523AbjK2UQB (ORCPT + 99 others); Wed, 29 Nov 2023 15:16:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233754AbjK2UQA (ORCPT ); Wed, 29 Nov 2023 15:16:00 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89E9510C8 for ; Wed, 29 Nov 2023 12:16:03 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B55CCC433CC; Wed, 29 Nov 2023 20:16:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701288963; bh=eJQhkQLzUwwUMvhzdNW9MAj6eRgcFa4pmRj/Q9yI5aQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=A58xGnKFd8gNAc7MIbjqM3wnqSqhoDtxaRuCpIVWNjwFgXMRCDGM6O7s/U+F7yyFy uFtlTSLv9XqLau5R0N9MPb/ciOx7gW1x9MGpmJazMX1Uc6uKlXrO3RbmCAX5n780PR 2eUFCyvP5Gzh8aThoQCLxK17xsQ8ddepch5wHQknLNhxoScaDKqgycFUV+18HhEWdO ol489AUDXcxReKqC385IxnEAJZYsSKqGMRg0QmBKI3IptwV7XtJAvbsmrYxQkRsmD3 9y+DxI8Y04LoX9ALLe4ASJVkwTY8rfE0CI5Eap7J80HzqBpHxNXGWkB5u+TQkO4C8p IsiMIOv1l6fsQ== Date: Wed, 29 Nov 2023 12:16:01 -0800 From: Eric Biggers To: Jerry Shih Cc: Paul Walmsley , palmer@dabbelt.com, Albert Ou , herbert@gondor.apana.org.au, davem@davemloft.net, conor.dooley@microchip.com, ardb@kernel.org, heiko@sntech.de, phoebe.chen@sifive.com, hongrong.hsu@sifive.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org Subject: Re: [PATCH v2 07/13] RISC-V: crypto: add accelerated AES-CBC/CTR/ECB/XTS implementations Message-ID: <20231129201601.GA1174@sol.localdomain> References: <20231127070703.1697-1-jerry.shih@sifive.com> <20231127070703.1697-8-jerry.shih@sifive.com> <20231128040716.GI1463@sol.localdomain> <7DFBB20D-B8D4-409B-8562-4C60E67FD279@sifive.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7DFBB20D-B8D4-409B-8562-4C60E67FD279@sifive.com> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 29 Nov 2023 12:16:16 -0800 (PST) On Wed, Nov 29, 2023 at 03:57:25PM +0800, Jerry Shih wrote: > On Nov 28, 2023, at 12:07, Eric Biggers wrote: > > On Mon, Nov 27, 2023 at 03:06:57PM +0800, Jerry Shih wrote: > >> +typedef void (*aes_xts_func)(const u8 *in, u8 *out, size_t length, > >> + const struct crypto_aes_ctx *key, u8 *iv, > >> + int update_iv); > > > > There's no need for this indirection, because the function pointer can only have > > one value. > > > > Note also that when Control Flow Integrity is enabled, assembly functions can > > only be called indirectly when they use SYM_TYPED_FUNC_START. That's another > > reason to avoid indirect calls that aren't actually necessary. > > We have two function pointers for encryption and decryption. > static int xts_encrypt(struct skcipher_request *req) > { > return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt); > } > > static int xts_decrypt(struct skcipher_request *req) > { > return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt); > } > The enc and dec path could be folded together into `xts_crypt()`, but we will have > additional branches for enc/decryption path if we don't want to have the indirect calls. > Use `SYM_TYPED_FUNC_START` in asm might be better. > Right. Normal branches are still more efficient and straightforward than indirect calls, though, and they don't need any special considerations for CFI. So I'd just add a 'bool encrypt' or 'bool decrypt' argument to xts_crypt(), and make xts_crypt() call the appropriate assembly function based on that. > > Did you consider writing xts_crypt() the way that arm64 and x86 do it? The > > above seems to reinvent sort of the same thing from first principles. I'm > > wondering if you should just copy the existing approach for now. Then there > > would be no need to add the scatterwalk_next() function, and also the handling > > of inputs that don't need ciphertext stealing would be a bit more streamlined. > > I will check the arm and x86's implementations. > But the `scatterwalk_next()` proposed in this series does the same thing as the > call `scatterwalk_ffwd()` in arm and x86's implementations. > The scatterwalk_ffwd() iterates from the beginning of scatterlist(O(n)), but the > scatterwalk_next() is just iterates from the end point of the last used > scatterlist(O(1)). Sure, but your scatterwalk_next() only matters when there are multiple scatterlist entries and the AES-XTS message length isn't a multiple of the AES block size. That's not an important case, so there's little need to micro-optimize it. The case that actually matters for AES-XTS is a single-entry scatterlist containing a whole number of AES blocks. - Eric