Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

## Unreleased

- QUIC: `Beryl::QUIC`, the cryptographic half of QUIC version 1 (RFC 9001),
sans-IO, for a QUIC transport such as Mica's.
`PacketProtection` is one direction of one encryption level: the initial
pair a server derives from the client's Destination Connection ID, packet
protection under the `quic key` and `quic iv` labels (with `into:` forms
that write into the caller's storage), the 5-byte header protection mask
(AES-ECB or ChaCha20 under `quic hp`), and key update (`quic ku`), which
keeps the header protection key. `ServerHandshake` runs the TLS 1.3
server handshake over CRYPTO data per encryption level and hands out the
Handshake and Application keys as `InstalledKeys`; it requires ALPN
(`no_application_protocol`) and the client's `quic_transport_parameters`
(`missing_extension`), refuses a non-empty `legacy_session_id` and a
KeyUpdate message, accepts no 0-RTT and issues no session tickets.
`HandshakeError` carries the alert and the QUIC error code
(CRYPTO_ERROR `0x0100` plus the alert, or PROTOCOL_VIOLATION).
`QUIC.retry_integrity_tag` computes the Retry Integrity Tag. Checked
against every packet in RFC 9001 Appendix A.
- TLS: the ClientHello checks and the choice of suite, group, ALPN protocol
and server name moved from `ServerHandshake` into
`NegotiateParametersFromClientHello`, which the TLS and QUIC servers
share, so the two choose alike. Told which transport it serves, it has
the TLS server refuse a ClientHello carrying `quic_transport_parameters`
with `unsupported_extension` (RFC 9001 §8.2); otherwise the TLS server
behaves as before.
- Secret-flow checks: QUIC header protection (keying from a traffic secret,
and the mask) joins `spec/probes/secret_flow.cr` and
`spec/probes/secret_taint.cr`. `scripts/check_secret_flow.rb` now also
treats the buffers of a cipher object passed to a probe entry point as
key material, so a method the optimizer inlined there is still checked.

## 0.2.8 (2026-12-05)

One TLS listener for several sites: a server chooses its certificate by the
Expand Down
21 changes: 21 additions & 0 deletions docs/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ context.refuses_unrecognized_server_names = true # the default
- `certified_key_for_server_name(name)` answers which key a name gets, for checks at boot.
- A session ticket records the server name it was issued under and resumes only under that same name, so a ticket never carries a connection from one certificate to another.

## QUIC (RFC 9001)

`Beryl::QUIC` carries the same TLS 1.3 server handshake in QUIC's CRYPTO frames, sans-IO, and protects QUIC packets. A transport (Mica's QUIC core, hosted by Amber) owns the UDP socket, the frames and the packet number spaces; Beryl owns every key, and hands out only objects that seal and open.

```crystal
read_initial, write_initial = Beryl::QUIC::PacketProtection.initial_pair_for_server(client_destination_connection_id)
handshake = Beryl::QUIC::ServerHandshake.new(context, encoded_transport_parameters)
handshake.receive_crypto_data(Beryl::QUIC::EncryptionLevel::Initial, crypto_bytes)
handshake.take_crypto_data_to_send(Beryl::QUIC::EncryptionLevel::Initial) # ServerHello (or a HelloRetryRequest)
handshake.take_list_of_installed_keys # Handshake, then Application
handshake.take_crypto_data_to_send(Beryl::QUIC::EncryptionLevel::Handshake) # EncryptedExtensions to Finished
handshake.complete? # once the client's Finished verified
```

- The server chooses its suite, group, certificate by server name and ALPN protocol exactly as the TLS server does, from the same `Context::Server`: both call `NegotiateParametersFromClientHello`.
- ALPN is required: no agreed protocol ends with `no_application_protocol`. The client's `quic_transport_parameters` (extension 57) is required (`missing_extension`) and is exposed unparsed as `peer_transport_parameters`; the server's goes in its EncryptedExtensions.
- A non-empty `legacy_session_id`, Initial data left unread when the Handshake keys arrive, and Handshake data after the client's Finished are PROTOCOL_VIOLATION; every other failure is CRYPTO_ERROR `0x0100` plus the alert. `HandshakeError#transport_error_code` gives the code to close with.
- No session resumption, no 0-RTT, no session tickets, no client certificates (a context that asks for them is refused when the handshake is made), no KeyUpdate message (QUIC uses the key phase bit: `PacketProtection#next_generation`).
- Each encryption level buffers at most one incomplete message, no larger than `Limits#largest_handshake_message_bytesize`, refused from its header.
- Secrets: the handshake wipes everything it derives when it completes or fails (`wipe` for a handshake the transport abandons). `PacketProtection` copies the traffic secret it is given; `wipe` erases its key, IV and secret, and the header protection key once no generation shares it.

## Limits

Set through `Context#limits = Beryl::TLS::Limits.new(...)`; the defaults are the plan's (§4).
Expand Down
22 changes: 17 additions & 5 deletions scripts/check_secret_flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
# field. The memory-checker run (spec/probes/secret_taint.cr under Valgrind)
# and the timing check cover the same code by two other methods.

ANALYZED_FUNCTION = /Beryl::Crypto|SecretFlowProbe|Crypto::Subtle/.freeze
ANALYZED_FUNCTION = /Beryl::Crypto|Beryl::QUIC::HeaderProtectionKey|SecretFlowProbe|Crypto::Subtle/.freeze

# Functions whose every pointer argument leads to secret memory: the cipher
# and authenticator cores, which work on keys, round keys, accumulators and
Expand All @@ -70,11 +70,12 @@
MontgomeryArithmetic::|KeccakPermutation::|
ModuleLatticeKeyEncapsulation768::PolynomialRing::(?!sample_ntt|decode_12_below_modulus)/x.freeze

# Methods of the cipher objects. A cipher object keeps integers that are
# public (a round count, a tag size) beside buffers that are secret (a key,
# Methods of the cipher objects, QUIC's header protection key among them.
# A cipher object keeps integers that are public (a round count, a tag size,
# a cipher suite, a generation count) beside buffers that are secret (a key,
# round keys, a hash key), so every buffer loaded from the object is secret
# and its integers are not.
CIPHER_OBJECT_METHOD = /AuthenticatedEncryption|GaloisCounterMode|CipherBlockChainingMode|RivestShamirAdleman::/.freeze
CIPHER_OBJECT_METHOD = /AuthenticatedEncryption|GaloisCounterMode|CipherBlockChainingMode|RivestShamirAdleman::|QUIC::HeaderProtectionKey/.freeze

# Functions of the elliptic-curve code that take secrets by value: field
# elements and scalars as arrays of limbs, points as structs of them, a
Expand Down Expand Up @@ -279,6 +280,17 @@ def initialize(function, state_paths_by_compression_function)
@is_compression = function.name.match?(COMPRESSION_FUNCTION)
@is_secret_buffer_function = function.name.match?(SECRET_BUFFER_FUNCTION)
@is_cipher_object_method = function.name.match?(CIPHER_OBJECT_METHOD)
# The cipher objects whose buffers hold key material: `self` in one of
# their methods, and the object a probe entry point takes, where the
# optimizer may have inlined the method (a probe passes its slices by
# value, so its pointer arguments are the objects).
@list_of_cipher_object_pointers = if @is_cipher_object_method && function.name.include?("SecretFlowProbe::")
function.parameters.select { |_name, kind| kind == :pointer }.keys
elsif @is_cipher_object_method
["%self"]
else
[]
end
@is_secret_value_function = function.name.match?(SECRET_VALUE_FUNCTION)
# A public argument passed as a Slice (an exponent) is not secret memory.
if @is_secret_value_function
Expand Down Expand Up @@ -385,7 +397,7 @@ def visit(instruction)
elsif instruction.text.start_with?("load ptr")
@origin[instruction.result] = [instruction.result, ""]
# A buffer that a cipher object owns holds key material.
@secret_bases[instruction.result] = true if @is_cipher_object_method && pointer && @origin[pointer]&.first == "%self"
@secret_bases[instruction.result] = true if pointer && @list_of_cipher_object_pointers.include?(@origin[pointer]&.first)
end
when "store"
value_text, pointer = instruction.text.sub(/\Astore (?:volatile )?/, "").split(", ptr ", 2)
Expand Down
Loading