java.lang.Object
com.southernstorm.noise.protocol.SymmetricState
- All Implemented Interfaces:
Destroyable, Cloneable
Symmetric state for helping manage a Noise handshake.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CipherStateThe cipher state, unused until the handshake ends.private final byte[]The chaining key.private final byte[]The handshake hash.private final MessageDigestThe hash used for the handshake.private booleanTrue once the state's keys have been wiped.private final byte[]The handshake hash before the last mix.Constructor Summary
ConstructorsModifierConstructorDescriptionSymmetricState(NoiseInit.PatternID patternId) Constructs a new symmetric state object.protectedCopy constructor for cloningMethod Summary
Modifier and TypeMethodDescriptionclone()I2PintdecryptAndHash(byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length) Decrypts a block of ciphertext and mixes it into the handshake hash.intdecryptOnly(byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length) I2P - Same as decryptAndHash() but without the post-mixHash(), for N only.voiddestroy()Destroys all sensitive state in the current object.intencryptAndHash(byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) Encrypts a block of plaintext and mixes the ciphertext into the handshake hash.intencryptOnly(byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) I2P - Same as encryptAndHash() but without the post-mixHash(), for N only.byte[]I2P for getting chaining key for siphash calculationbyte[]Gets the current value of the handshake hash.intGets the length of MAC values in the current state.Gets the name of the Noise protocol.private voidhashOne(byte[] data, int offset, int length, byte[] output, int outputOffset, int outputLength) Hashes a single data buffer.private voidhashTwo(byte[] data1, int offset1, int length1, byte[] data2, int offset2, int length2, byte[] output, int outputOffset, int outputLength) Hashes two data buffers.private voidhkdf(byte[] key, int keyOffset, int keyLength, byte[] data, int dataOffset, int dataLength, byte[] output1, int output1Offset, int output1Length, byte[] output2, int output2Offset, int output2Length) Computes a HKDF value.private voidhmac(byte[] key, int keyOffset, int keyLength, byte[] data, int dataOffset, int dataLength, byte[] output, int outputOffset, int outputLength) Computes a HMAC value using key and data values.voidmixHash(byte[] data, int offset, int length) Mixes data into the handshake hash.voidmixKey(byte[] data, int offset, int length) Mixes data into the chaining key.voidmixPreSharedKey(byte[] key) Mixes a pre-shared key into the chaining key and handshake hash.voidmixPublicKey(DHState dh) Mixes a pre-supplied public key into the handshake hash.voidMixes a pre-supplied public key into the chaining key.split()Splits the symmetric state into two ciphers for session encryption.split(byte[] secondaryKey, int offset, int length) Splits the symmetric state into two ciphers for session encryption, and optionally mixes in a secondary symmetric key.toString()I2P debug
Field Details
cipher
The cipher state, unused until the handshake ends.hash
The hash used for the handshake.ck
private final byte[] ckThe chaining key.h
private final byte[] hThe handshake hash.prev_h
private final byte[] prev_hThe handshake hash before the last mix.isDestroyed
private boolean isDestroyedTrue once the state's keys have been wiped.
Constructor Details
SymmetricState
Constructs a new symmetric state object.- Parameters:
patternId- the handshake pattern naming the cipher, hash and initial chaining key to use- Throws:
NoSuchAlgorithmException- The cipher or hash algorithm in the protocol name is not supported.
SymmetricState
Copy constructor for cloning- Parameters:
o- the state whose cipher, hash, chaining key and handshake hash are copied- Throws:
CloneNotSupportedException- if the cipher state cannot be cloned
Method Details
getProtocolName
getMACLength
public int getMACLength()Gets the length of MAC values in the current state.- Returns:
- The length of the MAC value for the underlying cipher or zero if the cipher has not yet been initialized with a key.
mixKey
public void mixKey(byte[] data, int offset, int length) Mixes data into the chaining key.- Parameters:
data- The buffer containing the data to mix in.offset- The offset of the first data byte to mix in.length- The number of bytes to mix in.
mixHash
public void mixHash(byte[] data, int offset, int length) Mixes data into the handshake hash.- Parameters:
data- The buffer containing the data to mix in.offset- The offset of the first data byte to mix in.length- The number of bytes to mix in.
mixPublicKey
Mixes a pre-supplied public key into the handshake hash.- Parameters:
dh- The object containing the public key.
mixPublicKeyIntoCK
Mixes a pre-supplied public key into the chaining key.- Parameters:
dh- The object containing the public key.
encryptAndHash
public int encryptAndHash(byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) throws ShortBufferException Encrypts a block of plaintext and mixes the ciphertext into the handshake hash.- Parameters:
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.
- Throws:
ShortBufferException- There is not enough space in the ciphertext buffer for the encrypted data plus MAC value. The plaintext and ciphertext buffers can be the same for in-place encryption. In that case, plaintextOffset must be identical to ciphertextOffset. There must be enough space in the ciphertext buffer to accomodate length + getMACLength() bytes of data starting at ciphertextOffset.
encryptOnly
public int encryptOnly(byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) throws ShortBufferException I2P - Same as encryptAndHash() but without the post-mixHash(), for N only.- Parameters:
plaintext- The buffer containing the plaintext to encrypt.plaintextOffset- The offset within the plaintext buffer of the first byte of plaintext data.ciphertext- The buffer to place the ciphertext in, which may be the plaintext buffer.ciphertextOffset- The offset within the ciphertext buffer of the first byte written.length- The length of the plaintext.- Returns:
- The length of the ciphertext written, including the MAC tag.
- Throws:
ShortBufferException- There is not enough space in the ciphertext buffer for the encrypted data plus MAC value.
decryptAndHash
public int decryptAndHash(byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length) throws ShortBufferException, BadPaddingException Decrypts a block of ciphertext and mixes it into the handshake hash.- Parameters:
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- There is not enough space in the plaintext buffer for the decrypted data.BadPaddingException- The MAC value failed to verify. The plaintext and ciphertext buffers can be the same for in-place decryption. In that case, ciphertextOffset must be identical to plaintextOffset.
decryptOnly
public int decryptOnly(byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length) throws ShortBufferException, BadPaddingException I2P - Same as decryptAndHash() but without the post-mixHash(), for N only.- Parameters:
ciphertext- The buffer containing the ciphertext and MAC tag to decrypt.ciphertextOffset- The offset within the ciphertext buffer of the first byte of ciphertext data.plaintext- The buffer to place the plaintext in, which may be the ciphertext buffer.plaintextOffset- The offset within the plaintext buffer of the first byte written.length- The length of the incoming ciphertext plus the MAC tag.- Returns:
- The length of the plaintext written, with the MAC tag stripped off.
- Throws:
ShortBufferException- There is not enough space in the plaintext buffer for the decrypted data.BadPaddingException- The MAC value failed to verify.
split
Splits the symmetric state into two ciphers for session encryption.- Returns:
- The pair of ciphers for sending and receiving.
split
Splits the symmetric state into two ciphers for session encryption, and optionally mixes in a secondary symmetric key.- Parameters:
secondaryKey- The buffer containing the secondary key.offset- The offset of the first secondary key byte.length- The length of the secondary key in bytes, which must be either 0 or 32.- Returns:
- The pair of ciphers for sending and receiving.
- Throws:
IllegalArgumentException- The length is not 0 or 32.
getHandshakeHash
public byte[] getHandshakeHash()Gets the current value of the handshake hash.- Returns:
- The handshake hash. This must not be modified by the caller. The handshake hash value is only of use to the application after split() has been called.
destroy
public void destroy()Description copied from interface:DestroyableDestroys all sensitive state in the current object.- Specified by:
destroyin interfaceDestroyable
hashOne
private void hashOne(byte[] data, int offset, int length, byte[] output, int outputOffset, int outputLength) Hashes a single data buffer.- Parameters:
data- The buffer containing the data to hash.offset- Offset into the data buffer of the first byte to hash.length- Length of the data to be hashed.output- The buffer to receive the output hash value.outputOffset- Offset into the output buffer to place the hash value.outputLength- The length of the hash output. The output buffer can be the same as the input data buffer.
hashTwo
private void hashTwo(byte[] data1, int offset1, int length1, byte[] data2, int offset2, int length2, byte[] output, int outputOffset, int outputLength) Hashes two data buffers.- Parameters:
data1- The buffer containing the first data to hash.offset1- Offset into the first data buffer of the first byte to hash.length1- Length of the first data to be hashed.data2- The buffer containing the second data to hash.offset2- Offset into the second data buffer of the first byte to hash.length2- Length of the second data to be hashed.output- The buffer to receive the output hash value.outputOffset- Offset into the output buffer to place the hash value.outputLength- The length of the hash output. The output buffer can be same as either of the input buffers.
hmac
private void hmac(byte[] key, int keyOffset, int keyLength, byte[] data, int dataOffset, int dataLength, byte[] output, int outputOffset, int outputLength) Computes a HMAC value using key and data values.- Parameters:
key- The buffer that contains the key.keyOffset- The offset of the key in the key buffer.keyLength- The length of the key in bytes.data- The buffer that contains the data.dataOffset- The offset of the data in the data buffer.dataLength- The length of the data in bytes.output- The output buffer to place the HMAC value in.outputOffset- Offset into the output buffer for the HMAC value.outputLength- The length of the HMAC output.
hkdf
private void hkdf(byte[] key, int keyOffset, int keyLength, byte[] data, int dataOffset, int dataLength, byte[] output1, int output1Offset, int output1Length, byte[] output2, int output2Offset, int output2Length) Computes a HKDF value.- Parameters:
key- The buffer that contains the key.keyOffset- The offset of the key in the key buffer.keyLength- The length of the key in bytes.data- The buffer that contains the data.dataOffset- The offset of the data in the data buffer.dataLength- The length of the data in bytes.output1- The first output buffer.output1Offset- Offset into the first output buffer.output1Length- Length of the first output which can be less than the hash length.output2- The second output buffer.output2Offset- Offset into the second output buffer.output2Length- Length of the second output which can be less than the hash length.
getChainingKey
public byte[] getChainingKey()I2P for getting chaining key for siphash calculation- Returns:
- a copy of the current chaining key, so callers cannot modify the state
clone
I2P- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
toString