Interface NoiseTransportWriter

All Known Subinterfaces:
NoiseTransport

public interface NoiseTransportWriter

A Noise transport writer encrypts Noise transport messages. In the terminology of the Noise Protocol Framework specification, a NoiseTransportWriter instance encapsulates a "cipher state" produced by "splitting" a NoiseHandshake instance.

Noise transport writer instances are stateful and are not thread-safe.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    getCiphertextLength(int plaintextLength)
    Returns the length of the ciphertext resulting from the encryption of a plaintext of the given length.
    void
    Sets the encryption key used by this writer to a new pseudo-random key derived from the current key.
    byte[]
    writeMessage(byte[] plaintext)
    Encrypts a Noise transport message, returning a byte array sized exactly to contain the resulting ciphertext.
    int
    writeMessage(byte[] plaintext, int plaintextOffset, int plaintextLength, byte[] ciphertext, int ciphertextOffset)
    Encrypts a Noise transport message.
    Encrypts a Noise transport message, returning a new byte buffer sized exactly to contain the resulting ciphertext.
    int
    writeMessage(ByteBuffer plaintext, ByteBuffer ciphertext)
    Encrypts a Noise transport message.
  • Method Details

    • getCiphertextLength

      int getCiphertextLength(int plaintextLength)
      Returns the length of the ciphertext resulting from the encryption of a plaintext of the given length.
      Parameters:
      plaintextLength - the length of a plaintext
      Returns:
      the length of the ciphertext resulting from the encryption of a plaintext of the given size
    • writeMessage

      ByteBuffer writeMessage(ByteBuffer plaintext)
      Encrypts a Noise transport message, returning a new byte buffer sized exactly to contain the resulting ciphertext.

      All plaintext.remaining() bytes starting at plaintext.position() are processed. Upon return, the plaintext buffer's position will be equal to its limit; its limit will not have changed. The returned ciphertext buffer's position will be zero, and its limit will be equal to its capacity.

      Parameters:
      plaintext - the plaintext to encrypt
      Returns:
      a new byte buffer containing the resulting ciphertext and AEAD tag
      Throws:
      IllegalArgumentException - if the ciphertext for the given plaintext would be larger than the maximum allowed Noise transport message size
      See Also:
    • writeMessage

      int writeMessage(ByteBuffer plaintext, ByteBuffer ciphertext) throws ShortBufferException
      Encrypts a Noise transport message. Callers are responsible for ensuring that the given ciphertext buffer has enough remaining capacity to hold the resulting ciphertext and AEAD tag.

      All plaintext.remaining() bytes starting at plaintext.position() are processed. Upon return, the plaintext buffer's position will be equal to its limit; its limit will not have changed. The ciphertext buffer's position will have advanced by n, where n is the value returned by this method; the ciphertext buffer's limit will not have changed.

      Note that the ciphertext and plaintext buffers must be different, but may refer to the same underlying byte array to facilitate in-place encryption.

      Parameters:
      plaintext - the plaintext to encrypt
      ciphertext - the buffer into which to write the resulting ciphertext and AEAD tag
      Returns:
      the number of bytes written into the ciphertext buffer
      Throws:
      IllegalArgumentException - if the ciphertext for the given plaintext would be larger than the maximum allowed Noise transport message size
      ShortBufferException - if the given ciphertext buffer does not have enough remaining capacity to hold the resulting ciphertext and AEAD tag
      See Also:
    • writeMessage

      byte[] writeMessage(byte[] plaintext)
      Encrypts a Noise transport message, returning a byte array sized exactly to contain the resulting ciphertext.
      Parameters:
      plaintext - the plaintext to encrypt
      Returns:
      a new byte array containing the resulting ciphertext
      Throws:
      IllegalArgumentException - if the ciphertext for the given plaintext would be larger than the maximum allowed Noise transport message size
    • writeMessage

      int writeMessage(byte[] plaintext, int plaintextOffset, int plaintextLength, byte[] ciphertext, int ciphertextOffset) throws ShortBufferException
      Encrypts a Noise transport message. Callers are responsible for ensuring that the given ciphertext array is large enough to hold the resulting ciphertext and AEAD tag.

      Note that the ciphertext and plaintext arrays may refer to the same array, allowing for in-place encryption.

      Parameters:
      plaintext - a byte array containing the plaintext to encrypt
      plaintextOffset - the offset within plaintext where the plaintext begins
      plaintextLength - the length of the plaintext within plaintext
      ciphertext - a byte array into which to write the ciphertext and AEAD tag from this encryption operation
      ciphertextOffset - the position within ciphertext at which to begin writing the ciphertext and AEAD tag
      Returns:
      the number of bytes written into the ciphertext array
      Throws:
      ShortBufferException - if the ciphertext array (after its offset) is too small to hold the resulting ciphertext and AEAD tag
      IndexOutOfBoundsException - if the given plaintext length exceeds the length of the plaintext array after its offset
      IllegalArgumentException - if the ciphertext for the given plaintext would be larger than the maximum allowed Noise transport message size
      See Also:
    • rekeyWriter

      void rekeyWriter()
      Sets the encryption key used by this writer to a new pseudo-random key derived from the current key. This operation must be coordinated with the receiving party, otherwise messages sent to the receiving party will be unintelligible and decrypting future messages will fail.