Skip to contentSkip to conversation

crypto: rsassa-pkcs1: use constant-time comparison for digest and signature verification

PATCH In review David C.C.M. Gall 5 participants 6 messages Jul 10 – Jul 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: rsassa-pkcs1: use constant-time comparison for digest and signature verification

Replace memcmp() with crypto_memneq() for cryptographic digest and signature comparisons to prevent timing side-channel attacks.

crypto/rsassa-pkcs1.c: RSA signature digest verification used memcmp which can leak valid prefix length via timing analysis, user data could reach the leaky comparison via the digest argument to verify.

Assisted-by gregkh_clanker_t1000
---
 crypto/rsassa-pkcs1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
crypto/rsassa-pkcs1.c +1 -1
index 94fa5e9600e7..a612a9eef2dd 100644
@@ -291,7 +291,7 @@ static int rsassa_pkcs1_verify(struct crypto_sig *tfm,
 	/* RFC 8017 sec 8.2.2 step 4 - comparison of digest with out_buf */
 	if (dlen != dst_len - pos)
 		return -EKEYREJECTED;
-	if (memcmp(digest, out_buf + pos, dlen) != 0)
+	if (crypto_memneq(digest, out_buf + pos, dlen))
 		return -EKEYREJECTED;
 
 	return 0;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · David C.C.M. Gall

Replace memcmp() with crypto_memneq() for cryptographic digest and signature comparisons to prevent timing side-channel attacks.

crypto/rsassa-pkcs1.c: RSA signature digest verification used memcmp which can leak valid prefix length via timing analysis, user data could reach the leaky comparison via the digest argument to verify.

Assisted-by gregkh_clanker_t1000

While we should use crypto_memneq() on MACs, auth tags, and other secret data, I don't think we should let it creep into domains where it is clearly not needed, like public key signature verification.

- Eric

#2 lore raw
Quoted 8 lines · Eric Biggers
Quoted 7 lines · David C.C.M. Gall

Replace memcmp() with crypto_memneq() for cryptographic digest and signature comparisons to prevent timing side-channel attacks.

crypto/rsassa-pkcs1.c: RSA signature digest verification used memcmp which can leak valid prefix length via timing analysis, user data could reach the leaky comparison via the digest argument to verify.

Quoted 6 lines
Quoted 2 lines
Assisted-by gregkh_clanker_t1000

While we should use crypto_memneq() on MACs, auth tags, and other secret data, I don't think we should let it creep into domains where it is clearly not needed, like public key signature verification.

But isn't this user-controlled data and so a user could use it to figure out the key?

thanks,

greg k-h

maintainer
#3 lore raw
Quoted 13 lines · Greg KH
Quoted 11 lines · Eric Biggers
Quoted 9 lines · David C.C.M. Gall

Replace memcmp() with crypto_memneq() for cryptographic digest and signature comparisons to prevent timing side-channel attacks.

crypto/rsassa-pkcs1.c: RSA signature digest verification used memcmp which can leak valid prefix length via timing analysis, user data could reach the leaky comparison via the digest argument to verify.

Assisted-by gregkh_clanker_t1000
Quoted 6 lines
Quoted 3 lines

While we should use crypto_memneq() on MACs, auth tags, and other secret data, I don't think we should let it creep into domains where it is clearly not needed, like public key signature verification.

But isn't this user-controlled data and so a user could use it to figure out the key?

This is signature verification with a public key. So the user knows the key already.

Quoted 4 lines

thanks,

greg k-h

Ignat

author
#4 lore raw
Quoted 24 lines · Ignat Korchagin
Quoted 13 lines · Greg KH
Quoted 11 lines · Eric Biggers
Quoted 9 lines · David C.C.M. Gall

Replace memcmp() with crypto_memneq() for cryptographic digest and signature comparisons to prevent timing side-channel attacks.

crypto/rsassa-pkcs1.c: RSA signature digest verification used memcmp which can leak valid prefix length via timing analysis, user data could reach the leaky comparison via the digest argument to verify.

Assisted-by gregkh_clanker_t1000
Quoted 6 lines
Quoted 3 lines

While we should use crypto_memneq() on MACs, auth tags, and other secret data, I don't think we should let it creep into domains where it is clearly not needed, like public key signature verification.

But isn't this user-controlled data and so a user could use it to figure out the key?

This is signature verification with a public key. So the user knows the key already.

Quoted 6 lines
Quoted 4 lines

thanks,

greg k-h

Ignat

Nevermind, my reasoning on how this method is used was faulty. The crypto_memneq call does not protect against digest forgery, the public key is already available to anyone attempting verification, so an attacker can compute the padding/digest offline without needing the kernel's comparison at all, timing or otherwise.

That said, this function already uses crypto_memneq for the hash-prefix check a few lines above. I'd argue for consistency it's worth using it for the digest comparison too.

David

maintainer
#5 lore raw
Quoted 4 lines · David Gall

That said, this function [rsassa_pkcs1_verify()] already uses crypto_memneq() for the hash-prefix check a few lines above. I'd argue for consistency it's worth using it for the digest comparison too.

I'd prefer the other way round, i.e. to use memcmp() for the hash_prefix comparison.

Thanks,

Lukas