Class ChaChaCipherContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, EncryptionContext, ContextAware
ChaCha20 cipher context (stream)
ConcreteEncryptionContext for the
ChaCha20 stream cipher. Relies on the parent
AbstractChaChaCipherContext for streaming, nonce management, and
optional header handling, while configuring ChaCha20-specific parameters:
- Transformation:
"ChaCha20". - 12-byte nonce via
ChaCha20ParameterSpec. - Initial counter sourced from
ChaChaSpec.initialCounter(), optionally overridden viaConfluxKeys.tagBits(String)in the bound context.
Counter handling
On attach, the counter is taken from the spec; if the active context contains an integer underConfluxKeys.tagBits(id),
that value overrides the spec and is used to initialize the cipher counter.
If absent, the spec's value is stored into the context for downstream
consumers.- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final CryptoAlgorithmAlgorithm definition that created this context.protected CtxInterfaceOptional per-operation context for exchanging headers, IVs, etc.protected final booleanOperation mode flag -truefor encryption,falsefor decryption.protected final SecretKeySymmetric key used by this context.protected final SecureRandomSecure random source for nonce generation.protected final ChaChaSpecAlgorithm-specific specification (e.g., AEAD parameters). -
Constructor Summary
ConstructorsConstructorDescriptionChaChaCipherContext(CryptoAlgorithm alg, SecretKey key, boolean enc, ChaChaSpec spec, SecureRandom rnd) Creates a ChaCha20 context. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm that created this context.attach(InputStream upstream) Attaches this context to an upstream input stream and returns a transformation stream.voidclose()Releases context resources.context()Returns the context currently in use.protected voidinitCipher(Cipher cipher, byte[] nonce) Initializes the cipher in the configured mode with the supplied nonce and counter.protected StringjceName()Returns"ChaCha20"as the JCE transformation.key()Returns the key bound to this context.voidsetContext(CtxInterface context) Attaches a context object that will be used to read and write per-session parameters.
-
Field Details
-
algorithm
Algorithm definition that created this context. -
key
Symmetric key used by this context. -
encrypt
protected final boolean encryptOperation mode flag -truefor encryption,falsefor decryption. -
spec
Algorithm-specific specification (e.g., AEAD parameters). -
rnd
Secure random source for nonce generation. -
ctx
Optional per-operation context for exchanging headers, IVs, etc.
-
-
Constructor Details
-
ChaChaCipherContext
public ChaChaCipherContext(CryptoAlgorithm alg, SecretKey key, boolean enc, ChaChaSpec spec, SecureRandom rnd) Creates a ChaCha20 context.- Parameters:
alg- algorithm definitionkey- ChaCha20 secret keyenc-truefor encryption,falsefor decryptionspec- ChaCha20 stream specification (includes initial counter)rnd- randomness source for nonce generation
-
-
Method Details
-
jceName
Returns"ChaCha20"as the JCE transformation.- Returns:
- transformation string
-
initCipher
Initializes the cipher in the configured mode with the supplied nonce and counter.Uses
ChaCha20ParameterSpecwith a 12-byte nonce and an initial counter taken fromChaChaSpec.initialCounter()or, if present, fromConfluxKeys.tagBits(String)in the bound context.- Parameters:
cipher- initialized cipher instancenonce- 12-byte nonce value- Throws:
GeneralSecurityException- if cipher initialization failsIOException- if context access or parameter resolution fails
-
algorithm
Returns the algorithm that created this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the algorithm definition that owns this context
-
key
Returns the key bound to this context.For unkeyed contexts such as digests, this may be
NullKey.INSTANCE.- Specified by:
keyin interfaceCryptoContext- Returns:
- the cryptographic key or sentinel bound to this context
-
setContext
Attaches a context object that will be used to read and write per-session parameters.Typical values include initialization vectors (IV), nonces, salts, or additional authenticated data (AAD). Implementations may throw if the context is missing required values or contains invalid entries.
- Specified by:
setContextin interfaceContextAware- Parameters:
context- non-null parameter context
-
context
Returns the context currently in use.This method must never return
nullafter the first successful invocation ofContextAware.setContext(CtxInterface)or after a context has been created internally by the implementation.Implementations must honour this post-condition: return a non-null context once one has been established. Implementations that violate it silently break callers that depend on the non-null guarantee without triggering a compile-time or early runtime error.
- Specified by:
contextin interfaceContextAware- Returns:
- the active parameter context; never
nullafter the context has been set or created
-
attach
Attaches this context to an upstream input stream and returns a transformation stream.Encryption prepends optional headers; decryption consumes headers first.
- Specified by:
attachin interfaceEncryptionContext- Parameters:
upstream- plaintext or ciphertext input stream- Returns:
- transformed stream (ciphertext for encryption, plaintext for decryption)
- Throws:
IOException- if cipher initialization fails
-
close
public void close()Releases context resources.No-op for ChaCha contexts.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-