Derive EC public key from private key if missing by simo5 · Pull Request #29054 · openssl/openssl · GitHub
Skip to content

Derive EC public key from private key if missing - #29054

Closed
simo5 wants to merge 1 commit into
openssl:masterfrom
simo5:ec_priv_pub
Closed

simo5 wants to merge 1 commit into
openssl:masterfrom
simo5:ec_priv_pub

Conversation

@simo5

@simo5 simo5 commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

Most of the key management implementations seem to generate public key material from private keys when the public key is not provided on import, however this is not available for classic EC keys.

This PR: latchset/kryoptic#357 tests (and depends on) this functionality for Edwards and Montgomery Curves as well as Mldsa, Mlkem and Slhdsa.

Checklist
  • documentation is added or updated
  • tests are added or updated

@simo5

simo5 commented Nov 4, 2025

Copy link
Copy Markdown
Contributor Author

@slontis

slontis commented Nov 4, 2025

Copy link
Copy Markdown
Member

You can see that some tests are failing because it was expected to fail.
I cant recall the history, but I am hoping an ex OTC member such as @romen or @levitte might recall.
(Maybe auto loading the public key when only the private key was needed was undesirable?)

@beldmit

beldmit commented Nov 4, 2025

Copy link
Copy Markdown
Member

If I remember correctly, it was because of perfomance reasons, probably DNSSec related

@romen

romen commented Nov 4, 2025 •

Copy link
Copy Markdown
Member

I definitely advocated not to derive the public key automatically on-demand from the private key during operations that required the public part, as that could be weaponized to trigger a scalar multiplication remotely to exploit the non-const-time bignum (and sometime EC, depending on the backend implementation).

This PR derives it in on _fromdata(), which normally should happen only on load, so mine shouldn't be a concern in this case.
But I'd like an expert confirmation that _fromdata() is not itself autotriggered during the execution of operations, in some weird scenario!

@simo5

simo5 commented Nov 4, 2025

Copy link
Copy Markdown
Contributor Author

So should I simply remove offending tests? Or try to make sure they do not use _fromdata() and preserve some of them? something else?

@davidben

davidben commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

I definitely advocated not to derive the public key automatically on-demand from the private key during operations that required the public part, as that could be weaponized to trigger a scalar multiplication remotely to exploit the non-cont-time bignum (and sometime EC, depending on the backend implementation).

Scalar multiplication is also the operation one performs for EC operations in the first place. If that's leaky, what hope do you have to of using the key under this threat model in the first place?

(I have not checked whether it's always constant-time or not in OpenSSL, but I don't see how deriving the public key has any impact here.)

beldmit
beldmit previously approved these changes Dec 9, 2025

@beldmit beldmit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@beldmit
beldmit dismissed their stale review December 9, 2025 21:45

Sorry, missed the test failures

@beldmit

beldmit commented Dec 9, 2025

Copy link
Copy Markdown
Member

@simo5 I think yes, try to make sure that they don't use _fromdata and preserve as much as possible

Update ossl_ec_key_fromdata to compute the public key if it is not provided in
the input parameters but the private key is. This allows for the creation of a
complete EC_KEY object from only private key data.

Signed-off-by: Simo Sorce <simo@redhat.com>

@beldmit beldmit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@beldmit
beldmit requested review from levitte, slontis and t8m December 11, 2025 11:34
@simo5

simo5 commented Dec 12, 2025

Copy link
Copy Markdown
Contributor Author

@openssl/committers need a second pair of eyes here

@t8m t8m added triaged: feature The issue/pr requests/adds a feature tests: present The PR has suitable tests present labels Dec 12, 2025
Comment thread crypto/ec/ec_backend.c
@nhorman nhorman added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Dec 12, 2025
@vdukhovni

Copy link
Copy Markdown

If I remember correctly, it was because of perfomance reasons, probably DNSSec related

Why would specifically DNSSEC deployments be averse to deriving the public key from the private key? In BIND, both parts of the key are pregenerated, and stored in custom internal formats (32 byte raw exponent and 64-byte uncompressed public point):

$ dnssec-keygen -K . -a ECDSAP256SHA256 acme.example
Generating key pair.
Kacme.example.+013+54234

$ cat Kacme.example.+013+54234.private 
Private-key-format: v1.3
Algorithm: 13 (ECDSAP256SHA256)
PrivateKey: aNFCp7VhAKUgsfRmKy7RGZb77wtuwID0281Lq0k7KZ0=
Created: 20251213011140
Publish: 20251213011140
Activate: 20251213011140

$ awk '/PrivateKey:/{print $2}' Kacme.example.+013+54234.private  |
    openssl base64 -d |
    xxd -c32 -p
68d142a7b56100a520b1f4662b2ed11996fbef0b6ec080f4dbcd4bab493b299d

$ cat Kacme.example.+013+54234.key 
; This is a zone-signing key, keyid 54234, for acme.example.
; Created: 20251213011140 (Sat Dec 13 12:11:40 2025)
; Publish: 20251213011140 (Sat Dec 13 12:11:40 2025)
; Activate: 20251213011140 (Sat Dec 13 12:11:40 2025)
acme.example. IN DNSKEY 256 3 13 D5c3Lj5KrTBAFHhsfY2WuZl5TZVMbyH9131eThs2cw4BWKMs1fgJeZFA UD4jmmrZDFxpz1SYUbXRmjLStO3tfA==

$ awk '/DNSKEY/{print $7 $8}' Kacme.example.+013+54234.key  |
    openssl base64 -A -d |
    xxd -p -c32
0f97372e3e4aad304014786c7d8d96b999794d954c6f21fdd77d5e4e1b36730e
0158a32cd5f809799140503e239a6ad90c5c69cf549851b5d19a32d2b4eded7c

The signer can choose to import both parts of the key or just the private key, though the keyid (54234) is then based only on the file name, rather than computed from the public key checksum.

The scenario that comes to mind is some system that hosts a very large number of zones, preloading all their private keys into memory on startup, loading only the private components.
The public component is available, and where computing the public points would be a significant startup cost. Any such systems can import both parts of the key (both are known), but perhaps they don't expect to have to do that???

I am not convinced that's a compelling use-case.

@openssl-machine

Copy link
Copy Markdown
Collaborator

24 hours has passed since 'approval: done' was set, but as this PR has been updated in that time the label 'approval: ready to merge' is not being automatically set. Please review the updates and set the label manually.

@beldmit beldmit added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Dec 13, 2025
@t8m

t8m commented Dec 15, 2025

Copy link
Copy Markdown
Member

@t8m t8m closed this Dec 15, 2025
openssl-machine pushed a commit that referenced this pull request Dec 15, 2025
Update ossl_ec_key_fromdata to compute the public key if it is not provided in
the input parameters but the private key is. This allows for the creation of a
complete EC_KEY object from only private key data.

Signed-off-by: Simo Sorce <simo@redhat.com>

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from #29054)
Sashan pushed a commit to Sashan/openssl that referenced this pull request Dec 16, 2025
Update ossl_ec_key_fromdata to compute the public key if it is not provided in
the input parameters but the private key is. This allows for the creation of a
complete EC_KEY object from only private key data.

Signed-off-by: Simo Sorce <simo@redhat.com>

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from openssl#29054)
ColtonWilley added a commit to ColtonWilley/wolfProvider that referenced this pull request Jan 28, 2026
The EC public key auto-derivation feature (deriving public key from
private key during fromdata()) was merged to OpenSSL master on
Dec 15, 2025 via PR #29054. This feature will be in OpenSSL 4.0.0,
not 3.6.x as previously assumed.

The original version check `> 0x30600000L` incorrectly triggered for
OpenSSL 3.6.x which does not have this feature, causing test_ec_import
to fail because wolfProvider was auto-deriving public keys while
OpenSSL 3.6.x was not.

Changes:
- Update version check from `> 0x30600000L` to `>= 0x40000000L`
- Update comments to reference the actual OpenSSL PR
- Clean up test code structure

See: openssl/openssl#29054

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
padelsbach pushed a commit to wolfSSL/wolfProvider that referenced this pull request Jan 28, 2026
The EC public key auto-derivation feature (deriving public key from
private key during fromdata()) was merged to OpenSSL master on
Dec 15, 2025 via PR #29054. This feature will be in OpenSSL 4.0.0,
not 3.6.x as previously assumed.

The original version check `> 0x30600000L` incorrectly triggered for
OpenSSL 3.6.x which does not have this feature, causing test_ec_import
to fail because wolfProvider was auto-deriving public keys while
OpenSSL 3.6.x was not.

Changes:
- Update version check from `> 0x30600000L` to `>= 0x40000000L`
- Update comments to reference the actual OpenSSL PR
- Clean up test code structure

See: openssl/openssl#29054

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch severity: fips change The pull request changes FIPS provider sources tests: present The PR has suitable tests present triaged: feature The issue/pr requests/adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants