From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE0CC481FC5 for ; Thu, 13 Aug 2026 13:50:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786629047; cv=none; b=jdXLn8dv6WSBsqaABSS/3ApIekQe9J9Wqr1CbDoSNOzkPmxInt8CmSLJqa1z8sPSA8+Fcge9gg9bQAuFeAjxjUoInz4PWhaH2I6XcUH4bJAWKmCOClvPYvscQtWmOL1ECf5sE/3OsNCNpPOXQkO63gmRpScPhMZ33LaZBY7zQiQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786629047; c=relaxed/simple; bh=LBJGRPEX+c7ESpwmdOsrAF1eS6K7PlwVdVAherwht48=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iuYEEnMZGqg+GQXyvfL1F4Mp7AEJWBAlQ6Z3pSLgd+BLBFBUz8BzWb43pH2ufU4qUH06Jqh+6dbJGyIgtq72prKcq87gIvw6ILZ8LP4QHndm8ATGbbOgy7JPwf5/BiF0anFrfJ0V2ZC3qiFjLdsfiRGaHiqhvbXCuGig4q1K7bk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FLoNdA+T; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FLoNdA+T" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1786629044; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qrmNWOfIIVDyeK/HAUK9wucBz8cLmmeO96RwNn6+/qo=; b=FLoNdA+Tb2+B2t2aceUVw5dz+fRabX1u00F7ZBfdqqGZJA21BKQFDSKVLFoiBD3GaBOr/A +zLSp/e8AOim8c7TXu/6pJlcs6uHss++3WqFy+ergqC8RG4qBPvLeD5DHojk+jI8DpgAV4 hJH7EYFy8I0wLvqg7OWk7C64BtArDgk= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-423-E9bXOoQfOG-mrSnz4Qjkww-1; Thu, 13 Aug 2026 09:50:41 -0400 X-MC-Unique: E9bXOoQfOG-mrSnz4Qjkww-1 X-Mimecast-MFC-AGG-ID: E9bXOoQfOG-mrSnz4Qjkww_1786629040 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E98DD180075E; Thu, 13 Aug 2026 13:50:39 +0000 (UTC) Received: from thuth-p1g4.redhat.corp (headnet03.pony-001.prod.iad2.dc.redhat.com [10.2.32.114]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 785791800348; Thu, 13 Aug 2026 13:50:37 +0000 (UTC) From: Thomas Huth To: Eric Biggers , "Jason A. Donenfeld" , Ard Biesheuvel , Herbert Xu , "David S. Miller" Cc: x86@kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 11/11] lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx structures Date: Thu, 13 Aug 2026 15:49:49 +0200 Message-ID: <20260813134953.979481-12-thuth@redhat.com> In-Reply-To: <20260813134953.979481-1-thuth@redhat.com> References: <20260813134953.979481-1-thuth@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 From: Thomas Huth Some crypto code functions need to zeroize their local SHA2 hmac_sha*_ctx structures after use to avoid leaking sensitive material on the stack. Provide hmac_sha*_zeroize_ctx() helper functions that can be used with __cleanup() to automatically zeroize the context when it goes out of scope. Signed-off-by: Thomas Huth --- Note: These will be useful in some spots in the fs/smb/ code later. include/crypto/sha2.h | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h index 7bb8fe169daf2..2b2b06ebff993 100644 --- a/include/crypto/sha2.h +++ b/include/crypto/sha2.h @@ -7,6 +7,7 @@ #define _CRYPTO_SHA2_H #include +#include #define SHA224_DIGEST_SIZE 28 #define SHA224_BLOCK_SIZE 64 @@ -218,6 +219,20 @@ struct hmac_sha224_ctx { struct __hmac_sha256_ctx ctx; }; +/** + * hmac_sha224_zeroize_ctx() - Zeroize an hmac_sha224_ctx structure + * @ctx: The location of the context that should be zeroized + * + * This function explicitly fills the hmac_sha224_ctx with zeroes. For + * example, use it with __cleanup() for local hmac_sha224_ctx structures + * on the stack, so that their content is not leaked when the context is + * left. Note: This is only required when not using hmac_sha224_final(). + */ +static inline void hmac_sha224_zeroize_ctx(struct hmac_sha224_ctx *ctx) +{ + memzero_explicit(ctx, sizeof(*ctx)); +} + /** * hmac_sha224_preparekey() - Prepare a key for HMAC-SHA224 * @key: (output) the key structure to initialize @@ -422,6 +437,20 @@ struct hmac_sha256_ctx { struct __hmac_sha256_ctx ctx; }; +/** + * hmac_sha256_zeroize_ctx() - Zeroize an hmac_sha256_ctx structure + * @ctx: The location of the context that should be zeroized + * + * This function explicitly fills the hmac_sha256_ctx with zeroes. For + * example, use it with __cleanup() for local hmac_sha256_ctx structures + * on the stack, so that their content is not leaked when the context is + * left. Note: This is only required when not using hmac_sha256_final(). + */ +static inline void hmac_sha256_zeroize_ctx(struct hmac_sha256_ctx *ctx) +{ + memzero_explicit(ctx, sizeof(*ctx)); +} + /** * hmac_sha256_preparekey() - Prepare a key for HMAC-SHA256 * @key: (output) the key structure to initialize @@ -631,6 +660,20 @@ struct hmac_sha384_ctx { struct __hmac_sha512_ctx ctx; }; +/** + * hmac_sha384_zeroize_ctx() - Zeroize an hmac_sha384_ctx structure + * @ctx: The location of the context that should be zeroized + * + * This function explicitly fills the hmac_sha384_ctx with zeroes. For + * example, use it with __cleanup() for local hmac_sha384_ctx structures + * on the stack, so that their content is not leaked when the context is + * left. Note: This is only required when not using hmac_sha384_final(). + */ +static inline void hmac_sha384_zeroize_ctx(struct hmac_sha384_ctx *ctx) +{ + memzero_explicit(ctx, sizeof(*ctx)); +} + /** * hmac_sha384_preparekey() - Prepare a key for HMAC-SHA384 * @key: (output) the key structure to initialize @@ -806,6 +849,20 @@ struct hmac_sha512_ctx { struct __hmac_sha512_ctx ctx; }; +/** + * hmac_sha512_zeroize_ctx() - Zeroize an hmac_sha512_ctx structure + * @ctx: The location of the context that should be zeroized + * + * This function explicitly fills the hmac_sha512_ctx with zeroes. For + * example, use it with __cleanup() for local hmac_sha512_ctx structures + * on the stack, so that their content is not leaked when the context is + * left. Note: This is only required when not using hmac_sha512_final(). + */ +static inline void hmac_sha512_zeroize_ctx(struct hmac_sha512_ctx *ctx) +{ + memzero_explicit(ctx, sizeof(*ctx)); +} + /** * hmac_sha512_preparekey() - Prepare a key for HMAC-SHA512 * @key: (output) the key structure to initialize -- 2.55.0