Class SntruPrimeKemContext
java.lang.Object
zeroecho.core.alg.ntruprime.SntruPrimeKemContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, 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 recipientPublicKey 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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface KemContext
KemContext.KemResult -
Constructor Summary
ConstructorsConstructorDescriptionSntruPrimeKemContext(CryptoAlgorithm algorithm, PrivateKey k) Creates a decapsulation context bound to a holder private key.SntruPrimeKemContext(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
-
SntruPrimeKemContext
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
-
SntruPrimeKemContext
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
-