Class GenericJcaAgreementContext
java.lang.Object
zeroecho.core.alg.common.agreement.GenericJcaAgreementContext
- All Implemented Interfaces:
Closeable, AutoCloseable, AgreementContext, CryptoContext
Generic JCA-based Key Agreement Context
AnAgreementContext backed by the standard JCA key-agreement API.
This class supports elliptic-curve and modern Diffie-Hellman variants such as
ECDH, XDH (X25519, X448), and others provided by the runtime or configured
provider.
Instances of this context are created with a local PrivateKey, and
require the peer’s PublicKey to be provided later via
setPeerPublic(PublicKey) before deriving the shared secret.
Lifecycle
- Construct with local private key and algorithm name.
- Call
setPeerPublic(PublicKey)with the remote party’s key. - Invoke
deriveSecret()to compute the raw shared secret. - Optionally call
close()(no resources are held here).
Notes
- The derived secret is the raw key agreement output; protocols should apply a KDF before using it as a symmetric key.
- If
providerisnull, the default JCA provider lookup is used; otherwise, the specific provider is requested.
- Since:
- 1.0
-
Constructor Summary
ConstructorsConstructorDescriptionGenericJcaAgreementContext(CryptoAlgorithm alg, PrivateKey priv, String jcaName, String provider) Creates a new JCA-based agreement context. -
Method Summary
Modifier and TypeMethodDescriptionReturns theCryptoAlgorithmthat created this context.voidclose()Closes this context.byte[]Computes the raw shared secret using the configured local private key and the previously assigned peer public key.key()Returns the local private key bound to this agreement context.voidsetPeerPublic(PublicKey peer) Assigns the peer’s public key for the key agreement.
-
Constructor Details
-
GenericJcaAgreementContext
public GenericJcaAgreementContext(CryptoAlgorithm alg, PrivateKey priv, String jcaName, String provider) Creates a new JCA-based agreement context.- Parameters:
alg- the enclosingCryptoAlgorithmdefinitionpriv- the local private key used in the key agreementjcaName- the JCA algorithm name (e.g.,"ECDH","X25519")provider- optional JCA provider name, ornullto use the default- Throws:
NullPointerException- ifalg,priv, orjcaNameisnull
-
-
Method Details
-
algorithm
Returns theCryptoAlgorithmthat created this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the parent algorithm definition
-
key
Returns the local private key bound to this agreement context.- Specified by:
keyin interfaceCryptoContext- Returns:
- the private
Keyused in the key agreement
-
setPeerPublic
Assigns the peer’s public key for the key agreement.This must be called before
deriveSecret(), otherwise the context cannot complete the protocol.- Specified by:
setPeerPublicin interfaceAgreementContext- Parameters:
peer- the remote party’s public key
-
deriveSecret
public byte[] deriveSecret()Computes the raw shared secret using the configured local private key and the previously assigned peer public key.Internally this delegates to the JCA key-agreement API with the given
jcaNameand optional provider.- Specified by:
deriveSecretin interfaceAgreementContext- Returns:
- the raw shared secret as a byte array
- Throws:
IllegalStateException- if the peer key has not been setIllegalArgumentException- if key agreement fails due to invalid keys, unsupported parameters, or provider errors
-
close
public void close()Closes this context.For this implementation, there are no system resources to release, so the method is a no-op. It exists to satisfy the
AgreementContextcontract and for future compatibility.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-