Skip to content
This repository has been archived by the owner on Mar 1, 2018. It is now read-only.

bug: Allow sign and verify to use different hashes than sha256 #53

Closed
wants to merge 1 commit into from

Conversation

jrconlin
Copy link

closes #52

jrconlin added a commit to mozilla-services/autopush that referenced this pull request Dec 23, 2016
This patch is dependent on two other library patches landing.
* mpdavis/python-jose#39
* yann2192/pyelliptic#53

Temporarily pointing to personal forks with patches applied.

closes #753
jrconlin added a commit to mozilla-services/autopush that referenced this pull request Dec 23, 2016
This patch is dependent on two other library patches landing.
* mpdavis/python-jose#39
* yann2192/pyelliptic#53

Temporarily pointing to personal forks with patches applied.

closes #753
jrconlin added a commit to mozilla-services/autopush that referenced this pull request Dec 23, 2016
This patch is dependent on two other library patches landing.
* mpdavis/python-jose#39
* yann2192/pyelliptic#53

Temporarily pointing to personal forks with patches applied.

closes #753
Copy link

@jcjones jcjones left a comment

Choose a reason for hiding this comment

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

The code change looks OK to me; I've got some nits you should consider.

That said, in a crypto interface library, I'd strongly recommend checking return values out of OpenSSL on the important calls. For that reason, I'm not going to hit 'Approve', but as it's not my project, I'll hold off on specifically requesting changes. :)

@@ -100,6 +100,10 @@ def __init__(self, library):
self.BN_bin2bn.argtypes = [ctypes.c_void_p, ctypes.c_int,
ctypes.c_void_p]

self.BN_bn2dec = self._lib.BN_bn2dec
Copy link

Choose a reason for hiding this comment

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

Nit: I'm not clear on why this function is being exposed in this PR. It seems fine, but maybe add a comment to the commit?

"sha512": OpenSSL.EVP_sha512(),
}
self.hashval = hasher
self.hasher = _hashers[hasher]
Copy link

Choose a reason for hiding this comment

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

Nit: This is going to raise without an explanation if they pick an invalid hash; not sure how developer-friendly you're aiming to be.

@@ -414,7 +424,7 @@ def sign(self, inputb):
raise Exception("[OpenSSL] EC_KEY_check_key FAIL ... " + OpenSSL.get_error())

OpenSSL.EVP_MD_CTX_init(md_ctx)
OpenSSL.EVP_DigestInit_ex(md_ctx, OpenSSL.EVP_sha256(), None)
OpenSSL.EVP_DigestInit_ex(md_ctx, self.hasher, None)
Copy link

Choose a reason for hiding this comment

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

EVP_DigestInit_ex can fail (it returns 0)... that could happen if OpenSSL was built with SHA512 explicitly disabled. (That used to happen on ARMv5; probably not a huge concern...)

Still, good security coding practices say you should check the return whereever you can; that'd include the EVP_MD_CTX_init, too.

if (OpenSSL.EVP_DigestUpdate(md_ctx, binputb, len(inputb))) == 0:
raise Exception("[OpenSSL] EVP_DigestUpdate FAIL ... " + OpenSSL.get_error())

OpenSSL.EVP_DigestFinal_ex(md_ctx, digest, dgst_len)
Copy link

Choose a reason for hiding this comment

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

If EVP_DigestFinal_ex returns 0, you shouldn't use the output digest.

OpenSSL.EVP_MD_CTX_init(md_ctx)
OpenSSL.EVP_DigestInit_ex(md_ctx, OpenSSL.EVP_sha256(), None)
OpenSSL.EVP_DigestInit_ex(md_ctx, self.hasher, None)
Copy link

Choose a reason for hiding this comment

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

Same as above about checking EVP_DigestInit and EVP_MD_CTX_init

d = OpenSSL.malloc(m, len(m))
md = OpenSSL.malloc(0, 64)
i = OpenSSL.pointer(OpenSSL.c_int(0))
OpenSSL.HMAC(OpenSSL.EVP_sha384(), key, len(k), d, len(m), md, i)
Copy link

Choose a reason for hiding this comment

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

Nit: The retval should be checked... as should the other calls to HMAC.

@jrconlin
Copy link
Author

jrconlin commented Jan 3, 2017

@jcjones, thanks for the r? I'm always happy to get whatever nits or oversights folks are willing to offer, so thank you for your time. Will address and push a new version shortly.

Copy link

@jcjones jcjones left a comment

Choose a reason for hiding this comment

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

I know it's not as clean looking, but it's important to be safe with crypto code. Vulnerabilities have been built upon code not checking the return value. 👍

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

Successfully merging this pull request may close these issues.

EC sign and verify automatically hash to SHA 256
3 participants