Interface NoiseCipher
-
Method Summary
Modifier and TypeMethodDescriptionbuildKey
(byte[] keyBytes) Converts an array of bytes into aKey
instance suitable for use with this cipher.default byte[]
Decrypts the given ciphertext and verifies its AEAD tag using the given key, nonce, and associated data.int
decrypt
(Key key, long nonce, byte[] associatedData, int aadOffset, int aadLength, byte[] ciphertext, int ciphertextOffset, int ciphertextLength, byte[] plaintext, int plaintextOffset) Decrypts the given ciphertext and verifies its AEAD tag.default ByteBuffer
decrypt
(Key key, long nonce, ByteBuffer associatedData, ByteBuffer ciphertext) Decrypts the given ciphertext and verifies its AEAD tag using the given key, nonce, and associated data.int
decrypt
(Key key, long nonce, ByteBuffer associatedData, ByteBuffer ciphertext, ByteBuffer plaintext) Decrypts the given ciphertext and verifies its AEAD tag using the given key, nonce, and associated data.default byte[]
Encrypts the given plaintext using the given key, nonce, and associated data.int
encrypt
(Key key, long nonce, byte[] associatedData, int aadOffset, int aadLength, byte[] plaintext, int plaintextOffset, int plaintextLength, byte[] ciphertext, int ciphertextOffset) Encrypts the given plaintext using the given key, nonce, and associated data.default ByteBuffer
encrypt
(Key key, long nonce, ByteBuffer associatedData, ByteBuffer plaintext) Encrypts the given plaintext using the given key, nonce, and associated data.int
encrypt
(Key key, long nonce, ByteBuffer associatedData, ByteBuffer plaintext, ByteBuffer ciphertext) Encrypts the given plaintext using the given key, nonce, and associated data.default int
getCiphertextLength
(int plaintextLength) Returns the size of a buffer needed to hold the ciphertext produced by encrypting a plaintext of the given length (the length of the plaintext plus the length of an AEAD tag).static NoiseCipher
getInstance
(String noiseCipherName) Returns aNoiseCipher
instance that implements the named cipher algorithm.getName()
Returns the name of this Noise cipher as it would appear in a full Noise protocol name.default int
getPlaintextLength
(int ciphertextLength) Returns the size of a buffer needed to hold the plaintext produced by decrypting a ciphertext of the given length (the length of the ciphertext minus the length of the AEAD tag).default Key
Generates a new pseudo-random key as a function of the given key.
-
Method Details
-
getInstance
Returns a
NoiseCipher
instance that implements the named cipher algorithm. This method recognizes the following cipher names:- ChaChaPoly
- Returns a Noise cipher implementation backed by the
Cipher
returned by the most preferred security provider that supports the "ChaCha20-Poly1305" cipher transformation - AESGCM
- Returns a Noise cipher implementation backed by the
Cipher
returned by the most preferred security provider that supports the "AES/GCM/NoPadding" cipher transformation
Every implementation of the Java platform is required to support the "AES/GCM/NoPadding" cipher transformation, which underpins the "AESGCM" Noise cipher.
- Parameters:
noiseCipherName
- the name of the Noise cipher algorithm for which to return a concreteNoiseCipher
implementation- Returns:
- a concrete
NoiseCipher
implementation for the given algorithm name - Throws:
NoSuchAlgorithmException
- if the given name is "ChaChaPoly" and the "ChaCha20-Poly1305" cipher transformation is not supported by any security provider in the current JVMIllegalArgumentException
- if the given name is not a known Noise cipher name- See Also:
-
getName
String getName()Returns the name of this Noise cipher as it would appear in a full Noise protocol name.- Returns:
- the name of this Noise cipher as it would appear in a full Noise protocol name
-
encrypt
default ByteBuffer encrypt(Key key, long nonce, @Nullable ByteBuffer associatedData, ByteBuffer plaintext) Encrypts the given plaintext using the given key, nonce, and associated data. This method returns a new byte buffer sized exactly to contain 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. If associated data is provided, the same is true of the associated data buffer. The returned ciphertext buffer's position will be zero, and its limit will be equal to its capacity.- Parameters:
key
- the key with which to encrypt the given plaintextnonce
- a nonce, which must be unique for the given keyassociatedData
- the associated data to use when calculating an AEAD tagplaintext
- the plaintext to encrypt- Returns:
- a new byte buffer containing the resulting ciphertext and AEAD tag
- See Also:
-
encrypt
int encrypt(Key key, long nonce, @Nullable ByteBuffer associatedData, ByteBuffer plaintext, ByteBuffer ciphertext) throws ShortBufferException Encrypts the given plaintext using the given key, nonce, and associated data. 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. If associated data is provided, the same will be true of the associated data buffer. 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:
key
- the key with which to encrypt the given plaintextnonce
- a nonce, which must be unique for the given keyassociatedData
- the associated data to use when calculating an AEAD tagplaintext
- 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:
ShortBufferException
- if the given ciphertext buffer does not have enough remaining capacity to hold the resulting ciphertext and AEAD tag- See Also:
-
encrypt
Encrypts the given plaintext using the given key, nonce, and associated data. This method returns a new byte array sized exactly to contain the resulting ciphertext and AEAD tag.- Parameters:
key
- the key with which to encrypt the given plaintextnonce
- a nonce, which must be unique for the given keyassociatedData
- the associated data to use when calculating an AEAD tagplaintext
- the plaintext to encrypt- Returns:
- a new byte array containing the resulting ciphertext and AEAD tag
- See Also:
-
encrypt
int encrypt(Key key, long nonce, @Nullable byte[] associatedData, int aadOffset, int aadLength, byte[] plaintext, int plaintextOffset, int plaintextLength, byte[] ciphertext, int ciphertextOffset) throws ShortBufferException Encrypts the given plaintext using the given key, nonce, and associated data. 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:
key
- the key with which to encrypt the given plaintextnonce
- a nonce, which must be unique for the given keyassociatedData
- a byte array containing the associated data (if any) to be used when encrypting the given plaintext; may benull
aadOffset
- the position withinassociatedData
where the associated data starts; ignored ifassociatedData
isnull
aadLength
- the length of the associated data withinassociatedData
; ignored ifassociatedData
isnull
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 offset- See Also:
-
decrypt
default ByteBuffer decrypt(Key key, long nonce, @Nullable ByteBuffer associatedData, ByteBuffer ciphertext) throws AEADBadTagException Decrypts the given ciphertext and verifies its AEAD tag using the given key, nonce, and associated data. This method returns a new
ByteBuffer
sized exactly to contain the resulting plaintext. The returned buffer's position will be zero, and its limit and capacity will be equal to the plaintext length.All
ciphertext.remaining()
bytes starting atciphertext.position()
are processed. Upon return, the ciphertext buffer's position will be equal to its limit; its limit will not have changed. If associated data is provided, the same will be true of the associated data buffer.- Parameters:
key
- the key with which to decrypt the given ciphertextnonce
- a nonce, which must be unique for the given keyassociatedData
- the associated data to use when verifying the AEAD tag; may benull
ciphertext
- the ciphertext to decrypt- Returns:
- a
ByteBuffer
containing the resulting plaintext - Throws:
AEADBadTagException
- if the AEAD tag in the given ciphertext does not match the calculated valueIllegalArgumentException
- if the given ciphertext is too short to contain a valid AEAD tag- See Also:
-
decrypt
int decrypt(Key key, long nonce, @Nullable ByteBuffer associatedData, ByteBuffer ciphertext, ByteBuffer plaintext) throws AEADBadTagException, ShortBufferException Decrypts the given ciphertext and verifies its AEAD tag using the given key, nonce, and associated data. This method writes the resulting plaintext into the given
plaintext
buffer. Callers are responsible for ensuring that the given plaintext buffer has enough remaining capacity to hold the resulting plaintext.All
ciphertext.remaining()
bytes starting atciphertext.position()
are processed. Upon return, the ciphertext buffer's position will be equal to its limit; its limit will not have changed. If associated data is provided, the same will be true of the associated data buffer. The plaintext buffer's position will have advanced by n, where n is the value returned by this method; the plaintext buffer's limit will not have changed.- Parameters:
key
- the key with which to decrypt the given ciphertextnonce
- a nonce, which must be unique for the given keyassociatedData
- the associated data to use when verifying the AEAD tag; may benull
ciphertext
- the ciphertext to decryptplaintext
- the buffer into which to write the resulting plaintext- Returns:
- the number of bytes written into the
plaintext
buffer - Throws:
AEADBadTagException
- if the AEAD tag in the given ciphertext does not match the calculated valueIllegalArgumentException
- if the given ciphertext is too short to contain a valid AEAD tagShortBufferException
- if the given plaintext buffer does not have enough remaining capacity to hold the resulting plaintext- See Also:
-
decrypt
default byte[] decrypt(Key key, long nonce, @Nullable byte[] associatedData, byte[] ciphertext) throws AEADBadTagException Decrypts the given ciphertext and verifies its AEAD tag using the given key, nonce, and associated data. This method returns a new byte array sized exactly to contain the resulting plaintext.- Parameters:
key
- the key with which to decrypt the given ciphertextnonce
- a nonce, which must be unique for the given keyassociatedData
- the associated data to use when verifying the AEAD tag; may benull
ciphertext
- the ciphertext to decrypt- Returns:
- a byte array containing the resulting plaintext
- Throws:
AEADBadTagException
- if the AEAD tag in the given ciphertext does not match the calculated valueIllegalArgumentException
- if the given ciphertext is too short to contain a valid AEAD tag- See Also:
-
decrypt
int decrypt(Key key, long nonce, @Nullable byte[] associatedData, int aadOffset, int aadLength, byte[] ciphertext, int ciphertextOffset, int ciphertextLength, byte[] plaintext, int plaintextOffset) throws AEADBadTagException, ShortBufferException Decrypts the given ciphertext and verifies its AEAD tag. This writes the resulting plaintext into a provided byte array.
Note that
ciphertext
andplaintext
may refer to the same byte array, allowing for in-place decryption.- Parameters:
key
- the key with which to decrypt the given plaintextnonce
- a nonce, which must be unique for the given keyassociatedData
- a byte array containing the associated data (if any) to be used when verifying the AEAD tag for the given ciphertext; may benull
aadOffset
- the position withinassociatedData
where the associated data starts; ignored ifassociatedData
isnull
aadLength
- the length of the associated data withinassociatedData
; ignored ifassociatedData
isnull
ciphertext
- a byte array containing the ciphertext and AEAD tag to be decrypted and verifiedciphertextOffset
- the position withinciphertext
at which to begin reading the ciphertext and AEAD tagciphertextLength
- the length of the ciphertext and AEAD tag withinciphertext
plaintext
- a byte array into which to write the decrypted plaintextplaintextOffset
- the offset withinplaintext
where the plaintext begins- Returns:
- the number of bytes written to
plaintext
- Throws:
AEADBadTagException
- if the AEAD tag in the given ciphertext does not match the calculated valueShortBufferException
- ifplaintext
is not long enough (after its offset) to contain the resulting plaintextIllegalArgumentException
- if the given ciphertext is too short to contain a valid AEAD tag- See Also:
-
getCiphertextLength
default int getCiphertextLength(int plaintextLength) Returns the size of a buffer needed to hold the ciphertext produced by encrypting a plaintext of the given length (the length of the plaintext plus the length of an AEAD tag).- Parameters:
plaintextLength
- the length of a plaintext- Returns:
- the length of the ciphertext that would be produced by encrypting a plaintext of the given length
-
getPlaintextLength
default int getPlaintextLength(int ciphertextLength) Returns the size of a buffer needed to hold the plaintext produced by decrypting a ciphertext of the given length (the length of the ciphertext minus the length of the AEAD tag).- Parameters:
ciphertextLength
- the length of a ciphertext- Returns:
- the length of the plaintext that would be produced by decrypting a ciphertext of the given length
-
buildKey
Converts an array of bytes into aKey
instance suitable for use with this cipher.- Parameters:
keyBytes
- the raw bytes of the key- Returns:
- a
Key
suitable for use with this cipher
-
rekey
Generates a new pseudo-random key as a function of the given key.- Parameters:
key
- the key from which to derive a new key- Returns:
- a new pseudo-random key derived from the given key
-