fix: allow ECDH KeyAgreement init() reuse - #252
Open
MarkAtwood wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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 recreatingecPrivate/ecPublicbefore importing a new private key. - Enable ECDH
KeyAgreement.init()reuse without trippingEcc.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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
KeyAgreement.init()could not be called twice on the same ECDH instance.wolfCryptInit()allocatesecPrivate/ecPubliconce per KeyAgreement instance, andEcc.importPrivateOnCurveis guarded bythrowIfKeyExists(), so a secondengineInit()threw:The
javax.crypto.KeyAgreementjavadoc requiresinit()to reset the object to its post-construction state, so it must be callable at any time.wcInitECDHParamsnow releases and recreates both native Ecc structs before importing the private key.The DH path needs no equivalent change:
Dh.setPrivateKeyoverwrites 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:
init()called twice on one instanceIllegalStateException: Object already has a keyFixes #221.
One behavioral note
If
importPrivateOnCurvethrows 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.