Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions wolfcrypt/src/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1882,8 +1882,15 @@ static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen,
return ret;
}

tmp[0] &= (byte)((1 << bits) - 1);
pkcsBlock[0] &= (byte)((1 << bits) - 1);
/* When bits==0 the key size is an exact multiple of 8 and pkcsBlock was
* already advanced past the leading 0x00 byte (see above); no masking is
* needed. (1<<0)-1 == 0 would zero both bytes and corrupt the XOR
* separator check below. RsaPad_PSS guards the same step with
* "if (hiBits)" for the same reason. */
Comment on lines +1885 to +1889
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment’s statement “the key size is an exact multiple of 8” appears inconsistent with how bits is derived for RSA-PSS MSB masking. With the common pattern bits = (mp_count_bits(n) - 1) & 0x7, bits == 0 corresponds to mp_count_bits(n) ≡ 1 (mod 8), not a multiple of 8. Suggest updating the comment to describe the actual condition (e.g., modulus bit length congruent to 1 mod 8, and the leading 0x00 handling) so future readers don’t infer the wrong key-size relationship.

Suggested change
/* When bits==0 the key size is an exact multiple of 8 and pkcsBlock was
* already advanced past the leading 0x00 byte (see above); no masking is
* needed. (1<<0)-1 == 0 would zero both bytes and corrupt the XOR
* separator check below. RsaPad_PSS guards the same step with
* "if (hiBits)" for the same reason. */
/* When bits==0, the modulus bit length is congruent to 1 mod 8, so the
* encoded block includes a leading 0x00 byte and pkcsBlock was already
* advanced past it (see above); no masking is needed. (1<<0)-1 == 0
* would zero both bytes and corrupt the XOR separator check below.
* RsaPad_PSS guards the same step with "if (hiBits)" for the same
* reason. */

Copilot uses AI. Check for mistakes.
if (bits) {
tmp[0] &= (byte)((1 << bits) - 1);
pkcsBlock[0] &= (byte)((1 << bits) - 1);
}
#ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER
if (saltLen == RSA_PSS_SALT_LEN_DISCOVER) {
for (i = 0; i < maskLen - 1; i++) {
Expand Down
Loading