Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp795275rdf; Tue, 21 Nov 2023 17:43:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IEQdF9xe6lhR3fvhX85eDdNljP11G7AgO3qRC6ob7qf2iAEz3sf8IxJ55bO1NfxC3lRUru2 X-Received: by 2002:a05:6808:2227:b0:3b2:f174:ffd4 with SMTP id bd39-20020a056808222700b003b2f174ffd4mr1691428oib.5.1700617438342; Tue, 21 Nov 2023 17:43:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700617438; cv=none; d=google.com; s=arc-20160816; b=T7o8i+qWMye9I4bsJIu/WM0l5LDkxurcW1rFM5vsjtT9BsUQ90ZLnYGuSrrnspieDm VuiwTs1W+w6iJ6Ag1iGA2MWjM9SaSGekAVqUa+q1KaEhgShEiLy+yHc8J3K/5qcQ2baH lF2uZoW9aYixM2709tcyB58aAleyFXgFk5iKDNXmuaxxuLyoWjs9TmjT0diYOB4RgqNF IYgSalkLHAtIzw0axkafNE4CTUHA303qbFelxMBbg+qnkcf67oiesp2jkknCc0WIxAzA CXJyL1RA/D6j+PkxdnE/jw/a2RO5dwLRaYVRXUKRUfzeMyaPNeDWN0ZuaAGLvEGarWj5 9o+Q== 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=7DJYNUsy7AlyQI+EgImMMXPzgCn9duvfZGR0NuzPHk4=; fh=r6l/0fNLL/AXSlcs77R52Y8eFsr4+G/MvscPXgtp5Xs=; b=KhXU8yM9jtq/O0tGFO8ao27SeJMymObDg4ZzhYB5qrqrEP46WSWpoQV2iTGYA9a4MU BZSjZHz7D734lm7Gliz2mINVn5YSlxe37rGm7kqc45QjG9C9T+hzAIln2Xkse5a2+AtY 4ihTRy+9yKJvNupypWzUOtn2G1u2MVrcUgNLgxQ60EqXUaZyJZ3eZLym8HgS1opHXyP3 4e3W9VC+a+xs5Qq1JyFaAk4cMxZePRhEn0lO+hoW/FGiHSA5o1M8eGVrQfR6seTwomsS 0FVwUruNrXP/IJ9vj1wiRUTzXFSZWt1/AdJqMPP1zeTnPdqSiZ2PffjOVE/85P5WGjvN LN9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QvdIFPLM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id 23-20020a631657000000b005b8ee1c0c68si11444206pgw.605.2023.11.21.17.43.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 17:43:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QvdIFPLM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id B7841812D239; Tue, 21 Nov 2023 17:42:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343493AbjKVBmX (ORCPT + 99 others); Tue, 21 Nov 2023 20:42:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbjKVBmW (ORCPT ); Tue, 21 Nov 2023 20:42:22 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E713810C for ; Tue, 21 Nov 2023 17:42:18 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 076F8C433C8; Wed, 22 Nov 2023 01:42:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700617338; bh=BNdihuo6rMnCzk1/H6PiwRe9wn5OD68XRMeNziQ7hKk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QvdIFPLM2XOIWcKAqJ/k3h+HTLKJW8KwrJsH1YD42WUtYML5oTfWjgYZcPZqhktiL ObmedCPih8bwLUhsNhT7IswXr4Fi3AlH4ekgn5ym+RCQpj9W5CAxJxkRevjyowspz/ UgIBvI6egKKR6JWAUHeAKI09Zs4LeZ/R7SNXL1Qqxy38Fa2/18SzxWYHl082RI+fUq SdiZelsghStv/d3IlXC40lRpsGVtOI+baigtMua9K2aTokV6OMkzfxM+TR0Y4NfBc7 LD3+QEnTT4w5zzmG0kciJ+1V688wsVUUFYhZvstXKPYpxNBp6MA7wPfj9iqp9BN0Sy 20wWeFKRPGiaA== Date: Tue, 21 Nov 2023 17:42:16 -0800 From: Eric Biggers To: Jerry Shih Cc: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, herbert@gondor.apana.org.au, davem@davemloft.net, andy.chiu@sifive.com, greentime.hu@sifive.com, conor.dooley@microchip.com, guoren@kernel.org, bjorn@rivosinc.com, heiko@sntech.de, ardb@kernel.org, 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 07/12] RISC-V: crypto: add Zvkg accelerated GCM GHASH implementation Message-ID: <20231122014216.GI2172@sol.localdomain> References: <20231025183644.8735-1-jerry.shih@sifive.com> <20231025183644.8735-8-jerry.shih@sifive.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231025183644.8735-8-jerry.shih@sifive.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Tue, 21 Nov 2023 17:42:35 -0800 (PST) On Thu, Oct 26, 2023 at 02:36:39AM +0800, Jerry Shih wrote: > +struct riscv64_ghash_context { > + be128 key; > +}; > + > +struct riscv64_ghash_desc_ctx { > + be128 shash; > + u8 buffer[GHASH_BLOCK_SIZE]; > + u32 bytes; > +}; I recommend calling the first struct 'riscv64_ghash_tfm_ctx', and making the pointers to it be named 'tctx'. That would more clearly distinguish it from the desc_ctx / dctx. > + > +typedef void (*ghash_func)(be128 *Xi, const be128 *H, const u8 *inp, > + size_t len); > + > +static inline void ghash_blocks(const struct riscv64_ghash_context *ctx, > + struct riscv64_ghash_desc_ctx *dctx, > + const u8 *src, size_t srclen, ghash_func func) > + if (crypto_simd_usable()) { > + kernel_vector_begin(); > + func(&dctx->shash, &ctx->key, src, srclen); > + kernel_vector_end(); The indirection to ghash_func is unnecessary, since the only value is gcm_ghash_rv64i_zvkg. This also means that ghash_update() should be folded into ghash_update_zvkg(), and ghash_final() into ghash_final_zvkg(). > + } else { > + while (srclen >= GHASH_BLOCK_SIZE) { > + crypto_xor((u8 *)&dctx->shash, src, GHASH_BLOCK_SIZE); > + gf128mul_lle(&dctx->shash, &ctx->key); > + srclen -= GHASH_BLOCK_SIZE; > + src += GHASH_BLOCK_SIZE; > + } > + } The assembly code uses the equivalent of the following do-while loop instead: do { srclen -= GHASH_BLOCK_SIZE; } while (srclen); I.e., it assumes the length here is nonzero and a multiple of 16, which it is. To avoid confusion, I recommend making the C code use the same do-while loop. > const struct riscv64_ghash_context *ctx = > crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); crypto_tfm_ctx(crypto_shash_tfm(tfm)) should be crypto_shash_ctx(tfm) > +static int ghash_final(struct shash_desc *desc, u8 *out, ghash_func func) > +{ > + const struct riscv64_ghash_context *ctx = > + crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); > + struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc); > + int i; > + > + if (dctx->bytes) { > + for (i = dctx->bytes; i < GHASH_BLOCK_SIZE; i++) > + dctx->buffer[i] = 0; > + > + ghash_blocks(ctx, dctx, dctx->buffer, GHASH_BLOCK_SIZE, func); > + dctx->bytes = 0; > + } > + Setting dctx->bytes above is unnecessary. > +static int ghash_init(struct shash_desc *desc) > +{ > + struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc); > + > + *dctx = (struct riscv64_ghash_desc_ctx){}; > + > + return 0; > +} > + > +static int ghash_update_zvkg(struct shash_desc *desc, const u8 *src, > + unsigned int srclen) > +{ > + return ghash_update(desc, src, srclen, gcm_ghash_rv64i_zvkg); > +} > + > +static int ghash_final_zvkg(struct shash_desc *desc, u8 *out) > +{ > + return ghash_final(desc, out, gcm_ghash_rv64i_zvkg); > +} > + > +static int ghash_setkey(struct crypto_shash *tfm, const u8 *key, > + unsigned int keylen) > +{ > + struct riscv64_ghash_context *ctx = > + crypto_tfm_ctx(crypto_shash_tfm(tfm)); > + > + if (keylen != GHASH_BLOCK_SIZE) > + return -EINVAL; > + > + memcpy(&ctx->key, key, GHASH_BLOCK_SIZE); > + > + return 0; > +} > + > +static struct shash_alg riscv64_ghash_alg_zvkg = { > + .digestsize = GHASH_DIGEST_SIZE, > + .init = ghash_init, > + .update = ghash_update_zvkg, > + .final = ghash_final_zvkg, > + .setkey = ghash_setkey, IMO it's helpful to order the shash functions as follows, both in their definitions and their fields in struct shash_alg: setkey init update final That matches the order in which they're called. - Eric