lib.rs
| 1 | //! OTP/1 — the opentracker wire protocol. |
| 2 | //! |
| 3 | //! One encrypted UDP datagram per report, no handshake, no connection state. A |
| 4 | //! cold TCP+TLS connection costs roughly ten round trips before the first byte |
| 5 | //! of payload, and a phone that sleeps between reports has a cold connection |
| 6 | //! nearly every time; a single datagram that survives the phone changing IP |
| 7 | //! mid-journey is the entire point of this protocol. |
| 8 | //! |
| 9 | //! This crate is the codec and nothing else: **no I/O, no async, no RNG, no |
| 10 | //! clock.** Nonces and timestamps are passed in by the caller. That is what |
| 11 | //! makes it fuzzable, property-testable, and able to emit reproducible golden |
| 12 | //! vectors — which are the contract the Kotlin implementation is checked |
| 13 | //! against, and the thing that stops a protocol change from silently bricking |
| 14 | //! installed apps. |
| 15 | //! |
| 16 | //! ## Shape of a datagram |
| 17 | //! |
| 18 | //! ```text |
| 19 | //! header[21] || ChaCha20Poly1305(K_dir, nonce, payload, aad = header) |
| 20 | //! ``` |
| 21 | //! |
| 22 | //! ## Why there is no replay protection |
| 23 | //! |
| 24 | //! Timestamps are absolute and client-supplied, and the server stores points |
| 25 | //! under `UNIQUE(user_id, ts)` with `ON CONFLICT DO UPDATE`. A replayed |
| 26 | //! datagram therefore carries a timestamp that already exists and collapses |
| 27 | //! into the row already there — replay is idempotent *by construction*. An |
| 28 | //! attacker without the key cannot forge new positions and cannot create |
| 29 | //! duplicate rows, so a counter, a persisted ledger and a replay window would |
| 30 | //! all be state to maintain for no gain. |
| 31 | //! |
| 32 | //! ## Example |
| 33 | //! |
| 34 | //! ``` |
| 35 | //! use otproto::{Header, Message, MsgType, Point, kdf, msg::Direction}; |
| 36 | //! |
| 37 | //! let token_key = [0x11; 32]; |
| 38 | //! let k_up = kdf::derive(&token_key, Direction::Up); |
| 39 | //! let nonce = [0x22; 12]; // in production: 12 fresh random bytes |
| 40 | //! |
| 41 | //! let msg = Message::Loc(vec![Point::new(1_785_000_042, 525_200_080, 134_050_000)]); |
| 42 | //! let datagram = otproto::seal_message(&k_up, 0x1122_3344_5566_7788, nonce, &msg); |
| 43 | //! assert_eq!(datagram.len(), 62); |
| 44 | //! |
| 45 | //! // The server peeks the header to choose a key, then opens. |
| 46 | //! let peeked = Header::peek(&datagram).unwrap(); |
| 47 | //! assert_eq!(peeked.msg_type, MsgType::Loc); |
| 48 | //! let (_, back) = otproto::open_message(&k_up, &datagram).unwrap(); |
| 49 | //! assert_eq!(back, msg); |
| 50 | //! ``` |
| 51 | |
| 52 | pub mod error; |
| 53 | pub mod frame; |
| 54 | pub mod kdf; |
| 55 | pub mod msg; |
| 56 | pub mod point; |
| 57 | |
| 58 | pub use error::{DecodeError, ValidationError}; |
| 59 | pub use frame::{ |
| 60 | HEADER_LEN, Header, MAX_DATAGRAM, MAX_REPLY, MIN_DATAGRAM, REVOKED_DATAGRAM_LEN, TAG_LEN, |
| 61 | VERSION, datagram_len, fits_reply_budget, may_answer_unverified, open, open_message, seal, |
| 62 | seal_message, |
| 63 | }; |
| 64 | pub use kdf::{KEY_LEN, Key, revocation_key}; |
| 65 | pub use msg::{ |
| 66 | Ack, AckFlags, Config, ConfigFlags, ConfigGet, Direction, Hello, HelloFlags, MAX_POINTS, |
| 67 | Message, MsgType, NONCE_LEN, Nack, NackReason, Nonce, Ping, Pong, Profile, RevokeReason, |
| 68 | Revoked, |
| 69 | }; |
| 70 | pub use point::{Flags as PointFlags, POINT_LEN, Point}; |
| 71 |