java.lang.Object
net.i2p.router.crypto.ratchet.ECIESAEADEngine
ECIES+AEAD encryption engine providing New Session, New Session Reply, and Existing Session message handling
No, this does not extend ElGamalAESEngine or AEADEngine or CryptixAEADEngine.
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classPer-thread pool of payload block objects and the block list, to avoid allocation per outbound payload.private class//////////////////////////////////////////////////////Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadLocal<ECIESAEADEngine.BlockPool> Per-thread pool of ratchet payload blocks.private static final ThreadLocal<ChaChaPolyCipherState> Per-thread ChaCha-Poly cipher state, reused to avoid per-decrypt allocation.private final RouterContextThe router context.private final Elg2KeyFactoryKey factory for the X25519 ratchet.private final HKDFKey derivation function for the ratchet.private booleanTrue while the engine is running.private final LogLogger for ratchet decryption.private MLKEMKeyFactoryKey factory for ML-KEM 768, started on demand.private final MuxedEngineEngine for the legacy muxed encryption types.private final MuxedPQEngineEngine for the post-quantum muxed types.private static final booleanDebug flag to send an ACKREQ in every ES.private static final intFirst size bucket for the padding distribution.private static final intSecond size bucket for the padding distribution.private static final intThird size bucket for the padding distribution.private static final intLength of a block header, in bytes.private static final intSize of a datetime block, in bytes.private static final intOverhead of an ES-encrypted payload, in bytes.private static final StringHKDF info string for the attach payload key.private static final intLength of an X25519 key, in bytes.private static final intLength of a truncated MAC, in bytes.(package private) static final longMaximum age of a New Session request in ms.private static final longAn NS datetime may be up to 2 minutes in the future.private static final intMaximum padding added to an encrypted payload.private static final intSmallest payload any encryption type accepts.private static final intSmallest valid ES-encrypted payload, in bytes.(package private) static final intMinimum ML-KEM-1024 new-session message size, 1568 + 112 + 7 = 1687 bytes.(package private) static final intMinimum ML-KEM-512 new-session message size, 800 + 112 + 7 = 919 bytes.(package private) static final intMinimum ML-KEM-768 new-session message size, 1184 + 112 + 7 = 1303 bytes.private static final intSmallest valid NS-N encrypted payload, in bytes.(package private) static final intMinimum new-session message size in bytes (103).private static final intSmallest next session request with ML-KEM-1024: 1656 bytes.private static final intSmallest next session request with ML-KEM-512: 856 bytes.private static final intSmallest next session request with ML-KEM-768: 1176 bytes.private static final intSmallest valid NSR-encrypted payload, in bytes.private static final CloveSetEmpty clove set returned on failure.private static final GarlicClove[]Empty clove array for failed ElGamal decryptions.private static final intNext session overhead for the ML-KEM types: 112 bytes.private static final intOverhead of an NS-N encrypted payload, in bytes.private static final intOverhead of a NS-encrypted payload, in bytes.private static final intOverhead of an NSR-encrypted payload, in bytes.private static final byte[]All-zero public key for comparisons.private static final long[]Rate stat period for the decryption statistics.private static final intLength of a short ratchet tag, in bytes.private static final byte[]Shared empty array for zero-length values.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate static voidVerify the encryption type is supported by this engine.private byte[]createPayload(CloveSet cloves, long expiration, boolean ackreq, NextSessionKey nextKey1, NextSessionKey nextKey2, List<Integer> acksToSend, int overhead) Create the payload from the given cloves and optional blocks.private byte[]createPayload(CloveSet cloves, long expiration, int overhead) Create the payload, adding a DateTime block if expiration is greater than zero.decrypt(byte[] data, PrivateKey ecKey, PrivateKey pqKey, MuxedPQSKM keyManager) Try to decrypt the message with one or both of the given private keys.decrypt(byte[] data, PrivateKey elgKey, PrivateKey ecKey, MuxedSKM keyManager) Try to decrypt the message with one or both of the given private keys.decrypt(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) Decrypt the message using the given private key and using tags from the specified key manager.private booleandecryptAEADBlock(byte[] ad, byte[] encrypted, int offset, int encryptedLen, SessionKey key, long n) With optional AD.private CloveSetdecryptExistingSession(byte[] tag, byte[] data, SessionKeyAndNonce key, PrivateKey _targetPrivateKey, RatchetSKM keyManager) Scenario 3: Existing Session Message.(package private) CloveSetdecryptFast(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) NSR/ES only.private CloveSetdecryptNewSession(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) Scenario 1: New Session Messageprivate CloveSetdecryptNewSession_N(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) Scenario 1A: New Session Message Anonymous.private CloveSetdecryptNewSessionReply(byte[] tag, byte[] data, HandshakeState oldState, RatchetSKM keyManager) Scenario 2: New Session Reply Message(package private) CloveSetdecryptSlow(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) NS only.(package private) static final PrivateKeydoDH(PrivateKey privkey, PublicKey pubkey) Compute the Curve25519 Diffie-Hellman shared secret for the given key pair.byte[]Encrypt the data to the target using the given key from an anonymous source, for netdb lookups.byte[]encrypt(CloveSet cloves, PublicKey target, Destination to, PrivateKey priv, RatchetSKM keyManager, ReplyCallback callback) Encrypt the data to the target using the given key and deliver the specified tags No new session key This is the one called from GarlicMessageBuilder and is the primary entry point.byte[]encrypt(CloveSet cloves, SessionKey key, RatchetSessionTag tag) Create an Existing Session Message to an anonymous target using the given session key and tag, for netdb DSM/DSRM replies.private final byte[]encryptAEADBlock(byte[] ad, byte[] data, SessionKey key, long n) Encrypt the data with the given key and nonce.private byte[]encryptExistingSession(CloveSet cloves, PublicKey target, RatchetEntry re, ReplyCallback callback, RatchetSKM keyManager) Scenario 3: Existing Session Message.private byte[]encryptNewSession(CloveSet cloves, PublicKey target) Scenario 1A: New Session Message from an anonymous source.private byte[]encryptNewSession(CloveSet cloves, PublicKey target, Destination to, PrivateKey priv, RatchetSKM keyManager, ReplyCallback callback) Scenario 1: New Session Messageprivate byte[]encryptNewSessionReply(CloveSet cloves, PublicKey target, HandshakeState state, RatchetSessionTag currentTag, RatchetSKM keyManager, ReplyCallback callback) Scenario 2: New Session Reply MessagegetEncTypeSet(EncType type) The lease set key types that go with the given encryption type.getHybridKeyFactory(EncType type) The hybrid key factory for the given encryption type, or null if not a hybrid type.private static intgetMinNSRSize(EncType type) The minimum New Session Reply message size for the given encryption type.private static intgetMinNSSize(EncType type) The minimum New Session message size for the given encryption type.private static NoiseInit.PatternIDgetNoisePattern(EncType type) The Noise handshake pattern for the given encryption type.private voidsetResponseTimer(PublicKey from, List<GarlicClove> cloveSet, RatchetSKM skm) Schedules the delayed reply to an NSR message.private voidsetResponseTimerNS(PublicKey from, List<GarlicClove> cloveSet, RatchetSKM skm) Schedules the delayed reply to an NS message.voidshutdown()Cannot be restarted.voidstartup()May be called multiple times.private CloveSetx_decrypt(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) Decrypts a received message into cloves.private CloveSetx_decryptFast(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) NSR/ES only.private CloveSetx_decryptSlow(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) NS only.private byte[]x_encrypt(CloveSet cloves, PublicKey target, Destination to, PrivateKey priv, RatchetSKM keyManager, ReplyCallback callback) Encrypt the cloves to the target, selecting New Session, New Session Reply, or Existing Session encryption as appropriate.private CloveSetxx_decryptFast(byte[] tag, RatchetSessionTag st, SessionKeyAndNonce key, byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) NSR/ES only.
Field Details
_context
The router context._log
Logger for ratchet decryption._muxedEngine
Engine for the legacy muxed encryption types._muxedPQEngine
Engine for the post-quantum muxed types._hkdf
Key derivation function for the ratchet._edhThread
Key factory for the X25519 ratchet._mlkem768Thread
Key factory for ML-KEM 768, started on demand._isRunning
private volatile boolean _isRunningTrue while the engine is running.ZEROLEN
private static final byte[] ZEROLENShared empty array for zero-length values.TAGLEN
private static final int TAGLENLength of a short ratchet tag, in bytes.- See Also:
MACLEN
KEYLEN
BHLEN
DATETIME_SIZE
private static final int DATETIME_SIZESize of a datetime block, in bytes.- See Also:
NS_OVERHEAD
private static final int NS_OVERHEADOverhead of a NS-encrypted payload, in bytes.- See Also:
NS_N_OVERHEAD
private static final int NS_N_OVERHEADOverhead of an NS-N encrypted payload, in bytes.- See Also:
NSR_OVERHEAD
private static final int NSR_OVERHEADOverhead of an NSR-encrypted payload, in bytes.- See Also:
ES_OVERHEAD
private static final int ES_OVERHEADOverhead of an ES-encrypted payload, in bytes.- See Also:
MIN_NS_SIZE
static final int MIN_NS_SIZEMinimum new-session message size in bytes (103).- See Also:
MIN_NS_N_SIZE
private static final int MIN_NS_N_SIZESmallest valid NS-N encrypted payload, in bytes.- See Also:
MIN_NSR_SIZE
private static final int MIN_NSR_SIZESmallest valid NSR-encrypted payload, in bytes.- See Also:
MIN_ES_SIZE
private static final int MIN_ES_SIZESmallest valid ES-encrypted payload, in bytes.- See Also:
MIN_ENCRYPTED_SIZE
private static final int MIN_ENCRYPTED_SIZESmallest payload any encryption type accepts.- See Also:
NULLPK
private static final byte[] NULLPKAll-zero public key for comparisons.MAXPAD
private static final int MAXPADMaximum padding added to an encrypted payload.- See Also:
MAX_NS_AGE
static final long MAX_NS_AGEMaximum age of a New Session request in ms.- See Also:
MAX_NS_FUTURE
private static final long MAX_NS_FUTUREAn NS datetime may be up to 2 minutes in the future.- See Also:
ACKREQ_IN_ES
private static final boolean ACKREQ_IN_ESDebug flag to send an ACKREQ in every ES.- See Also:
NO_GARLIC
Empty clove array for failed ElGamal decryptions.NO_CLOVES
Empty clove set returned on failure.INFO_6
HKDF info string for the attach payload key.- See Also:
RATES
private static final long[] RATESRate stat period for the decryption statistics.NS_MLKEM_OVERHEAD
private static final int NS_MLKEM_OVERHEADNext session overhead for the ML-KEM types: 112 bytes.- See Also:
MIN_NS_MLKEM512_SIZE
static final int MIN_NS_MLKEM512_SIZEMinimum ML-KEM-512 new-session message size, 800 + 112 + 7 = 919 bytes.MIN_NS_MLKEM768_SIZE
static final int MIN_NS_MLKEM768_SIZEMinimum ML-KEM-768 new-session message size, 1184 + 112 + 7 = 1303 bytes.MIN_NS_MLKEM1024_SIZE
static final int MIN_NS_MLKEM1024_SIZEMinimum ML-KEM-1024 new-session message size, 1568 + 112 + 7 = 1687 bytes.MIN_NSR_MLKEM512_SIZE
private static final int MIN_NSR_MLKEM512_SIZESmallest next session request with ML-KEM-512: 856 bytes.MIN_NSR_MLKEM768_SIZE
private static final int MIN_NSR_MLKEM768_SIZESmallest next session request with ML-KEM-768: 1176 bytes.MIN_NSR_MLKEM1024_SIZE
private static final int MIN_NSR_MLKEM1024_SIZESmallest next session request with ML-KEM-1024: 1656 bytes._chachaLocal
Per-thread ChaCha-Poly cipher state, reused to avoid per-decrypt allocation.B1
private static final int B1First size bucket for the padding distribution.- See Also:
B2
private static final int B2Second size bucket for the padding distribution.- See Also:
B3
private static final int B3Third size bucket for the padding distribution.- See Also:
_blockPool
Per-thread pool of ratchet payload blocks.
Constructor Details
ECIESAEADEngine
Caller MUST callstartup()to get threaded generation. Will still work without, will just generate inline.startup() is called from
RatchetSKMconstructor so it's deferred until we need it.- Parameters:
ctx- the router context used to obtain the log, stats and sub-engines
Method Details
startup
public void startup()May be called multiple times.shutdown
public void shutdown()Cannot be restarted.decrypt
public CloveSet decrypt(byte[] data, PrivateKey elgKey, PrivateKey ecKey, MuxedSKM keyManager) throws DataFormatException Try to decrypt the message with one or both of the given private keys.- Parameters:
data- the encrypted CloveSet to decryptelgKey- must be ElG, non-nullecKey- must be EC, non-nullkeyManager- holds the session key material and ratchet preference- Returns:
- decrypted data or null on failure
- Throws:
DataFormatException- if the underlying garlic parse fails and cannot be recovered by trying the remaining crypto schemes
decrypt
public CloveSet decrypt(byte[] data, PrivateKey ecKey, PrivateKey pqKey, MuxedPQSKM keyManager) throws DataFormatException Try to decrypt the message with one or both of the given private keys.- Parameters:
data- the encrypted CloveSet to decryptecKey- must be EC, non-nullpqKey- must be PQ, non-nullkeyManager- holds the session key material and ratchet preference- Returns:
- decrypted data or null on failure
- Throws:
DataFormatException- if the data is malformed
decrypt
public CloveSet decrypt(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException Decrypt the message using the given private key and using tags from the specified key manager. This works according to the ECIES+AEAD algorithm in the data structure spec.Warning - use the correct SessionKeyManager. Clients should instantiate their own. Clients using
I2PAppContext.sessionKeyManager()may be correlated with the router, unless you are careful to use different keys.- Parameters:
data- the encrypted CloveSet to decrypttargetPrivateKey- private key matching the encryption type of the datakeyManager- holds the session tags and key material used to decrypt- Returns:
- decrypted data or null on failure
- Throws:
DataFormatException- if the underlying garlic parse fails and cannot be recovered by trying the remaining crypto schemes
x_decrypt
private CloveSet x_decrypt(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException Decrypts a received message into cloves.- Parameters:
data- the message bytestargetPrivateKey- the key to decrypt withkeyManager- the manager of the session keys- Returns:
- the decrypted clove set
- Throws:
DataFormatException- if the message is invalid
decryptFast
CloveSet decryptFast(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException NSR/ES only. For MuxedEngine use only.- Parameters:
data- the encrypted CloveSet to decrypttargetPrivateKey- private key matching the encryption type of the datakeyManager- holds the session tags and key material used to decrypt- Returns:
- the decrypted cloves, null if no key was found for the tag, or an empty set if the decrypt or the payload parse failed
- Throws:
DataFormatException- if the payload cannot be parsed as a garlic clove set
x_decryptFast
private CloveSet x_decryptFast(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException NSR/ES only.- Parameters:
data- the encrypted NewSessionReply or ExistingSession messagetargetPrivateKey- our private key matching the message's encryption typekeyManager- the key manager whose session tags are searched for the tag- Returns:
- the decrypted cloves, or null if the 8-byte tag is unknown or the data is below the minimum size
- Throws:
DataFormatException- if the payload cannot be parsed as a garlic clove set
xx_decryptFast
private CloveSet xx_decryptFast(byte[] tag, RatchetSessionTag st, SessionKeyAndNonce key, byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException NSR/ES only.- Parameters:
tag- the 8-byte session tag copied out of the start of datast- the session tag object built from those 8 byteskey- non-null key and nonce the key manager holds for that tagdata- the encrypted NewSessionReply or ExistingSession messagetargetPrivateKey- our private key matching the message's encryption typekeyManager- holds the session tags and key material used to decrypt- Returns:
- the decrypted cloves, or null if the reply is too short or the MAC check fails
- Throws:
DataFormatException- if the payload cannot be parsed as a garlic clove set
decryptSlow
CloveSet decryptSlow(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException NS only. For MuxedEngine use only.- Parameters:
data- the encrypted CloveSet to decrypttargetPrivateKey- private key matching the encryption type of the datakeyManager- holds the session tags and key material used to decrypt- Returns:
- decrypted data or null on failure
- Throws:
DataFormatException- if the payload cannot be parsed as a garlic clove set
x_decryptSlow
private CloveSet x_decryptSlow(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException NS only.- Parameters:
data- the encrypted NewSession or NewSession/LeaseSet messagetargetPrivateKey- our private key matching the message's encryption typekeyManager- holds the session tags and key material; a null destination marks the router variant, NewSession_N- Returns:
- the decrypted cloves, or null if the data is below the minimum size for the key type or the decrypt fails
- Throws:
DataFormatException- if the payload cannot be parsed as a garlic clove set
checkType
Verify the encryption type is supported by this engine.- Parameters:
type- the encryption type this engine must be able to handle- Throws:
IllegalArgumentException- if type is not one this engine handles
getNoisePattern
The Noise handshake pattern for the given encryption type.- Parameters:
type- the encryption type to select a handshake pattern for- Returns:
- IK for X25519, or IKHFS_n for the matching ML-KEM hybrid type
- Throws:
IllegalArgumentException- if no pattern is defined for the type
getHybridKeyFactory
The hybrid key factory for the given encryption type, or null if not a hybrid type.- Parameters:
type- the hybrid encryption type to get a key factory for- Returns:
- the hybrid key factory
getMinNSSize
The minimum New Session message size for the given encryption type.- Parameters:
type- the encryption type whose New Session minimum is wanted- Returns:
- the minimum new-session message size in bytes for this type
- Throws:
IllegalArgumentException- if no minimum is defined for the type
getMinNSRSize
The minimum New Session Reply message size for the given encryption type.- Parameters:
type- the encryption type whose reply minimum is wanted- Returns:
- the minimum new-session-reply message size in bytes for this type
- Throws:
IllegalArgumentException- if no minimum is defined for the type
getEncTypeSet
The lease set key types that go with the given encryption type.- Parameters:
type- the encryption type to map to a lease set key set- Returns:
- the
LeaseSetKeysset for that type, one of SET_EC, SET_PQ1, SET_PQ2 or SET_PQ3 - Throws:
IllegalArgumentException- if no set is defined for the type
decryptNewSession
private CloveSet decryptNewSession(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException Scenario 1: New Session MessageBegin with 80 bytes, ECIES encrypted, containing:
- 32 byte Elligator2 key - 32 byte static key - 16 byte MAC
And then the data:- payload (7 bytes minimum for DateTime block) - 16 byte MAC
- Parameters:
data- 103 bytes minimumtargetPrivateKey- our private key matching the message's encryption typekeyManager- holds the ephemeral-key replay cache and receives the new session on success- Returns:
- the decrypted cloves, null if the Elligator2 decode or the handshake fails, or an empty set for a replayed key or a payload with no garlic
- Throws:
DataFormatException- if the decrypted payload blocks cannot be parsed
decryptNewSession_N
private CloveSet decryptNewSession_N(byte[] data, PrivateKey targetPrivateKey, RatchetSKM keyManager) throws DataFormatException Scenario 1A: New Session Message Anonymous.- 32 byte ephemeral key (NOT Elligator2) - payload (7 bytes minimum for DateTime block) - 16 byte MAC
- Parameters:
data- 55 bytes minimumtargetPrivateKey- our private key matching the message's encryption typekeyManager- holds the ephemeral-key replay cache checked before the DH work- Returns:
- the decrypted cloves, null if the key or the handshake check fails, or an empty set for a replayed key or a payload with no garlic
- Throws:
DataFormatException- if the decrypted payload blocks cannot be parsed
decryptNewSessionReply
private CloveSet decryptNewSessionReply(byte[] tag, byte[] data, HandshakeState oldState, RatchetSKM keyManager) throws DataFormatException Scenario 2: New Session Reply MessageBegin with 56 bytes, containing:
- 8 byte SessionTag - 32 byte Elligator2 key - 16 byte MAC
And then the data:- payload - 16 byte MAC
- Parameters:
tag- 8 bytes, same as first 8 bytes of datadata- 72 bytes minimumoldState- will be cloned here, then destroyed on failurekeyManager- the session key manager for this handshake- Returns:
- the decrypted cloves, null if the state clone, the Elligator2 decode or part 1 fails, or an empty set if the payload MAC fails or the payload has no garlic block
- Throws:
DataFormatException- if the decrypted payload blocks cannot be parsed
decryptExistingSession
private CloveSet decryptExistingSession(byte[] tag, byte[] data, SessionKeyAndNonce key, PrivateKey _targetPrivateKey, RatchetSKM keyManager) throws DataFormatException Scenario 3: Existing Session Message.- 8 byte SessionTag - payload - 16 byte MAC
If anything doesn't match up in decryption, it returns null
- Parameters:
tag- 8 bytes for ad, same as first 8 bytes of datadata- 24 bytes minimum, first 8 bytes will be skippedkey- the session key and nonce the key manager holds for that tag_targetPrivateKey- our private key, unused as the session key is set upkeyManager- for ack callbacks- Returns:
- the decrypted cloves, null if the MAC check fails, or an empty set if the payload is empty or carries no garlic block
- Throws:
DataFormatException- if the decrypted payload blocks cannot be parsed
decryptAEADBlock
private boolean decryptAEADBlock(byte[] ad, byte[] encrypted, int offset, int encryptedLen, SessionKey key, long n) With optional AD. Decrypts IN PLACE. Decrypted data will be at encrypted[offset:offset + len - 16].- Parameters:
ad- optional associated data, may be nullencrypted- the ciphertext, overwritten in place with the plaintextoffset- where the ciphertext starts in encryptedencryptedLen- bytes to decrypt at offset, including the 16-byte MACkey- the 32-byte ChaCha20-Poly1305 key to decrypt withn- the nonce for this message, taken from the session key's counter- Returns:
- true if the MAC verified and the data was decrypted in place
encrypt
public byte[] encrypt(CloveSet cloves, PublicKey target, Destination to, PrivateKey priv, RatchetSKM keyManager, ReplyCallback callback) Encrypt the data to the target using the given key and deliver the specified tags No new session key This is the one called from GarlicMessageBuilder and is the primary entry point.- Parameters:
cloves- the cloves to encrypt and sendtarget- public key to which the data should be encrypted.to- destination to encrypt for, required when priv is non-nullpriv- local private key to encrypt with, from the leaseset may be null for anonymous (N-in-IK)keyManager- session key manager, required when priv is non-nullcallback- may be null, if non-null an ack will be requested (except NS/NSR), not used when priv is null- Returns:
- encrypted data or null on failure
x_encrypt
private byte[] x_encrypt(CloveSet cloves, PublicKey target, Destination to, PrivateKey priv, RatchetSKM keyManager, ReplyCallback callback) Encrypt the cloves to the target, selecting New Session, New Session Reply, or Existing Session encryption as appropriate.- Parameters:
cloves- the cloves to encrypt and sendtarget- the peer's static public key to encrypt to, never the all-zero keyto- destination to encrypt for, required when priv is non-nullpriv- local private key to encrypt with, from the leaseset may be null for anonymous (N-in-IK)keyManager- session key manager, required when priv is non-nullcallback- may be null, not used when priv is null- Returns:
- the encrypted message, or null if the target is the all-zero key or the selected scheme fails
encryptNewSession
private byte[] encryptNewSession(CloveSet cloves, PublicKey target, Destination to, PrivateKey priv, RatchetSKM keyManager, ReplyCallback callback) Scenario 1: New Session MessageBegin with 80 bytes, ECIES encrypted, containing:
- 32 byte Elligator2 key - 32 byte static key - 16 byte MAC
And then the data:- payload - 16 byte MAC
- Parameters:
cloves- the cloves to encrypt and sendtarget- the peer's static public key to encrypt toto- destination to encrypt for, required when priv is non-nullpriv- local private key to encrypt with, from the leaseset may be null for anonymous (N-in-IK)keyManager- session key manager, required when priv is non-nullcallback- may be null, not used when priv is null- Returns:
- the encrypted NewSession message, or null if the handshake or the Elligator2 encoding fails
encryptNewSession
Scenario 1A: New Session Message from an anonymous source.- 32 byte ephemeral key (NOT Elligator2) - payload - 16 byte MAC
- Parameters:
cloves- the cloves to encrypt and sendtarget- the peer's static public key to encrypt to- Returns:
- the encrypted anonymous NewSession message, or null if the handshake fails
encryptNewSessionReply
private byte[] encryptNewSessionReply(CloveSet cloves, PublicKey target, HandshakeState state, RatchetSessionTag currentTag, RatchetSKM keyManager, ReplyCallback callback) Scenario 2: New Session Reply MessageBegin with 56 bytes, containing:
- 8 byte SessionTag - 32 byte Elligator2 key - 16 byte MAC
And then the data:- payload - 16 byte MAC
- Parameters:
cloves- the cloves to encrypt and sendtarget- the peer's static public key we are replying tostate- must have already been clonedcurrentTag- the 8-byte session tag to prefix the message with, mixed into the handshake hashkeyManager- updated with the split keys and the callback on successcallback- may be null- Returns:
- the encrypted NewSessionReply, or null if either part of the encrypt fails
encryptExistingSession
private byte[] encryptExistingSession(CloveSet cloves, PublicKey target, RatchetEntry re, ReplyCallback callback, RatchetSKM keyManager) Scenario 3: Existing Session Message.- 8 byte SessionTag - payload - 16 byte MAC
- Parameters:
cloves- the cloves to encrypt and sendtarget- only used if callback is non-null to register itre- the tag, key and pending next keys and acks taken from the key managercallback- may be null, if non-null an ack is requested and the callback registered for this tagkeyManager- registers the callback for this tag when callback is non-null- Returns:
- the encrypted ExistingSession message, or null if the AEAD encrypt fails
encrypt
Create an Existing Session Message to an anonymous target using the given session key and tag, for netdb DSM/DSRM replies. Called from MessageWrapper.No datetime, no next key, no acks, no ack requests. n=0, ad = the 8-byte session tag.
- 8 byte SessionTag - payload - 16 byte MAC
- Parameters:
cloves- the cloves to encrypt and sendkey- existing session key for the ES schemetag- session tag to prefix the block with, naming the key to the receiver- Returns:
- encrypted data or null on failure
encrypt
Encrypt the data to the target using the given key from an anonymous source, for netdb lookups. Called from MessageWrapper.- Parameters:
cloves- the cloves to encrypt and sendtarget- public key to which the data should be encrypted.- Returns:
- encrypted data or null on failure
encryptAEADBlock
Encrypt the data with the given key and nonce.- Parameters:
ad- may be nulldata- the payload to encryptkey- the 32-byte ChaCha20-Poly1305 session keyn- the nonce for this message, 0 for the anonymous netdb replies- Returns:
- a buffer with room for the ad, then the ciphertext and the 16-byte MAC, or null if the cipher fails
doDH
Compute the Curve25519 Diffie-Hellman shared secret for the given key pair.- Parameters:
privkey- local ECIES_X25519 private keypubkey- remote ECIES_X25519 public key- Returns:
- the 32 byte shared secret wrapped as an ECIES_X25519 private key
createPayload
Create the payload, adding a DateTime block if expiration is greater than zero.- Parameters:
cloves- the garlic cloves to include as payload blocksexpiration- if greater than zero, add a DateTime blockoverhead- bytes to be added later, to assist in padding calculation- Returns:
- the padded payload blocks, ready for the handshake or AEAD layer
createPayload
private byte[] createPayload(CloveSet cloves, long expiration, boolean ackreq, NextSessionKey nextKey1, NextSessionKey nextKey2, List<Integer> acksToSend, int overhead) Create the payload from the given cloves and optional blocks.- Parameters:
cloves- the garlic cloves to include as payload blocksexpiration- if greater than zero, add a DateTime blockackreq- to request an ack, must be false for NS/NSRnextKey1- next session key block to include, may be nullnextKey2- second next session key block, may be nullacksToSend- acknowledgement ids to send, may be nulloverhead- bytes to be added later, to assist in padding calculation- Returns:
- the payload blocks, padded to stay under the tunnel message size boundaries of 944, 1936 or 2932 bytes
setResponseTimerNS
Schedules the delayed reply to an NS message.- Parameters:
from- the sender's ratchet keycloveSet- the decrypted cloves to reply withskm- the session key manager for the sender
setResponseTimer
Schedules the delayed reply to an NSR message.- Parameters:
from- the sender's ratchet keycloveSet- the decrypted cloves to reply withskm- the session key manager for the sender