Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp5415038pxu; Thu, 22 Oct 2020 01:24:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqiQEg7dNnd6AxNLNLZrdxh4LRuawA2KAD797oBWwt8LvLYaJ4sADUD/1vPUL3HgdqkvO/ X-Received: by 2002:a17:906:4dc3:: with SMTP id f3mr1274017ejw.50.1603355084619; Thu, 22 Oct 2020 01:24:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603355084; cv=none; d=google.com; s=arc-20160816; b=M398WcoiEZRJ4PzbvQh/BW13fBbUfyfJiNLD9s8AAuZUwEn2Zz+jksEztrJQg3bsXq DQTnniFA5GQanOQ+d3ClMem11crvBUGTEXIo3JDq8maG4K4fwSbfrayDPLU0D6E7NZZR 3Eii/Wp8IxnYnGAgGTMvlFDC9kwZ5GRHzSSNjPNn7NV3PgmLqzYee/Bt0EAfDjHdrgQ6 baeOSOEHm+ePDs8LXvUfb1KXa2o1jZIwxYnBlDS7zy2f4/1/0c2DQ8XL18mBA9IhT41g 7WIfC5kZcrt5cxd+fFlvvlUMkdqDTuQWsHdTkGkcNd/YCnHbF7lnMr0wyuoOdHtRTKmL QU0g== 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=UzqlxgMkUqXZJ46VccKO/IA/2wru7mSd6B7HtlIiN9Q=; b=MVdgIvCrN3qt2BLNXkDM2NaEyxGUFsxL8A+d7htJ9aRTqmP4vGF3YycmCbHmjK1tVc ykkJOBKm+5880ezL8ak7tqFePgrMA7hRB+F5IcqV4FdXzRStsALIJ8WuVXDMoYWZgolD xfOTDRuN6Fsi028QSXmiQyT/7Ug649o3WQX120YfRxnHovUK5anTi0hS38Gr90kU83Kx 9fthVnB4il6B9VysTbPxSXkkivnwlWXRlNBIZbPWe1K56ppDtWJtk/QCjhYAkuXAaVTO CL1GgpovHZ6n9cH6h/ETWnbT54JjPt0Pd/heqEBrfz65KKW/imlXIkdmrVmk9A0n0GdJ n9kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AWbhcZ7C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q20si530914ejb.102.2020.10.22.01.24.21; Thu, 22 Oct 2020 01:24:44 -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; dkim=pass header.i=@kernel.org header.s=default header.b=AWbhcZ7C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2507648AbgJVE6x (ORCPT + 99 others); Thu, 22 Oct 2020 00:58:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:57320 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2507647AbgJVE6x (ORCPT ); Thu, 22 Oct 2020 00:58:53 -0400 Received: from sol.localdomain (172-10-235-113.lightspeed.sntcca.sbcglobal.net [172.10.235.113]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2C333223C7; Thu, 22 Oct 2020 04:58:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603342732; bh=hN7GiMRaOq0FOXfdA6xo4EAlbLaq+CosLc5jqhzgnvA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=AWbhcZ7C04iuVaR0/RhfNSWB1YYayIzQXbZCg5sitAbKcayPqtKCY1r2NJ22CrSDT OXgIY04zLiM+FPK97mEZqMffcqp1HlmacPN3md38OuFuKxz8u8JAJIk5JZWcr2br+U wKXqRjMxAiQ3QDb7roMwfmQXuexgpgIfqucEeVRg= Date: Wed, 21 Oct 2020 21:58:50 -0700 From: Eric Biggers To: Arvind Sankar Cc: Herbert Xu , "David S. Miller" , "linux-crypto@vger.kernel.org" , David Laight , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/6] crypto: lib/sha256 - Don't clear temporary variables Message-ID: <20201022045850.GE857@sol.localdomain> References: <20201020203957.3512851-1-nivedita@alum.mit.edu> <20201020203957.3512851-3-nivedita@alum.mit.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201020203957.3512851-3-nivedita@alum.mit.edu> Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Tue, Oct 20, 2020 at 04:39:53PM -0400, Arvind Sankar wrote: > The assignments to clear a through h and t1/t2 are optimized out by the > compiler because they are unused after the assignments. > > These variables shouldn't be very sensitive: t1/t2 can be calculated > from a through h, so they don't reveal any additional information. > Knowing a through h is equivalent to knowing one 64-byte block's SHA256 > hash (with non-standard initial value) which, assuming SHA256 is secure, > doesn't reveal any information about the input. > > Signed-off-by: Arvind Sankar I don't entirely buy the second paragraph. It could be the case that the input is less than or equal to one SHA-256 block (64 bytes), in which case leaking 'a' through 'h' would reveal the final SHA-256 hash if the input length is known. And note that callers might consider either the input, the resulting hash, or both to be sensitive information -- it depends. > --- > lib/crypto/sha256.c | 1 - > 1 file changed, 1 deletion(-) > > diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c > index d43bc39ab05e..099cd11f83c1 100644 > --- a/lib/crypto/sha256.c > +++ b/lib/crypto/sha256.c > @@ -202,7 +202,6 @@ static void sha256_transform(u32 *state, const u8 *input) > state[4] += e; state[5] += f; state[6] += g; state[7] += h; > > /* clear any sensitive info... */ > - a = b = c = d = e = f = g = h = t1 = t2 = 0; > memzero_explicit(W, 64 * sizeof(u32)); > } Your change itself is fine, though. As you mentioned, these assignments get optimized out, so they weren't accomplishing anything. The fact is, there just isn't any way to guarantee in C code that all sensitive variables get cleared. So we shouldn't (and generally don't) bother trying to clear individual u32's, ints, etc. like this, but rather only structs and arrays, as clearing those is more likely to work as intended. - Eric