Class NtrulPrimeKemContext
java.lang.Object
zeroecho.core.alg.ntruprime.NtrulPrimeKemContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, KemContext
KEM context for NTRU LPRime that performs encapsulation and decapsulation
using the underlying key material.
Overview
Instances of this context are initialized either for encapsulation with a recipientPublicKey or for decapsulation with a
holder PrivateKey. The role is fixed at construction time. On
encapsulation, the context generates a fresh shared secret and its
corresponding ciphertext. On decapsulation, it derives the shared secret from
a provided ciphertext.
Usage
// Encapsulation side (initiator)
KemContext enc = new NtrulPrimeKemContext(algorithm, recipientPublicKey);
KemResult kem = enc.encapsulate();
byte[] ct = kem.ciphertext();
byte[] secret = kem.secret();
// Decapsulation side (responder)
KemContext dec = new NtrulPrimeKemContext(algorithm, myPrivateKey);
byte[] secret2 = dec.decapsulate(ct);
-
Nested Class Summary
Nested classes/interfaces inherited from interface KemContext
KemContext.KemResult -
Constructor Summary
ConstructorsConstructorDescriptionNtrulPrimeKemContext(CryptoAlgorithm algorithm, PrivateKey k) Creates a decapsulation context bound to a holder private key.NtrulPrimeKemContext(CryptoAlgorithm algorithm, PublicKey k) Creates an encapsulation context bound to a recipient public key. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm that owns this context.voidclose()Closes this context and releases any resources.byte[]decapsulate(byte[] ciphertext) Derives the shared secret from the provided ciphertext using the holder private key.Generates a fresh shared secret and its ciphertext using the recipient public key.key()Returns the key bound to this context.
-
Constructor Details
-
NtrulPrimeKemContext
Creates an encapsulation context bound to a recipient public key.- Parameters:
algorithm- the owning algorithm that defines provider and naming conventions; must not be nullk- the recipient public key used to encapsulate to; must not be null- Throws:
NullPointerException- ifalgorithmorkis null
-
NtrulPrimeKemContext
Creates a decapsulation context bound to a holder private key.- Parameters:
algorithm- the owning algorithm that defines provider and naming conventions; must not be nullk- the private key used to decapsulate ciphertexts; must not be null- Throws:
NullPointerException- ifalgorithmorkis null
-
-
Method Details
-
algorithm
Returns the algorithm that owns this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the associated algorithm
-
key
Returns the key bound to this context.For encapsulation this is a
PublicKey. For decapsulation this is aPrivateKey.- Specified by:
keyin interfaceCryptoContext- 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
encapsulate
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
IllegalStateExceptionis thrown.- Specified by:
encapsulatein interfaceKemContext- Returns:
- a KEM result containing the ciphertext and the derived shared secret
- Throws:
IllegalStateException- if this context is not initialized for encapsulationIOException- if the underlying provider fails to perform encapsulation
-
decapsulate
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
IllegalStateExceptionis thrown.- Specified by:
decapsulatein interfaceKemContext- Parameters:
ciphertext- the KEM ciphertext produced by encapsulation- Returns:
- the derived shared secret bytes
- Throws:
NullPointerException- ifciphertextis nullIllegalStateException- if this context is not initialized for decapsulationIOException- if the underlying provider fails to perform decapsulation
-