1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Sphinx cells of the onion Kleisli loop: the pure primitives of #834 Phase 2a (#840).
//!
//! Every edge of a loop of class `b` carries one fixed-width cell (#834 D6, D6″),
//!
//! ```text
//! cell = α ‖ β ‖ γ ‖ y |α| = 33, |β| = Ĥ·ℓ, |γ| = 16, |y| = C_b = b − |χ| − F
//! └── χ ──┘ Ĥ = MAX_ONION_LOOP_HOPS = 14, ℓ = 205, |χ| = 2919, F = 0
//! ```
//!
//! where the header `χ = (α, β, γ)` routes the cell (Sphinx, [`header`]) and the carry slot `y`
//! transports one segment value under nested AEZ layers ([`carry`]); [`cell`] parses a received
//! string into `(b, χ, y)` with `b` its observed length, the only source of `b`. A hop maps its
//! cell by
//!
//! ```text
//! peel_i : χ_i ↦ (λ_i, χ_{i+1}) one header layer (Def. header)
//! y_{k,j} = Dec⁰_{k_{r_{k,j}}}(y_{k,j−1}) one AEZ layer at a relay (D7)
//! m_k = Dec^τ_{k_{c_k}}(y_{k,s}) authenticated at the consumer
//! ```
//!
//! with every key derived from a 32-byte seed in the hop's own layer `λ_i` ([`seed`]); `λ_i`
//! itself is the uniform layer of [`layer`], and [`class`] fixes `C_b` per loop class.
//!
//! Laws (discharged by `tests`, one module per law):
//!
//! - **Header correctness** (Prop. header correctness). For `1 ≤ H ≤ Ĥ` and every position
//! `1 ≤ i ≤ H`, `i = H` (the guard facing the client, D6′) included, `peel_i` under hop `i`'s key
//! yields `λ_i`, every `γ_i` verifies, and the client receives its tag `t_⋄ = γ_{H+1}`.
//! - **Class binding** (#834 H1). `γ_i` covers the class `b`, so a cell relabelled to another
//! class fails `γ` at the next honest hop.
//! - **L5′ lengths.** `|χ_i| = |χ|` and `|y_i| = C_b` at every position and for every `H`: the
//! lengths are functions of the class `b` alone, never of `H` or `i`.
//! - **L8 unlinkability.** `χ_i ≠ χ_{i+1}` and `y_{i−1} ≠ y_i` on every edge, and a one-bit change
//! anywhere in a segment's carry makes the consumer reject the whole value.
//! - **D7 seeds.** Derived keys equal their HKDF definition, and a key with a zero AEZ subkey is
//! detected, so the client re-draws the segment seed.
//!
//! Assumptions this module relies on, which the data plane (`onion::circuit`, #843) meets:
//!
//! 1. **No discriminant** (`F = 0`). A cell is exactly `b` bytes and its class is its length,
//! bound by `γ = MAC(b ‖ β)`. Link cover traffic is a uniformly random `b`-byte cell, which the
//! receiving hop rejects at `α` decoding (`≈ 99.6 %`, before any ECDH) or at `γ`; both are one
//! outcome, [`header::OnionPeelError::Invalid`], charged `u(b)` by admission (#834) and
//! checked before any replay-store insertion (L9 counts admitted layers only).
//! 2. **Integrity and neighbour.** Header integrity is `γ`, carry integrity is the consumer's AEZ
//! authenticator, and the neighbour `from` is the authenticated transport link.
//!
//! The module is crate-private: its one consumer is the loop data plane (`onion::circuit`), whose
//! hop step and loop client are the only callers of the cell and the builder.
use Hkdf;
use HkdfExtract;
use Sha256;
use Zeroize;
use Zeroizing;
pub
pub
pub
pub
pub
pub
pub
/// `target ← target ⊕ mask`, bytewise over the common prefix of the two streams.
/// `HKDF-Expand(PRK, info₁ ‖ … ‖ infoₙ)[0, N)`, zeroized on drop.
///
/// HKDF-Expand is defined exactly for `N ≤ 255·|SHA-256|`, which is asserted at compile time, so
/// its only error, `InvalidLength`, cannot occur and the result is discarded.
///
/// Each block's HMAC state is wiped on drop (sha2's `zeroize`). hkdf 0.13 does not wipe its
/// previous block `T(i)`, a stack copy of the key or of its first half; that, HMAC's padded key
/// block and SHA-256's message schedule are the residue SECURITY.md lists (#852).
/// `HKDF-Extract(salt, ikm₁ ‖ … ‖ ikmₙ)`, with the pseudorandom key the extract step returns
/// wiped at once.
///
/// The PRK is also held inside the returned `Hkdf`'s HMAC state, as the `K ⊕ ipad` and
/// `K ⊕ opad` chaining values; sha2's `zeroize` wipes them when the `Hkdf` drops (#852).