com.southernstorm.noise.protocol

## Class ChaChaPolyCipherState

- java.lang.Object
- - com.southernstorm.noise.protocol.ChaChaPolyCipherState

- All Implemented Interfaces:
    CipherState, Destroyable, Cloneable

      public class ChaChaPolyCipherState
      extends Object
      implements CipherState

  Implements the ChaChaPoly cipher for Noise.

- - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `` | `ChaChaPolyCipherState()` Constructs a new cipher state for the "ChaChaPoly" algorithm.
    `protected` | `ChaChaPolyCipherState(ChaChaPolyCipherState o)` Copy constructor for cloning

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `ChaChaPolyCipherState` | `clone()` I2P
    `int` | `decryptWithAd(byte[] ad, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length)` Decrypts a ciphertext buffer using the cipher and a block of associated data.
    `int` | `decryptWithAd(byte[] ad, int adOffset, int adLength, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length)` I2P
    `void` | `destroy()` Destroys all sensitive state in the current object.
    `int` | `encryptWithAd(byte[] ad, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)` Encrypts a plaintext buffer using the cipher and a block of associated data.
    `int` | `encryptWithAd(byte[] ad, int adOffset, int adLength, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)` I2P
    `CipherState` | `fork(byte[] key, int offset)` Creates a new instance of this cipher and initializes it with a key.
    `String` | `getCipherName()` Gets the Noise protocol name for this cipher.
    `int` | `getKeyLength()` Gets the length of the key values for this cipher.
    `int` | `getMACLength()` Gets the length of the MAC values for this cipher.
    `boolean` | `hasKey()` Determine if this cipher object has been configured with a key.
    `void` | `initializeKey(byte[] key, int offset)` Initializes the key on this cipher object.
    `void` | `setNonce(long nonce)` Sets the nonce value.
    `String` | `toString()` I2P debug

    - ### Methods inherited from class java.lang.Object

      `equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### ChaChaPolyCipherState

          public ChaChaPolyCipherState()

      Constructs a new cipher state for the "ChaChaPoly" algorithm.

    - #### ChaChaPolyCipherState

          protected ChaChaPolyCipherState(ChaChaPolyCipherState o)
                                   throws CloneNotSupportedException

      Copy constructor for cloning
      Throws:
        `CloneNotSupportedException`

  - ### Method Detail

    - #### clone

          public ChaChaPolyCipherState clone()
                                      throws CloneNotSupportedException

      I2P
      Specified by:
        `clone` in interface `CipherState`
      Overrides:
        `clone` in class `Object`
      Throws:
        `CloneNotSupportedException`

    - #### decryptWithAd

          public int decryptWithAd(byte[] ad,
                                   byte[] ciphertext,
                                   int ciphertextOffset,
                                   byte[] plaintext,
                                   int plaintextOffset,
                                   int length)
                            throws ShortBufferException,
                                   BadPaddingException

      Description copied from interface: `CipherState`
      Decrypts a ciphertext buffer using the cipher and a block of associated data.
      Specified by:
        `decryptWithAd` in interface `CipherState`
      Parameters:
        `ad` - The associated data, or null if there is none.
        `ciphertext` - The buffer containing the ciphertext to decrypt.
        `ciphertextOffset` - The offset within the ciphertext buffer of the first byte of ciphertext data.
        `plaintext` - The buffer to place the plaintext in. This can be the same as the ciphertext buffer.
        `plaintextOffset` - The first offset within the plaintext buffer to place the plaintext.
        `length` - The length of the incoming ciphertext plus the MAC tag.
      Returns:
        The length of the plaintext with the MAC tag stripped off.
      Throws:
        `ShortBufferException` - The plaintext buffer does not have enough space to store the decrypted data.
        `BadPaddingException` - The MAC value failed to verify.

    - #### decryptWithAd

          public int decryptWithAd(byte[] ad,
                                   int adOffset,
                                   int adLength,
                                   byte[] ciphertext,
                                   int ciphertextOffset,
                                   byte[] plaintext,
                                   int plaintextOffset,
                                   int length)
                            throws ShortBufferException,
                                   BadPaddingException

      I2P
      Specified by:
        `decryptWithAd` in interface `CipherState`
      Throws:
        `ShortBufferException`
        `BadPaddingException`

    - #### destroy

          public void destroy()

      Description copied from interface: `Destroyable`
      Destroys all sensitive state in the current object.
      Specified by:
        `destroy` in interface `Destroyable`

    - #### encryptWithAd

          public int encryptWithAd(byte[] ad,
                                   byte[] plaintext,
                                   int plaintextOffset,
                                   byte[] ciphertext,
                                   int ciphertextOffset,
                                   int length)
                            throws ShortBufferException

      Description copied from interface: `CipherState`
      Encrypts a plaintext buffer using the cipher and a block of associated data.
      Specified by:
        `encryptWithAd` in interface `CipherState`
      Parameters:
        `ad` - The associated data, or null if there is none.
        `plaintext` - The buffer containing the plaintext to encrypt.
        `plaintextOffset` - The offset within the plaintext buffer of the first byte or plaintext data.
        `ciphertext` - The buffer to place the ciphertext in. This can be the same as the plaintext buffer.
        `ciphertextOffset` - The first offset within the ciphertext buffer to place the ciphertext and the MAC tag.
        `length` - The length of the plaintext.
      Returns:
        The length of the ciphertext plus the MAC tag, or -1 if the ciphertext buffer is not large enough to hold the result.
      Throws:
        `ShortBufferException` - The ciphertext buffer does not have enough space to hold the ciphertext plus MAC.

    - #### encryptWithAd

          public int encryptWithAd(byte[] ad,
                                   int adOffset,
                                   int adLength,
                                   byte[] plaintext,
                                   int plaintextOffset,
                                   byte[] ciphertext,
                                   int ciphertextOffset,
                                   int length)
                            throws ShortBufferException

      I2P
      Specified by:
        `encryptWithAd` in interface `CipherState`
      Throws:
        `ShortBufferException`

    - #### fork

          public CipherState fork(byte[] key,
                                  int offset)

      Description copied from interface: `CipherState`
      Creates a new instance of this cipher and initializes it with a key.
      Specified by:
        `fork` in interface `CipherState`
      Parameters:
        `key` - The buffer containing the key.
        `offset` - The offset into the key buffer of the first key byte.
      Returns:
        A new CipherState of the same class as this one.

    - #### getCipherName

          public String getCipherName()

      Description copied from interface: `CipherState`
      Gets the Noise protocol name for this cipher.
      Specified by:
        `getCipherName` in interface `CipherState`
      Returns:
        The cipher name.

    - #### getKeyLength

          public int getKeyLength()

      Description copied from interface: `CipherState`
      Gets the length of the key values for this cipher.
      Specified by:
        `getKeyLength` in interface `CipherState`
      Returns:
        The length of the key in bytes; usually 32.

    - #### getMACLength

          public int getMACLength()

      Description copied from interface: `CipherState`
      Gets the length of the MAC values for this cipher.
      Specified by:
        `getMACLength` in interface `CipherState`
      Returns:
        The length of MAC values in bytes, or zero if the key has not yet been initialized.

    - #### hasKey

          public boolean hasKey()

      Description copied from interface: `CipherState`
      Determine if this cipher object has been configured with a key.
      Specified by:
        `hasKey` in interface `CipherState`
      Returns:
        true if this cipher object has a key; false if the key has not yet been set with initializeKey().
      See Also:
        `CipherState.initializeKey(byte[], int)`

    - #### initializeKey

          public void initializeKey(byte[] key,
                                    int offset)

      Description copied from interface: `CipherState`
      Initializes the key on this cipher object.
      Specified by:
        `initializeKey` in interface `CipherState`
      Parameters:
        `key` - Points to a buffer that contains the key.
        `offset` - The offset of the key in the key buffer. The key buffer must contain at least getKeyLength() bytes starting at offset.
      See Also:
        `CipherState.hasKey()`

    - #### setNonce

          public void setNonce(long nonce)

      Description copied from interface: `CipherState`
      Sets the nonce value.
      Specified by:
        `setNonce` in interface `CipherState`
      Parameters:
        `nonce` - The new nonce value, which must be greater than or equal to the current value. This function is intended for testing purposes only. If the nonce value goes backwards then security may be compromised.

    - #### toString

          public String toString()

      I2P debug
      Overrides:
        `toString` in class `Object`