use core::fmt;
use crate::drip::{AssembledAuth, AuthAssembler, Det};
use crate::geo::FloatExt;
use crate::wifi::BeaconInfo;
use crate::{
geo, Auth, Frame, IdType, Location, Message, SerialNumber, Transport, UasData, MESSAGE_SIZE,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Verdict {
Spoofed,
Suspicious,
Plausible,
Authenticated,
}
impl Verdict {
pub fn name(self) -> &'static str {
match self {
Verdict::Spoofed => "spoofed",
Verdict::Suspicious => "suspicious",
Verdict::Plausible => "plausible",
Verdict::Authenticated => "authenticated",
}
}
}
impl fmt::Display for Verdict {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Spoofer {
DroneRemoteIdSpoofer,
RemoteIdSpoofer,
}
impl Spoofer {
pub fn name(self) -> &'static str {
match self {
Spoofer::DroneRemoteIdSpoofer => "cyber-defence-campus/droneRemoteIDSpoofer",
Spoofer::RemoteIdSpoofer => "jjshoots/RemoteIDSpoofer",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Severity {
Weak,
Moderate,
Strong,
}
macro_rules! reasons {
($( $(#[$m:meta])* $name:ident = $code:literal, $sev:ident, $label:literal; )*) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
#[non_exhaustive]
pub enum Reason { $( $(#[$m])* $name = $code, )* }
impl Reason {
pub const ALL: &'static [Reason] = &[ $( Reason::$name, )* ];
const COUNT: usize = [ $( $code, )* ].len();
pub fn severity(self) -> Severity {
match self { $( Reason::$name => Severity::$sev, )* }
}
pub fn name(self) -> &'static str {
match self { $( Reason::$name => $label, )* }
}
}
};
}
reasons! {
KnownSpoofer = 0, Strong, "known_spoofer";
AuthFailed = 1, Strong, "auth_failed";
Teleport = 2, Strong, "teleport";
CounterConflict = 3, Strong, "counter_conflict";
IdentityClone = 4, Strong, "identity_clone";
TransportDisagreement = 5, Strong, "transport_disagreement";
SpeedImplausible = 6, Moderate, "speed_implausible";
ReportedSpeedMismatch = 7, Moderate, "reported_speed_mismatch";
ReportedCourseMismatch = 8, Moderate, "reported_course_mismatch";
VerticalRateImplausible = 9, Moderate, "vertical_rate_implausible";
TimestampFrozen = 10, Moderate, "timestamp_frozen";
LocationClockDrift = 11, Moderate, "location_clock_drift";
SystemClockDrift = 12, Moderate, "system_clock_drift";
TsfWallClock = 13, Moderate, "tsf_wall_clock";
TsfDiscontinuity = 14, Moderate, "tsf_discontinuity";
CounterInterleave = 15, Moderate, "counter_interleave";
SignalJump = 16, Moderate, "signal_jump";
SharedRadio = 17, Moderate, "shared_radio";
InvalidFields = 18, Moderate, "invalid_fields";
SerialFormat = 19, Weak, "serial_format";
NoBasicId = 20, Weak, "no_basic_id";
AuthExpired = 21, Weak, "auth_expired";
}
impl fmt::Display for Reason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ReasonSet(u32);
impl ReasonSet {
pub fn insert(&mut self, r: Reason) {
self.0 |= 1 << r as u8;
}
pub fn contains(&self, r: Reason) -> bool {
self.0 & (1 << r as u8) != 0
}
pub fn is_empty(&self) -> bool {
self.0 == 0
}
pub fn len(&self) -> usize {
self.0.count_ones() as usize
}
pub fn iter(&self) -> impl Iterator<Item = Reason> + '_ {
Reason::ALL
.iter()
.copied()
.filter(move |r| self.contains(*r))
}
pub fn count(&self, s: Severity) -> usize {
self.iter().filter(|r| r.severity() == s).count()
}
}
impl fmt::Debug for ReasonSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AuthState {
#[default]
None,
Partial,
Unsupported,
Unverifiable,
Verified,
Trusted,
Unverified,
Questionable,
Conflicting,
}
impl AuthState {
pub fn name(self) -> &'static str {
match self {
AuthState::None => "none",
AuthState::Partial => "partial",
AuthState::Unsupported => "unsupported",
AuthState::Unverifiable => "unverifiable",
AuthState::Verified => "verified",
AuthState::Trusted => "trusted",
AuthState::Unverified => "unverified",
AuthState::Questionable => "questionable",
AuthState::Conflicting => "conflicting",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Assessment {
pub verdict: Verdict,
pub score: f32,
pub reasons: ReasonSet,
pub spoofer: Option<Spoofer>,
pub auth: AuthState,
}
impl Default for Assessment {
fn default() -> Self {
Assessment {
verdict: Verdict::Plausible,
score: 0.5,
reasons: ReasonSet::default(),
spoofer: None,
auth: AuthState::None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct TrustConfig {
pub max_speed_mps: f64,
pub max_vertical_mps: f64,
pub default_accuracy_m: f64,
pub max_location_drift_s: f64,
pub max_system_drift_s: f64,
pub memory_ms: u64,
pub accept_self_certified: bool,
pub auth_fresh_ms: u64,
pub auth_skew_s: u32,
pub auth_requires_clock: bool,
pub moderate_spoofed: Option<u8>,
pub min_evaluations: u16,
pub fingerprints: bool,
}
impl Default for TrustConfig {
fn default() -> Self {
TrustConfig {
max_speed_mps: 100.0,
max_vertical_mps: 40.0,
default_accuracy_m: 30.0,
max_location_drift_s: 30.0,
max_system_drift_s: 900.0,
memory_ms: 300_000,
accept_self_certified: false,
auth_fresh_ms: 30_000,
auth_skew_s: 120,
auth_requires_clock: true,
moderate_spoofed: None,
min_evaluations: 3,
fingerprints: true,
}
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct Observation<'a> {
pub transport: Transport,
pub counter: Option<u8>,
pub payload: &'a [u8],
pub now_ms: u64,
pub unix_ms: Option<u64>,
pub rssi_dbm: Option<i8>,
pub beacon: Option<BeaconInfo>,
pub receiver_id: u16,
}
impl<'a> Observation<'a> {
pub fn new(transport: Transport, counter: Option<u8>, payload: &'a [u8], now_ms: u64) -> Self {
Observation {
transport,
counter,
payload,
now_ms,
unix_ms: None,
rssi_dbm: None,
beacon: None,
receiver_id: 0,
}
}
pub fn with_unix_ms(mut self, unix_ms: u64) -> Self {
self.unix_ms = Some(unix_ms);
self
}
pub fn with_rssi(mut self, rssi_dbm: i8) -> Self {
self.rssi_dbm = Some(rssi_dbm);
self
}
pub fn with_beacon(mut self, beacon: BeaconInfo) -> Self {
self.beacon = Some(beacon);
self
}
pub fn with_receiver(mut self, receiver_id: u16) -> Self {
self.receiver_id = receiver_id;
self
}
}
const BUCKETS: usize = 4;
#[derive(Debug, Clone, Copy, Default)]
struct Tally {
total: u16,
last_hit_ms: u64,
buckets: [(u32, u16, u16); BUCKETS],
}
impl Tally {
fn record(&mut self, hit: bool, now: u64, bucket_ms: u64) {
let epoch = (now / bucket_ms) as u32;
let b = &mut self.buckets[epoch as usize % BUCKETS];
if b.0 != epoch {
*b = (epoch, 0, 0);
}
b.2 = b.2.saturating_add(1);
if hit {
b.1 = b.1.saturating_add(1);
self.total = self.total.saturating_add(1);
self.last_hit_ms = now;
}
}
fn window(&self, now: u64, bucket_ms: u64) -> (u32, u32) {
let epoch = (now / bucket_ms) as u32;
self.buckets
.iter()
.filter(|b| (b.1 > 0 || b.2 > 0) && epoch.wrapping_sub(b.0) < BUCKETS as u32)
.fold((0, 0), |(h, e), b| (h + b.1 as u32, e + b.2 as u32))
}
}
const OUTLIER: usize = Reason::COUNT;
#[derive(Debug, Clone, Copy, Default)]
struct Fix {
t: u64,
lat: f64,
lon: f64,
alt: Option<f32>,
speed: Option<f32>,
dir: Option<f32>,
vspeed: Option<f32>,
hacc: f64,
vacc: f64,
ts: Option<f32>,
}
const FIXES: usize = 16;
const RECENT: usize = 16;
#[cfg(feature = "verify")]
const HASHES: usize = 48;
const COUNTER_SLOTS: usize = 8;
const RECEIVERS: usize = 4;
fn receiver_slot<T: Copy>(table: &[Option<(u16, T, u64)>; RECEIVERS], id: u16) -> usize {
if let Some(i) = table.iter().position(|e| e.is_some_and(|e| e.0 == id)) {
return i;
}
if let Some(i) = table.iter().position(Option::is_none) {
return i;
}
(0..RECEIVERS)
.min_by_key(|&i| table[i].map_or(0, |e| e.2))
.unwrap_or(0)
}
#[derive(Debug, Clone, Copy, Default)]
struct AuthTally {
pages: u32,
complete: u32,
unsupported: u32,
unverifiable: u32,
valid: u32,
invalid: u32,
verified_ms: Option<u64>,
trusted_ms: Option<u64>,
verified_clocked_ms: Option<u64>,
trusted_clocked_ms: Option<u64>,
any_verified: bool,
any_trusted: bool,
det: Option<Det>,
}
#[derive(Debug, Clone)]
pub struct TrustState {
data: UasData,
frames: u64,
decode_errors: u64,
first_ms: Option<u64>,
last_ms: u64,
tallies: [Tally; Reason::COUNT + 1],
bucket_ms: u64,
spoofer: Option<Spoofer>,
fixes: [Fix; FIXES],
nfix: usize,
jump_from: Option<Fix>,
ts_nonzero: bool,
last_window_ms: Option<u64>,
locations: u32,
counters: [[Option<(u8, u32, u64)>; COUNTER_SLOTS]; 6],
tsf: [Option<(u16, u64, u64)>; RECEIVERS],
beacon: Option<BeaconInfo>,
last_rssi: [Option<(u16, i8, u64)>; RECEIVERS],
system_ts_zero: bool,
transports: u8,
recent: [[u8; MESSAGE_SIZE]; RECENT],
nrecent: usize,
#[cfg(feature = "verify")]
hashes: [([u8; 8], u8, bool); HASHES],
#[cfg(feature = "verify")]
nhash: usize,
assembler: AuthAssembler,
pending: Option<AssembledAuth>,
pack_msgs: [[u8; MESSAGE_SIZE]; 9],
npack: usize,
auth: AuthTally,
}
impl Default for TrustState {
fn default() -> Self {
TrustState {
data: UasData::default(),
frames: 0,
decode_errors: 0,
first_ms: None,
last_ms: 0,
tallies: [Tally::default(); Reason::COUNT + 1],
bucket_ms: TrustConfig::default().memory_ms / BUCKETS as u64,
spoofer: None,
fixes: [Fix::default(); FIXES],
nfix: 0,
jump_from: None,
ts_nonzero: false,
last_window_ms: None,
locations: 0,
counters: [[None; COUNTER_SLOTS]; 6],
tsf: [None; RECEIVERS],
beacon: None,
last_rssi: [None; RECEIVERS],
system_ts_zero: false,
transports: 0,
recent: [[0; MESSAGE_SIZE]; RECENT],
nrecent: 0,
#[cfg(feature = "verify")]
hashes: [([0; 8], 0, false); HASHES],
#[cfg(feature = "verify")]
nhash: 0,
assembler: AuthAssembler::new(),
pending: None,
pack_msgs: [[0; MESSAGE_SIZE]; 9],
npack: 0,
auth: AuthTally::default(),
}
}
}
fn fnv1a(data: &[u8]) -> u32 {
let mut h: u32 = 0x811C_9DC5;
for b in data {
h ^= *b as u32;
h = h.wrapping_mul(0x0100_0193);
}
h
}
fn wrap_hour(mut x: f64) -> f64 {
while x >= 1800.0 {
x -= 3600.0;
}
while x < -1800.0 {
x += 3600.0;
}
x
}
fn circular_mean_deg(sum_sin: f64, sum_cos: f64) -> f64 {
let m = geo::atan2(sum_sin, sum_cos).to_degrees();
if m < 0.0 {
m + 360.0
} else {
m
}
}
fn is_alnum16(s: &str) -> bool {
s.len() == 16 && s.bytes().all(|c| c.is_ascii_alphanumeric())
}
impl TrustState {
pub fn new() -> Self {
Self::default()
}
pub fn data(&self) -> &UasData {
&self.data
}
pub fn frames(&self) -> u64 {
self.frames
}
pub fn decode_errors(&self) -> u64 {
self.decode_errors
}
pub fn transports(&self) -> impl Iterator<Item = Transport> + '_ {
Transport::ALL
.iter()
.copied()
.filter(move |t| self.transports & (1 << t.code()) != 0)
}
pub fn dets(&self) -> impl Iterator<Item = Det> + '_ {
self.data
.basic_id
.iter()
.flatten()
.filter_map(Det::from_basic_id)
}
pub fn note(&mut self, reason: Reason, now_ms: u64) {
self.hit(reason, now_ms);
}
fn hit(&mut self, r: Reason, now: u64) {
self.eval(r, true, now);
}
fn eval(&mut self, r: Reason, hit: bool, now: u64) {
let bucket_ms = self.bucket_ms;
self.tallies[r as usize].record(hit, now, bucket_ms);
}
fn active(&self, r: Reason, cfg: &TrustConfig, now: u64) -> bool {
let t = &self.tallies[r as usize];
if r == Reason::Teleport {
let (jumps, _) = t.window(now, self.bucket_ms);
let (outliers, _) = self.tallies[OUTLIER].window(now, self.bucket_ms);
return 2 * jumps + outliers >= 3;
}
if t.total == 0 {
return false;
}
if r == Reason::KnownSpoofer {
return true;
}
if now.saturating_sub(t.last_hit_ms) > cfg.memory_ms {
return false;
}
let (hits, evals) = t.window(now, self.bucket_ms);
let ratio = hits >= cfg.min_evaluations as u32 && hits * 2 >= evals;
match r {
Reason::KnownSpoofer | Reason::AuthFailed | Reason::TsfWallClock => true,
Reason::SerialFormat | Reason::NoBasicId | Reason::AuthExpired => true,
Reason::Teleport => unreachable!("handled above"),
Reason::IdentityClone | Reason::TransportDisagreement => hits >= 2,
Reason::SystemClockDrift | Reason::SharedRadio => hits >= 2,
Reason::CounterConflict
| Reason::TsfDiscontinuity
| Reason::CounterInterleave
| Reason::SignalJump
| Reason::InvalidFields
| Reason::TimestampFrozen => hits >= 3,
Reason::SpeedImplausible
| Reason::ReportedSpeedMismatch
| Reason::ReportedCourseMismatch
| Reason::VerticalRateImplausible
| Reason::LocationClockDrift => ratio,
}
}
pub fn observe(&mut self, cfg: &TrustConfig, obs: &Observation<'_>) -> Assessment {
let now = obs.now_ms;
self.bucket_ms = (cfg.memory_ms / BUCKETS as u64).max(1);
self.frames += 1;
self.first_ms.get_or_insert(now);
self.last_ms = self.last_ms.max(now);
self.transports |= 1 << obs.transport.code();
if let Some(rssi) = obs.rssi_dbm {
let i = receiver_slot(&self.last_rssi, obs.receiver_id);
if let Some((id, prev, t)) = self.last_rssi[i] {
if id == obs.receiver_id && now.saturating_sub(t) <= 500 {
let jump = (rssi as i16 - prev as i16).abs() >= 25;
self.eval(Reason::SignalJump, jump, now);
}
}
self.last_rssi[i] = Some((obs.receiver_id, rssi, now));
}
if let Some(b) = obs.beacon {
self.check_beacon(&b, obs.unix_ms, obs.receiver_id, now);
self.beacon = Some(b);
}
let frame = match Frame::decode(obs.payload) {
Ok(f) => f,
Err(_) => {
self.decode_errors += 1;
return self.assessment(cfg, now);
}
};
if let Some(c) = obs.counter {
self.check_counter(obs.transport, &frame, c, obs.payload, now);
}
let mut auth_pages: [Option<Auth>; 9] = Default::default();
let mut nauth = 0;
self.npack = 0;
let is_pack = matches!(frame, Frame::Pack(_));
#[cfg(feature = "verify")]
if is_pack {
let has_loc =
matches!(&frame, Frame::Pack(p) if p.raw_messages().any(|m| m[0] >> 4 == 1));
self.push_hash(crate::auth::auth_hash(obs.payload), 15, has_loc);
}
let mut raws: [Option<[u8; MESSAGE_SIZE]>; 9] = [None; 9];
match &frame {
Frame::Message(_) => raws[0] = obs.payload[..MESSAGE_SIZE].try_into().ok(),
Frame::Pack(p) => {
for (slot, raw) in raws.iter_mut().zip(p.raw_messages()) {
*slot = Some(*raw);
}
}
}
for raw in raws.iter().flatten() {
let Ok(msg) = Message::decode(raw) else {
continue;
};
if raw[0] >> 4 != 2 {
self.push_recent(raw);
if is_pack && self.npack < 9 {
self.pack_msgs[self.npack] = *raw;
self.npack += 1;
}
#[cfg(feature = "verify")]
if !is_pack {
self.push_hash(crate::auth::auth_hash(raw), raw[0] >> 4, false);
}
}
self.data.update(&msg);
match &msg {
Message::Location(l) => self.on_location(cfg, l, obs.unix_ms, now),
Message::System(s) => {
if s.protocol_version >= 1 && s.timestamp == 0 {
self.system_ts_zero = true;
self.hit(Reason::SystemClockDrift, now);
} else if let (Some(ts), Some(unix_ms)) = (s.unix_timestamp(), obs.unix_ms) {
let drift = (ts as f64 - unix_ms as f64 / 1000.0).fabs();
self.eval(
Reason::SystemClockDrift,
drift > cfg.max_system_drift_s,
now,
);
}
}
Message::Auth(a) => {
self.auth.pages += 1;
if is_pack {
if nauth < auth_pages.len() {
auth_pages[nauth] = Some(a.clone());
nauth += 1;
}
} else {
let mut out = AssembledAuth::default();
let counter = obs.counter.unwrap_or(0);
if self.assembler.push_legacy(a, counter, &mut out) {
self.complete_auth(out, obs.unix_ms, cfg, now);
}
}
}
_ => {}
}
}
if nauth > 0 {
let mut out = AssembledAuth::default();
if AuthAssembler::push_pack(auth_pages.iter().flatten(), &mut out) {
self.complete_auth(out, obs.unix_ms, cfg, now);
}
}
self.check_identity(cfg, now);
self.assessment(cfg, now)
}
fn complete_auth(
&mut self,
out: AssembledAuth,
unix_ms: Option<u64>,
cfg: &TrustConfig,
now: u64,
) {
self.auth.complete += 1;
if let Some(unix_ms) = unix_ms {
if out.timestamp == 0 && out.protocol_version >= 1 {
self.hit(Reason::SystemClockDrift, now);
} else if out.timestamp != 0 {
let drift = (out.unix_timestamp() as f64 - unix_ms as f64 / 1000.0).fabs();
self.eval(
Reason::SystemClockDrift,
drift > cfg.max_system_drift_s,
now,
);
}
}
self.pending = Some(out);
}
fn push_recent(&mut self, raw: &[u8; MESSAGE_SIZE]) {
if self.recent[..self.nrecent.min(RECENT)].contains(raw) {
return;
}
self.recent[self.nrecent % RECENT] = *raw;
self.nrecent += 1;
}
#[cfg(feature = "verify")]
fn push_hash(&mut self, h: [u8; 8], t: u8, has_loc: bool) {
if self.hashes[..self.nhash.min(HASHES)]
.iter()
.any(|e| e.0 == h)
{
return;
}
self.hashes[self.nhash % HASHES] = (h, t, has_loc);
self.nhash += 1;
}
fn check_counter(
&mut self,
transport: Transport,
frame: &Frame<'_>,
counter: u8,
payload: &[u8],
now: u64,
) {
let slot = match frame {
Frame::Pack(_) => 7,
Frame::Message(m) => (u8::from(m.message_type()) as usize).min(6),
};
let hash = fnv1a(payload);
let entry = &mut self.counters[transport.code() as usize % 6][slot];
let mut conflict = false;
let mut restart = false;
if let Some((prev, prev_hash, t)) = *entry {
if prev == counter && prev_hash != hash && now.saturating_sub(t) < 3_000 {
conflict = true;
}
let step = counter.wrapping_sub(prev);
if step > 128 && 256 - step as u16 > 8 {
restart = true;
}
}
*entry = Some((counter, hash, now));
if conflict {
self.hit(Reason::CounterConflict, now);
}
if restart {
self.hit(Reason::CounterInterleave, now);
}
}
fn check_beacon(&mut self, b: &BeaconInfo, unix_ms: Option<u64>, receiver: u16, now: u64) {
let wall = match unix_ms {
Some(u) => {
b.tsf >= 1_000_000_000_000_000 || (b.tsf / 1000).abs_diff(u) < 400 * 86_400_000
}
None => b.tsf >= 1_000_000_000_000_000,
};
if wall {
self.hit(Reason::TsfWallClock, now);
}
let i = receiver_slot(&self.tsf, receiver);
if let Some((id, prev_tsf, prev_now)) = self.tsf[i] {
if id == receiver && b.tsf != 0 && prev_tsf != 0 {
let drx = now.saturating_sub(prev_now) as f64;
let dtsf = (b.tsf as f64 - prev_tsf as f64) / 1000.0;
let jump = dtsf < 0.0 || (dtsf - drx).fabs() > 500.0 + 0.05 * drx;
self.eval(Reason::TsfDiscontinuity, jump, now);
}
}
self.tsf[i] = Some((receiver, b.tsf, now));
}
fn on_location(&mut self, cfg: &TrustConfig, l: &Location, unix_ms: Option<u64>, now: u64) {
let issues = l.check().count();
if issues > 0 {
self.hit(Reason::InvalidFields, now);
}
let Some((lat, lon)) = l.position() else {
return;
};
if issues > 0 {
return;
}
self.locations += 1;
let acc = |m: Option<f32>| m.map_or(cfg.default_accuracy_m, |v| v as f64);
let fix = Fix {
t: now,
lat,
lon,
alt: l.altitude_geo.or(l.altitude_baro),
speed: l.speed_horizontal,
dir: l.direction,
vspeed: l.speed_vertical,
hacc: acc(l.horizontal_accuracy.meters()),
vacc: acc(l.vertical_accuracy.meters()),
ts: l.timestamp.filter(|t| *t != 0.0 || self.ts_nonzero),
};
if fix.ts.is_some_and(|t| t != 0.0) {
self.ts_nonzero = true;
}
if let (Some(ts), Some(unix_ms)) = (fix.ts, unix_ms) {
let rx = (unix_ms % 3_600_000) as f64 / 1000.0;
let drift = wrap_hour(ts as f64 - rx).fabs();
self.eval(
Reason::LocationClockDrift,
drift > cfg.max_location_drift_s,
now,
);
}
let reachable = |from: &Fix| {
let dt = now.saturating_sub(from.t) as f64 / 1000.0;
let d = geo::distance_m(from.lat, from.lon, lat, lon);
d <= 50.0 || d <= from.hacc + fix.hacc + cfg.max_speed_mps * dt
};
let mut stuck = false;
if let Some(before) = self.jump_from.take() {
if reachable(&before) {
self.nfix -= 1;
let bucket_ms = self.bucket_ms;
self.tallies[OUTLIER].record(true, now, bucket_ms);
} else {
self.hit(Reason::Teleport, now);
stuck = true;
}
}
let mut jumped = false;
if self.nfix > 0 {
let prev = self.fixes[(self.nfix - 1) % FIXES];
if now.saturating_sub(prev.t) >= 50 && !reachable(&prev) {
self.jump_from = Some(prev);
jumped = true;
}
}
self.fixes[self.nfix % FIXES] = fix;
self.nfix += 1;
if jumped && !stuck {
return;
}
if self
.last_window_ms
.is_some_and(|t| now.saturating_sub(t) < 1000)
{
return;
}
let held = self.nfix.min(FIXES);
let mut anchor = None;
for k in 1..held {
let f = self.fixes[(self.nfix - 1 - k) % FIXES];
let age = now.saturating_sub(f.t);
if age > 15_000 {
break;
}
if age >= 2_000 {
anchor = Some((k, f));
break;
}
}
let Some((k, a)) = anchor else { return };
self.last_window_ms = Some(now);
let dt = now.saturating_sub(a.t) as f64 / 1000.0;
let d = geo::distance_m(a.lat, a.lon, lat, lon);
let slack_v = (a.hacc + fix.hacc) / dt;
let v_obs = d / dt;
self.eval(
Reason::SpeedImplausible,
v_obs - slack_v > cfg.max_speed_mps,
now,
);
let (mut sp, mut nsp, mut ss, mut sc, mut ndir, mut vs, mut nvs) =
(0.0, 0, 0.0, 0.0, 0, 0.0, 0);
for j in 0..k {
let f = self.fixes[(self.nfix - 1 - j) % FIXES];
if let Some(s) = f.speed {
sp += s as f64;
nsp += 1;
}
if let Some(dir) = f.dir {
ss += geo::sin((dir as f64).to_radians());
sc += geo::cos((dir as f64).to_radians());
ndir += 1;
}
if let Some(v) = f.vspeed {
vs += v as f64;
nvs += 1;
}
}
if nsp > 0 {
let v_rep = sp / nsp as f64;
let tol = 3.0 + 0.3 * v_obs.max(v_rep) + slack_v;
self.eval(
Reason::ReportedSpeedMismatch,
(v_obs - v_rep).fabs() > tol,
now,
);
if ndir > 0 && v_rep >= 3.0 && v_obs - slack_v >= 3.0 {
let course = geo::bearing_deg(a.lat, a.lon, lat, lon);
let dir = circular_mean_deg(ss, sc);
self.eval(
Reason::ReportedCourseMismatch,
geo::angle_diff_deg(course, dir) > 45.0,
now,
);
}
}
if let (Some(h0), Some(h1)) = (a.alt, fix.alt) {
let rate = (h1 - h0) as f64 / dt;
let vslack = (a.vacc + fix.vacc) / dt;
let mut bad = rate.fabs() - vslack > cfg.max_vertical_mps;
if nvs > 0 {
let v_rep = vs / nvs as f64;
bad |= (rate - v_rep).fabs() > 3.0 + 0.5 * rate.fabs().max(v_rep.fabs()) + vslack;
}
self.eval(Reason::VerticalRateImplausible, bad, now);
}
if let (Some(t0), Some(t1)) = (a.ts, fix.ts) {
let dts = wrap_hour(t1 as f64 - t0 as f64);
if dts == 0.0 {
self.eval(Reason::TimestampFrozen, d > 5.0 || k >= 3, now);
} else if unix_ms.is_none() {
self.eval(Reason::LocationClockDrift, (dts - dt).fabs() > 5.0, now);
}
}
}
fn check_identity(&mut self, cfg: &TrustConfig, now: u64) {
let mut cdc = false;
let mut jj = false;
let mut serial_bad = false;
let mut has_basic = false;
let mut serial_text: Option<[u8; 20]> = None;
for b in self.data.basic_id.iter().flatten() {
if b.id_type == IdType::None {
continue;
}
has_basic = true;
let id = b.uas_id_str().unwrap_or("");
if id.starts_with("Spoofed_Serial_") {
cdc = true;
}
if b.id_type == IdType::SerialNumber {
serial_bad |= SerialNumber::parse(id).is_none();
serial_text = Some(b.uas_id);
}
}
let self_id = self
.data
.self_id
.as_ref()
.and_then(|s| s.description_str())
.unwrap_or("");
if self_id == "Spoofing test" {
cdc = true;
}
let operator = self
.data
.operator_id
.as_ref()
.and_then(|o| o.operator_id_str())
.unwrap_or("");
if let Some(b) = self.beacon {
let ssid = b.ssid();
if let Some(serial) = serial_text {
let n = serial.iter().position(|&c| c == 0).unwrap_or(20);
if ssid.len() == 4 + n && ssid.starts_with(b"RID-") && ssid[4..] == serial[..n] {
cdc = true;
}
}
if b.capability == 0 && self.system_ts_zero {
cdc = true;
}
if !operator.is_empty() && is_alnum16(operator) && ssid == operator.as_bytes() {
jj = true;
}
if b.interval_tu == 10 && self_id == "Recreational" {
jj = true;
}
}
if !cfg.fingerprints {
cdc = false;
jj = false;
}
if cdc {
self.spoofer = Some(Spoofer::DroneRemoteIdSpoofer);
self.hit(Reason::KnownSpoofer, now);
} else if jj {
self.spoofer = Some(Spoofer::RemoteIdSpoofer);
self.hit(Reason::KnownSpoofer, now);
}
if serial_bad {
self.hit(Reason::SerialFormat, now);
}
if !has_basic
&& self.locations >= 5
&& self
.first_ms
.is_some_and(|f| now.saturating_sub(f) >= 15_000)
{
self.hit(Reason::NoBasicId, now);
}
}
pub fn take_pending_auth(&mut self) -> Option<AssembledAuth> {
self.pending.take()
}
#[cfg(feature = "verify")]
pub fn record_auth(
&mut self,
msg: &AssembledAuth,
result: crate::auth::AuthResult,
now_ms: u64,
) {
use crate::auth::{AuthKind, AuthResult};
match result {
AuthResult::NotHandled | AuthResult::Unsupported => self.auth.unsupported += 1,
AuthResult::Unverifiable => self.auth.unverifiable += 1,
AuthResult::Valid {
kind,
signer,
trusted,
content_validated,
fresh,
} => {
if kind != AuthKind::Link
&& kind != AuthKind::Other
&& !self.dets().any(|d| d == signer)
{
self.auth.unverifiable += 1;
return;
}
self.auth.valid += 1;
if fresh == Some(false) {
self.hit(Reason::AuthExpired, now_ms);
return;
}
if kind != AuthKind::Link && content_validated {
self.auth.any_verified = true;
self.auth.verified_ms = Some(now_ms);
self.auth.det = Some(signer);
let clocked = fresh == Some(true);
if clocked {
self.auth.verified_clocked_ms = Some(now_ms);
}
if trusted {
self.auth.any_trusted = true;
self.auth.trusted_ms = Some(now_ms);
if clocked {
self.auth.trusted_clocked_ms = Some(now_ms);
}
}
}
}
AuthResult::Invalid(_) => {
if msg.from_pack || msg.recovered_page.is_none() {
self.auth.invalid += 1;
self.hit(Reason::AuthFailed, now_ms);
}
}
}
}
#[cfg(feature = "verify")]
pub fn verify_pending(
&mut self,
cfg: &TrustConfig,
scheme: &mut dyn crate::auth::AuthScheme,
keys: &mut dyn crate::auth::KeyStore,
now_ms: u64,
unix_ms: Option<u64>,
) -> Option<crate::auth::AuthResult> {
let msg = self.pending.take()?;
let mut dets = [Det::default(); 2];
let mut nd = 0;
for d in self.dets() {
if nd < 2 {
dets[nd] = d;
nd += 1;
}
}
let ctx = crate::auth::AuthContext {
uas_dets: &dets[..nd],
recent_messages: &self.recent[..self.nrecent.min(RECENT)],
recent_hashes: &self.hashes[..self.nhash.min(HASHES)],
pack_messages: if msg.from_pack {
&self.pack_msgs[..self.npack]
} else {
&[]
},
now_unix: unix_ms.map(|u| u / 1000),
skew_s: cfg.auth_skew_s,
};
let result = scheme.verify(&msg, &ctx, keys);
self.record_auth(&msg, result, now_ms);
Some(result)
}
pub fn authenticated_det(&self) -> Option<Det> {
if matches!(self.auth_state(), AuthState::Verified | AuthState::Trusted) {
self.auth.det
} else {
None
}
}
pub fn auth_state(&self) -> AuthState {
let a = &self.auth;
if a.invalid > 0 {
if a.any_trusted {
AuthState::Conflicting
} else if a.any_verified {
AuthState::Questionable
} else {
AuthState::Unverified
}
} else if a.any_trusted {
AuthState::Trusted
} else if a.any_verified {
AuthState::Verified
} else if a.unverifiable > 0 || a.valid > 0 || (a.complete > 0 && a.unsupported == 0) {
AuthState::Unverifiable
} else if a.unsupported > 0 {
AuthState::Unsupported
} else if a.pages > 0 {
AuthState::Partial
} else {
AuthState::None
}
}
pub fn assessment(&self, cfg: &TrustConfig, now_ms: u64) -> Assessment {
let mut reasons = ReasonSet::default();
for &r in Reason::ALL {
if self.active(r, cfg, now_ms) {
reasons.insert(r);
}
}
let strong = reasons.count(Severity::Strong);
let moderate = reasons.count(Severity::Moderate);
let weak = reasons.count(Severity::Weak);
let auth = self.auth_state();
let fresh =
|t: Option<u64>| t.is_some_and(|t| now_ms.saturating_sub(t) <= cfg.auth_fresh_ms);
let (trusted_ms, verified_ms) = if cfg.auth_requires_clock {
(self.auth.trusted_clocked_ms, self.auth.verified_clocked_ms)
} else {
(self.auth.trusted_ms, self.auth.verified_ms)
};
let authenticated = match auth {
AuthState::Trusted => {
fresh(trusted_ms) || (cfg.accept_self_certified && fresh(verified_ms))
}
AuthState::Verified => cfg.accept_self_certified && fresh(verified_ms),
_ => false,
};
let mut score: f32 =
0.5 - 0.4 * strong as f32 - 0.15 * moderate as f32 - 0.05 * weak as f32;
if authenticated {
score += 0.5;
} else if matches!(auth, AuthState::Verified | AuthState::Trusted) {
score += 0.2;
}
let moderate_spoofed = cfg
.moderate_spoofed
.is_some_and(|n| n > 0 && moderate >= n as usize);
let verdict = if strong > 0 || moderate_spoofed {
Verdict::Spoofed
} else if moderate > 0 || weak >= 2 {
Verdict::Suspicious
} else if authenticated {
Verdict::Authenticated
} else {
Verdict::Plausible
};
Assessment {
verdict,
score: score.clamp(0.0, 1.0),
reasons,
spoofer: if reasons.contains(Reason::KnownSpoofer) {
self.spoofer
} else {
None
},
auth,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{BasicId, Location, MessagePackBuilder, UaType};
fn loc(lat: f64, lon: f64, speed: f32, dir: f32, ts: f32) -> [u8; 25] {
Message::Location(Location {
latitude: lat,
longitude: lon,
speed_horizontal: Some(speed),
direction: Some(dir),
altitude_geo: Some(100.0),
timestamp: Some(ts),
horizontal_accuracy: crate::HorizontalAccuracy(10),
..Location::default()
})
.encode()
.unwrap()
}
#[test]
fn steady_flight_is_plausible() {
let cfg = TrustConfig::default();
let mut st = TrustState::new();
let id = Message::BasicId(
BasicId::new(
IdType::SerialNumber,
UaType::HelicopterOrMultirotor,
"1596F35074529ABCDEFG",
)
.unwrap(),
)
.encode()
.unwrap();
let mut a = Assessment::default();
for i in 0..30u64 {
let lat = 47.0 + (10.0 * i as f64) / 111_195.0;
let mut b = MessagePackBuilder::new();
b.push_raw(&id).unwrap();
b.push_raw(&loc(lat, 8.0, 10.0, 0.0, 600.0 + i as f32))
.unwrap();
let obs = Observation::new(
Transport::Bluetooth5,
Some(i as u8),
b.as_bytes(),
1_000 * i,
);
a = st.observe(&cfg, &obs);
}
assert_eq!(a.verdict, Verdict::Plausible, "{:?}", a.reasons);
assert!(a.reasons.is_empty(), "{:?}", a.reasons);
}
#[test]
fn teleport_and_lies_are_caught() {
let cfg = TrustConfig::default();
let mut st = TrustState::new();
let mut a = Assessment::default();
for i in 0..20u64 {
let lon = 8.0 + (2_000.0 * i as f64) / 75_800.0;
let raw = loc(47.0, lon, 5.0, 0.0, 600.0 + i as f32);
let obs = Observation::new(Transport::Bluetooth4, Some(i as u8), &raw, 1_000 * i);
a = st.observe(&cfg, &obs);
}
assert_eq!(a.verdict, Verdict::Spoofed);
assert!(a.reasons.contains(Reason::Teleport));
assert!(a.reasons.contains(Reason::ReportedSpeedMismatch));
assert!(a.reasons.contains(Reason::ReportedCourseMismatch));
}
#[test]
fn counter_reuse_with_new_content_is_a_conflict() {
let cfg = TrustConfig::default();
let mut st = TrustState::new();
let mut a = Assessment::default();
for i in 0..10u64 {
for (k, lon) in [(0u8, 8.0), (1, 8.001)] {
let raw = loc(47.0, lon + i as f64 * 1e-5, 1.0, 90.0, 600.0 + i as f32);
let obs = Observation::new(
Transport::Bluetooth4,
Some(i as u8),
&raw,
1_000 * i + k as u64,
);
a = st.observe(&cfg, &obs);
}
}
assert!(
a.reasons.contains(Reason::CounterConflict),
"{:?}",
a.reasons
);
assert_eq!(a.verdict, Verdict::Spoofed);
}
#[test]
fn reasons_have_unique_names_and_codes() {
for (i, r) in Reason::ALL.iter().enumerate() {
assert_eq!(*r as usize, i);
assert_eq!(
Reason::ALL.iter().filter(|o| o.name() == r.name()).count(),
1
);
}
}
}