Package io.github.mahmoudimus.quic
Class StatelessResetToken
java.lang.Object
io.github.mahmoudimus.quic.StatelessResetToken
A 16-byte stateless reset token (RFC 9000 section 10.3), as carried in NEW_CONNECTION_ID
frames, the
stateless_reset_token transport parameter and the tail of a Stateless Reset.
equals(Object) and matches(ByteBuffer) compare in constant time (MessageDigest.isEqual(byte[], byte[])), so a peer cannot learn a token byte by byte from timing. Instances are
immutable and thread-safe.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe token length in bytes.static final intThe smallest datagram that can be a Stateless Reset: 5 bytes that look like a short header plus the token (RFC 9000 section 10.3). -
Method Summary
Modifier and TypeMethodDescriptionstatic StatelessResetTokenderive(byte[] staticKey, ConnectionId connectionId) Derives the token for a connection ID from a static key, as quinn does: the first 16 bytes of HMAC-SHA256(key, connection ID).booleanCompares in constant time.inthashCode()booleanmatches(ByteBuffer datagram) Whether a received datagram ends in this token, which identifies it as a Stateless Reset (RFC 9000 section 10.3.1).static StatelessResetTokenof(byte[] bytes) A token with a copy of some bytes.static StatelessResetTokenread(ByteBuffer in) Reads a token at the buffer's position.byte[]A copy of the bytes.toString()The token in lowercase hexadecimal.voidwriteTo(ByteBuffer out) Writes the token.
-
Field Details
-
LENGTH
public static final int LENGTHThe token length in bytes.- See Also:
-
MIN_RESET_DATAGRAM
public static final int MIN_RESET_DATAGRAMThe smallest datagram that can be a Stateless Reset: 5 bytes that look like a short header plus the token (RFC 9000 section 10.3).- See Also:
-
-
Method Details
-
of
A token with a copy of some bytes.- Parameters:
bytes- exactlyLENGTHbytes- Returns:
- the token
- Throws:
IllegalArgumentException- if the length is not 16
-
read
Reads a token at the buffer's position.- Parameters:
in- the buffer; its position moves past the token- Returns:
- the token
- Throws:
BufferUnderflowException- if fewer than 16 bytes remain
-
derive
Derives the token for a connection ID from a static key, as quinn does: the first 16 bytes of HMAC-SHA256(key, connection ID). An endpoint that keeps the key can then reset connections it has forgotten (RFC 9000 section 10.3.2).- Parameters:
staticKey- a secret key of at least 32 random bytesconnectionId- the connection ID the token belongs to- Returns:
- the token
-
toByteArray
public byte[] toByteArray()A copy of the bytes.- Returns:
- a new 16-byte array
-
writeTo
Writes the token.- Parameters:
out- the buffer, written at its position
-
matches
Whether a received datagram ends in this token, which identifies it as a Stateless Reset (RFC 9000 section 10.3.1). Datagrams shorter thanMIN_RESET_DATAGRAMnever match. The comparison takes the same time wherever the bytes differ.- Parameters:
datagram- the datagram between its position and limit; the buffer is not changed- Returns:
- true if the last 16 bytes equal the token
-
equals
Compares in constant time. -
hashCode
public int hashCode() -
toString
The token in lowercase hexadecimal.
-