Package zeroecho.core.alg.cmce
Class CmceKemContext
java.lang.Object
zeroecho.core.alg.cmce.CmceKemContext
- All Implemented Interfaces:
Closeable,AutoCloseable,CryptoContext,KemContext
Classic McEliece (CMCE) KEM context
Holds the state required to perform CMCE key encapsulation or decapsulation. The operational mode is determined by the constructor used:
- PublicKey constructor - encapsulate mode
- PrivateKey constructor - decapsulate mode
Usage:
CryptoAlgorithm alg = ...;
PublicKey recipient = ...;
// Encapsulation
try (CmceKemContext ctx = new CmceKemContext(alg, recipient)) {
KemResult kem = ctx.encapsulate();
byte[] ct = kem.ciphertext();
byte[] secret = kem.secret();
// send ct to recipient; use secret for key derivation
}
// Decapsulation
PrivateKey myPriv = ...;
byte[] ct = ...;
try (CmceKemContext ctx = new CmceKemContext(alg, myPriv)) {
byte[] secret = ctx.decapsulate(ct);
}
Notes:
- Encapsulation requires a CMCE public key; decapsulation requires a CMCE private key.
- Returned arrays are owned by the caller; callers should clear secrets when no longer needed.
- This class holds no external resources and is safe to close repeatedly.
- Since:
- 1.0
-
Nested Class Summary
Nested classes/interfaces inherited from interface zeroecho.core.context.KemContext
KemContext.KemResult -
Constructor Summary
ConstructorsConstructorDescriptionCmceKemContext(CryptoAlgorithm algorithm, PrivateKey k) Creates a decapsulation context bound to a private key.CmceKemContext(CryptoAlgorithm algorithm, PublicKey k) Creates an encapsulation context bound to a recipient public key. -
Method Summary
Modifier and TypeMethodDescriptionReturns the parent algorithm descriptor for this context.voidclose()Releases resources held by this context.byte[]decapsulate(byte[] ciphertext) Extracts the shared secret from the given ciphertext using the stored private key.Generates a CMCE ciphertext and shared secret using the stored public key.key()Returns the key bound to this context.
-
Constructor Details
-
CmceKemContext
Creates an encapsulation context bound to a recipient public key.- Parameters:
algorithm- parent algorithm metadata (for diagnostics)k- CMCE public key- Throws:
NullPointerException- if any argument is null
-
CmceKemContext
Creates a decapsulation context bound to a private key.- Parameters:
algorithm- parent algorithm metadata (for diagnostics)k- CMCE private key- Throws:
NullPointerException- if any argument is null
-
-
Method Details
-
algorithm
Returns the parent algorithm descriptor for this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- algorithm descriptor; never null
-
key
Returns the key bound to this context.In encapsulate mode this is a
PublicKey; in decapsulate mode it is aPrivateKey.- Specified by:
keyin interfaceCryptoContext- Returns:
- key used by this context; never null
-
close
public void close()Releases resources held by this context.This implementation holds no resources and performs no action. It is safe to call multiple times.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
encapsulate
Generates a CMCE ciphertext and shared secret using the stored public key.- Specified by:
encapsulatein interfaceKemContext- Returns:
- result containing ciphertext and secret
- Throws:
IllegalStateException- if this context is not in encapsulate modeIOException- if encapsulation fails
-
decapsulate
Extracts the shared secret from the given ciphertext using the stored private key.- Specified by:
decapsulatein interfaceKemContext- Parameters:
ciphertext- CMCE ciphertext (must be non-null and non-empty)- Returns:
- shared secret bytes
- Throws:
IllegalStateException- if this context is not in decapsulate modeIllegalArgumentException- ifciphertextis null or emptyIOException- if decapsulation fails
-