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

Replace PyCrypto with cryptography. #46

Closed
wants to merge 8 commits into from
Closed

Replace PyCrypto with cryptography. #46

wants to merge 8 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Mar 25, 2017

I know the cryptodome route is easier, but cryptography is the way to go for the long run because it is supported by the python software foundation. Feel free to give feedback and suggestions!

@mpdavis
Copy link
Owner

mpdavis commented Mar 25, 2017

Unfortunately, there are many people that rely on the PyCrypto support in environments where cryptography is not supported, such as Google App Engine.

That said, with the recent work around custom algorithms would make it pretty easy to add a implementation based on cryptography that ships with python-jose that could easily be registered and used. I would even be open to conversations about setting it as the default, but PyCrypto would still need to be supported.

@ghost
Copy link
Author

ghost commented Mar 25, 2017

Unfortunately, there are many people that rely on the PyCrypto support in environments where cryptography is not supported, such as Google App Engine.

This is really Google's narrative; that there is "investment" and "community support" for these old libraries. I understand that from your perspective, PyCrypto support is necessary, but the only reason why is Google, frankly. And I'm not suggesting that this is an unreasonable perspective to have, considering that GAE standard is considerably cheaper than GAE flexible.

What I think will be satisfactory is provide a backend that minimally emulates cryptography using pycrypto. I'm currently down to 3 test failures on pypy only (which tests the pycrypto backend), so that should be completed today.

@codecov-io
Copy link

codecov-io commented Mar 25, 2017

Codecov Report

Merging #46 into master will increase coverage by 0.12%.
The diff coverage is 96.04%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #46      +/-   ##
=========================================
+ Coverage   95.57%   95.7%   +0.12%     
=========================================
  Files           7      10       +3     
  Lines         542     628      +86     
=========================================
+ Hits          518     601      +83     
- Misses         24      27       +3
Impacted Files Coverage Δ
jose/key.py 100% <100%> (ø)
jose/jwk.py 97.52% <100%> (+1.11%) ⬆️
jose/pycrypto.py 92.75% <92.75%> (ø)
jose/cryptphy.py 96.96% <96.96%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1a23c39...80b5219. Read the comment docs.

@ghost
Copy link
Author

ghost commented Mar 25, 2017

@mpdavis

Should be done now.

With patch applied, requirement is cryptography except on pypy, and runtime behavior is that fallback to pycrypto will occur if cryptography cannot be imported.

@ghost ghost changed the title WIP: Replace PyCrypto with cryptography. Replace PyCrypto with cryptography. Mar 25, 2017
jose/jwk.py Outdated
sig,
msg,
padding.PKCS1v15(),
self.hash_alg(),
Copy link
Author

Choose a reason for hiding this comment

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

One thing to note is that I'm not completely sure what happens when the hash_alg is not one of the SHA* hashes. Maybe some feedback on that?

@mpdavis
Copy link
Owner

mpdavis commented Mar 25, 2017

I'm not of fan of trying to fake the cryptography api with PyCrypto. There is no requirement that each hashing algorithm needs to be implemented in a single Key class. I would make a new class that implements the Key interface and is implemented with the cryptography library.

That key class could then be registered over top of the PyCrypto class (or set as the default, allowing the PyCrypto class to overwrite the cryptography version).

Doing so would keep all of that code separate and much easier to understand.

@ghost
Copy link
Author

ghost commented Mar 25, 2017

I'm not of fan of trying to fake the cryptography api with PyCrypto. There is no requirement that each hashing algorithm needs to be implemented in a single Key class. I would make a new class that implements the Key interface and is implemented with the cryptography library.

Much of the code is shared between classes, which means that we would have code duplication with that option.

Doing so would keep all of that code separate and much easier to understand.

Not necessarily. Cryptography is the de facto cryptographic library in Python which means its functions are extensively documented. PyCrypto is significantly less documented which means that its functions are more unclear. I can add docstrings to the compat code to ease maintainability.

@mpdavis
Copy link
Owner

mpdavis commented Mar 25, 2017

We have a system in place for swapping out key implementations. I don't see how ignoring that system and doing it in a one-off manner is going to be more maintainable, despite some code duplication. Code that does what the maintainers expect it to is important.

@ghost
Copy link
Author

ghost commented Mar 26, 2017

Okay, I'll fold.

@ghost
Copy link
Author

ghost commented Mar 26, 2017

Alright, should be done now. I had some trouble with tox and some ancient testing requirements, so I took it out.

@@ -0,0 +1,112 @@
import six

from cryptography.hazmat.backends import default_backend
Copy link
Contributor

Choose a reason for hiding this comment

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

Are all the hazmat imports necessary because cryptography doesn't (yet?) expose these algorithms in their 'public' layer?

@bjmc
Copy link
Contributor

bjmc commented Apr 7, 2017

👍 I strongly endorse getting away from insecure, unmaintained pycrypto, and cryptography does seem to have the most momentum behind it.

@bjmc
Copy link
Contributor

bjmc commented Apr 7, 2017

Is it worth putting pycrypto and cryptphy into a package like jose._crypto_backends.cryptphy, instead of having them both loose at the top level? Or would that further complicate the circular import problems?

@ghost
Copy link
Author

ghost commented Apr 7, 2017

@bjmc To be perfectly honest, I am no longer going to maintain this PR because I have convinced other projects to remove python-jose as a dependency. Feel free to fork this if you wish, though.

@mpdavis
Copy link
Owner

mpdavis commented May 3, 2017

Fixed in #49

@mpdavis mpdavis closed this May 3, 2017
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.

4 participants