Class NtruKemContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, KemContext
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface KemContext
KemContext.KemResult -
Constructor Summary
ConstructorsConstructorDescriptionNtruKemContext(CryptoAlgorithm algorithm, PrivateKey k) Creates a decapsulation context bound to the provided private key.NtruKemContext(CryptoAlgorithm algorithm, PublicKey k) Creates an encapsulation context bound to the provided public key. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm descriptor that created this context.voidclose()Closes this context.byte[]decapsulate(byte[] ciphertext) Performs NTRU key decapsulation using the bound private key.Performs NTRU key encapsulation using the bound public key.key()Returns the key this context is bound to.
-
Constructor Details
-
NtruKemContext
Creates an encapsulation context bound to the provided public key.The resulting instance supports
encapsulate()and forbidsdecapsulate(byte[]).- Parameters:
algorithm- the catalog algorithm descriptor this context belongs to, must not be nullk- the recipient's public key in the NTRU parameter set of interest, must not be null- Throws:
NullPointerException- if any argument is null
-
NtruKemContext
Creates a decapsulation context bound to the provided private key.The resulting instance supports
decapsulate(byte[])and forbidsencapsulate().- Parameters:
algorithm- the catalog algorithm descriptor this context belongs to, must not be nullk- the private key matching the recipient's public key, must not be null- Throws:
NullPointerException- if any argument is null
-
-
Method Details
-
algorithm
Returns the algorithm descriptor that created this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the algorithm descriptor, never null
-
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:
keyin interfaceCryptoContext- 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
KemContextcontract and to allow future implementations to free resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
encapsulate
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:
encapsulatein interfaceKemContext- Returns:
- the encapsulation result containing ciphertext and shared secret
- Throws:
IOException- if the underlying cryptographic operation failsIllegalStateException- if this context was constructed for decapsulation
-
decapsulate
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:
decapsulatein interfaceKemContext- 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 invalidIllegalStateException- if this context was constructed for encapsulationNullPointerException- ifciphertextis null
-