Class PacketProtection

java.lang.Object
io.github.mahmoudimus.quic.packet.PacketProtection

public final class PacketProtection extends Object
Applies packet protection (RFC 9001 section 5) to an encoded packet in place: fills in the Length field, seals the payload and then masks the header. Removing protection is ProtectedPacket.unprotect(io.github.mahmoudimus.quic.crypto.DirectionalKeys, long).

All methods are static; thread confinement follows from the keys passed in.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The longest long header packet, from the packet number to the end of the tag, that the two-byte Length field this module writes can describe.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    minPayloadLength(int packetNumberLength)
    The fewest payload bytes a packet needs so that the header protection sample, which starts four bytes after the packet number, lies within the ciphertext and tag.
    static int
    protect(ByteBuffer packet, HeaderLayout layout, int payloadEnd, long packetNumber, DirectionalKeys keys)
    Protects an encoded packet in place.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_LONG_PACKET_REMAINDER

      public static final int MAX_LONG_PACKET_REMAINDER
      The longest long header packet, from the packet number to the end of the tag, that the two-byte Length field this module writes can describe.
      See Also:
  • Method Details

    • minPayloadLength

      public static int minPayloadLength(int packetNumberLength)
      The fewest payload bytes a packet needs so that the header protection sample, which starts four bytes after the packet number, lies within the ciphertext and tag.
      Parameters:
      packetNumberLength - the packet number length, 1 to 4
      Returns:
      the minimum plaintext payload length
    • protect

      public static int protect(ByteBuffer packet, HeaderLayout layout, int payloadEnd, long packetNumber, DirectionalKeys keys)
      Protects an encoded packet in place. The header was written by PacketHeader.encode(java.nio.ByteBuffer) at layout, followed by the plaintext payload up to payloadEnd; the buffer needs 16 bytes of capacity after it for the tag. Indexes are absolute; the buffer's position and limit are not changed.
      Parameters:
      packet - the packet buffer
      layout - where the header's fields are
      payloadEnd - the index just past the plaintext payload
      packetNumber - the full packet number in the header
      keys - the sender's keys for the packet's level
      Returns:
      the index just past the protected packet
      Throws:
      IllegalArgumentException - if the payload is shorter than minPayloadLength(int) or too long for the Length field, or the header is not a protected one