Module
whiplash/quic/crypto
src/whiplash/quic/crypto.pookaKeys and packet protection for QUIC (RFC 9001).
HKDF (RFC 5869) and the TLS 1.3 HKDF-Expand-Label (RFC 8446 section 7.1), the Initial secrets derived from the client's connection ID, AEAD payload protection and header protection, the keys of each key update, the Retry Integrity Tag, and the sealing of session tickets. The primitives come from Erlang's crypto application through a small FFI; nothing here is cryptography of our own.
API reference
- functioninitial_salt
- enumCipher
- classKeys
- functionhkdf_extract
- functionhkdf_expand_label
- functionhkdf_label
- functioninitial_keys
- functionkeys
- functionnonce
- functionheader_mask
- functionprotect
- functionunprotect
- functionopen
- classUnmasked
- functionunmask
- functiondecrypt
- functionnext_secret
- functionupdated
- functionretry_tag
- functionseal_ticket
- functionopen_ticket
initial_salt() -> BitArray
fn() -> BitArrayThe salt for QUIC version 1's Initial secrets (RFC 9001 section 5.2).
enum Cipher
The AEAD a connection agreed on in its handshake.
Cipher.Aes128Gcm
Cipher.Aes256Gcm
Cipher.ChaCha20Poly1305
class Keys(cipher: Cipher, key: BitArray, iv: BitArray, hp: BitArray)
The keys protecting one direction of one packet number space.
cipher: Cipher
whiplash/quic/crypto.Cipherkey: BitArray
BitArrayiv: BitArray
BitArrayhp: BitArray
BitArray
fn(whiplash/quic/crypto.Cipher, BitArray, BitArray, BitArray) -> whiplash/quic/crypto.Keyshkdf_extract(salt: BitArray, input: BitArray) -> BitArray
fn(BitArray, BitArray) -> BitArrayHKDF-Extract with SHA-256.
hkdf_expand_label(
secret: BitArray,
label: String,
context: BitArray,
length: Int
) -> BitArray
fn(BitArray, String, BitArray, Int) -> BitArrayHKDF-Expand-Label with SHA-256 (RFC 8446 section 7.1).
hkdf_label(label: String, context: BitArray, length: Int) -> BitArray
fn(String, BitArray, Int) -> BitArrayThe HkdfLabel structure HKDF-Expand-Label expands, as bytes: the length, then "tls13 " and the label, then the context.
initial_keys(destination: BitArray) -> #(Keys, Keys)
fn(BitArray) -> #(whiplash/quic/crypto.Keys, whiplash/quic/crypto.Keys)The Initial keys for both directions, from the client's first
destination connection ID: #(client, server).
keys(cipher: Cipher, secret: BitArray) -> Keys
fn(whiplash/quic/crypto.Cipher, BitArray) -> whiplash/quic/crypto.KeysThe keys of a secret the handshake produced.
nonce(iv: BitArray, packet_number: Int) -> BitArray
fn(BitArray, Int) -> BitArrayThe nonce for a packet: the IV XORed with the packet number.
header_mask(keys: Keys, sample: BitArray) -> BitArray
fn(whiplash/quic/crypto.Keys, BitArray) -> BitArrayThe five bytes of mask for a sample of protected payload.
protect(
keys: Keys,
header: BitArray,
pn_length: Int,
packet_number: Int,
payload: BitArray
) -> BitArray
fn(whiplash/quic/crypto.Keys, BitArray, Int, Int, BitArray) -> BitArrayProtect a packet: encrypt the payload with the header as associated
data, then protect the header. header ends with the packet number,
pn_length bytes long.
unprotect(keys: Keys, packet: BitArray, pn_offset: Int, largest: Int) -> Result(
#(BitArray, Int, BitArray),
Nil
)
fn(whiplash/quic/crypto.Keys, BitArray, Int, Int) -> Result(#(BitArray, Int, BitArray), Nil)Remove protection from a packet whose packet number starts at
pn_offset: the header, the packet number, and the payload.
open(keys: Keys, packet: BitArray, pn_offset: Int, largest: Int) -> Result(
#(BitArray, Int, BitArray, Bool),
Nil
)
fn(whiplash/quic/crypto.Keys, BitArray, Int, Int) -> Result(#(BitArray, Int, BitArray, Bool), Nil)unprotect, but a packet whose reserved header bits are set still opens;
the fourth element says whether they were clear. RFC 9000 makes set
reserved bits a PROTOCOL_VIOLATION, which the connection reports.
class Unmasked(
header: BitArray,
number: Int,
sealed: BitArray,
reserved_clear: Bool
)
A packet with its header protection removed and its payload still sealed: the header as the AEAD sees it, the full packet number, the sealed bytes, and whether the reserved bits are clear and the fixed bit set.
header: BitArray
BitArraynumber: Int
Intsealed: BitArray
BitArrayreserved_clear: Bool
Bool
fn(BitArray, Int, BitArray, Bool) -> whiplash/quic/crypto.Unmaskedkey_phase() -> Int
fn() -> IntThe key phase bit of a short header, 0 or 1 (RFC 9001 section 6).
unmask(keys: Keys, packet: BitArray, pn_offset: Int, largest: Int) -> Result(
Unmasked,
Nil
)
fn(whiplash/quic/crypto.Keys, BitArray, Int, Int) -> Result(whiplash/quic/crypto.Unmasked, Nil)Remove header protection only. Header protection keys never change, so the key phase bit can be read before choosing the keys that open the payload (RFC 9001 section 6.3).
let found = crypto.unmask(keys, packet, 9, largest)?
let payload = crypto.decrypt(keys, found)?
decrypt(keys: Keys, found: Unmasked) -> Result(BitArray, Nil)
fn(whiplash/quic/crypto.Keys, whiplash/quic/crypto.Unmasked) -> Result(BitArray, Nil)Open the payload of an unmasked packet.
next_secret(secret: BitArray) -> BitArray
fn(BitArray) -> BitArrayThe secret of the next key phase (RFC 9001 section 6.1): HKDF-Expand-Label with "quic ku".
updated(old: Keys, secret: BitArray) -> Keys
fn(whiplash/quic/crypto.Keys, BitArray) -> whiplash/quic/crypto.KeysKeys for the next key phase. The header protection key stays: it is not updated (RFC 9001 section 6).
let secret = crypto.next_secret(secret)
let keys = crypto.updated(keys, secret)
retry_tag(original: BitArray, retry: BitArray) -> BitArray
fn(BitArray, BitArray) -> BitArrayThe Retry Integrity Tag (RFC 9001 section 5.8): AES-128-GCM over the Retry pseudo-packet, with the fixed key and nonce of QUIC version 1.
seal_ticket(key: BitArray, contents: BitArray) -> BitArray
fn(BitArray, BitArray) -> BitArraySeal a session ticket's contents under a server's ticket key (32 bytes, AES-256-GCM). The result starts with the twelve random bytes of its nonce, which also name the ticket.
open_ticket(key: BitArray, ticket: BitArray) -> Result(BitArray, Nil)
fn(BitArray, BitArray) -> Result(BitArray, Nil)Open a ticket sealed with seal_ticket; an error for any other bytes.