Skip to content

crypto:generate_key/3 on explicit curves is broken #9723

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

Open
RoadRunnr opened this issue Apr 14, 2025 · 0 comments · May be fixed by #9729
Open

crypto:generate_key/3 on explicit curves is broken #9723

RoadRunnr opened this issue Apr 14, 2025 · 0 comments · May be fixed by #9729
Assignees
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM

Comments

@RoadRunnr
Copy link
Contributor

Describe the bug

The spec is -spec generate_key(Type, Params, PrivKeyIn) -> {PublicKey, PrivKeyOut} where Params is allow to be a ec_explicit_curve(). That type contains all parameters to define a curve. However, attempting to generate a key on a valid, explicit curve fails.

To Reproduce

Define a curve that uses the curve parameters of secp256k1, but uses a different generator,

Curve = {{prime_field,<<"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿÿü/">>},
 {<<0>>,<<7>>,none},
 <<4,198,4,127,148,65,237,125,109,48,69,64,110,149,192,124,
   216,92,119,142,75,140,239,60,167,171,172,9,185,92,112,
   158,229,26,225,104,254,166,61,195,57,163,197,132,25,70,
   108,234,238,247,246,50,101,50,102,208,225,35,100,49,169,
   80,207,229,42>>,
 <<255,255,255,255,255,255,255,255,255,255,255,255,255,255,
   255,254,186,174,220,230,175,72,160,59,191,210,94,140,
   208,54,65,65>>,
 <<1>>}.
crypto:generate_key(ecdh, Curve, 2).
** exception error: {error,{"ec.c",465},"Couldn't get EC group name size"}
     in function  crypto:generate_key/2
        called as crypto:generate_key({{{prime_field,<<"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿÿü/">>},
                                        {<<0>>,<<7>>,none},
                                        <<4,198,4,127,148,65,237,125,109,48,69,64,110,149,192,124,
                                          216,92,119,142,75,140,239,60,...>>,
                                        <<"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþº®Üæ¯H ;¿Ò^"...>>,
                                        <<1>>},
                                       undefined},
                                      <<2>>)

The problem is that code attempts to get a name for curve and then use that named curve to initialize the curve parameters. That obviously won't work for curves with non-standard parameters.
Since all curve parameters are given, the code should instead initialize the curve context from the given values.

Expected behavior

Generating points on explicit curves should work.

Affected versions

  • OTP-28.0-rc2
  • OTP-27.3

Additional context

I am relatively certain that it did work when I submitted the original version of the EC key support in 2014. A quick inspection suggests that the OpenSSL 3.0 API changes broken it.

@RoadRunnr RoadRunnr added the bug Issue is reported as a bug label Apr 14, 2025
RoadRunnr added a commit to RoadRunnr/otp that referenced this issue Apr 15, 2025
Going though the curve NID or name only works for standardized
curves. However, explicit curves can use parameters that that deviate
from the named curves. The public key generation would in that case
fail to extract the group parameters from the key.

Instead extract all parameters explicitly and build the group from
that.

fixes erlang#9723
@IngelaAndin IngelaAndin added the team:VM Assigned to OTP team VM label Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants