Class BCChaCha20ParameterSpec

java.lang.Object
org.bouncycastle.jcajce.spec.BCChaCha20ParameterSpec
All Implemented Interfaces:
AlgorithmParameterSpec

public class BCChaCha20ParameterSpec extends Object implements AlgorithmParameterSpec
Parameters for the RFC 8439 (formerly RFC 7539) form of the ChaCha20 stream cipher - "ChaCha7539", also registered as "ChaCha20", in the BC provider: a 12 byte nonce and the initial value of the 32 bit block counter.

This is the BC equivalent of javax.crypto.spec.ChaCha20ParameterSpec, which only exists from Java 11, and carries the same values with the same meaning; the BC provider accepts either. Starting the cipher at a given block is what, for example, the record number protection of DTLS 1.3 (RFC 9147 sec. 4.2.3) and the header protection of QUIC (RFC 9001 sec. 5.4.4) require.

The counter is an unsigned 32 bit value carried in an int, so a negative counter addresses one of the blocks from 2^31 to 2^32 - 1. The cipher returns to this block, not block 0, after each doFinal.

An AlgorithmParameters object has no way of carrying the counter, so a cipher initialised with a non-zero counter returns null from getParameters(), as the JDK's ChaCha20 cipher does, rather than parameters that would recreate the wrong key stream.

  • Constructor Details

    • BCChaCha20ParameterSpec

      public BCChaCha20ParameterSpec(byte[] nonce, int counter)
      Base constructor.
      Parameters:
      nonce - the 12 byte nonce.
      counter - the initial block counter, an unsigned 32 bit value.
      Throws:
      NullPointerException - if nonce is null.
      IllegalArgumentException - if nonce is not 12 bytes long.
  • Method Details

    • getNonce

      public byte[] getNonce()
      Return the nonce.
      Returns:
      a copy of the 12 byte nonce.
    • getCounter

      public int getCounter()
      Return the initial block counter.
      Returns:
      the initial block counter, an unsigned 32 bit value.