Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3288512pxu; Mon, 19 Oct 2020 08:31:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxhuUpBPb7lR8gLELipbPBOB4woKOUGG3Rh49OMTpwDKcjeN5wMZPeROkrEM11/o5QwLFC X-Received: by 2002:aa7:d7ce:: with SMTP id e14mr379606eds.258.1603121510600; Mon, 19 Oct 2020 08:31:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603121510; cv=none; d=google.com; s=arc-20160816; b=ln78/PahbHyAnp4r++fMqmww0Ijj2PyMSJ6+/AcYWa0JWdj3Zxs3Wfg67Q8NgT5v5X cnBGhirSZpjyMRvOBhuF2R2g9waokJbg+B/Dt42Ia1HYgft+N3qeuhilMahdlLLXCKTo yHcndz2cKAvp7Jl4Bud5B3Jf3dnM3GIAXhJy6Y6c7DGadisBBYBxwRe4IQnicF8YhYd4 J8H1pDVeRCRtgaK882d7rM+/iWiILyJhiJ1bJyNnQd/i17X0F0lbT8o26pLmhzk0mCtY D/7tvDPSs7vu2VATwYcuvTtOILnDMqZfdFOn9cWLYux4ibp1FMHBQQjZYVU+wARsdVnL tfRw== 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=RL0IUKseEvfvIK0Ckpij8eOauCt3HiAtDn+mwKdXAa2TlVLBRVHADmtYIzJBvH76xN 6nHd+dBp835a9YScZM0/d6Xu/GdL2ClZ/UYE+kRtT39OstNm9WjROqcBnhCCxmy67LVj DOlP8yw4Slo2ysuXggctVymZhHLe55qUemzWU4pswjfsp+H/7Geryv1c2C+JVDMlraeA QkpW+ZFXHhwXt4thpLRbQVzXYPnwnfLR/JtHtiPM2nM2OCAcaJYJNNMHVbs4jD3jZFew XOLq/z3j2gP4wDU62UdJfr6qUB1bLSKRc5u2Gu1tyUXc9/WivN9nAY5idc98N1MekOAY dQ4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 s8si19534edy.195.2020.10.19.08.31.28; Mon, 19 Oct 2020 08:31:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730185AbgJSPaY (ORCPT + 99 others); Mon, 19 Oct 2020 11:30:24 -0400 Received: from mail-io1-f67.google.com ([209.85.166.67]:34344 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730159AbgJSPaX (ORCPT ); Mon, 19 Oct 2020 11:30:23 -0400 Received: by mail-io1-f67.google.com with SMTP id z5so120141iob.1; Mon, 19 Oct 2020 08:30:22 -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=MSWB6fqIC6j6CuW5pNwGF6kA9prfZNxEGnGCkLHeyHeN2vqvRGcbZ5n3F1Ug71+3TR S3pnKQ/qV9Son6pwJjK41QHMvK5DFRhX6iM/TicSgVZttJb8lj/voLn8ct+abl5qsLK0 kyTwMN05MYFIKMfcGPNGDQ0txA5McrADbXjbUOHusMltZAtxnbH1v48Y/xejve1Yx48q V9SZIA8QziVRTVma/PUiZ2uJ+7PRxyxJT7NnMspIsZ0XWo0gftZ7xPHru4oJ/LuLjNwb h0su3WyzRrTd3gsFd4lJ8e9d/yZea8gJQsoHStpO2jT0B9brQsEUiC3LvSHqhCRY7wnU kzFQ== X-Gm-Message-State: AOAM530FvBO7nVHsR8R+KFgPZ5zj4anzcNpRQLpJwQqBqvIH8wfiVBtw gVdpsyW+gdg0F6NaBgShKn8uGKB5Jy1PrA== X-Received: by 2002:a02:8661:: with SMTP id e88mr390665jai.43.1603121421961; Mon, 19 Oct 2020 08:30:21 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id m86sm20898ilb.44.2020.10.19.08.30.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2020 08:30:21 -0700 (PDT) From: Arvind Sankar To: Herbert Xu , "David S. Miller" , linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 3/5] crypto: lib/sha256 - Clear W[] in sha256_update() instead of sha256_transform() Date: Mon, 19 Oct 2020 11:30:14 -0400 Message-Id: <20201019153016.2698303-4-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201019153016.2698303-1-nivedita@alum.mit.edu> References: <20201019153016.2698303-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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