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

Conversation

lukpueh
Copy link
Member

@lukpueh lukpueh commented Oct 13, 2022

Fixes #430, Supersedes #431


In #422 we changed create_rsa_signature to use the maximum salt length available, instead of the digest length, when creating rsassa-pss signatures, and adapted verify_rsa_signature to infer the salt length automatically.

This made it impossible for users of the old verify function, which could only handle digest sized salts, to verify signatures created by users of the new signing function (see #430).

Since the advantage of max salt lengths is mostly academic, this patch reverts create_rsa_signature to use digest sized salts.

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.

Also note that the patch does not revert the verify_rsa_signature part of #422. This allows verifying signatures created with the securesystemslib release that used max salt lengths, or created outside of securesystemslib.

In secure-systems-lab#422 we changed `create_rsa_signature` to use the maximum salt
length available, instead of the digest length, when creating
rsassa-pss signatures, and adapted `verify_rsa_signature` to infer
the salt length automatically.

This made it impossible for users of the old verify function, which
could only handle digest sized salts, to verify signatures created
by users of the new signing function (see secure-systems-lab#430).

Since the advantage of max salt lengths is mostly academic, this
patch reverts  `create_rsa_signature` to use digest sized salts (as
agreed in secure-systems-lab#431).

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.

Also note that the patch does not revert the `verify_rsa_signature`
part of secure-systems-lab#422. This allows verifying signatures created with the
securesystemslib release that used max salt lengths, or created
outside of securesystemslib.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
@lukpueh
Copy link
Member Author

lukpueh commented Oct 13, 2022

@trishankatdatadog, @yzhan289, does this work for you?

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 🤦

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. ;)

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)
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.

@@ -27,6 +27,7 @@

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.hashes import SHA256
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused constant? We really need a linter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch

Copy link
Member Author

Choose a reason for hiding this comment

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

And yes, we need a linter!! I've been hesitant to turn one on because of the flag day it will entail, and because I hope to soon remove larger chunks of internals (see #270). But I don't know how quickly I will get to the latter. So maybe the flag day is okay?

Copy link
Contributor

Choose a reason for hiding this comment

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

What's a "flag day"?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ultimately, I think a linter (and using a standard coding style) makes the code easier to work on so I am in favour of the flag day (event where the entire codebase changes to the new style, making a large diff with only mechanical changes which invalidate patches and make revision control "noise").

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
@lukpueh lukpueh merged commit 5d0f478 into secure-systems-lab:master Oct 14, 2022
@trishankatdatadog
Copy link
Contributor

Thanks very much, @lukpueh and friends! No rush, but in which release could we expect this, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintain backwards-compatibility by default in RSA PSS signatures
4 participants