Package zeroecho.core.alg.kyber
Class KyberKemContext
java.lang.Object
zeroecho.core.alg.kyber.KyberKemContext
- All Implemented Interfaces:
Closeable,AutoCloseable,CryptoContext,KemContext
KyberKemContext is a lightweight KEM context used to perform Kyber (ML-KEM)
encapsulation or decapsulation.
Overview
A context is created in exactly one role:- Encapsulation role when constructed with a
PublicKey. In this roleencapsulate()is enabled anddecapsulate(byte[])is invalid. - Decapsulation role when constructed with a
PrivateKey. In this roledecapsulate(byte[])is enabled andencapsulate()is invalid.
CryptoAlgorithm
instance to report metadata via algorithm().
Thread-safety
Instances are not explicitly synchronized. Do not share a single context instance across threads while operations are running.Usage examples
// Encapsulation role (sender knows recipient's public key):
KemContext encCtx = new KyberKemContext(alg, recipientPublicKey);
KemResult result = encCtx.encapsulate();
byte[] ciphertext = result.ciphertext();
byte[] sharedSecret = result.sharedSecret();
// Decapsulation role (recipient uses its private key):
KemContext decCtx = new KyberKemContext(alg, recipientPrivateKey);
byte[] agreedSecret = decCtx.decapsulate(ciphertext);
-
Nested Class Summary
Nested classes/interfaces inherited from interface zeroecho.core.context.KemContext
KemContext.KemResult -
Constructor Summary
ConstructorsConstructorDescriptionKyberKemContext(CryptoAlgorithm algorithm, PrivateKey k) Creates a context in decapsulation role bound to the given private key.KyberKemContext(CryptoAlgorithm algorithm, PublicKey k) Creates a context in encapsulation role bound to the given public key. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm descriptor associated with this context.voidclose()Closes this context.byte[]decapsulate(byte[] ciphertext) Performs Kyber decapsulation using the bound private key and returns the shared secret.Performs Kyber encapsulation using the bound public key and returns the ciphertext and shared secret.key()Returns the key bound to this context.
-
Constructor Details
-
KyberKemContext
Creates a context in encapsulation role bound to the given public key.- Parameters:
algorithm- the owning algorithm instance that exposes provider name and metadata; must not be null.k- the recipient public key for encapsulation; must not be null.- Throws:
NullPointerException- if any argument is null.
-
KyberKemContext
Creates a context in decapsulation role bound to the given private key.- Parameters:
algorithm- the owning algorithm instance that exposes provider name and metadata; must not be null.k- the local private key for decapsulation; must not be null.- Throws:
NullPointerException- if any argument is null.
-
-
Method Details
-
algorithm
Returns the algorithm descriptor associated with this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the algorithm that created this context.
-
key
Returns the key bound to this context.- Specified by:
keyin interfaceCryptoContext- Returns:
- the public key when in encapsulation role, or the private key when in decapsulation role.
-
close
public void close()Closes this context.This implementation does not hold external resources, so the method is a no-op. It is provided for API symmetry and future compatibility.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
encapsulate
Performs Kyber encapsulation using the bound public key and returns the ciphertext and shared secret.This method is valid only if the context was constructed with a
PublicKey. If the context is in decapsulation role, anIllegalStateExceptionis thrown.- Specified by:
encapsulatein interfaceKemContext- Returns:
- a result containing the encapsulated ciphertext and the derived shared secret.
- Throws:
IllegalStateException- if the context is not initialized for encapsulation.IOException- if encapsulation fails in the underlying provider.
-
decapsulate
Performs Kyber decapsulation using the bound private key and returns the shared secret.This method is valid only if the context was constructed with a
PrivateKey. If the context is in encapsulation role, anIllegalStateExceptionis thrown.- Specified by:
decapsulatein interfaceKemContext- Parameters:
ciphertext- the Kyber ciphertext to decapsulate; must be the exact bytes produced by the peer's encapsulate call.- Returns:
- the derived shared secret corresponding to the provided ciphertext.
- Throws:
IllegalStateException- if the context is not initialized for decapsulation.IOException- if decapsulation fails in the underlying provider.
-