java.lang.Object
net.i2p.data.SimpleDataStructure
net.i2p.data.SessionKey
- All Implemented Interfaces:
DataStructure
- Direct Known Subclasses:
SessionKeyAndNonce
Cryptographic session key for symmetric encryption in I2P.
SessionKey provides symmetric encryption capabilities for I2P communications:
- Fixed Size: 32-byte key suitable for modern symmetric algorithms
- Session-Based: Temporary keys for message encryption/decryption
- Algorithm Agnostic: Can be used with various symmetric ciphers
- Efficient Storage: Optimized for high-frequency operations
Key Generation:
- Random Creation: Use
I2PAppContext.keyGenerator().generateSessionKey() - Cryptographically Secure: Generated using cryptographically strong randomness
- Factory Methods: Static creation methods for convenience
- Invalid Key: Pre-defined all-zero key for special cases
Usage Patterns:
- Message Encryption: Encrypt I2NP messages and I2CP data
- Tunnel Encryption: Protect tunnel payload contents
- Session Establishment: Derive from key exchange protocols
- Temporary Storage: Short-lived keys for enhanced security
Security Considerations:
- Key Protection: Never expose session keys to unauthorized parties
- Secure Generation: Use cryptographically secure random sources
- Key Rotation: Change keys regularly to limit exposure
- Memory Security: Zeroize memory when keys are no longer needed
Performance Features:
- Prepared Keys: Cache for algorithm-specific prepared keys
- Efficient Storage: Optimized byte array representation
- Fast Operations: Quick access and comparison methods
- Memory Management: Careful allocation and cleanup
Integration:
- I2P Client: Used for end-to-end encryption in I2CP
- Router Operations: Tunnel and message encryption
- Garlic Encryption: Multi-layer message protection
- ElGamal Integration: Combined with asymmetric encryption for key exchange
Constants:
KEYSIZE_BYTES- Standard 32-byte key lengthINVALID_KEY- All-zero key for invalid/placeholder use
Best Practices:
- Unique Sessions: Use different keys for different communication sessions
- Limited Lifetime: Replace keys after reasonable time period
- Secure Disposal: Clear key data when session ends
- Algorithm Selection: Use appropriate symmetric cipher for key size
Field Summary
Modifier and TypeFieldDescriptionprivate ObjectThe key expanded for the cipher, built once per use.static final SessionKeyA key with all zeroes in the data.static final intKey size in bytes (32).Fields inherited from class SimpleDataStructure
_dataConstructor Summary
ConstructorsConstructorDescriptionCreate an empty key; the data must be set before use.SessionKey(byte[] data) Key data.Method Summary
All MethodsModifier and TypeMethodDescriptionRetrieve an internal representation of the session key, as known by the AES engine used.intlength()The legal length of the byte array in this data structure.voidsetData(byte[] data) Session key data.voidsetPreparedKey(Object obj) Set the internal prepared AES key representation.Methods inherited from class SimpleDataStructure
calculateHash, equals, fromBase64, fromByteArray, getData, hashCode, read, readBytes, toBase64, toByteArray, toString, writeBytesModifier and TypeMethodDescriptionSHA256 hash of the byte array, or null if data is null.booleanCompares the raw data only: two different SimpleDataStructure subclasses of the same length holding the same bytes are equal, e.g.voidfromBase64(String data) Data from the base64 string.voidfromByteArray(byte[] data) Does the same thing asSimpleDataStructure.setData(byte[])but null not allowed.byte[]getData()Data reference (not a copy).inthashCode()We assume the data has enough randomness in it, so use the first 4 bytes for speed.protected intread(InputStream in, byte[] target) Repeated reads until the buffer is full or IOException is thrown.voidreadBytes(InputStream in) Data read from the stream.toBase64()Renders the structure into modified base 64 notation.byte[]Same thing as getData().toString()Returns a string representation of this data structure.voidwriteBytes(OutputStream out) Writes the data to the output stream.
Field Details
_preparedKey
The key expanded for the cipher, built once per use.KEYSIZE_BYTES
INVALID_KEY
A key with all zeroes in the data.
Constructor Details
SessionKey
public SessionKey()Create an empty key; the data must be set before use.SessionKey
public SessionKey(byte[] data) Key data.- Parameters:
data- the key data
Method Details
length
public int length()The legal length of the byte array in this data structure.- Specified by:
lengthin classSimpleDataStructure- Returns:
- the legal length of the byte array
setData
public void setData(byte[] data) Session key data.- Overrides:
setDatain classSimpleDataStructure- Parameters:
data- 32 bytes, or null- Throws:
IllegalArgumentException- if data is not the legal number of bytes (but null is ok)RuntimeException- if data already set.
getPreparedKey
Retrieve an internal representation of the session key, as known by the AES engine used. This can be reused safely.- Returns:
- the prepared key
setPreparedKey
Set the internal prepared AES key representation.- Parameters:
obj- the prepared key object