java.lang.Object
com.southernstorm.noise.protocol.ChaChaPolyCipherState
- All Implemented Interfaces:
CipherState, Destroyable, Cloneable
Implements the ChaChaPoly cipher for Noise.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final booleanSwitch for the key-dump debug logs.private booleanTrue 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 longThe nonce counter.private final int[]Output words of the ChaCha20 block.private final Poly1305The Poly1305 MAC calculator.private final byte[]The one-time key for the MAC.Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs a new cipher state for the "ChaChaPoly" algorithm.protectedCopy constructor for cloning.Method Summary
Modifier and TypeMethodDescriptionclone()I2PintdecryptWithAd(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.intdecryptWithAd(byte[] ad, int adOffset, int adLength, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length) I2Pvoiddestroy()Destroys all sensitive state in the current object.private voidencrypt(byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) Encrypts or decrypts a buffer of bytes for the active packet.intencryptWithAd(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.intencryptWithAd(byte[] ad, int adOffset, int adLength, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) I2Pprivate voidfinish(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.intGets the length of the key values for this cipher.intGets the length of the MAC values for this cipher.booleanhasKey()Determine if this cipher object has been configured with a key.voidinitializeKey(byte[] key, int offset) Initializes the key on this cipher object.private static voidputLittleEndian64(byte[] output, int offset, long value) Puts a 64-bit integer into a buffer in little-endian order.voidsetNonce(long nonce) Sets the nonce value.private voidsetup(byte[] ad, int off, int len) Set up to encrypt or decrypt the next packet.toString()I2P debug
Field Details
poly
The Poly1305 MAC calculator.input
private final int[] inputScratch words for the ChaCha20 block.output
private final int[] outputOutput words of the ChaCha20 block.polyKey
private final byte[] polyKeyThe one-time key for the MAC.n
private long nThe nonce counter.haskey
private boolean haskeyTrue once a key has been set.initialKey
private byte[] initialKeyCopy of the initial key, debug only.DEBUG
Constructor Details
ChaChaPolyCipherState
public ChaChaPolyCipherState()Constructs a new cipher state for the "ChaChaPoly" algorithm.ChaChaPolyCipherState
Copy constructor for cloning.- Parameters:
o- the cipher state to copy- Throws:
CloneNotSupportedException- if cloning is not supported
Method Details
destroy
public void destroy()Description copied from interface:DestroyableDestroys all sensitive state in the current object.- Specified by:
destroyin interfaceDestroyable
getCipherName
Description copied from interface:CipherStateGets the Noise protocol name for this cipher.- Specified by:
getCipherNamein interfaceCipherState- Returns:
- The cipher name.
getKeyLength
public int getKeyLength()Description copied from interface:CipherStateGets the length of the key values for this cipher.- Specified by:
getKeyLengthin interfaceCipherState- Returns:
- The length of the key in bytes; usually 32.
getMACLength
public int getMACLength()Description copied from interface:CipherStateGets the length of the MAC values for this cipher.- Specified by:
getMACLengthin interfaceCipherState- 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:CipherStateInitializes the key on this cipher object.- Specified by:
initializeKeyin interfaceCipherState- 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:CipherStateDetermine if this cipher object has been configured with a key.- Specified by:
hasKeyin interfaceCipherState- 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 startslen- 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:CipherStateEncrypts a plaintext buffer using the cipher and a block of associated data.- Specified by:
encryptWithAdin interfaceCipherState- 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:
encryptWithAdin interfaceCipherState- 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:CipherStateDecrypts a ciphertext buffer using the cipher and a block of associated data.- Specified by:
decryptWithAdin interfaceCipherState- 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:
decryptWithAdin interfaceCipherState- 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
Description copied from interface:CipherStateCreates a new instance of this cipher and initializes it with a key.- Specified by:
forkin interfaceCipherState- 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:CipherStateSets the nonce value.- Specified by:
setNoncein interfaceCipherState- 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:
clonein interfaceCipherState- Overrides:
clonein classObject- Returns:
- A clone of this CipherState.
- Throws:
CloneNotSupportedException- If cloning is not supported.
toString