Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3288479pxu; Mon, 19 Oct 2020 08:31:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJya2OJ2ujLfm7mvMJcFKlNnlUTSyraWAqvBAkvyLMwRD+lD1ecNd7ZLsw7iLRI9oxl7yJx+ X-Received: by 2002:a17:906:6a82:: with SMTP id p2mr423270ejr.3.1603121510821; 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=CaT51fjVf4pFeA7rS2w+4+Wcn+NFbuDKnaLJJs2C0JxamYSGlBAAOYtjSiUvpiPgQn DhivflMczGVCS6UPyRsmLB3DOAhQ4jBZPdKje0hxgC4mAuKTSkgdQtD0Widz6KYaj2PE S1GlppTrJN3nRZu0YNqiUOZeNHcDwVEbMuPopxNaFInHTWu2FU+xU361vOkpnhi5NU33 okZlLgR1g6eqelp2Kgbt/LrcJ21MjAoO+aH0jRLTYY+qOKGvJVNgmf38EvScoJ+ludc/ PIIfYTpt4/Tse0I6tEP+KUxBeboSJAofr/T45FVvRrlFaMTUknm15qFxchislgSw98wE x7vA== 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=oRMf8eK1KvMjfAflD4Pf4h9XYH++ind//dYyDMVAMdw=; b=QExLlFPtwL+zbJewRKh3sMl7e2ABOeTuaC+HmYuyl6H/7mk3tk8KIlUJeBvGAF3KXq Op3Zj2XRBhCPULLAUaSVBKvKWTXy9ZBAjP/74GighDBwB52vicTBKDmHeKgIcjj0rlM6 ntrSyAxC81kp2Z1CyjPVvneIYnoARpmCLSNybjCCWH6LIC15XkROe4ymnVLI9r8PHV0E brIq2bE/tH2CrUsPhSCpBMVDaOBpYniK+Ayf5LAMf0r+Yz96KL7mNL0cwpN3RGWgOIy7 /syjahnoO4HAHRoHB2uQS1xcg/ec7s8w1F0nThfMFOgQ9qIp6wSrbbEMidwoKsh7BtVY 18bQ== 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 gw19si192654ejb.650.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 S1730150AbgJSPaW (ORCPT + 99 others); Mon, 19 Oct 2020 11:30:22 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:41545 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729075AbgJSPaU (ORCPT ); Mon, 19 Oct 2020 11:30:20 -0400 Received: by mail-io1-f68.google.com with SMTP id 67so41872iob.8; Mon, 19 Oct 2020 08:30:19 -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=oRMf8eK1KvMjfAflD4Pf4h9XYH++ind//dYyDMVAMdw=; b=UOmzfKM0wvNbMOBoA/La7t0l9tA75WQNjMZ7qYtZ2vFV/S2MO8tYdwTm2Z1Qk9aTrw Umh5cdZToscvtHrSBsnkW3wSUZwoKLXv+OTQWeHlpNRIB3hFxcNEPomo2O5nKy9jirv9 bPm1MEWLCeBIWq7TnC1Hh4ApZpLVh+GvckbsU1KKEF3dAJCJR/j9ZBAVE/pZAvPXjwQE GKbKcXE17SksdcNXo/B4Gtn10OnOylN4LeEQfDIOo8s41OWyImdun2MNn9iajZX4nW2O qzwpfehpImKzVOtbfHTqQw6V0ESJRprrzY7+ibRioc2SwQzX4WJStZk/aYOtIgwYk62V 6PGg== X-Gm-Message-State: AOAM532URCCzrAoD2JmdZqU06vL3wwnqqR21yMF1dnovBwuv6Comm0HO CfObSiFffK29GSrDi/zeeY4= X-Received: by 2002:a5d:8908:: with SMTP id b8mr3749227ion.96.1603121419444; Mon, 19 Oct 2020 08:30:19 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2020 08:30:18 -0700 (PDT) From: Arvind Sankar To: Herbert Xu , "David S. Miller" , linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 1/5] crypto: Use memzero_explicit() for clearing state Date: Mon, 19 Oct 2020 11:30:12 -0400 Message-Id: <20201019153016.2698303-2-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 Without the barrier_data() inside memzero_explicit(), the compiler may optimize away the state-clearing if it can tell that the state is not used afterwards. At least in lib/crypto/sha256.c:__sha256_final(), the function can get inlined into sha256(), in which case the memset is optimized away. Signed-off-by: Arvind Sankar --- include/crypto/sha1_base.h | 3 ++- include/crypto/sha256_base.h | 3 ++- include/crypto/sha512_base.h | 3 ++- include/crypto/sm3_base.h | 3 ++- lib/crypto/sha256.c | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/crypto/sha1_base.h b/include/crypto/sha1_base.h index 20fd1f7468af..a5d6033efef7 100644 --- a/include/crypto/sha1_base.h +++ b/include/crypto/sha1_base.h @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -101,7 +102,7 @@ static inline int sha1_base_finish(struct shash_desc *desc, u8 *out) for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++) put_unaligned_be32(sctx->state[i], digest++); - *sctx = (struct sha1_state){}; + memzero_explicit(sctx, sizeof(*sctx)); return 0; } diff --git a/include/crypto/sha256_base.h b/include/crypto/sha256_base.h index 6ded110783ae..93f9fd21cc06 100644 --- a/include/crypto/sha256_base.h +++ b/include/crypto/sha256_base.h @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -105,7 +106,7 @@ static inline int sha256_base_finish(struct shash_desc *desc, u8 *out) for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be32)) put_unaligned_be32(sctx->state[i], digest++); - *sctx = (struct sha256_state){}; + memzero_explicit(sctx, sizeof(*sctx)); return 0; } diff --git a/include/crypto/sha512_base.h b/include/crypto/sha512_base.h index fb19c77494dc..93ab73baa38e 100644 --- a/include/crypto/sha512_base.h +++ b/include/crypto/sha512_base.h @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -126,7 +127,7 @@ static inline int sha512_base_finish(struct shash_desc *desc, u8 *out) for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64)) put_unaligned_be64(sctx->state[i], digest++); - *sctx = (struct sha512_state){}; + memzero_explicit(sctx, sizeof(*sctx)); return 0; } diff --git a/include/crypto/sm3_base.h b/include/crypto/sm3_base.h index 1cbf9aa1fe52..2f3a32ab97bb 100644 --- a/include/crypto/sm3_base.h +++ b/include/crypto/sm3_base.h @@ -13,6 +13,7 @@ #include #include #include +#include #include typedef void (sm3_block_fn)(struct sm3_state *sst, u8 const *src, int blocks); @@ -104,7 +105,7 @@ static inline int sm3_base_finish(struct shash_desc *desc, u8 *out) for (i = 0; i < SM3_DIGEST_SIZE / sizeof(__be32); i++) put_unaligned_be32(sctx->state[i], digest++); - *sctx = (struct sm3_state){}; + memzero_explicit(sctx, sizeof(*sctx)); return 0; } diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c index 2321f6cb322f..d43bc39ab05e 100644 --- a/lib/crypto/sha256.c +++ b/lib/crypto/sha256.c @@ -265,7 +265,7 @@ static void __sha256_final(struct sha256_state *sctx, u8 *out, int digest_words) put_unaligned_be32(sctx->state[i], &dst[i]); /* Zeroize sensitive information. */ - memset(sctx, 0, sizeof(*sctx)); + memzero_explicit(sctx, sizeof(*sctx)); } void sha256_final(struct sha256_state *sctx, u8 *out) -- 2.26.2