Class SaberKemContext

java.lang.Object
zeroecho.core.alg.saber.SaberKemContext
All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, KemContext

public final class SaberKemContext extends Object implements KemContext
KEM context for SABER supporting encapsulation with a public key and decapsulation with a private key.

A SaberKemContext is created in one of two roles:

  • Encapsulator: constructed with a PublicKey, it produces an encapsulation ciphertext and a fresh shared secret.
  • Decapsulator: constructed with a PrivateKey, it derives the shared secret from a received encapsulation ciphertext.

Lifecycle and thread-safety

Instances are single-use per operation style and not thread-safe. Callers should not share a context across threads without external synchronization. close() is a no-op because this context maintains no stream resources.

Usage example

// Encapsulator side
KemContext enc = new SaberKemContext(algo, recipientPublicKey);
KemContext.KemResult r = enc.encapsulate();
byte[] ciphertext = r.ciphertext();
byte[] shared = r.sharedSecret();

// Decapsulator side
KemContext dec = new SaberKemContext(algo, myPrivateKey);
byte[] shared2 = dec.decapsulate(ciphertext);
  • Constructor Details

    • SaberKemContext

      public SaberKemContext(CryptoAlgorithm algorithm, PublicKey k)
      Creates an encapsulation context bound to the recipient's PublicKey.
      Parameters:
      algorithm - the owning algorithm used for metadata and provider identity
      k - recipient public key for SABER encapsulation
      Throws:
      NullPointerException - if algorithm or k is null
    • SaberKemContext

      public SaberKemContext(CryptoAlgorithm algorithm, PrivateKey k)
      Creates a decapsulation context bound to our PrivateKey.
      Parameters:
      algorithm - the owning algorithm used for metadata and provider identity
      k - private key for SABER decapsulation
      Throws:
      NullPointerException - if algorithm or k is null
  • Method Details

    • algorithm

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

      public Key key()
      Returns the key bound to this context.

      For encapsulation this is a PublicKey. For decapsulation this is a PrivateKey.

      Specified by:
      key in interface CryptoContext
      Returns:
      the bound Key
    • close

      public void close()
      Closes the context and releases resources.

      This implementation holds no stream resources, so the method is a no-op.

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

      public KemContext.KemResult encapsulate() throws IOException
      Performs SABER encapsulation and returns the ciphertext and shared secret.

      This method is valid only when the context was constructed with a PublicKey. The returned KemContext.KemResult contains the encapsulation to transmit and the derived shared secret.

      Specified by:
      encapsulate in interface KemContext
      Returns:
      a KemContext.KemResult holding the encapsulation and shared secret
      Throws:
      IllegalStateException - if the context was initialized for decapsulation
      IOException - if the underlying SABER operation fails
    • decapsulate

      public byte[] decapsulate(byte[] ciphertext) throws IOException
      Performs SABER decapsulation and returns the shared secret derived from ciphertext.

      This method is valid only when the context was constructed with a PrivateKey.

      Specified by:
      decapsulate in interface KemContext
      Parameters:
      ciphertext - the peer's encapsulation ciphertext
      Returns:
      the derived shared secret bytes
      Throws:
      IllegalStateException - if the context was initialized for encapsulation
      IOException - if the underlying SABER operation fails or input is invalid