Class ChaChaCipherContext

java.lang.Object
zeroecho.core.alg.chacha.ChaChaCipherContext
All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, EncryptionContext, ContextAware

public final class ChaChaCipherContext extends Object

ChaCha20 cipher context (stream)

Concrete EncryptionContext for the ChaCha20 stream cipher. Relies on the parent AbstractChaChaCipherContext for streaming, nonce management, and optional header handling, while configuring ChaCha20-specific parameters:

Counter handling

On attach, the counter is taken from the spec; if the active context contains an integer under ConfluxKeys.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 Details

    • algorithm

      protected final CryptoAlgorithm algorithm
      Algorithm definition that created this context.
    • key

      protected final SecretKey key
      Symmetric key used by this context.
    • encrypt

      protected final boolean encrypt
      Operation mode flag - true for encryption, false for decryption.
    • spec

      protected final ChaChaSpec spec
      Algorithm-specific specification (e.g., AEAD parameters).
    • rnd

      protected final SecureRandom rnd
      Secure random source for nonce generation.
    • ctx

      protected CtxInterface 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 definition
      key - ChaCha20 secret key
      enc - true for encryption, false for decryption
      spec - ChaCha20 stream specification (includes initial counter)
      rnd - randomness source for nonce generation
  • Method Details

    • jceName

      protected String jceName()
      Returns "ChaCha20" as the JCE transformation.
      Returns:
      transformation string
    • initCipher

      protected void initCipher(Cipher cipher, byte[] nonce) throws GeneralSecurityException, IOException
      Initializes the cipher in the configured mode with the supplied nonce and counter.

      Uses ChaCha20ParameterSpec with a 12-byte nonce and an initial counter taken from ChaChaSpec.initialCounter() or, if present, from ConfluxKeys.tagBits(String) in the bound context.

      Parameters:
      cipher - initialized cipher instance
      nonce - 12-byte nonce value
      Throws:
      GeneralSecurityException - if cipher initialization fails
      IOException - if context access or parameter resolution fails
    • algorithm

      public CryptoAlgorithm algorithm()
      Returns the algorithm that created this context.
      Specified by:
      algorithm in interface CryptoContext
      Returns:
      the algorithm definition that owns this context
    • key

      public Key key()
      Returns the key bound to this context.

      For unkeyed contexts such as digests, this may be NullKey.INSTANCE.

      Specified by:
      key in interface CryptoContext
      Returns:
      the cryptographic key or sentinel bound to this context
    • setContext

      public void setContext(CtxInterface context)
      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:
      setContext in interface ContextAware
      Parameters:
      context - non-null parameter context
    • context

      public CtxInterface context()
      Returns the context currently in use.

      This method must never return null after the first successful invocation of ContextAware.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:
      context in interface ContextAware
      Returns:
      the active parameter context; never null after the context has been set or created
    • attach

      public InputStream attach(InputStream upstream) throws IOException
      Attaches this context to an upstream input stream and returns a transformation stream.

      Encryption prepends optional headers; decryption consumes headers first.

      Specified by:
      attach in interface EncryptionContext
      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:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface CryptoContext