Class GenericJcaAgreementContext

java.lang.Object
zeroecho.core.alg.common.agreement.GenericJcaAgreementContext
All Implemented Interfaces:
Closeable, AutoCloseable, AgreementContext, CryptoContext

public final class GenericJcaAgreementContext extends Object implements AgreementContext

Generic JCA-based Key Agreement Context

An AgreementContext 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

  1. Construct with local private key and algorithm name.
  2. Call setPeerPublic(PublicKey) with the remote party’s key.
  3. Invoke deriveSecret() to compute the raw shared secret.
  4. 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 provider is null, the default JCA provider lookup is used; otherwise, the specific provider is requested.
Since:
1.0
  • Constructor Details

    • GenericJcaAgreementContext

      public GenericJcaAgreementContext(CryptoAlgorithm alg, PrivateKey priv, String jcaName, String provider)
      Creates a new JCA-based agreement context.
      Parameters:
      alg - the enclosing CryptoAlgorithm definition
      priv - the local private key used in the key agreement
      jcaName - the JCA algorithm name (e.g., "ECDH", "X25519")
      provider - optional JCA provider name, or null to use the default
      Throws:
      NullPointerException - if alg, priv, or jcaName is null
  • Method Details

    • algorithm

      public CryptoAlgorithm algorithm()
      Returns the CryptoAlgorithm that created this context.
      Specified by:
      algorithm in interface CryptoContext
      Returns:
      the parent algorithm definition
    • key

      public Key key()
      Returns the local private key bound to this agreement context.
      Specified by:
      key in interface CryptoContext
      Returns:
      the private Key used in the key agreement
    • setPeerPublic

      public void setPeerPublic(PublicKey peer)
      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:
      setPeerPublic in interface AgreementContext
      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 jcaName and optional provider.

      Specified by:
      deriveSecret in interface AgreementContext
      Returns:
      the raw shared secret as a byte array
      Throws:
      IllegalStateException - if the peer key has not been set
      IllegalArgumentException - 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 AgreementContext contract and for future compatibility.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface CryptoContext