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

Support signing a pre-calculated hash #87

Merged
merged 6 commits into from
May 7, 2017

Conversation

jls5177
Copy link
Contributor

@jls5177 jls5177 commented Mar 27, 2017

This code change adds a new function that takes a pre-computed hash of a message that should be signed (as apposed to a message). This change allows one process to generate the hash of message and another process (over RPC in my case) to perform the signing.

This code change adds support to split the hashing of a message
and the actual signing of the message.
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.09%) to 91.509% when pulling 58c7c2c on jls5177:split-signing into 2d81e0f on sybrenstuvel:master.

Copy link
Owner

@sybrenstuvel sybrenstuvel left a comment

Choose a reason for hiding this comment

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

Thanks for your pull request, it looks like useful functionality. Please also write a unit test that covers the proposed flow, and update the documentation for this flow too. Without that, I can't accept this pull request.

rsa/pkcs1.py Outdated
"""

# Verify hash_method is a valid hash algorithm
if hash_method not in HASH_ASN1:
Copy link
Owner

Choose a reason for hiding this comment

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

Why perform this check here? It's the same as in sign_hash() anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an oversight in my original commit. This is removed in the new commit.

This commit updates the unit test and usage docs. In addition,
This change removes a redundant error check inside rsa.sign().
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 91.608% when pulling 0247d9d on jls5177:split-signing into 2d81e0f on sybrenstuvel:master.

@jls5177
Copy link
Contributor Author

jls5177 commented Apr 10, 2017

I added 2 new unit tests to cover the new hashing flow and updated the usage docs with the new flow. I am new to this type of documentation. Let me know if I missed anything.

Copy link
Owner

@sybrenstuvel sybrenstuvel left a comment

Choose a reason for hiding this comment

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

Just a few minor remarks.

rsa/pkcs1.py Outdated
"""

# Calculate the hash and perform the signing
return sign_hash(hash(message, hash_method), priv_key, hash_method)
Copy link
Owner

Choose a reason for hiding this comment

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

Please splitt his up into two lines, using a helper variable. The way it is now it does too much on one line; a traceback referring to an error in this line will be too ambiguous.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, this will be fixed in the next commit.

signature1 = pkcs1.sign_hash(msg_hash, self.priv, 'SHA-256')
print("\tSignature1: %r" % signature1)

# Calculate the signature using the original method
Copy link
Owner

Choose a reason for hiding this comment

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

The comment "the original method" implies that the reader knows that your method is something that was added later. This is only true within the context of this pull request, but not later once everything has been merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I switched it to now say unified method.

"""Hashing and then signing should match with directly signing the message. """

message = b'je moeder'
print("\tMessage: %r" % message)
Copy link
Owner

Choose a reason for hiding this comment

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

Please don't print things in a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was using the other test cases in this file as a template and many of them had prints. The prints are now removed.

Removed the print statements from the unit test and refactored a
few code comments to improve readability.
@sybrenstuvel
Copy link
Owner

Another thing: hash() is not a good name for a function, as it's also a built-in Python function.

The new hash function had the same name as a function in the
standard library. This commit changes the name to avoid conflicts.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 91.715% when pulling cb6b7b3 on jls5177:split-signing into 000e84a on sybrenstuvel:master.

@sybrenstuvel
Copy link
Owner

Hmm not too fond of "generate", maybe "compute_hash"?

This commit renames the hash function to compute_hash().
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 91.715% when pulling 8f48cd9 on jls5177:split-signing into 000e84a on sybrenstuvel:master.

@sybrenstuvel sybrenstuvel merged commit 425eb24 into sybrenstuvel:master May 7, 2017
@sybrenstuvel
Copy link
Owner

Thanks, it's in!

@ilmesi ilmesi mentioned this pull request May 17, 2018
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.

None yet

3 participants