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

Fails with Invalid private key #3

Open
waptaff opened this issue Dec 3, 2021 · 6 comments · May be fixed by #4
Open

Fails with Invalid private key #3

waptaff opened this issue Dec 3, 2021 · 6 comments · May be fixed by #4

Comments

@waptaff
Copy link

waptaff commented Dec 3, 2021

No matter what I use as vanity, I get this:

$ python ./vanity_rsa.py k
Traceback (most recent call last):
  File "./vanity_rsa.py", line 291, in <module>
    main()
  File "./vanity_rsa.py", line 254, in main
    key = make_key(args.vanity, key_length=args.key_length,·
  File "./vanity_rsa.py", line 233, in make_key
    return make_valid_rsa_key(priv_key, pub_key)
  File "./vanity_rsa.py", line 208, in make_valid_rsa_key
    return rsa.RSAPrivateNumbers(
  File "/usr/lib/python3.9/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.py", line 367, in private_key
    return backend.load_rsa_private_numbers(self)
  File "/usr/lib/python3.9/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 602, in load_rsa_private_numbers
    return _RSAPrivateKey(self, rsa_cdata, evp_pkey)
  File "/usr/lib/python3.9/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 389, in __init__
    raise ValueError("Invalid private key", errors)
ValueError: ('Invalid private key', [_OpenSSLErrorWithText(code=67764348, lib=4, func=160, reason=124, reason_text=b'error:040A007C:rsa routines:RSA_check_key_ex:dmp1 not congruent to d'), _OpenSSLErrorWithText(code=67764349, lib=4, func=160, reason=125, reason_text=b'error:040A007D:rsa routines:RSA_check_key_ex:dmq1 not congruent to d')])
  • Python 3.9
  • cryptography==3.4.7
  • OpenSSL 1.1.1l
@thenger
Copy link

thenger commented Jan 17, 2022

Same results with:

# python --version
Python 3.9.2
# openssl version
OpenSSL 1.1.1k  25 Mar 2021
# pip freeze|grep -E 'cryptography|gmpy2'
cryptography==36.0.1
gmpy2==2.1.2

@colindean
Copy link

Essentially the same error with

$ python --version
Python 3.9.11
$ openssl version
OpenSSL 1.1.1s  1 Nov 2022
$ pip freeze|grep -E 'cryptography|gmpy2'
cryptography==39.0.1
gmpy2==2.1.5

@Lomanic
Copy link

Lomanic commented Feb 28, 2023

The script works only with cryptography up to v3.0, tested with python 3.9.15

pip install cryptography==3.1 gmpy2 && python3 vanity_rsa.py ++vanity++ # fails
pip install cryptography==3.0 gmpy2 && python3 vanity_rsa.py ++vanity++ # works

Probably because of this change pyca/cryptography@fce9a79

@colindean
Copy link

Just in case anyone else thinks that compiling is an option, cryptography 3.0 won't compile on ARM64 macOS 13, even if you give it openssl includes:

PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig" CFLAGS=$(pkg-config --cflags openssl) pip install cryptography==3.0

There are many compile errors with clang 15 with the arm64-apple-darwin22.3.0 target.

Best option is to spend your time making this script work against the latest version of cryptography.

@colindean
Copy link

I pored over the docs trying to figure out how to make this work and found that cryptography 39.0.0 added a new parameter to skip the RSA validation added in 3.1, unsafe_skip_rsa_key_validation.

Adding this parameter to the RSAPrivateNumbers.private_key() call made it work for me!

@colindean
Copy link

I put up a PR in #4 that works for me and adds some logging of what the script is doing.

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 a pull request may close this issue.

4 participants