Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp8218076rwr; Wed, 10 May 2023 20:41:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5G3VNt+88PaBui35YjjVMCYh0prSfoF7jfZzxNIRkXi658z4Iw5DSNSEwSrUm+FsAkQ1gG X-Received: by 2002:a05:6a00:a21:b0:62a:d752:acc5 with SMTP id p33-20020a056a000a2100b0062ad752acc5mr27452125pfh.32.1683776471911; Wed, 10 May 2023 20:41:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683776471; cv=none; d=google.com; s=arc-20160816; b=N5f2Uk4OxAP3652fRGKsu13su/jTgRa4QYnP+EfXwu6fFDpzT7pK/tR2SGvy1LPVYf j1/q2qGoyYWon+7NJVJc5hMJc26zmH4TxMA0IrawpjY2Yc5L4AHt27O3SORavvpsRZf1 BGvfBS0wMiflm+eaWZumzZWaOPwfjefvButT1lELbBk9jVuyHlNqdyffKu9grJmiEhLV WNJNgexMeRDl4yG0MfuZ3NUAGNHO3zvln6LMvBFZrHUVMSauBj9vhlON0khw31x1vZno /jQaXlgcEZ+ynF4B8JG6PCxfW9BRcX9PT7nSTK0+J4eXD4w2JAwyoRhYq4Sidi977kke LZfg== 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 :message-id:subject:cc:to:from:date; bh=rCk2OQxZTgQsTjhUkW7/ww4yycgUSqeVsEz9e/DTCR0=; b=XJlRc6m1TPoPxcTNd1oOw5TgMckE8hxcKyl1/XNw0fqOl/DoY3N5UZuWUgt80bqrYc cw8nehThI9ObWF9MEHwPRQPPLcnV7oibqIzdUMdMP6Pt39Q8tTMTyM8zBj8arDxxjOF5 vbpREYXXZ5Tww+28ep8F4BNalXDgvS1xHoS/H4KJXJMlmSY+PMGv4ivt/UVbReN049Om Ht3I33xD7Xd/6RrSFIQHtWmwxaBpT89kiKYHgExF5W/HYUEbrH0N5I+HYU36YmJKUS2S fhbQWLQ2R/JUDCfnG+hiFFNIVwnPG/QL1IGFUfoXg5L5y1fxD62Yy8kDObBATwzsoJf7 Ni9A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y4-20020aa79e04000000b006436618b22bsi2825577pfq.155.2023.05.10.20.40.50; Wed, 10 May 2023 20:41:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230520AbjEKDed (ORCPT + 99 others); Wed, 10 May 2023 23:34:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbjEKDec (ORCPT ); Wed, 10 May 2023 23:34:32 -0400 Received: from 167-179-156-38.a7b39c.syd.nbn.aussiebb.net (167-179-156-38.a7b39c.syd.nbn.aussiebb.net [167.179.156.38]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0A682D64; Wed, 10 May 2023 20:34:28 -0700 (PDT) Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.94.2 #2 (Debian)) id 1pwx3z-007Ytd-Ii; Thu, 11 May 2023 11:33:57 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Thu, 11 May 2023 11:33:56 +0800 Date: Thu, 11 May 2023 11:33:56 +0800 From: Herbert Xu To: Ross Philipson Cc: linux-kernel@vger.kernel.org, x86@kernel.org, linux-integrity@vger.kernel.org, linux-doc@vger.kernel.org, linux-crypto@vger.kernel.org, iommu@lists.linux-foundation.org, kexec@lists.infradead.org, linux-efi@vger.kernel.org, ross.philipson@oracle.com, dpsmith@apertussolutions.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, ardb@kernel.org, mjg59@srcf.ucam.org, James.Bottomley@hansenpartnership.com, luto@amacapital.net, nivedita@alum.mit.edu, kanth.ghatraju@oracle.com, trenchboot-devel@googlegroups.com Subject: Re: [PATCH v6 06/14] x86: Add early SHA support for Secure Launch early measurements Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230504145023.835096-7-ross.philipson@oracle.com> X-Newsgroups: apana.lists.os.linux.cryptoapi,apana.lists.os.linux.doc,apana.lists.os.linux.kernel X-Spam-Status: No, score=2.7 required=5.0 tests=BAYES_00,HELO_DYNAMIC_IPADDR2, RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS,TVD_RCVD_IP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Ross Philipson wrote: > > +static void __sha_transform(u32 *digest, const char *data) > +{ > + u32 ws[SHA1_WORKSPACE_WORDS]; > + > + sha1_transform(digest, data, ws); > + > + memzero_explicit(ws, sizeof(ws)); > +} > + > +void early_sha1_init(struct sha1_state *sctx) > +{ > + sha1_init(sctx->state); > + sctx->count = 0; > +} > + > +void early_sha1_update(struct sha1_state *sctx, > + const u8 *data, > + unsigned int len) > +{ > + unsigned int partial = sctx->count % SHA1_BLOCK_SIZE; > + > + sctx->count += len; > + > + if (likely((partial + len) >= SHA1_BLOCK_SIZE)) { > + int blocks; > + > + if (partial) { > + int p = SHA1_BLOCK_SIZE - partial; > + > + memcpy(sctx->buffer + partial, data, p); > + data += p; > + len -= p; > + > + __sha_transform(sctx->state, sctx->buffer); > + } > + > + blocks = len / SHA1_BLOCK_SIZE; > + len %= SHA1_BLOCK_SIZE; > + > + if (blocks) { > + while (blocks--) { > + __sha_transform(sctx->state, data); > + data += SHA1_BLOCK_SIZE; > + } > + } > + partial = 0; > + } > + > + if (len) > + memcpy(sctx->buffer + partial, data, len); > +} > + > +void early_sha1_final(struct sha1_state *sctx, u8 *out) > +{ > + const int bit_offset = SHA1_BLOCK_SIZE - sizeof(__be64); > + unsigned int partial = sctx->count % SHA1_BLOCK_SIZE; > + __be64 *bits = (__be64 *)(sctx->buffer + bit_offset); > + __be32 *digest = (__be32 *)out; > + int i; > + > + sctx->buffer[partial++] = 0x80; > + if (partial > bit_offset) { > + memset(sctx->buffer + partial, 0x0, SHA1_BLOCK_SIZE - partial); > + partial = 0; > + > + __sha_transform(sctx->state, sctx->buffer); > + } > + > + memset(sctx->buffer + partial, 0x0, bit_offset - partial); > + *bits = cpu_to_be64(sctx->count << 3); > + __sha_transform(sctx->state, sctx->buffer); > + > + for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++) > + put_unaligned_be32(sctx->state[i], digest++); > + > + *sctx = (struct sha1_state){}; > +} If we're going to add SHA1 then this should go into lib/crypto just like SHA2. Thanks, -- Email: Herbert Xu Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt