Class NtruKemContext

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

public final class NtruKemContext extends Object implements KemContext
NtruKemContext performs NTRU key encapsulation and decapsulation against a single key and parameter set.

This context is created either with a public key (encapsulation role) or a private key (decapsulation role) and then drives the NTRU KEM using Bouncy Castle's lightweight API under the hood. Public keys enable encapsulate(), private keys enable decapsulate(byte[]). Calling a method that does not match the role results in IllegalStateException.

Usage

// Key generation and import are outside of this class; shown here for completeness.
KeyPairGenerator kpg = KeyPairGenerator.getInstance("NTRU", "BCPQC");
kpg.initialize(NTRUParameterSpec.ntruhrss701, new SecureRandom());
KeyPair kp = kpg.generateKeyPair();

// Initiator (encapsulation) uses the recipient's public key
try (NtruKemContext enc = new NtruKemContext(algorithm, kp.getPublic())) {
    KemContext.KemResult r = enc.encapsulate();
    byte[] ct = r.ciphertext();     // message to send to the recipient
    byte[] ssA = r.sharedSecret();  // initiator's shared secret

    // Responder (decapsulation) uses the matching private key
    try (NtruKemContext dec = new NtruKemContext(algorithm, kp.getPrivate())) {
        byte[] ssB = dec.decapsulate(ct);
        // ssA and ssB are identical
    }
}

Key material and formats

The context consumes standard JCA keys via Key.getEncoded(): X.509 SubjectPublicKeyInfo for public keys and PKCS#8 PrivateKeyInfo for private keys. Internally these are converted to Bouncy Castle NTRU key parameters for the KEM operations.

Thread-safety

Instances are not thread-safe. Create a separate context per thread or per protocol run.

Since:
1.0
  • Constructor Details

    • NtruKemContext

      public NtruKemContext(CryptoAlgorithm algorithm, PublicKey k)
      Creates an encapsulation context bound to the provided public key.

      The resulting instance supports encapsulate() and forbids decapsulate(byte[]).

      Parameters:
      algorithm - the catalog algorithm descriptor this context belongs to, must not be null
      k - the recipient's public key in the NTRU parameter set of interest, must not be null
      Throws:
      NullPointerException - if any argument is null
    • NtruKemContext

      public NtruKemContext(CryptoAlgorithm algorithm, PrivateKey k)
      Creates a decapsulation context bound to the provided private key.

      The resulting instance supports decapsulate(byte[]) and forbids encapsulate().

      Parameters:
      algorithm - the catalog algorithm descriptor this context belongs to, must not be null
      k - the private key matching the recipient's public key, must not be null
      Throws:
      NullPointerException - if any argument is null
  • Method Details

    • algorithm

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

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

      For encapsulation this is a public key; for decapsulation it is the matching private key.

      Specified by:
      key in interface CryptoContext
      Returns:
      the bound key reference, never null
    • close

      public void close()
      Closes this context.

      There are no native resources to release in the current implementation, so this method is a no-op. It exists to honor the KemContext contract and to allow future implementations to free resources.

      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 NTRU key encapsulation using the bound public key.

      On success, returns the encapsulation ciphertext and the derived shared secret. The ciphertext must be delivered to the holder of the matching private key, who can derive the same secret by calling decapsulate(byte[]).

      Specified by:
      encapsulate in interface KemContext
      Returns:
      the encapsulation result containing ciphertext and shared secret
      Throws:
      IOException - if the underlying cryptographic operation fails
      IllegalStateException - if this context was constructed for decapsulation
    • decapsulate

      public byte[] decapsulate(byte[] ciphertext) throws IOException
      Performs NTRU key decapsulation using the bound private key.

      The input must be a valid encapsulation ciphertext produced for the matching public key and parameter set. On success, the returned byte array is the shared secret that matches the secret produced during encapsulation.

      Specified by:
      decapsulate in interface KemContext
      Parameters:
      ciphertext - the encapsulation ciphertext received from the initiator, must not be null
      Returns:
      the derived shared secret
      Throws:
      IOException - if the underlying cryptographic operation fails or the ciphertext is invalid
      IllegalStateException - if this context was constructed for encapsulation
      NullPointerException - if ciphertext is null