Module

whiplash/quic/crypto

src/whiplash/quic/crypto.pooka

Keys 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

function

initial_salt

src/whiplash/quic/crypto.pooka:43
initial_salt() -> BitArray
INFERREDfn() -> BitArray

The salt for QUIC version 1's Initial secrets (RFC 9001 section 5.2).

enum

Cipher

src/whiplash/quic/crypto.pooka:48
enum Cipher

The AEAD a connection agreed on in its handshake.

variant

Aes128Gcm

src/whiplash/quic/crypto.pooka:49
Cipher.Aes128Gcm
variant

Aes256Gcm

src/whiplash/quic/crypto.pooka:50
Cipher.Aes256Gcm
variant

ChaCha20Poly1305

src/whiplash/quic/crypto.pooka:51
Cipher.ChaCha20Poly1305
class

Keys

src/whiplash/quic/crypto.pooka:55
class Keys(cipher: Cipher, key: BitArray, iv: BitArray, hp: BitArray)

The keys protecting one direction of one packet number space.

field

cipher

src/whiplash/quic/crypto.pooka:55
cipher: Cipher
field

key

src/whiplash/quic/crypto.pooka:55
key: BitArray
INFERREDBitArray
field

iv

src/whiplash/quic/crypto.pooka:55
iv: BitArray
INFERREDBitArray
field

hp

src/whiplash/quic/crypto.pooka:55
hp: BitArray
INFERREDBitArray
constructor

new

src/whiplash/quic/crypto.pooka:55
INFERREDfn(whiplash/quic/crypto.Cipher, BitArray, BitArray, BitArray) -> whiplash/quic/crypto.Keys
function

hkdf_extract

src/whiplash/quic/crypto.pooka:58
hkdf_extract(salt: BitArray, input: BitArray) -> BitArray
INFERREDfn(BitArray, BitArray) -> BitArray

HKDF-Extract with SHA-256.

function

hkdf_expand_label

src/whiplash/quic/crypto.pooka:63
hkdf_expand_label(
  secret: BitArray,
  label: String,
  context: BitArray,
  length: Int
) -> BitArray
INFERREDfn(BitArray, String, BitArray, Int) -> BitArray

HKDF-Expand-Label with SHA-256 (RFC 8446 section 7.1).

function

hkdf_label

src/whiplash/quic/crypto.pooka:74
hkdf_label(label: String, context: BitArray, length: Int) -> BitArray
INFERREDfn(String, BitArray, Int) -> BitArray

The HkdfLabel structure HKDF-Expand-Label expands, as bytes: the length, then "tls13 " and the label, then the context.

function

initial_keys

src/whiplash/quic/crypto.pooka:86
initial_keys(destination: BitArray) -> #(Keys, Keys)

The Initial keys for both directions, from the client's first destination connection ID: #(client, server).

function

keys

src/whiplash/quic/crypto.pooka:95
keys(cipher: Cipher, secret: BitArray) -> Keys

The keys of a secret the handshake produced.

function

nonce

src/whiplash/quic/crypto.pooka:115
nonce(iv: BitArray, packet_number: Int) -> BitArray
INFERREDfn(BitArray, Int) -> BitArray

The nonce for a packet: the IV XORed with the packet number.

function

header_mask

src/whiplash/quic/crypto.pooka:122
header_mask(keys: Keys, sample: BitArray) -> BitArray
INFERREDfn(whiplash/quic/crypto.Keys, BitArray) -> BitArray

The five bytes of mask for a sample of protected payload.

function

protect

src/whiplash/quic/crypto.pooka:130
protect(
  keys: Keys,
  header: BitArray,
  pn_length: Int,
  packet_number: Int,
  payload: BitArray
) -> BitArray
INFERREDfn(whiplash/quic/crypto.Keys, BitArray, Int, Int, BitArray) -> BitArray

Protect 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.

function

unprotect

src/whiplash/quic/crypto.pooka:159
unprotect(keys: Keys, packet: BitArray, pn_offset: Int, largest: Int) -> Result(
  #(BitArray, Int, BitArray),
  Nil
)
INFERREDfn(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.

function

open

src/whiplash/quic/crypto.pooka:172
open(keys: Keys, packet: BitArray, pn_offset: Int, largest: Int) -> Result(
  #(BitArray, Int, BitArray, Bool),
  Nil
)
INFERREDfn(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

src/whiplash/quic/crypto.pooka:184
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.

field

header

src/whiplash/quic/crypto.pooka:185
header: BitArray
INFERREDBitArray
field

number

src/whiplash/quic/crypto.pooka:186
number: Int
INFERREDInt
field

sealed

src/whiplash/quic/crypto.pooka:187
sealed: BitArray
INFERREDBitArray
field

reserved_clear

src/whiplash/quic/crypto.pooka:188
reserved_clear: Bool
INFERREDBool
constructor

new

src/whiplash/quic/crypto.pooka:185
INFERREDfn(BitArray, Int, BitArray, Bool) -> whiplash/quic/crypto.Unmasked
method

key_phase

src/whiplash/quic/crypto.pooka:191
key_phase() -> Int
INFERREDfn() -> Int

The key phase bit of a short header, 0 or 1 (RFC 9001 section 6).

function

unmask

src/whiplash/quic/crypto.pooka:208
unmask(keys: Keys, packet: BitArray, pn_offset: Int, largest: Int) -> Result(
  Unmasked,
  Nil
)
INFERREDfn(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).

Pooka
let found = crypto.unmask(keys, packet, 9, largest)?
let payload = crypto.decrypt(keys, found)?
function

decrypt

src/whiplash/quic/crypto.pooka:256
decrypt(keys: Keys, found: Unmasked) -> Result(BitArray, Nil)
INFERREDfn(whiplash/quic/crypto.Keys, whiplash/quic/crypto.Unmasked) -> Result(BitArray, Nil)

Open the payload of an unmasked packet.

function

next_secret

src/whiplash/quic/crypto.pooka:268
next_secret(secret: BitArray) -> BitArray
INFERREDfn(BitArray) -> BitArray

The secret of the next key phase (RFC 9001 section 6.1): HKDF-Expand-Label with "quic ku".

function

updated

src/whiplash/quic/crypto.pooka:279
updated(old: Keys, secret: BitArray) -> Keys

Keys for the next key phase. The header protection key stays: it is not updated (RFC 9001 section 6).

Pooka
let secret = crypto.next_secret(secret)
let keys = crypto.updated(keys, secret)
function

retry_tag

src/whiplash/quic/crypto.pooka:286
retry_tag(original: BitArray, retry: BitArray) -> BitArray
INFERREDfn(BitArray, BitArray) -> BitArray

The 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.

function

seal_ticket

src/whiplash/quic/crypto.pooka:376
seal_ticket(key: BitArray, contents: BitArray) -> BitArray
INFERREDfn(BitArray, BitArray) -> BitArray

Seal 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.

function

open_ticket

src/whiplash/quic/crypto.pooka:389
open_ticket(key: BitArray, ticket: BitArray) -> Result(BitArray, Nil)
INFERREDfn(BitArray, BitArray) -> Result(BitArray, Nil)

Open a ticket sealed with seal_ticket; an error for any other bytes.