Skip to content

smite: add BOLT 4 onion routing (Sphinx) - #170

Open
erickcestari wants to merge 2 commits into
morehouse:masterfrom
erickcestari:bolt4-onion
Open

smite: add BOLT 4 onion routing (Sphinx)#170
erickcestari wants to merge 2 commits into
morehouse:masterfrom
erickcestari:bolt4-onion

Conversation

@erickcestari

Copy link
Copy Markdown
Contributor

Adds an onion module that builds and peels BOLT 4 packets.

  • keys: shared secrets and the rho/mu/um/pad/ammag/ammagext key derivation, plus the zero-nonce ChaCha20 stream.
  • payload: the HopPayload TLV (types 2, 4, 6, 8, 10, 12, 16, 18), with forward() and receive() constructors.
  • packet: OnionBuilder for the 1366-byte packet, and peel for the reverse direction.

Verified against onion-test.json and onion-error-test.json from the BOLT repository: the reference packet is reproduced byte for byte, peels at every hop, and the derived shared secrets and keys match.

What this enables today: constructing a spec-valid onion for any route and decrypting one addressed to us. Malformed onions are reachable through OnionBuilder::raw_hop, which accepts the reserved payload lengths 0 and 1, non-TLV bytes, and oversized payloads, and through the public fields of OnionPacket.

Deferred to follow-ups:

  • Failure decryption. KeyType::Ammag and Um are derived here but unused; obfuscating a reason, recovering the erring hop, and the failure codes are not implemented, so update_fail_htlc.reason stays opaque.
  • Attribution data crypto. The wire type already exists in bolt::attribution_data; the ammagext stream and the 210 truncated HMACs over it do not.
  • Route blinding. HopPayload carries encrypted_recipient_data and current_path_key, and peel accepts a path key, but there is no blinded path builder or encrypted_data TLV to produce either side.
  • Onion messages (type 513), which use a variable-size packet.

Depends on #166

BOLT 1 truncated integers show up as TLV values in BOLT 4 hop payloads
(amt_to_forward, outgoing_cltv_value, total_msat). They are big-endian
with leading zero bytes stripped, so their width is only recoverable
from the enclosing TLV record: read consumes all remaining bytes, which
makes them usable with TlvStream::get_as but not as a field in the
middle of a message.

Non-minimal encodings and values wider than the integer are rejected.
Adds an `onion` module that builds and peels BOLT 4 packets.

- `keys`: shared secrets and the rho/mu/um/pad/ammag/ammagext key
  derivation, plus the zero-nonce ChaCha20 stream.
- `payload`: the `HopPayload` TLV (types 2, 4, 6, 8, 10, 12, 16, 18),
  with `forward()` and `receive()` constructors.
- `packet`: `OnionBuilder` for the 1366-byte packet, and `peel` for the
  reverse direction.

Verified against `onion-test.json` and `onion-error-test.json` from the
BOLT repository, vendored verbatim: the reference packet is reproduced
byte for byte, peels at every hop, and the derived shared secrets and
keys match.

What this enables today: constructing a spec-valid onion for any route
and decrypting one addressed to us. Malformed onions are reachable
through `OnionBuilder::raw_hop`, which accepts the reserved payload
lengths 0 and 1, non-TLV bytes, and oversized payloads, and through the
public fields of `OnionPacket`.

Deferred to follow-ups:

- Failure decryption. `KeyType::Ammag` and `Um` are derived here but
  unused; obfuscating a `reason`, recovering the erring hop, and the
  failure codes are not implemented, so `update_fail_htlc.reason` stays
  opaque.
- Attribution data crypto. The wire type already exists in
  `bolt::attribution_data`; the `ammagext` stream and the 210 truncated
  HMACs over it do not.
- Route blinding. `HopPayload` carries `encrypted_recipient_data` and
  `current_path_key`, and `peel` accepts a path key, but there is no
  blinded path builder or `encrypted_data` TLV to produce either side.
- Onion messages (type 513), which use a variable-size packet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant