Interface NoiseTransportWriter
- All Known Subinterfaces:
NoiseTransport
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 TypeMethodDescriptionint
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.writeMessage
(ByteBuffer plaintext) 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
Encrypts a Noise transport message, returning a new byte buffer sized exactly to contain the resulting ciphertext.All
plaintext.remaining()
bytes starting atplaintext.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
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 atplaintext.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 encryptciphertext
- 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 sizeShortBufferException
- 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 encryptplaintextOffset
- the offset withinplaintext
where the plaintext beginsplaintextLength
- the length of the plaintext withinplaintext
ciphertext
- a byte array into which to write the ciphertext and AEAD tag from this encryption operationciphertextOffset
- the position withinciphertext
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 tagIndexOutOfBoundsException
- if the given plaintext length exceeds the length of the plaintext array after its offsetIllegalArgumentException
- 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.
-