Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4698458pxu; Wed, 21 Oct 2020 03:06:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxjW/+AKSRu/riab6KPBZyUJuU/QSeDWKPsnjI7ehf/LFS0kMWBe+GSaMHv5zyoWnRWlcvc X-Received: by 2002:a50:88e5:: with SMTP id d92mr2312572edd.145.1603274786653; Wed, 21 Oct 2020 03:06:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603274786; cv=none; d=google.com; s=arc-20160816; b=aQTKqHH3LH9JzXcoWA7eQAWT3tisC6ZKCEasP2KAcuExwpnpNPkcYdqftTMdgXlTae 5kxj6Yt8Y93Kt2q0bmgKKuV/KSZPrPoBGvvuYBD4CsQE4HHXNcscQP9mUPpDZq7aoRdW W2ez0nXgimlKp4bXIOv4nZpzgju2+3dDQdpFvxKSDvV7RqH+HWtwtpzgDx31VFqwHBOH bSwFp4s8Zl2DICLFgzCJOaQB7pMU47sTC7q7MTO24Ndw18JZa/BooIOL/BqGfxc/e66g Lrdc57Uqoy7kqfIHTVX48LFAWx0TEGHpWEx5IVuvWfyTEmFoX1JRlD/EuYbcg3z07Rrv P+dQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=aC0csafA10XEKxluLw+TGr+3B4XctKQH20XSmRK1UrE=; b=YA2X3xdzVvssbMl/dpz4d484ZUgmrIcvaArGQZQgaMykmDkSS2BErL7vZeZwOolgaM 7hphUmzKr+gsvCsDBVu42E9gd96ipUql1OOBfVfLDQUQlZI8AmRyQJHEJ5mg3A5UCVrr arXLCBQfnl1H3/qQEIrv4xxu2KPDnz1V/CfzXY+khHmZ/RDdWooS5FI4RA7w31dCeaGz PcBSf+Ekf1cWCRboORvSgl590Nh3srMP0CruY6ju/QepAEF7NVLk6SLzH/kq4lcRGumj QJ1P4G0E5R1vRqrP4dqrqRzR4bI5fMCfXpbeTONPlnVwqIhm4I9+qMGz3jfHiLTAuKWL RB/w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g10si1047924edm.124.2020.10.21.03.06.02; Wed, 21 Oct 2020 03:06:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438583AbgJTUkF (ORCPT + 99 others); Tue, 20 Oct 2020 16:40:05 -0400 Received: from mail-qv1-f66.google.com ([209.85.219.66]:37877 "EHLO mail-qv1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438579AbgJTUkE (ORCPT ); Tue, 20 Oct 2020 16:40:04 -0400 Received: by mail-qv1-f66.google.com with SMTP id t6so1621570qvz.4; Tue, 20 Oct 2020 13:40:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aC0csafA10XEKxluLw+TGr+3B4XctKQH20XSmRK1UrE=; b=l+N9kGRbHb5p1FzQmgPSf84R+mww8Tb+Xfig/BDNCb7hSiqh8jJeIoWu0A9oCH9a3x iWTmJ5cWMYDTUGArKprzRpZ7evoUPbpEtUcl3F3KvoJ6bFuVecHbj7ZaZ5Xx8LwkOdlQ ogHVLrfyJQo6jFz7G/3XZK2SUPVlIydhc+vX0cmtL5nqefHTLf4BBgv/nH5GMnp8MQwA eyC8m5z5nXYQWNMqH+G51SwcqMZQqUXRLaAR1um7XaPdAdCRFZ8LkZ7ARAxNuO1qFLkO FNG9vIWzJZWLyS+IMpcYNNximhKUduXHcjSW9RQgFwYU/v+7K3DU9Uv0pUPJ3EUcxPaZ AM0Q== X-Gm-Message-State: AOAM533Q5AO/3BrBHImD81pjvNjo2s0LXmy6cSeph81a+OpbVaUSZmxH 5f+Kw7KQNuUIMoWnC3nWlbM= X-Received: by 2002:a0c:f102:: with SMTP id i2mr5264617qvl.29.1603226403277; Tue, 20 Oct 2020 13:40:03 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id m18sm1411165qkk.102.2020.10.20.13.40.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Oct 2020 13:40:02 -0700 (PDT) From: Arvind Sankar To: Herbert Xu , "David S. Miller" , "linux-crypto@vger.kernel.org" , David Laight Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 3/6] crypto: lib/sha256 - Clear W[] in sha256_update() instead of sha256_transform() Date: Tue, 20 Oct 2020 16:39:54 -0400 Message-Id: <20201020203957.3512851-4-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201020203957.3512851-1-nivedita@alum.mit.edu> References: <20201020203957.3512851-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The temporary W[] array is currently zeroed out once every call to sha256_transform(), i.e. once every 64 bytes of input data. Moving it to sha256_update() instead so that it is cleared only once per update can save about 2-3% of the total time taken to compute the digest, with a reasonable memset() implementation, and considerably more (~20%) with a bad one (eg the x86 purgatory currently uses a memset() coded in C). Signed-off-by: Arvind Sankar --- lib/crypto/sha256.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c index 099cd11f83c1..c6bfeacc5b81 100644 --- a/lib/crypto/sha256.c +++ b/lib/crypto/sha256.c @@ -43,10 +43,9 @@ static inline void BLEND_OP(int I, u32 *W) W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16]; } -static void sha256_transform(u32 *state, const u8 *input) +static void sha256_transform(u32 *state, const u8 *input, u32 *W) { u32 a, b, c, d, e, f, g, h, t1, t2; - u32 W[64]; int i; /* load the input */ @@ -200,15 +199,13 @@ static void sha256_transform(u32 *state, const u8 *input) state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; state[5] += f; state[6] += g; state[7] += h; - - /* clear any sensitive info... */ - memzero_explicit(W, 64 * sizeof(u32)); } void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len) { unsigned int partial, done; const u8 *src; + u32 W[64]; partial = sctx->count & 0x3f; sctx->count += len; @@ -223,11 +220,13 @@ void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len) } do { - sha256_transform(sctx->state, src); + sha256_transform(sctx->state, src, W); done += 64; src = data + done; } while (done + 63 < len); + memzero_explicit(W, sizeof(W)); + partial = 0; } memcpy(sctx->buf + partial, src, len - done); -- 2.26.2