diff --git a/docs/cell-m6.md b/docs/cell-m6.md index d7305d00..a336bdf2 100644 --- a/docs/cell-m6.md +++ b/docs/cell-m6.md @@ -1634,13 +1634,19 @@ measure and the CU could hand over to. Two checks make a re-aired copy useless unless it is relayed live and on time (`src/cell/MeasGap.h`, `examples/cell/main.cpp`). -- **A signed window.** The NS IE (version 2) carries `win`, the network - superframe / `kNlWindowSf` (64, ~6.5 s at 100 TU), inside the signature - (`'S' || NL IE || win`). The DU re-signs at every window change, and signs - only once it knows the network superframe. The UE takes its serving - cell's window as "now" and refuses a serving window that goes back. It - takes a neighbour's beacon only if the neighbour's window is within one of - the serving window, i.e. only if it was signed in the last ~13 s. +- **A signed window.** The NS IE (version 3) carries `win`, the network + superframe / `win_sf`, AND `win_sf` itself, both inside the signature + (`'S' || NL IE || win || win_sf`). `win_sf` is the DU's + `CELL_NL_WINDOW_SF` (default `kNlWindowSf`, 64, ~6.5 s at 100 TU; down to + 1 superframe, ~102 ms). Signing the length too stops a replayer relabelling + a 64-superframe signature as a 1-superframe one or back. The DU re-signs + at every window change, and signs only once it knows the network + superframe. The UE takes its serving cell's window as "now" and refuses a + serving window that goes back, or one of a different length. It takes a + neighbour's beacon only if the neighbour's window is within one of the + serving window, i.e. only if it was signed in the last ~2·`win_sf` + superframes. The smaller `win_sf` is, the shorter a re-aired copy stays + fresh — at the cost of an Ed25519 signature every `win_sf` superframes. - **The phase.** All DUs share the PTP timebase, and the serving list names each neighbour's TBTT offset on the serving grid. The UE keeps the phase where its serving beacons land (on its fitted DU clock) and refuses a @@ -1670,9 +1676,39 @@ Refusals are logged per reason (`cell.ue.nbr_refused`, counted in `cell.ue.done`). `CELL_UE_NBR_FRESH=0` turns the checks off, which the rig uses for the counterfactual. -**What remains** is a real-time repeater placed on the right phase: it -relays the live beacon within the tolerance, so it is the real cell, only -louder. Only distance bounding would stop that. +**What remains, and how far.** Two replay shapes survive the signature: +- **A store-and-forward relay** — a device that decodes a genuine beacon and + re-airs it. It is beaten by the phase check: it cannot re-air a beacon + before it has received it, so its copy lands about a beacon's airtime + after the cell's own TBTT, well outside the tolerance. Measured below. +- **An on-phase copy** — a cut-through repeater or reflector that re-radiates + the beacon with almost no delay, so it lands within the tolerance, the + real cell only louder. The phase check cannot tell it from the cell. What + bounds it is the freshness window: the UE accepts a neighbour signature + within one window of the serving one, so a copy more than ~2·`win_sf` + superframes old is refused (stale), and `CELL_NL_WINDOW_SF=1` caps its + useful life at ~205 ms rather than ~13 s. A copy fresher than that, on + phase, is + physically the cell's own signal re-radiated; only distance bounding (or a + per-beacon nonce the UE challenges, not on this one-way broadcast path) + would stop it. + +**Rig result, the live relay** (testrig `scripts/m6_nlrelay.sh`): 2 runs, +all pass. Per-cell keys and signed lists, both cells on one channel, DU2 +alive throughout. The witness runs the replayer in relay mode: it re-aired +every genuine cell-2 beacon it heard (1267–1310 in a run), carrying a +current signature. +- **Refused on phase, not freshness.** The relay's copies landed a median + 636 µs off the cell's TBTT and were refused on phase (895–896 per run); + **0** were refused as stale — their signatures were fresh. A + store-and-forward relay cannot be on time. +- **The genuine cell is undisturbed.** UE1 measured genuine cell 2 in 100 % + of its reports throughout. +- **The phase check's margin.** Genuine neighbour beacons landed within + 6–7 µs (p99) and 8 µs (max) of their predicted phase — against the 300 µs + tolerance, a ~40× margin. The tolerance could be tightened a long way + before genuine beacons were at risk, which narrows the window a near-zero + latency repeater has. **Costs.** A neighbour is measured only once the serving phase is known. On the rig that is about the first 16 neighbour beacons after the UE starts or diff --git a/examples/cell/main.cpp b/examples/cell/main.cpp index b4438c5b..7a7ceb7e 100644 --- a/examples/cell/main.cpp +++ b/examples/cell/main.cpp @@ -73,6 +73,10 @@ // and the area CA's signature over its public key // (128 hex): the NL rides with its signature and // the certificate (src/cell/MeasGap.h). +// CELL_NL_WINDOW_SF [64] DU, signing: the signature's freshness window in +// superframes (signed with it, NS IE v3); 1 = re- +// signed every superframe, so a re-aired copy is +// fresh for about one superframe, not one window // CELL_CA_PUB UE: the area CA's public key (64 hex): a beacon // counts as a cell's (measured, its NL taken) only // through CA -> cell key -> NL. @@ -83,11 +87,23 @@ // CELL_UE_NBR_PHASE_US [300] of the TBTT offset the // serving list names. Else it is not measured // (cell.ue.nbr_refused, the first 20 per reason; -// counts in cell.ue.done). +// counts in cell.ue.done, with the phase error of +// the neighbour beacons it took: nbr_phase_err_us). +// CELL_UE_NBR_PHASE_LOG [0] UE, test: 1 = log every neighbour beacon's +// phase error and verdict (cell.ue.nbr_phase). // CELL_ROLE=replayer test only: records cell CELL_REPLAY_CELL [2]'s // DU beacons for CELL_REPLAY_FREEZE_S [30] s, then // re-airs the last one every CELL_REPLAY_EVERY_MS // [100] ms (cell.replay.frozen, cell.replay.done). +// CELL_REPLAY_MODE=relay: instead, re-airs each +// genuine beacon as soon as it has it (a live +// store-and-forward relay), or with +// CELL_REPLAY_DELAY_SF=N the one captured N +// beacons earlier (a delayed store-and-forward +// replay — still off-phase, since send_packet has +// no sub-slot departure control; it ages the copy +// to probe the staleness check, not the phase +// one); cell.replay.relay per frame. // CELL_UE_KEY UE, per-cell keys: its own key K_(c,u) (home // cell and id), which unwraps the target cell's // key a handover carries. Without it, or for a @@ -601,7 +617,7 @@ bool du_budget_ok(const Cfg &c, const std::vector &ues) { c.cell, 0, std::vector(devourer::cell::kMaxNeighbours), {}); if (const char *sk = std::getenv("CELL_NL_SIGN_KEY"); sk && *sk) { const auto sig = devourer::cell::encode_nl_sig_ie( - c.cell, 0, 0, extra, [](const uint8_t *, size_t, uint8_t s[64]) { std::memset(s, 0, 64); return true; }); + c.cell, 0, 0, 1, extra, [](const uint8_t *, size_t, uint8_t s[64]) { std::memset(s, 0, 64); return true; }); const auto cert = devourer::cell::encode_nl_cert_ie(c.cell, {}, {}); extra.insert(extra.end(), sig.begin(), sig.end()); extra.insert(extra.end(), cert.begin(), cert.end()); @@ -725,7 +741,8 @@ int run_du(IRadio *dev, const Cfg &c) { } std::vector nl_signed_for, nl_sig_ie; /* the last NL signed, and its NS IE */ bool nl_sign_failed = false, nl_unsigned_no_net = false; - uint32_t nl_signed_win = 0; + const int64_t nl_win_sf = envl("CELL_NL_WINDOW_SF", devourer::cell::kNlWindowSf, 1, 64); + uint64_t nl_signed_win = 0; const int64_t release_sf = envl("CELL_RELEASE_SF", 50, 2, 100000); /* Suspend gaps (MeasGap.h): a UE some neighbour leaves no idle window * goes deaf in its suspend superframes; no DL to it then or the next one. @@ -3036,11 +3053,9 @@ int run_du(IRadio *dev, const Cfg &c) { ho_ies.insert(ho_ies.end(), cie.begin(), cie.end()); } /* Signed once per list and network window (its freshness: the - * network superframe / kNlWindowSf, on the shared timebase). */ + * network superframe / CELL_NL_WINDOW_SF, on the shared timebase). */ const int64_t net_sf_now = net_ok.load() ? sf + net_off.load() : -1; - const uint32_t win = net_sf_now >= 0 - ? static_cast(net_sf_now / devourer::cell::kNlWindowSf) - : 0; + const uint64_t win = net_sf_now >= 0 ? static_cast(net_sf_now / nl_win_sf) : 0; if (nl_sign && !nl.empty() && net_sf_now < 0 && !nl_unsigned_no_net) { /* No network superframe (no PTP service mode, or sync lost): no * window to sign, so the list airs unsigned and signed UEs refuse it. */ @@ -3053,7 +3068,8 @@ int run_du(IRadio *dev, const Cfg &c) { emit("{\"ev\":\"cell.nl.signed_again\"}"); } if (nl != nl_signed_for || win != nl_signed_win) { - auto sig = devourer::cell::encode_nl_sig_ie(c.cell, nl_gen_snap, win, nl, nl_sign); + auto sig = devourer::cell::encode_nl_sig_ie(c.cell, nl_gen_snap, win, static_cast(nl_win_sf), + nl, nl_sign); if (!sig.empty()) { nl_sig_ie = std::move(sig); nl_signed_for = nl; @@ -3471,13 +3487,18 @@ int run_ue(IRadio *dev, const Cfg &c) { * checks off (a test's counterfactual). */ const bool nbr_fresh = envl("CELL_UE_NBR_FRESH", 1, 0, 1) != 0; const int64_t nbr_phase_tol_us = envl("CELL_UE_NBR_PHASE_US", 300, 10, 50000); + const bool nbr_phase_log = envl("CELL_UE_NBR_PHASE_LOG", 0, 0, 1) != 0; + /* |phase error| of the neighbour beacons taken (us, bounded): how tightly + * genuine beacons land, against CELL_UE_NBR_PHASE_US. */ + std::vector nbr_phase_errs; + uint16_t serv_win_sf = devourer::cell::kNlWindowSf; /* the serving signature's window length */ bool have_serv_win = false, have_serv_phase = false; /* A cell switch: the new serving cell's first window may be one behind * (both sit on the one network superframe, but not at the same instant). */ bool serv_switched = false; uint64_t serv_win_ms = 0; /* when the serving window last advanced */ int serv_off_run = 0; /* serving copies off the reference phase in a row */ - uint32_t serv_win = 0; + uint64_t serv_win = 0; int64_t serv_phase = 0; std::map nbr_refused; if (const char *ca = std::getenv("CELL_CA_PUB"); ca && *ca) { @@ -3717,10 +3738,19 @@ int run_ue(IRadio *dev, const Cfg &c) { int64_t d = std::llabs(a - b) % P; return std::min(d, P - d); }; + /* Windows of different lengths (win_sf) are not comparable as raw + * `win`: compare on the shared network-superframe timeline, the + * net_sf a signature's window started at (win * win_sf). A switch + * or a length change gets one (coarser) window of slack; a + * same-length steady cell is strictly monotone, as before. */ + const int64_t sig_net = static_cast(sn->win) * sn->win_sf; + const int64_t serv_net = static_cast(serv_win) * serv_win_sf; if (serving) { - const uint32_t back = serv_switched ? 1u : 0u; - if (nbr_fresh && have_serv_win && sn->win + back < serv_win) { - why = "stale"; + const bool len_change = have_serv_win && sn->win_sf != serv_win_sf; + const int64_t unit = std::max(sn->win_sf, serv_win_sf); + const int64_t slack = (serv_switched || len_change) ? unit : 0; + if (nbr_fresh && have_serv_win && sig_net + slack < serv_net) { + why = "stale"; /* the signed window went back */ } else if (nbr_fresh && have_serv_phase && phase >= 0 && phase_off(phase, serv_phase) > nbr_phase_tol_us && ++serv_off_run < 20) { why = "phase"; /* 20 in a row, none on the reference: the cell moved */ @@ -3728,9 +3758,10 @@ int run_ue(IRadio *dev, const Cfg &c) { if (phase >= 0 && have_serv_phase && serv_off_run >= 20) have_serv_phase = false; /* re-taken below */ serv_off_run = 0; - if (!have_serv_win || sn->win > serv_win || serv_switched) + if (!have_serv_win || sig_net > serv_net || serv_switched || len_change) serv_win_ms = ue_ms(); serv_win = sn->win; + serv_win_sf = sn->win_sf; have_serv_win = true; serv_switched = false; if (phase >= 0) { /* where serving beacons land: the reference */ @@ -3746,15 +3777,19 @@ int run_ue(IRadio *dev, const Cfg &c) { } } } else if (nbr_fresh) { - const int64_t dw = static_cast(sn->win) - static_cast(serv_win); const auto it = std::find_if(nbrs.begin(), nbrs.end(), [&](const devourer::cell::Neighbour &x) { return x.cell == bc; }); /* The serving window itself must be current: one that has not * advanced for two windows' time is no "now" to compare with. */ const bool serv_current = - have_serv_win && ue_ms() - serv_win_ms <= - static_cast(2 * devourer::cell::kNlWindowSf * P / 1000); - if (!serv_current || dw < -1 || dw > 1) { + have_serv_win && ue_ms() - serv_win_ms <= static_cast(2 * serv_win_sf * P / 1000); + /* Fresh on the shared net_sf timeline: a live neighbour signs at + * the same net_sf as serving, so its window start is within one + * window (the coarser of the two lengths) of the serving one — + * whatever each cell's win_sf. An older on-phase copy is refused + * here; the window length each cell uses need not match. */ + const int64_t unit = std::max(sn->win_sf, serv_win_sf); + if (!serv_current || std::llabs(sig_net - serv_net) > unit) { why = "stale"; } else if (it == nbrs.end()) { why = "unlisted"; @@ -3765,6 +3800,24 @@ int run_ue(IRadio *dev, const Cfg &c) { if (phase_off(phase, want) > nbr_phase_tol_us) why = "phase"; } + int64_t err = 0; + if (want >= 0 && phase >= 0) { + err = (phase - want) % P; + if (err > P / 2) err -= P; + if (err < -P / 2) err += P; + if (!why && nbr_phase_errs.size() < 50000) + nbr_phase_errs.push_back(std::llabs(err)); + } + if (nbr_phase_log && (want >= 0 || why)) { + char l[200]; + snprintf(l, sizeof l, + "{\"ev\":\"cell.ue.nbr_phase\",\"cell\":%u,\"err_us\":%lld,\"ok\":%d,\"why\":\"%s\"," + "\"win\":%llu,\"serving_win\":%llu,\"rssi\":%d,\"tsfl\":%u}", + bc, want >= 0 ? (long long)err : 0LL, why ? 0 : 1, why ? why : "", + (unsigned long long)sn->win, (unsigned long long)serv_win, + static_cast(p.RxAtrib.rssi[0]) - 110, p.RxAtrib.tsfl); + emit(l); + } } if (why) { nl.reset(); @@ -3772,9 +3825,10 @@ int run_ue(IRadio *dev, const Cfg &c) { if (n++ < 20) { char l[240]; snprintf(l, sizeof l, - "{\"ev\":\"cell.ue.nbr_refused\",\"cell\":%u,\"why\":\"%s\",\"win\":%u,\"serving_win\":%u," + "{\"ev\":\"cell.ue.nbr_refused\",\"cell\":%u,\"why\":\"%s\",\"win\":%llu,\"serving_win\":%llu," "\"phase\":%lld,\"want\":%lld,\"serv_phase\":%lld}", - bc, why, sn->win, serv_win, (long long)phase, (long long)want, + bc, why, (unsigned long long)sn->win, (unsigned long long)serv_win, + (long long)phase, (long long)want, have_serv_phase ? (long long)serv_phase : -1LL); emit(l); } @@ -5248,6 +5302,21 @@ int run_ue(IRadio *dev, const Cfg &c) { (unsigned long long)susp_heard, (unsigned long long)susp_fail, (unsigned long long)visits, (unsigned long long)visits_ok, (unsigned long long)visit_dl_rx, (unsigned long long)steps_held); emit(l); + /* How tightly the neighbour beacons it took landed (|phase error|, us), + * against CELL_UE_NBR_PHASE_US. */ + if (!nbr_phase_errs.empty()) { + std::sort(nbr_phase_errs.begin(), nbr_phase_errs.end()); + auto q = [&](double f) { + return nbr_phase_errs[std::min(nbr_phase_errs.size() - 1, static_cast(f * nbr_phase_errs.size()))]; + }; + char l2[220]; + snprintf(l2, sizeof l2, + "{\"ev\":\"cell.ue.nbr_phase_stats\",\"ue\":%u,\"n\":%zu,\"p50_us\":%lld,\"p99_us\":%lld," + "\"p999_us\":%lld,\"max_us\":%lld,\"tol_us\":%lld}", + me_a.load(), nbr_phase_errs.size(), (long long)q(0.5), (long long)q(0.99), (long long)q(0.999), + (long long)nbr_phase_errs.back(), (long long)nbr_phase_tol_us); + emit(l2); + } return 0; } @@ -5354,8 +5423,11 @@ int run_witness(IRadio *dev, const Cfg &c) { * cell CELL_REPLAY_CELL's genuine DU beacons until CELL_REPLAY_FREEZE_S, then * keeps re-airing the last one every CELL_REPLAY_EVERY_MS [100] until the end * — signature, certificate and all, never on the cell's TBTT phase. */ +int run_relay(IRadio *dev, const Cfg &c, uint16_t cell); int run_replayer(IRadio *dev, const Cfg &c) { const auto cell = static_cast(envl("CELL_REPLAY_CELL", 2, 1, 65535)); + if (const char *md = std::getenv("CELL_REPLAY_MODE"); md && std::strcmp(md, "relay") == 0) + return run_relay(dev, c, cell); const int freeze_s = static_cast(envl("CELL_REPLAY_FREEZE_S", 30, 1, 86400)); const int every_ms = static_cast(envl("CELL_REPLAY_EVERY_MS", 100, 10, 60000)); uint8_t du[6]; @@ -5405,6 +5477,81 @@ int run_replayer(IRadio *dev, const Cfg &c) { return 0; } +/* CELL_REPLAY_MODE=relay (test only): a live store-and-forward relay of cell + * `cell`'s genuine DU beacons — each re-aired as soon as the RX path has it + * (whole, CRC-good: what any radio that decodes before it re-airs has), or, + * with CELL_REPLAY_DELAY_SF=N, the one captured N beacons earlier — still a + * store-and-forward replay (off-phase: send_packet cannot place a frame on a + * sub-slot departure), N superframes staler, to probe the staleness check. + * Never sent from the RX callback (a DU froze doing that): a sender thread + * waits on it. */ +int run_relay(IRadio *dev, const Cfg &c, uint16_t cell) { + const int delay = static_cast(envl("CELL_REPLAY_DELAY_SF", 0, 0, 1000)); + uint8_t du[6]; + cell_mac(du, cell, 0); + std::mutex m; + std::condition_variable cv; + std::deque> ring; /* the last delay + 1 genuine beacons */ + std::optional> pending; + uint32_t pending_tsfl = 0; + uint64_t heard = 0; + dev->InitWrite(SelectedChannel{c.ch, 0, CHANNEL_WIDTH_20}); + std::thread rx([&] { + dev->StartRxLoop([&](const Packet &p) { + if (p.RxAtrib.crc_err || p.Data.size() < 36 || p.Data[0] != 0x80 || + std::memcmp(p.Data.data() + 10, du, 6) != 0) + return; + std::lock_guard lk(m); + ++heard; + ring.emplace_back(p.Data.begin(), p.Data.end()); + while (ring.size() > static_cast(delay) + 1) + ring.pop_front(); + if (ring.size() == static_cast(delay) + 1) { + pending = ring.front(); + pending_tsfl = p.RxAtrib.tsfl; + cv.notify_one(); + } + }); + }); + uint64_t sent = 0; + const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(c.secs); + while (std::chrono::steady_clock::now() < end) { + std::vector frame; + uint32_t rx_tsfl = 0; + { + std::unique_lock lk(m); + cv.wait_for(lk, std::chrono::milliseconds(200), [&] { return pending.has_value(); }); + if (!pending) + continue; + frame = std::move(*pending); + pending.reset(); + rx_tsfl = pending_tsfl; + } + auto pkt = build_dl_radiotap(6); + pkt.insert(pkt.end(), frame.begin(), frame.end()); + const bool ok = dev->send_packet(pkt.data(), pkt.size()); + sent += ok ? 1 : 0; + uint64_t t = 0, f = 0; + const bool rd = dev->ReadTsfAndFreeRun(t, f); + char l[200]; + snprintf(l, sizeof l, + "{\"ev\":\"cell.replay.relay\",\"cell\":%u,\"ok\":%d,\"len\":%zu,\"rx_tsfl\":%u," + "\"queued_tsfl\":%u,\"delay_sf\":%d}", + cell, ok ? 1 : 0, frame.size(), rx_tsfl, rd ? static_cast(t) : 0u, delay); + emit(l); + } + char l[140]; + { + std::lock_guard lk(m); + snprintf(l, sizeof l, "{\"ev\":\"cell.replay.done\",\"cell\":%u,\"mode\":\"relay\",\"heard\":%llu,\"sent\":%llu}", + cell, (unsigned long long)heard, (unsigned long long)sent); + } + emit(l); + dev->StopRxLoop(); + rx.join(); + return 0; +} + } // namespace int main() { diff --git a/src/cell/MeasGap.h b/src/cell/MeasGap.h index 6a227942..22ba1f97 100644 --- a/src/cell/MeasGap.h +++ b/src/cell/MeasGap.h @@ -51,10 +51,13 @@ * it could forge, so with the area key every UE could fake any cell's NL. A * DU therefore also SIGNS its NL (Ed25519, its own key) and airs a * certificate for that key signed by the area CA: - * NS IE: 'N' 'S' ver(1)=2 cell(2) gen(4) win(4) sig(64) - * sig over 'S' || the NL IE || win(4): win = the network superframe - * / kNlWindowSf, the signature's freshness (re-signed per window) - * NC IE: 'N' 'C' ver(1)=2 cell(2) pub(32) ca_sig(64) ca_sig over 'C' cell(2) pub + * NS IE: 'N' 'S' ver(1)=3 cell(2) gen(4) win(8) win_sf(2) sig(64) + * sig over 'S' || the NL IE || win(8) || win_sf(2): win = the network + * superframe / win_sf, the signature's freshness (re-signed per + * window). win_sf is the DU's (CELL_NL_WINDOW_SF, default + * kNlWindowSf); 1 = re-signed every superframe, so a copy is + * fresh for about one superframe, not about one window + * NC IE: 'N' 'C' ver(1)=3 cell(2) pub(32) ca_sig(64) ca_sig over 'C' cell(2) pub * A UE holding only the CA's PUBLIC key (CELL_CA_PUB) checks the chain * (NlVerifier) — and can forge nothing. It caches the NL bytes it verified * per cell, so a signature is checked once per list and window, not per @@ -64,8 +67,12 @@ * (fresh: the UE hears it live), and the serving window never goes back; * and the beacon must arrive at the TBTT offset the serving list names for * that neighbour (main.cpp: on the shared timebase a re-aired copy lands - * off-phase). What remains is a real-time repeater placed on the right - * phase — only distance bounding would stop that. + * off-phase). What remains is a copy within the freshness window that lands + * within the phase tolerance: a live relay, or an on-phase replay at most + * about one window old. A store-and-forward relay must receive the whole + * beacon before re-airing it, so a tolerance below its airtime leaves only + * a cut-through relay (a repeater with almost no latency: physically, a + * reflector). Only distance bounding would stop that. * The crypto is injected (examples fill it from OpenSSL), as the HMAC is. * * Pure header; the cell_m6 ctest covers it. */ @@ -287,8 +294,8 @@ inline std::optional find_mr_ie(const uint8_t *mpdu, size_t len, uint16_t } /* --- Signed neighbour lists (see the header). --- */ -constexpr uint8_t kNlSigVersion = 2; -constexpr uint32_t kNlWindowSf = 64; /* ~6.5 s at 100 TU: a signature's freshness window */ +constexpr uint8_t kNlSigVersion = 3; +constexpr uint32_t kNlWindowSf = 64; /* ~6.5 s at 100 TU: the default freshness window */ using Ed25519Pub = std::array; using Ed25519Sig = std::array; /* msg -> its signature under the signer's key. False on failure. */ @@ -341,27 +348,31 @@ inline std::optional> raw_nl_ie(const uint8_t *mpdu, size_t } /* namespace mg_detail */ namespace mg_detail { -inline std::vector nl_sig_message(const std::vector &nl_ie, uint32_t win) { +inline std::vector nl_sig_message(const std::vector &nl_ie, uint64_t win, uint16_t win_sf) { std::vector m = {'S'}; m.insert(m.end(), nl_ie.begin(), nl_ie.end()); - put(m, win, 4); + put(m, win, 8); + put(m, win_sf, 2); return m; } } /* namespace mg_detail */ -/* The NS IE for an NL IE (its full bytes, as aired) in network window `win`. - * Empty if signing fails. */ -inline std::vector encode_nl_sig_ie(uint16_t cell, uint32_t gen, uint32_t win, +/* The NS IE for an NL IE (its full bytes, as aired) in network window `win` + * of `win_sf` superframes. Empty if signing fails (or win_sf is 0). */ +inline std::vector encode_nl_sig_ie(uint16_t cell, uint32_t gen, uint64_t win, uint16_t win_sf, const std::vector &nl_ie, const NlSigner &sign) { using namespace mg_detail; - const auto msg = nl_sig_message(nl_ie, win); + if (win_sf == 0) + return {}; + const auto msg = nl_sig_message(nl_ie, win, win_sf); uint8_t sig[64]; if (!sign || !sign(msg.data(), msg.size(), sig)) return {}; std::vector b = {'N', 'S', kNlSigVersion}; put(b, cell, 2); put(b, gen, 4); - put(b, win, 4); + put(b, win, 8); + put(b, win_sf, 2); b.insert(b.end(), sig, sig + 64); return plain_ie(b); } @@ -369,7 +380,8 @@ inline std::vector encode_nl_sig_ie(uint16_t cell, uint32_t gen, uint32 /* A verified list and the network window its signature names. */ struct SignedNl { NlIe nl; - uint32_t win = 0; + uint64_t win = 0; + uint16_t win_sf = kNlWindowSf; /* the window's length, as signed */ }; /* The NC IE: this cell's signing key and the CA's signature over it. */ inline std::vector encode_nl_cert_ie(uint16_t cell, const Ed25519Pub &pub, const Ed25519Sig &ca_sig) { @@ -404,20 +416,22 @@ class NlVerifier { if (!nl) return std::nullopt; /* The signature IE for this list's gen: its window (and signature). */ - std::optional win; + std::optional win; + uint16_t win_sf = 0; std::array sig{}; for_each_ie(mpdu, len, 'N', 'S', [&](const uint8_t *b, size_t n) { - if (win || n != 13 + 64 || b[2] != kNlSigVersion || get(b + 3, 2) != cell || - static_cast(get(b + 5, 4)) != nl->gen) + if (win || n != 19 + 64 || b[2] != kNlSigVersion || get(b + 3, 2) != cell || + static_cast(get(b + 5, 4)) != nl->gen || get(b + 17, 2) == 0) return; - win = static_cast(get(b + 9, 4)); - std::copy(b + 13, b + 77, sig.begin()); + win = get(b + 9, 8); + win_sf = static_cast(get(b + 17, 2)); + std::copy(b + 19, b + 83, sig.begin()); }); if (!win) return std::nullopt; - const auto msg = nl_sig_message(*raw, *win); + const auto msg = nl_sig_message(*raw, *win, win_sf); if (auto it = ok_.find(cell); it != ok_.end() && it->second == msg) - return SignedNl{*nl, *win}; /* verified before, byte for byte */ + return SignedNl{*nl, *win, win_sf}; /* verified before, byte for byte */ /* The cell's certificate (verified once per key). */ for_each_ie(mpdu, len, 'N', 'C', [&](const uint8_t *b, size_t n) { if (n != 5 + 32 + 64 || b[2] != kNlSigVersion || get(b + 3, 2) != cell) @@ -436,7 +450,7 @@ class NlVerifier { if (!verify_ || !verify_(key->second.data(), msg.data(), msg.size(), sig.data())) return std::nullopt; ok_[cell] = msg; - return SignedNl{*nl, *win}; + return SignedNl{*nl, *win, win_sf}; } private: diff --git a/tests/cell_m6_selftest.cpp b/tests/cell_m6_selftest.cpp index 9f540467..bc4860e0 100644 --- a/tests/cell_m6_selftest.cpp +++ b/tests/cell_m6_selftest.cpp @@ -1039,7 +1039,7 @@ int main() { const auto nl = encode_nl_ie(cell, gen, list, Mac{}); b.insert(b.end(), nl.begin(), nl.end()); if (with_sig) { - const auto sg = encode_nl_sig_ie(cell, gen, 500, nl, signer(sign_key)); + const auto sg = encode_nl_sig_ie(cell, gen, 500, 64, nl, signer(sign_key)); b.insert(b.end(), sg.begin(), sg.end()); } const auto ce = encode_nl_cert_ie(cell, cert_pub, cert_sig); @@ -1050,7 +1050,7 @@ int main() { NlVerifier v(fake_pub(ca), verify); auto good = beacon(2, 7, l1, k2, true, fake_pub(k2), cert2); auto r = v.check(good.data(), good.size(), 2); - CHECK(r && r->nl.gen == 7 && r->nl.cells == l1 && r->win == 500); /* the chain holds, with its window */ + CHECK(r && r->nl.gen == 7 && r->nl.cells == l1 && r->win == 500 && r->win_sf == 64); /* the chain holds */ auto again = beacon(2, 7, l1, k2, true, fake_pub(k2), cert2); CHECK(v.check(again.data(), again.size(), 2)); /* the same list + window: from the cache */ const std::vector l2 = {{1, 149, 12345}}; @@ -1075,7 +1075,7 @@ int main() { swapped[0] = 0x80; const auto nl_a = encode_nl_ie(2, 9, l1, Mac{}); const auto nl_b = encode_nl_ie(2, 9, l2, Mac{}); - const auto sg = encode_nl_sig_ie(2, 9, 500, nl_b, signer(k2)); + const auto sg = encode_nl_sig_ie(2, 9, 500, 64, nl_b, signer(k2)); const auto ce = encode_nl_cert_ie(2, fake_pub(k2), cert2); for (const auto *ie : {&nl_a, &sg, &ce}) swapped.insert(swapped.end(), ie->begin(), ie->end()); @@ -1098,6 +1098,42 @@ int main() { auto w2 = beacon(2, 12, l1, k2, true, fake_pub(k2), cert2); auto ok2 = v3.check(w2.data(), w2.size(), 2); CHECK(ok2 && ok2->win == 500); + /* v3: the window's LENGTH is signed too — relabelling a 64-superframe + * signature as a 1-superframe one (or back) is refused; a zero length + * is never valid. */ + NlVerifier v4(fake_pub(ca), verify); + auto w3 = beacon(2, 13, l1, k2, true, fake_pub(k2), cert2); + for (size_t i = 36; i + 1 < w3.size(); ++i) + if (w3[i] == 221 && w3[i + 5] == 'N' && w3[i + 6] == 'S') { + w3[i + 2 + 3 + 17] = 1; /* win_sf's low byte (after the 8-byte win): 64 -> 1 */ + break; + } + CHECK(!v4.check(w3.data(), w3.size(), 2)); + { + std::vector b1(36, 0); + b1[0] = 0x80; + const auto nl = encode_nl_ie(2, 14, l1, Mac{}); + const auto sg = encode_nl_sig_ie(2, 14, 77000, 1, nl, signer(k2)); /* per superframe */ + const auto ce = encode_nl_cert_ie(2, fake_pub(k2), cert2); + for (const auto *ie : {&nl, &sg, &ce}) + b1.insert(b1.end(), ie->begin(), ie->end()); + auto o1 = v4.check(b1.data(), b1.size(), 2); + CHECK(o1 && o1->win == 77000 && o1->win_sf == 1); + CHECK(encode_nl_sig_ie(2, 14, 77000, 0, nl, signer(k2)).empty()); + /* win is 64-bit: net_sf / win_sf=1 can exceed 2^32 (net_sf is + * PTP-derived, ~2^34), so a small window must not overflow it. */ + NlVerifier v5(fake_pub(ca), verify); + std::vector b2(36, 0); + b2[0] = 0x80; + const uint64_t big = (uint64_t{1} << 34) + 123; /* > 2^32 */ + const auto nl2 = encode_nl_ie(2, 15, l1, Mac{}); + const auto sg2 = encode_nl_sig_ie(2, 15, big, 1, nl2, signer(k2)); + const auto ce2 = encode_nl_cert_ie(2, fake_pub(k2), cert2); + for (const auto *ie : {&nl2, &sg2, &ce2}) + b2.insert(b2.end(), ie->begin(), ie->end()); + auto o2 = v5.check(b2.data(), b2.size(), 2); + CHECK(o2 && o2->win == big && o2->win_sf == 1); + } /* A list with no signature IE at all: refused even when cached. */ auto bare = beacon(2, 12, l1, k2, false, fake_pub(k2), cert2); CHECK(!v3.check(bare.data(), bare.size(), 2));