Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] crypto: pkcs7 - Use constant-time message digest comparison
@ 2026-07-20  3:23 Jiangshan Yi
  2026-07-20  6:21 ` Lukas Wunner
  0 siblings, 1 reply; 2+ messages in thread
From: Jiangshan Yi @ 2026-07-20  3:23 UTC (permalink / raw)
  To: dhowells, lukas, ignat, herbert, davem
  Cc: linux-kernel, linux-crypto, keyrings, 13667453960, Jiangshan Yi

The message digest comparison in pkcs7_verify() uses memcmp(), whose
execution time depends on the position of the first mismatched byte.
This creates a timing side-channel that could allow an attacker to
forge PKCS#7 signatures byte by byte.

Replace memcmp() with crypto_memneq() to perform a constant-time
comparison, consistent with the rest of the crypto subsystem
(gcm, ccm, chacha20poly1305, rsassa-pkcs1, krb5, verify_pefile, etc.).
Add an explicit <crypto/utils.h> include since this file did not
previously pull in the crypto_memneq() declaration.

Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
---
 crypto/asymmetric_keys/pkcs7_verify.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/crypto/asymmetric_keys/pkcs7_verify.c b/crypto/asymmetric_keys/pkcs7_verify.c
index 474e2c1ae21b..8c619b252ca5 100644
--- a/crypto/asymmetric_keys/pkcs7_verify.c
+++ b/crypto/asymmetric_keys/pkcs7_verify.c
@@ -14,6 +14,7 @@
 #include <crypto/hash.h>
 #include <crypto/hash_info.h>
 #include <crypto/public_key.h>
+#include <crypto/utils.h>
 #include "pkcs7_parser.h"
 
 /*
@@ -93,8 +94,8 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
 			goto error;
 		}
 
-		if (memcmp(sig->m, sinfo->msgdigest,
-			   sinfo->msgdigest_len) != 0) {
+		if (crypto_memneq(sig->m, sinfo->msgdigest,
+				  sinfo->msgdigest_len)) {
 			pr_warn("Sig %u: Message digest doesn't match\n",
 				sinfo->index);
 			ret = -EKEYREJECTED;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] crypto: pkcs7 - Use constant-time message digest comparison
  2026-07-20  3:23 [PATCH] crypto: pkcs7 - Use constant-time message digest comparison Jiangshan Yi
@ 2026-07-20  6:21 ` Lukas Wunner
  0 siblings, 0 replies; 2+ messages in thread
From: Lukas Wunner @ 2026-07-20  6:21 UTC (permalink / raw)
  To: Jiangshan Yi
  Cc: dhowells, ignat, herbert, davem, linux-kernel, linux-crypto,
	keyrings, 13667453960

On Mon, Jul 20, 2026 at 11:23:16AM +0800, Jiangshan Yi wrote:
> +++ b/crypto/asymmetric_keys/pkcs7_verify.c
> @@ -93,8 +94,8 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
>  			goto error;
>  		}
>  
> -		if (memcmp(sig->m, sinfo->msgdigest,
> -			   sinfo->msgdigest_len) != 0) {
> +		if (crypto_memneq(sig->m, sinfo->msgdigest,
> +				  sinfo->msgdigest_len)) {
>  			pr_warn("Sig %u: Message digest doesn't match\n",
>  				sinfo->index);
>  			ret = -EKEYREJECTED;

The exact same patch was previously submitted by someone else
and rejected:

https://lore.kernel.org/all/alEsSl8i1_FpoU0f@fudgebox/T/#u

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-20  6:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-20  3:23 [PATCH] crypto: pkcs7 - Use constant-time message digest comparison Jiangshan Yi
2026-07-20  6:21 ` Lukas Wunner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox