-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/pyca/cryptography/blob/25d794e079b44e6fac23791ca51da3a3fb9cb526/src/cryptography/hazmat/backends/openssl/rsa.py#L53-L54
There was a problem hiding this comment.
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 internalcryptography
constant that does look like it's used to end up usingdigest_obj.algorithm.digest_size
in the end. This flag is also much easier to understand.There was a problem hiding this comment.
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 🤦