All Implemented Interfaces:
DataStructure
Direct Known Subclasses:
SessionKeyAndNonce

public class SessionKeyextends SimpleDataStructure
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:

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 Details

    • _preparedKey

      private Object _preparedKey
      The key expanded for the cipher, built once per use.
    • KEYSIZE_BYTES

      public static final int KEYSIZE_BYTES
      Key size in bytes (32).
      See Also:
    • INVALID_KEY

      public static final SessionKey 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:
      length in class SimpleDataStructure
      Returns:
      the legal length of the byte array
    • setData

      public void setData(byte[] data)
      Session key data.
      Overrides:
      setData in class SimpleDataStructure
      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

      public Object 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

      public void setPreparedKey(Object obj)
      Set the internal prepared AES key representation.
      Parameters:
      obj - the prepared key object