Class SntruPrimeKemContext

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

public final class SntruPrimeKemContext extends Object implements KemContext
KEM context for SNTRU Prime that performs encapsulation and decapsulation using the underlying key material.

Overview

Instances of this context are initialized either for encapsulation with a recipient PublicKey or for decapsulation with a holder PrivateKey. The role is fixed at construction time.
  • Encapsulation: generates a fresh shared secret and its corresponding ciphertext.
  • Decapsulation: derives the shared secret from a provided ciphertext.

Usage

// Encapsulation side (initiator)
KemContext enc = new SntruPrimeKemContext(algorithm, recipientPublicKey);
KemResult kem = enc.encapsulate();
byte[] ct = kem.ciphertext();
byte[] secret = kem.sharedSecret();

// Decapsulation side (responder)
KemContext dec = new SntruPrimeKemContext(algorithm, myPrivateKey);
byte[] secret2 = dec.decapsulate(ct);
See Also:
  • Constructor Details

    • SntruPrimeKemContext

      public SntruPrimeKemContext(CryptoAlgorithm algorithm, PublicKey k)
      Creates an encapsulation context bound to a recipient public key.
      Parameters:
      algorithm - the owning algorithm that defines provider and naming conventions; must not be null
      k - the recipient public key used to encapsulate to; must not be null
      Throws:
      NullPointerException - if algorithm or k is null
    • SntruPrimeKemContext

      public SntruPrimeKemContext(CryptoAlgorithm algorithm, PrivateKey k)
      Creates a decapsulation context bound to a holder private key.
      Parameters:
      algorithm - the owning algorithm that defines provider and naming conventions; must not be null
      k - the private key used to decapsulate ciphertexts; must not be null
      Throws:
      NullPointerException - if algorithm or k is null
  • Method Details

    • algorithm

      public CryptoAlgorithm algorithm()
      Returns the algorithm that owns this context.
      Specified by:
      algorithm in interface CryptoContext
      Returns:
      the associated algorithm
    • 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 this context and releases any resources.

      This implementation has no external resources to release and 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
      Generates a fresh shared secret and its ciphertext using the recipient public key.

      This method may only be called on contexts constructed for encapsulation. If the context was constructed with a private key, an IllegalStateException is thrown.

      Specified by:
      encapsulate in interface KemContext
      Returns:
      a KEM result containing the ciphertext and the derived shared secret
      Throws:
      IllegalStateException - if this context is not initialized for encapsulation
      IOException - if the underlying provider fails to perform encapsulation
    • decapsulate

      public byte[] decapsulate(byte[] ciphertext) throws IOException
      Derives the shared secret from the provided ciphertext using the holder private key.

      This method may only be called on contexts constructed for decapsulation. If the context was constructed with a public key, an IllegalStateException is thrown.

      Specified by:
      decapsulate in interface KemContext
      Parameters:
      ciphertext - the KEM ciphertext produced by encapsulation
      Returns:
      the derived shared secret bytes
      Throws:
      NullPointerException - if ciphertext is null
      IllegalStateException - if this context is not initialized for decapsulation
      IOException - if the underlying provider fails to perform decapsulation