Skip to content

fix: allow ECDH KeyAgreement init() reuse - #252

Open
MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:fix/ecdh-keyagreement-reinit
Open

fix: allow ECDH KeyAgreement init() reuse#252
MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:fix/ecdh-keyagreement-reinit

Conversation

@MarkAtwood

Copy link
Copy Markdown

KeyAgreement.init() could not be called twice on the same ECDH instance.

wolfCryptInit() allocates ecPrivate/ecPublic once per KeyAgreement instance, and Ecc.importPrivateOnCurve is guarded by throwIfKeyExists(), so a second engineInit() threw:

java.lang.IllegalStateException: Object already has a key

The javax.crypto.KeyAgreement javadoc requires init() to reset the object to its post-construction state, so it must be callable at any time. wcInitECDHParams now releases and recreates both native Ecc structs before importing the private key.

The DH path needs no equivalent change: Dh.setPrivateKey overwrites rather than rejecting.

Verification

ant test: 1630 run, 0 failures, 0 errors, 293 skipped.

Pristine master returns byte-identical totals, so the existing suite does not cover this defect and passing it is not evidence of a fix. The behavior change was measured directly instead, running the same probe class against a master-built jar and this branch's jar:

master 7d8188f this branch
init() called twice on one instance IllegalStateException: Object already has a key both rounds succeed, identical 32-byte secret

Fixes #221.

One behavioral note

If importPrivateOnCurve throws after the new release-and-recreate step, the instance is left holding fresh but unimported Ecc structs rather than the previously working ones. That is the only consequence beyond the fix itself.

Split out of #239, which retains the AES-GCM streaming feature and the RSA/OAEP work. The invalid-key exception mapping from that PR is in a separate PR against issue #220.

Copilot AI lite review requested due to automatic review settings August 10, 2026 21:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a JCE contract violation in WolfCryptKeyAgreement by allowing KeyAgreement.init() to be called repeatedly on the same ECDH KeyAgreement instance, addressing issue #221 (Wycheproof-reported object reuse failure after generateSecret()).

Changes:

  • Reset ECDH native key state during engineInit() by releasing and recreating ecPrivate/ecPublic before importing a new private key.
  • Enable ECDH KeyAgreement.init() reuse without tripping Ecc.throwIfKeyExists() (“Object already has a key”).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +606 to +610
/* Release and recreate native structs to support re-initialization.
* JCE requires KeyAgreement.init() to be callable multiple times. */
if (this.ecPrivate != null) {
this.ecPrivate.releaseNativeStruct();
}
Comment on lines +606 to +608
/* Release and recreate native structs to support re-initialization.
* JCE requires KeyAgreement.init() to be callable multiple times. */
if (this.ecPrivate != null) {
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.

Wycheproof nit: ECDH KeyAgreement throws on re-init after generateSecret()

2 participants