-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Diffie-Hellman PrivKey generation fails in OpenSSL when providing q #12232
Comments
I'm able to reproduce with: from cryptography.hazmat.primitives.asymmetric import dh
params = dh.generate_parameters(generator=2, key_size=2048)
p = params.parameter_numbers().p
g = params.parameter_numbers().g
print(p)
print(g)
parameters = dh.DHParameterNumbers(p, g).parameters()
print(parameters.generate_private_key())
q = (p - 1) // 2 if (p - 1) % 2 == 0 else None
print(q)
parameters = dh.DHParameterNumbers(p, g, q).parameters()
print(parameters.generate_private_key()) |
Could the issue here be that Specifically, we check simple example:
|
Sorry, are you asking if there may be a bug in OP's code, or
suggesting that this is the bug in cryptography? (Notwithstanding that
I did basic triage, I haven't dug into this in depth)
…On Tue, Jan 28, 2025 at 7:09 PM Tanmay Ghai ***@***.***> wrote:
Could the issue here be that q = (p - 1) // 2 if (p - 1) % 2 == 0 else None assumes p is a safe prime?
Specifically, we check (p - 1) % 2, which checks p = 2q + 1 and thus ensures p - 1 is even, but we also need to check that q is prime to satisfy p being safe prime. i.e. q = (p - 1) // 2 should also be checked for primality
simple example:
p = 23, so p - 1 = 22 ✅
q = 22/2 = 11 (11 is prime) ✅
p = 21, so p - 1 = 20 ✅
q = 20 / 2 = 10 (but 10 is not prime) ❌
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
--
All that is necessary for evil to succeed is for good people to do nothing.
|
Not really suggesting a bug per-say, but I was just suggesting that perhaps the parameters |
Thanks for looking at this @tanmayghai18. |
Trying to use this function which optionally accepts a subgroup order value q: https://cryptography.io/en/latest/hazmat/primitives/asymmetric/dh/#cryptography.hazmat.primitives.asymmetric.dh.DHParameterNumbers
It points to https://github.com/openssl/openssl/blob/master/crypto/dh/dh_key.c#L378 which has many possible points of failure, none of which look like obvious issues with my code.
The text was updated successfully, but these errors were encountered: