java.lang.Object
net.i2p.router.transport.udp.SSU2Header

final class SSU2Headerextends Object
Encrypt/decrypt headers.
  • Field Details

    • HEADER_PROT_DATA

      public static final byte[] HEADER_PROT_DATA
      8 bytes of zeros.
    • CHACHA_IV_0

      public static final byte[] CHACHA_IV_0
      12 bytes of zeros.
    • SCRATCH

      private static final ThreadLocal<SSU2Header.Scratch> SCRATCH
      Per-thread buffers for building headers.
  • Constructor Details

    • SSU2Header

      private SSU2Header()
      Utility class, not intended for instantiation.
  • Method Details

    • trialDecryptHandshakeHeader

      public static SSU2Header.Header trialDecryptHandshakeHeader(UDPPacket packet, byte[] key1, byte[] key2)
      Session Request and Session Created only. 64 bytes. Packet is unmodified.

      The returned Header is scratch, valid only until the next trial decrypt on this thread. It must not be retained.

      Parameters:
      packet - must be 88 bytes min
      key1 - the 32-byte ChaCha20 key protecting header bytes 0-7
      key2 - the 32-byte ChaCha20 key protecting header bytes 8-15
      Returns:
      64 byte header, null if data too short
    • trialDecryptLongHeader

      public static SSU2Header.Header trialDecryptLongHeader(UDPPacket packet, byte[] key1, byte[] key2)
      Retry, Token Request, Peer Test only. 32 bytes. Packet is unmodified.

      The returned Header is scratch, valid only until the next trial decrypt on this thread. It must not be retained.

      Parameters:
      packet - must be 56 bytes min
      key1 - the 32-byte ChaCha20 key protecting header bytes 0-7
      key2 - the 32-byte ChaCha20 key protecting header bytes 8-15
      Returns:
      32 byte header, null if data too short
    • trialDecryptShortHeader

      public static SSU2Header.Header trialDecryptShortHeader(UDPPacket packet, byte[] key1, byte[] key2)
      Session Confirmed and data phase. 16 bytes. Packet is unmodified.

      The returned Header is scratch, valid only until the next trial decrypt on this thread. It must not be retained.

      Parameters:
      packet - must be 40 bytes min
      key1 - the 32-byte ChaCha20 key protecting header bytes 0-7
      key2 - the 32-byte ChaCha20 key protecting header bytes 8-15
      Returns:
      16 byte header, null if data too short, must be 40 bytes min
    • decryptDestConnID

      public static long decryptDestConnID(DatagramPacket pkt, byte[] key1)
      Decrypt bytes 0-7 in header. Packet is unmodified.
      Parameters:
      pkt - must be 8 bytes min
      key1 - the 32-byte ChaCha20 key protecting header bytes 0-7
      Returns:
      the destination connection ID
      Throws:
      IndexOutOfBoundsException - if too short
    • acceptTrialDecrypt

      public static void acceptTrialDecrypt(UDPPacket packet, SSU2Header.Header header)
      Copy the header back to the packet. Cannot be undone.
      Parameters:
      packet - the packet the trial-decrypted header came from
      header - the scratch Header to copy back over the packet's header
    • decryptHandshakeHeader

      private static void decryptHandshakeHeader(DatagramPacket pkt, byte[] key1, byte[] key2, SSU2Header.Header header, SSU2Header.Scratch scratch)
      Decrypt bytes 0-63 from pkt to header First 64 bytes Packet is unmodified.
      Parameters:
      pkt - the received packet, 64 bytes min
      key1 - the 32-byte ChaCha20 key for the short header
      key2 - the 32-byte ChaCha20 key for the remaining bytes
      header - the scratch Header to write the decrypted bytes to
      scratch - per-thread scratch buffers for the header protection
    • decryptLongHeader

      private static void decryptLongHeader(DatagramPacket pkt, byte[] key1, byte[] key2, SSU2Header.Header header, SSU2Header.Scratch scratch)
      Decrypt bytes 0-31 from pkt to header. First 32 bytes Packet is unmodified.
      Parameters:
      pkt - the received packet, 32 bytes min
      key1 - the 32-byte ChaCha20 key for the short header
      key2 - the 32-byte ChaCha20 key for the remaining bytes
      header - the scratch Header to write the decrypted bytes to
      scratch - per-thread scratch buffers for the header protection
    • decryptShortHeader

      private static void decryptShortHeader(DatagramPacket pkt, byte[] key1, byte[] key2, SSU2Header.Header header, SSU2Header.Scratch scratch)
      Decrypt bytes 0-15 to header. Packet is unmodified.

      First 8 bytes uses key1 and the next-to-last 12 bytes as the IV. Next 8 bytes uses key2 and the last 12 bytes as the IV.

      Parameters:
      pkt - the received packet, 16 bytes min
      key1 - the 32-byte ChaCha20 key for the first 8 bytes
      key2 - the 32-byte ChaCha20 key for the next 8 bytes
      header - the scratch Header to write the decrypted bytes to
      scratch - per-thread scratch buffers for the header protection
    • encryptHandshakeHeader

      public static void encryptHandshakeHeader(UDPPacket packet, byte[] key1, byte[] key2)
      Encryption - first 64 bytes.
      Parameters:
      packet - the outbound packet whose header bytes are encrypted in place
      key1 - the 32-byte ChaCha20 key protecting header bytes 0-7
      key2 - the 32-byte ChaCha20 key protecting header bytes 8-63
    • encryptLongHeader

      public static void encryptLongHeader(UDPPacket packet, byte[] key1, byte[] key2)
      First 32 bytes.
      Parameters:
      packet - the outbound packet whose header bytes are encrypted in place
      key1 - the 32-byte ChaCha20 key protecting header bytes 0-7
      key2 - the 32-byte ChaCha20 key protecting header bytes 8-31
    • encryptShortHeader

      public static void encryptShortHeader(UDPPacket packet, byte[] key1, byte[] key2)
      First 16 bytes.

      First 8 bytes uses key1 and the next-to-last 12 bytes as the IV. Next 8 bytes uses key2 and the last 12 bytes as the IV.

      Parameters:
      packet - the outbound packet whose header bytes are encrypted in place
      key1 - the 32-byte ChaCha20 key protecting header bytes 0-7
      key2 - the 32-byte ChaCha20 key protecting header bytes 8-15