java.lang.Object
com.southernstorm.noise.protocol.ChaChaPolyCipherState
All Implemented Interfaces:
CipherState, Destroyable, Cloneable

public class ChaChaPolyCipherStateextends Object implements CipherState
Implements the ChaChaPoly cipher for Noise.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final boolean
    Switch for the key-dump debug logs.
    private boolean
    True once a key has been set.
    private byte[]
    Copy of the initial key, debug only.
    private final int[]
    Scratch words for the ChaCha20 block.
    private long
    The nonce counter.
    private final int[]
    Output words of the ChaCha20 block.
    private final Poly1305
    The Poly1305 MAC calculator.
    private final byte[]
    The one-time key for the MAC.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    Constructs a new cipher state for the "ChaChaPoly" algorithm.
    protected
    Copy constructor for cloning.
  • Method Summary

    Modifier and Type
    Method
    Description
    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
    Destroys all sensitive state in the current object.
    private void
    encrypt(byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)
    Encrypts or decrypts a buffer of bytes for the active packet.
    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
    private void
    finish(int adLength, int length)
    Finishes up the authentication tag for a packet.
    fork(byte[] key, int offset)
    Creates a new instance of this cipher and initializes it with a key.
    Gets the Noise protocol name for this cipher.
    int
    Gets the length of the key values for this cipher.
    int
    Gets the length of the MAC values for this cipher.
    boolean
    Determine if this cipher object has been configured with a key.
    void
    initializeKey(byte[] key, int offset)
    Initializes the key on this cipher object.
    private static void
    putLittleEndian64(byte[] output, int offset, long value)
    Puts a 64-bit integer into a buffer in little-endian order.
    void
    setNonce(long nonce)
    Sets the nonce value.
    private void
    setup(byte[] ad, int off, int len)
    Set up to encrypt or decrypt the next packet.
    I2P debug

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • poly

      private final Poly1305 poly
      The Poly1305 MAC calculator.
    • input

      private final int[] input
      Scratch words for the ChaCha20 block.
    • output

      private final int[] output
      Output words of the ChaCha20 block.
    • polyKey

      private final byte[] polyKey
      The one-time key for the MAC.
    • n

      private long n
      The nonce counter.
    • haskey

      private boolean haskey
      True once a key has been set.
    • initialKey

      private byte[] initialKey
      Copy of the initial key, debug only.
    • DEBUG

      private static final boolean DEBUG
      Switch for the key-dump debug logs.
      See Also:
  • Constructor Details

  • Method Details

    • destroy

      public void destroy()
      Description copied from interface: Destroyable
      Destroys all sensitive state in the current object.
      Specified by:
      destroy in interface Destroyable
    • 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.
    • 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:
    • 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:
    • setup

      private void setup(byte[] ad, int off, int len)
      Set up to encrypt or decrypt the next packet. I2P add off/len
      Parameters:
      ad - The associated data for the packet.
      off - where in ad the associated data starts
      len - how many bytes of ad to absorb, all of them when ad is non-null
    • putLittleEndian64

      private static void putLittleEndian64(byte[] output, int offset, long value)
      Puts a 64-bit integer into a buffer in little-endian order.
      Parameters:
      output - The output buffer.
      offset - The offset into the output buffer.
      value - The 64-bit integer value.
    • finish

      private void finish(int adLength, int length)
      Finishes up the authentication tag for a packet. I2P changed ad to adLength; ad data not used here
      Parameters:
      adLength - The length of the associated data, 0 if none.
      length - The length of the plaintext data.
    • encrypt

      private void encrypt(byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)
      Encrypts or decrypts a buffer of bytes for the active packet.
      Parameters:
      plaintext - The plaintext data to be encrypted.
      plaintextOffset - The offset to the first plaintext byte.
      ciphertext - The ciphertext data that results from encryption.
      ciphertextOffset - The offset to the first ciphertext byte.
      length - The number of bytes to encrypt.
    • 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
      Parameters:
      ad - The associated data, or null if there is none.
      adOffset - The offset within the ad buffer of the first byte of associated data.
      adLength - The length of the associated data within the ad buffer.
      plaintext - The buffer containing the plaintext to encrypt.
      plaintextOffset - The offset within the plaintext buffer of the first byte to encrypt.
      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.
      length - The length of the plaintext.
      Returns:
      The length of the ciphertext with the MAC tag appended.
      Throws:
      ShortBufferException - The ciphertext buffer does not have enough space to store the encrypted data.
    • 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
      Parameters:
      ad - The associated data, or null if there is none.
      adOffset - The offset within the ad buffer of the first byte of associated data.
      adLength - The length of the associated data within the ad buffer.
      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.
    • 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.
    • 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.
    • clone

      I2P
      Specified by:
      clone in interface CipherState
      Overrides:
      clone in class Object
      Returns:
      A clone of this CipherState.
      Throws:
      CloneNotSupportedException - If cloning is not supported.
    • toString

      public String toString()
      I2P debug
      Overrides:
      toString in class Object