java.lang.Object
net.i2p.router.transport.udp.SSU2Header
Encrypt/decrypt headers.
Nested Class Summary
Modifier and TypeClassDescriptionstatic classA temporary structure returned from trial decrypt, with methods to access the fields.private static final classThread-scoped scratch for the trial decrypts, which run 3-6 times per inbound datagram on the UDP handle threads.Field Summary
Modifier and TypeFieldDescriptionstatic final byte[]12 bytes of zeros.static final byte[]8 bytes of zeros.private static final ThreadLocal<SSU2Header.Scratch> Per-thread buffers for building headers.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic voidacceptTrialDecrypt(UDPPacket packet, SSU2Header.Header header) Copy the header back to the packet.static longdecryptDestConnID(DatagramPacket pkt, byte[] key1) Decrypt bytes 0-7 in header.private static voiddecryptHandshakeHeader(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.private static voiddecryptLongHeader(DatagramPacket pkt, byte[] key1, byte[] key2, SSU2Header.Header header, SSU2Header.Scratch scratch) Decrypt bytes 0-31 from pkt to header.private static voiddecryptShortHeader(DatagramPacket pkt, byte[] key1, byte[] key2, SSU2Header.Header header, SSU2Header.Scratch scratch) Decrypt bytes 0-15 to header.static voidencryptHandshakeHeader(UDPPacket packet, byte[] key1, byte[] key2) Encryption - first 64 bytes.static voidencryptLongHeader(UDPPacket packet, byte[] key1, byte[] key2) First 32 bytes.static voidencryptShortHeader(UDPPacket packet, byte[] key1, byte[] key2) First 16 bytes.static SSU2Header.HeadertrialDecryptHandshakeHeader(UDPPacket packet, byte[] key1, byte[] key2) Session Request and Session Created only.static SSU2Header.HeadertrialDecryptLongHeader(UDPPacket packet, byte[] key1, byte[] key2) Retry, Token Request, Peer Test only.static SSU2Header.HeadertrialDecryptShortHeader(UDPPacket packet, byte[] key1, byte[] key2) Session Confirmed and data phase.
Field Details
HEADER_PROT_DATA
public static final byte[] HEADER_PROT_DATA8 bytes of zeros.CHACHA_IV_0
public static final byte[] CHACHA_IV_012 bytes of zeros.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 minkey1- the 32-byte ChaCha20 key protecting header bytes 0-7key2- the 32-byte ChaCha20 key protecting header bytes 8-15- Returns:
- 64 byte header, null if data too short
trialDecryptLongHeader
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 minkey1- the 32-byte ChaCha20 key protecting header bytes 0-7key2- the 32-byte ChaCha20 key protecting header bytes 8-15- Returns:
- 32 byte header, null if data too short
trialDecryptShortHeader
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 minkey1- the 32-byte ChaCha20 key protecting header bytes 0-7key2- 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
Decrypt bytes 0-7 in header. Packet is unmodified.- Parameters:
pkt- must be 8 bytes minkey1- the 32-byte ChaCha20 key protecting header bytes 0-7- Returns:
- the destination connection ID
- Throws:
IndexOutOfBoundsException- if too short
acceptTrialDecrypt
Copy the header back to the packet. Cannot be undone.- Parameters:
packet- the packet the trial-decrypted header came fromheader- 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 minkey1- the 32-byte ChaCha20 key for the short headerkey2- the 32-byte ChaCha20 key for the remaining bytesheader- the scratch Header to write the decrypted bytes toscratch- 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 minkey1- the 32-byte ChaCha20 key for the short headerkey2- the 32-byte ChaCha20 key for the remaining bytesheader- the scratch Header to write the decrypted bytes toscratch- 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 minkey1- the 32-byte ChaCha20 key for the first 8 byteskey2- the 32-byte ChaCha20 key for the next 8 bytesheader- the scratch Header to write the decrypted bytes toscratch- per-thread scratch buffers for the header protection
encryptHandshakeHeader
Encryption - first 64 bytes.- Parameters:
packet- the outbound packet whose header bytes are encrypted in placekey1- the 32-byte ChaCha20 key protecting header bytes 0-7key2- the 32-byte ChaCha20 key protecting header bytes 8-63
encryptLongHeader
First 32 bytes.- Parameters:
packet- the outbound packet whose header bytes are encrypted in placekey1- the 32-byte ChaCha20 key protecting header bytes 0-7key2- the 32-byte ChaCha20 key protecting header bytes 8-31
encryptShortHeader
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 placekey1- the 32-byte ChaCha20 key protecting header bytes 0-7key2- the 32-byte ChaCha20 key protecting header bytes 8-15