Class KemDataContentBuilder
java.lang.Object
zeroecho.sdk.builders.alg.KemDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<DataContent>
KEM-based envelope builder that first collects KEM parameters and only then
delegates to an explicit symmetric builder for the payload.
This builder fixes the earlier unintuitive flow where each symmetric builder
duplicated KEM settings. With KemDataContentBuilder you configure the
KEM once (algorithm id, recipient key, KDF) and then select which payload
cipher to use by supplying an AesDataContentBuilder or a
ChaChaDataContentBuilder. The derived symmetric key is injected into
the provided symmetric builder prior to building the payload stage.
Header format
The produced envelope prepends the KEM encapsulation ciphertext using the following simple framing:length L in Pack7 format L bytes of KEM ciphertext [payload stream produced by the symmetric builder]The symmetric builder (AES or ChaCha) remains fully responsible for its own optional header (IV, AAD tag, etc.) which follows immediately after the KEM prefix.
Usage
// Encryption
DataContent content = DataContentChainBuilder.encrypt()
.add(PlainStringBuilder.builder().value("hello"))
.add(KemDataContentBuilder.builder()
.kem("ML-KEM")
.recipientPublic(recipientPub)
.hkdfSha256("KEM-demo".getBytes(StandardCharsets.US_ASCII))
.withAes(AesDataContentBuilder.builder().modeGcm(128).withHeader()))
.build();
// Decryption
DataContent plain = DataContentChainBuilder.decrypt()
.add(PlainBytesBuilder.builder().bytes(cipherBytes))
.add(KemDataContentBuilder.builder()
.kem("ML-KEM")
.recipientPrivate(recipientPriv)
.hkdfSha256("KEM-demo".getBytes(StandardCharsets.US_ASCII))
.withAes(AesDataContentBuilder.builder().modeGcm(128).withHeader()))
.build();
Thread-safety
Instances are not thread-safe. Create a new builder per pipeline/use.- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionbuild(boolean encrypt) Builds the KEM envelope stage in either encryption or decryption mode.static KemDataContentBuilderbuilder(ZeroEchoSession session) Returns a newKemDataContentBuilder.derivedKeyBytes(int bytes) Sets the number of derived key bytes to feed into the selected symmetric builder.Disables HKDF and uses the shared secret directly (or truncated) for the symmetric key.hkdfSha256(byte[] info) Configures HKDF-SHA256 as the key derivation function using the providedinfocontext string.Sets the canonical KEM algorithm identifier to use (e.g., "ML-KEM").maxKemCiphertextLen(int maxBytes) Sets a safety limit for the KEM ciphertext length accepted in decryption.Sets the recipient private key for decapsulation (decryption path).Sets the recipient public key for encapsulation (encryption path).Selects AES as the payload cipher and supplies a configuredAesDataContentBuilderthat will receive the derived key.Selects ChaCha20/ChaCha20-Poly1305 as the payload cipher and supplies a configuredChaChaDataContentBuilderthat will receive the derived key.
-
Method Details
-
builder
Returns a newKemDataContentBuilder.- Parameters:
session- active session used to create cryptographic contexts- Returns:
- fresh builder instance
-
kem
Sets the canonical KEM algorithm identifier to use (e.g., "ML-KEM").- Parameters:
id- algorithm identifier resolved by the builder'sZeroEchoSession- Returns:
- this builder
- Throws:
NullPointerException- ifidis null
-
recipientPublic
Sets the recipient public key for encapsulation (encryption path).- Parameters:
k- recipient public key- Returns:
- this builder
- Throws:
NullPointerException- ifkis null
-
recipientPrivate
Sets the recipient private key for decapsulation (decryption path).- Parameters:
k- recipient private key- Returns:
- this builder
- Throws:
NullPointerException- ifkis null
-
hkdfSha256
Configures HKDF-SHA256 as the key derivation function using the providedinfocontext string. If not set, a default constant is used.- Parameters:
info- application/context info; copied defensively; may be empty but must not be null- Returns:
- this builder
- Throws:
NullPointerException- ifinfois null
-
directSecret
Disables HKDF and uses the shared secret directly (or truncated) for the symmetric key. This is usually discouraged unless the KEM already outputs a uniformly random key.- Returns:
- this builder
-
maxKemCiphertextLen
Sets a safety limit for the KEM ciphertext length accepted in decryption. Values up to the algorithm's maximum should be allowed.- Parameters:
maxBytes- maximum number of ciphertext bytes; must be positive- Returns:
- this builder
- Throws:
IllegalArgumentException- ifmaxBytesis not positive
-
withAes
Selects AES as the payload cipher and supplies a configuredAesDataContentBuilderthat will receive the derived key.The builder instance will be mutated to inject the derived key. Supply a fresh instance per pipeline to avoid cross-use.
- Parameters:
b- AES payload builder to use- Returns:
- this builder
- Throws:
NullPointerException- ifbis null- See Also:
-
withChaCha
Selects ChaCha20/ChaCha20-Poly1305 as the payload cipher and supplies a configuredChaChaDataContentBuilderthat will receive the derived key.The builder instance will be mutated to inject the derived key. Supply a fresh instance per pipeline to avoid cross-use.
- Parameters:
b- ChaCha payload builder to use- Returns:
- this builder
- Throws:
NullPointerException- ifbis null- See Also:
-
derivedKeyBytes
Sets the number of derived key bytes to feed into the selected symmetric builder. Defaults to 32 (AES-256 or ChaCha20).- Parameters:
bytes- number of key bytes; typical values are 16, 24, or 32- Returns:
- this builder
- Throws:
IllegalArgumentException- ifbytesis not positive
-
build
Builds the KEM envelope stage in either encryption or decryption mode.On encryption, the returned
DataContentwill:- encapsulate with the configured KEM and recipient public key,
- derive a symmetric key via HKDF-SHA256 (or directly),
- inject the key into the selected symmetric builder,
- prepend the KEM encapsulation ciphertext, and
- stream the symmetric payload output.
- read and decapsulate the KEM ciphertext using the recipient private key,
- derive the symmetric key,
- inject it into the symmetric builder, and
- stream-decrypt the remaining payload bytes.
- Specified by:
buildin interfaceDataContentBuilder<DataContent>- Parameters:
encrypt-trueto build the encryption stage,falsefor decryption- Returns:
- a
DataContentrepresenting the configured KEM envelope - Throws:
IllegalStateException- if required parameters are missing or invalid
-