Class AesCipherContext

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

public final class AesCipherContext extends Object implements EncryptionContext, ContextAware
Streaming AES cipher context for GCM / CBC / CTR.

IV and optional AAD are exchanged via a CtxInterface set with setContext(conflux.CtxInterface). Encryption always generates a fresh IV after atomically claiming the context; a caller-provided IV is never used for encryption. Decryption requires the IV from the context or encoded header.

When a SymmetricHeaderCodec is embedded in the AesSpec and a context is present, the context reads/writes a minimal header carrying IV, tag bits, and an optional AAD hash.

Since:
1.0
  • Constructor Details

    • AesCipherContext

      public AesCipherContext(CryptoAlgorithm algorithm, SecretKey key, boolean encrypt, AesSpec spec, SecureRandom rnd)
      Creates a new AES streaming context.
      Parameters:
      algorithm - the owning algorithm descriptor (ID "AES"); not null
      key - the AES SecretKey; not null
      encrypt - whether this context encrypts (true) or decrypts (false)
      spec - static AES settings (mode/padding and GCM tag bits); not null
      rnd - secure random source; if null, the library's shared source is used
      Throws:
      NullPointerException - if any required parameter is null
      IllegalArgumentException - if spec is inconsistent (e.g., GCM without NOPADDING)
  • Method Details

    • algorithm

      public CryptoAlgorithm algorithm()
      Returns the algorithm descriptor (ID "AES").
      Specified by:
      algorithm in interface CryptoContext
      Returns:
      the algorithm
    • key

      public Key key()
      Returns the key bound to this context.
      Specified by:
      key in interface CryptoContext
      Returns:
      the secret key
    • setContext

      public void setContext(CtxInterface context)
      Sets the Conflux session context used to exchange IV/AAD/tagBits and to enable header I/O.
      Specified by:
      setContext in interface ContextAware
      Parameters:
      context - the context; may be null to disable context features
    • context

      public CtxInterface context()
      Returns the currently set Conflux context (possibly null).
      Specified by:
      context in interface ContextAware
      Returns:
      the context or null
    • attach

      public InputStream attach(InputStream upstream) throws IOException
      Attaches this context to an upstream stream and returns a transforming stream.
      • Decrypt + header → reads header first, hydrates context, then transforms
      • Transform → initializes JCA cipher with IV/AAD (GCM), staging final output if necessary
      • Encrypt + header → prepends header after IV is determined
      Specified by:
      attach in interface EncryptionContext
      Parameters:
      upstream - the source stream; not null
      Returns:
      a stream that yields ciphertext (encrypt) or plaintext (decrypt)
      Throws:
      IOException - on I/O or provider errors; includes IV length mismatches and missing IV for decrypt
    • close

      public void close()
      Description copied from interface: CryptoContext
      Completes the context's implementation-specific cleanup.

      Implementations release resources they explicitly own; cleanup can be a no-op for contexts holding only borrowed or garbage-collected state. The bound key remains caller-owned: closing a context never destroys the key. Callers that own a destroyable key must destroy it explicitly after all contexts and other consumers have released it. Streams returned by the context have independent lifecycles: callers must complete and close those streams before closing the context. This method does not promise to finalize stream output or verify authentication tags.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface CryptoContext