Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert to digest sized salts in rsassa-pss signing #436

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions securesystemslib/rsa_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,10 @@ def create_rsa_signature(private_key, data, scheme='rsassa-pss-sha256'):
if scheme.startswith('rsassa-pss'):
# Generate an RSSA-PSS signature. Raise
# 'securesystemslib.exceptions.CryptoError' for any of the expected
# exceptions raised by pyca/cryptography. 'salt_length' is set to
# the maximum length available.
# exceptions raised by pyca/cryptography.
signature = private_key_object.sign(
data, padding.PSS(mgf=padding.MGF1(digest_obj.algorithm),
salt_length=padding.PSS.MAX_LENGTH), digest_obj.algorithm)
salt_length=padding.PSS.DIGEST_LENGTH), digest_obj.algorithm)

Choose a reason for hiding this comment

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

The original code had digest_obj.algorithm.digest_size instead, is there a difference here?

Copy link
Member Author

Choose a reason for hiding this comment

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

See PR description / commit mesesage:

Note that we now use the padding.PSS.DIGEST_LENGTH constant instead of passing the actual digest length, as we did before. Using the constant has the same result, but is recommended by the library documentation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I locally created two signature one with the constant and one with the digest length and can confirm that they do the same thing.

I also checked the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@trishankatdatadog trishankatdatadog Oct 13, 2022

Choose a reason for hiding this comment

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

Right: I think padding.PSS.DIGEST_LENGTH is simply an internal cryptography constant that does look like it's used to end up using digest_obj.algorithm.digest_size in the end. This flag is also much easier to understand.

Choose a reason for hiding this comment

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

Thanks! Oof my bad I didn't read the description first 🤦


elif scheme.startswith('rsa-pkcs1v15'):
# Generate an RSA-PKCS1v15 signature. Raise
Expand Down
26 changes: 15 additions & 11 deletions tests/test_rsa_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,30 @@ def test_verify_rsa_signature(self):
scheme, public_rsa, data))


def test_verify_rsa_pss_sha256(self):
def test_verify_rsa_pss_different_salt_lengths(self):
rsa_scheme = 'rsassa-pss-sha256'
data = 'The ancients say the longer the salt, the more provable the security'.encode('utf-8')
data = 'The ancients say, salt length does not matter that much'.encode('utf-8')
Copy link
Contributor

Choose a reason for hiding this comment

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

Mathematically speaking, apparently this is not strictly true, although practically it might be the case 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I would also not trust every word the ancients say. ;)


private_key = load_pem_private_key(private_rsa.encode('utf-8'),
password=None, backend=default_backend())
digest = securesystemslib.hash.digest_from_rsa_scheme(rsa_scheme, 'pyca_crypto')

# Old-style signature: use the hash length as the salt length.
old_signature = private_key.sign(data,
padding.PSS(mgf=padding.MGF1(digest.algorithm), salt_length=padding.PSS.DIGEST_LENGTH),
digest.algorithm)
# Make sure digest size and max salt length are not accidentally the same
self.assertNotEqual(digest.algorithm.digest_size,
padding.calculate_max_pss_salt_length(private_key, digest.algorithm))

# New-style signature: use the automatic salt length.
new_signature, _ = securesystemslib.rsa_keys.create_rsa_signature(private_rsa, data)
# Sign with max salt length (briefly available in sslib v0.24.0):
max_salt_sig = private_key.sign(data,
padding.PSS(mgf=padding.MGF1(digest.algorithm), salt_length=padding.PSS.MAX_LENGTH),
digest.algorithm)

# Verify both old-style and new-style signatures.
for signature in (old_signature, new_signature):
# Sign with salt length == digest length
fix_salt_sig, _ = securesystemslib.rsa_keys.create_rsa_signature(private_rsa, data)

# Verification infers salt length automatically and so works for both
for signature in (max_salt_sig, fix_salt_sig):
verified = securesystemslib.rsa_keys.verify_rsa_signature(signature, rsa_scheme,
public_rsa, data)
public_rsa, data)
self.assertTrue(verified)


Expand Down