-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow RSA signing with raw data (without a DigestInfo) #13740
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
base: main
Are you sure you want to change the base?
Conversation
|
I haven't reviewed in depth -- but I don't think we should use @reaperhulk wdyt? |
|
@alex yeah that is a fair concern 👍 in some early attempt I did add a I'll be happy to apply what you folks think is the best, if possible, please provide pointers where in the code-base a similar pattern is used so I can take inspiration from it. |
I would like to highlight that the API already uses None for this purpose (for the RSA signature recover functionality, ref. issue #5495). So whatever you decide, you may want to use the same method in all the API functions to make them symmetric. |
|
Hmm, the inconsistency is a bit unfortunate. I'd be inclined to do a |
|
@alex @reaperhulk I did add a sentinel Please have an other look and let me know what you think |
cca3371 to
31d0cfd
Compare
reaperhulk
left a comment
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.
We also need docs for this new value in RSAPrivateKey.sign
tests/hazmat/primitives/test_rsa.py
Outdated
| signature = private_key.sign( | ||
| binascii.unhexlify( | ||
| compute_rsa_hash_digest( | ||
| backend, hashes.SHA1(), example["message"] |
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'd prefer we used SHA256 for the tests here because otherwise we'll increasingly have coverage challenges as more and more things disable SHA1.
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 problem with that change is that the test vectors we use here (pkcs1v15sign-vectors.txt) do use SHA1.
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.
We don't need to use pkcs1v15sign-vectors.txt here, we can use SigVer15_186-3.rsp and filter to passing non-SHA1 tests.
31d0cfd to
5cc4ab6
Compare
|
Hey @reaperhulk, I finally had some time to have an other pass on this. I could apply your feedback, there is just one comment I could not fix (SHA1 vs SHA256) because SHA1 is used by the test vectors themself (#13740 (comment)). Please have a look at the last two commits and let me know if some more polish is needed. Many thanks! |
|
|
||
| .. class:: NoDigestInfo() | ||
|
|
||
| .. versionadded:: 47.0 |
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.
| .. versionadded:: 47.0 | |
| .. versionadded:: 47.0.0 |
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.
Fixed and squashed in 405302f
tests/hazmat/primitives/test_rsa.py
Outdated
| signature = private_key.sign( | ||
| binascii.unhexlify( | ||
| compute_rsa_hash_digest( | ||
| backend, hashes.SHA1(), example["message"] |
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.
We don't need to use pkcs1v15sign-vectors.txt here, we can use SigVer15_186-3.rsp and filter to passing non-SHA1 tests.
|
Oh, forgot one thing: we need a changelog entry in
|
|
@reaperhulk I gave a try at using I could add the code to parse Also I'm not sure how to differentiate private/public exponent... could you please give some guidance? For reference, here is the field used by the test: I looked in the rest of the codebase but I could not find any test that use |
In a straightforward RSA implementation it would have been sufficient with the modulus The cryptography library always requires the CRT parameters to be available. Since they are not present in It would of course be nice if the lib supported private RSA with only Perhaps the maintainers have more context or a better answer. |
5cc4ab6 to
2eaf3d9
Compare
|
@misterzed88 thanks for your kind explanation, I was able to write code to recompute those values, and that was an opportunity to understand something new, much appreciated. @reaperhulk sadly, even with the help from @misterzed88 I am not able to make it work, there is two things that I find particularly odd and concerning:
This prevents me from writing the required tests, do you have any ideas what I'm doing wrong here? I'm just confused after doing those other tests... at this point, I'm not sure where could be the issue. (I did fix the other points though) |
2eaf3d9 to
e47bf81
Compare
|
@reaperhulk thanks for your guidance and patience! I was able to address the tests related feedaback: e47bf81 I did also update the CHANGELOG as requested and add the proper version number in the documentation. Ready for an other pass when you have time, thank you :) |
CHANGELOG.rst
Outdated
| :class:`~cryptography.hazmat.primitives.ciphers.aead.ChaCha20Poly1305` to | ||
| allow encrypting directly into a pre-allocated buffer. | ||
| * Added support for PKCS1v15 signing without DigestInfo using | ||
| :class:~cryptography.path.to.NoDigestInfo. |
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.
This was just intended as an example. You need to define the actual path to NoDigestInfo here. I believe it's in cryptography.hazmat.primitives.asymmetric.utils
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.
Oops, I was so focused an having the other test working that I did not even read carefully!
Anyway, this is fixed in -> 5ee18d4
This time I did copy/pasted a line for Prehashed so it must be right.
tests/hazmat/primitives/test_rsa.py
Outdated
| ).private_key(backend, unsafe_skip_rsa_key_validation=True) | ||
| signature = private_key.sign( | ||
| binascii.unhexlify( | ||
| compute_rsa_hash_digest( |
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.
This is the only call site for this so I don't think we need this to be a function.
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.
It is not the only call site, it is used for other tests thru: https://github.com/pyca/cryptography/pull/13740/files#diff-adca63e2e988b9f614480a194688af9467b6b54710a658d4aa958e48b2bc18c2R526
tests/hazmat/primitives/utils.py
Outdated
|
|
||
|
|
||
| def compute_rsa_hash_digest(backend, hash_alg, msg): | ||
| oid = _hash_alg_oids[hash_alg.name] |
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.
This isn't necessary if we're only doing SHA256.
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.
Fixed in 1f35994
|
Almost there! |
Instead of relying on outdated vectors that uses SHA1
e47bf81 to
1f35994
Compare
|
@reaperhulk done applying your last feedback! The only thing that I did not do is inlining Please have an other look :-) |
|
Tests are failing only on the FIPS builders, almost certainly due to key size restrictions. You can either skip this test on FIPS or try adding a check for whether fips is enabled and skip if the modulus is < 2048-bit and see if that fixes it. You can see examples of both of these ideas in test_rsa.py |
This fixes #3713 and #10226.
Instead of using the script written by @misterzed88 in #10226 to generate modified tests vectors, I did directly implement the same logic in the test infrastructure, so we can reuse directly the NIST (or others...) vectors stored in the tests.