diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d905205 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,574 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aead" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" +dependencies = [ + "crypto-common", + "inout", +] + +[[package]] +name = "aes" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fc36d7440d015b45468ba08a21e80e056ebbd014c611c6298bb7503e04bd1" +dependencies = [ + "cipher", + "cpubits", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2b8006a0c83f52b62ba44a97b58bf76fe2f70a329e588f67f89691d93d498f" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ctutils", + "ghash", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "base16ct" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd307490d624467aa6f74b0eabb77633d1f758a7b25f12bceb0b22e08d9726f6" + +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", + "zeroize", +] + +[[package]] +name = "block-padding" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "710f1dd022ef4e93f8a438b4ba958de7f64308434fa6a87104481645cc30068b" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "cbc" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2dc9ee5f88d11e0beb842c88b33c8a5cf0d1329c4b19494af42b07dbfe8896" +dependencies = [ + "cipher", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "block-buffer", + "crypto-common", + "inout", +] + +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + +[[package]] +name = "cpubits" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-bigint" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "914c195c4d7fbc5ed7f88d14cbf85914d5c664967658d43404b263913a477e82" +dependencies = [ + "cpubits", + "ctutils", + "getrandom", + "num-traits", + "rand_core", + "serdect", + "zeroize", +] + +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "getrandom", + "hybrid-array", + "rand_core", +] + +[[package]] +name = "crypto-primes" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3633a51a39c69ebbaa4feaa694bd83d241e4093901c84a0963b19d9bb3f0cf8f" +dependencies = [ + "crypto-bigint", + "rand_core", +] + +[[package]] +name = "ctr" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baaca1c4b237092596f64d571e9db6ce4109c4ef9742e27590f1709594461f21" +dependencies = [ + "cipher", +] + +[[package]] +name = "ctutils" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" +dependencies = [ + "cmov", +] + +[[package]] +name = "der" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a996a108feb8b427a6e12fcf6af4aa83409b79d72c15337d7741368131ee004" +dependencies = [ + "const-oid", + "der_derive", + "flagset", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "der_derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59600e2c2d636fde9b65e99cc6445ac770c63d3628195ff39932b8d6d7409903" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer", + "const-oid", + "crypto-common", + "ctutils", + "zeroize", +] + +[[package]] +name = "domistream" +version = "0.1.0" +dependencies = [ + "aes", + "aes-gcm", + "cbc", + "rand_core", + "rsa", + "sha2", + "x509-cert", + "zeroize", +] + +[[package]] +name = "flagset" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7ac824320a75a52197e8f2d787f6a38b6718bb6897a35142d749af3c0e8f4fe" + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "rand_core", +] + +[[package]] +name = "ghash" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eecf2d5dc9b66b732b97707a0210906b1d30523eb773193ab777c0c84b3e8d5" +dependencies = [ + "polyval", +] + +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest", +] + +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + +[[package]] +name = "inout" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9" +dependencies = [ + "block-padding", + "hybrid-array", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "pem-rfc7468" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6305423e0e7738146434843d1694d621cce767262b2a86910beab705e4493d9" +dependencies = [ + "base64ct", +] + +[[package]] +name = "pkcs1" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6250dd7321adbe93d12b750aee0ced159fd13e77f625edcdf1cb6f83cd99f650" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "pkcs8" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "polyval" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0fa31d631f2b2cb2a544d0aa321ce847a94764d701ca2becc411138b93d49cd" +dependencies = [ + "cpubits", + "cpufeatures", + "universal-hash", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rsa" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e77fd77e0dea28b3011fd62d15445b7de7959333b0276d930e03be0f755699" +dependencies = [ + "const-oid", + "crypto-bigint", + "crypto-common", + "crypto-primes", + "digest", + "hmac", + "pkcs1", + "pkcs8", + "rand_core", + "sha2", + "signature", + "spki", + "zeroize", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serdect" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66cf8fedced2fcf12406bcb34223dffb92eaf34908ede12fed414c82b7f00b3e" +dependencies = [ + "base16ct", + "serde", +] + +[[package]] +name = "sha1" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "signature" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d567dcbaf0049cb8ac2608a76cd95ff9e4412e1899d389ee400918ca7537f5" +dependencies = [ + "digest", + "rand_core", +] + +[[package]] +name = "spki" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef958a98b9d5da290cfc78946e9f3e61e1e62a18db0d92cac0b83cc161491a9" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tls_codec" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de2e01245e2bb89d6f05801c564fa27624dbd7b1846859876c7dad82e90bf6b" +dependencies = [ + "tls_codec_derive", + "zeroize", +] + +[[package]] +name = "tls_codec_derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2e76690929402faae40aebdda620a2c0e25dd6d3b9afe48867dfd95991f4bd" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "universal-hash" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96" +dependencies = [ + "crypto-common", + "ctutils", +] + +[[package]] +name = "x509-cert" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9f58ad685a2945c7c063f5d69b5c27105d75bb85c740380d8fc64e79a098417" +dependencies = [ + "const-oid", + "der", + "sha1", + "signature", + "spki", + "tls_codec", +] + +[[package]] +name = "zeroize" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85ccff8a4805aba1eaa4cd4afcd49f7557dd2cf5fd79ffc37f8959466effc5aa" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c20750d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,27 @@ +[workspace] +members = ["crates/domistream"] +# fuzz/ is its own workspace (cargo-fuzz layout), never part of this one. +exclude = ["fuzz"] +resolver = "3" + +[workspace.package] +version = "0.1.0" +edition = "2024" +rust-version = "1.89" +license = "CPAL-1.0" +repository = "https://gitea.marek-novak.cz/mnovak/DomiStream" +publish = false + +[workspace.lints.rust] +unsafe_code = "forbid" +missing_docs = "warn" +unreachable_pub = "warn" + +[workspace.lints.clippy] +# No panics on any input: these must never appear in library code. +unwrap_used = "deny" +expect_used = "deny" +panic = "deny" +todo = "deny" +unimplemented = "deny" +indexing_slicing = "warn" diff --git a/crates/domistream/Cargo.toml b/crates/domistream/Cargo.toml new file mode 100644 index 0000000..990a745 --- /dev/null +++ b/crates/domistream/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "domistream" +description = "Sans-I/O GameStream client protocol for Arena Domi Play remotely" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +publish.workspace = true + +[lints] +workspace = true + +# Crypto: RustCrypto only, pure Rust, all MIT OR Apache-2.0 (fits CPAL-1.0). Chosen in Phase A, used in Phase B. +[dependencies] +aes = "0.9" # AES-128 block cipher (pairing ECB) +aes-gcm = "0.11" # control and video AES-128-GCM +cbc = "0.2" # audio AES-128-CBC +sha2 = "0.11" # pairing SHA-256 +rsa = "0.10" # client key, PKCS#1 v1.5 SHA-256 sign/verify (crypto-bigint, constant time) +x509-cert = { version = "0.3", features = ["builder", "pem"] } # client certificate, host certificate parsing +rand_core = "0.10" # adapter from our RandomSource to the RustCrypto RNG traits +zeroize = "1.8" # wipe keys on drop diff --git a/crates/domistream/src/entropy.rs b/crates/domistream/src/entropy.rs new file mode 100644 index 0000000..4957e8c --- /dev/null +++ b/crates/domistream/src/entropy.rs @@ -0,0 +1,11 @@ +//! Randomness comes in from the caller, so every test is deterministic. + +/// A source of random bytes supplied by the caller. +/// +/// In production this must be a cryptographically secure generator (the OS generator). Tests pass a fixed, +/// seeded source. DomiStream never reads randomness any other way: salts, challenges, secrets, the client key, +/// `rikey`, ENet connect ids and PINs all come from here. +pub trait RandomSource { + /// Fills `dest` completely with random bytes. + fn fill(&mut self, dest: &mut [u8]); +} diff --git a/crates/domistream/src/error.rs b/crates/domistream/src/error.rs new file mode 100644 index 0000000..1fb00c6 --- /dev/null +++ b/crates/domistream/src/error.rs @@ -0,0 +1,94 @@ +//! The one error type of the crate. + +use core::fmt; + +/// Result with the crate's [`Error`]. +pub type Result = core::result::Result; + +/// Everything that can go wrong. Errors never carry key material or decrypted bytes. +#[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub enum Error { + /// Phase A: the API exists, the logic does not yet. + Unimplemented, + /// Input is larger than the parser's `MAX_*` constant. Checked before anything is allocated. + TooLarge { + /// What was being parsed, e.g. `"rtsp message"`. + what: &'static str, + /// The cap. + max: usize, + /// The size that was offered. + actual: usize, + }, + /// Input ends before a field it announces. + Truncated { + /// What was being parsed. + what: &'static str, + /// Bytes the field needs. + needed: usize, + /// Bytes that were there. + actual: usize, + }, + /// Input does not follow the format (bad syntax, bad length field, wrong constant, bad encoding). + Malformed { + /// What was being parsed and what was wrong, a fixed string. + what: &'static str, + }, + /// A field holds a value this client does not accept (unknown type code, out-of-range number). + UnexpectedValue { + /// The field. + what: &'static str, + /// The value seen. + value: u64, + }, + /// The host answered with a non-200 `status_code` (HTTP XML) or RTSP status. + HostStatus { + /// The status code. + code: u32, + }, + /// Decryption, tag or signature check failed. Deliberately without detail. + Crypto, + /// Pairing refused: wrong PIN, failed host proof (possible man in the middle), or `paired=0`. + PairingRefused, + /// The host does not offer something this client requires (for example control encryption). + Unsupported { + /// What is missing. + what: &'static str, + }, + /// The call is not valid in the current state of the state machine. + State { + /// The state the call needs, a fixed string. + expected: &'static str, + }, + /// The peer stopped answering within its timeout. + TimedOut, + /// The session is closed; no more calls are accepted. + Closed, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Unimplemented => f.write_str("not implemented yet"), + Self::TooLarge { what, max, actual } => { + write!(f, "{what}: {actual} bytes is over the {max}-byte limit") + } + Self::Truncated { what, needed, actual } => { + write!(f, "{what}: needs {needed} bytes, got {actual}") + } + Self::Malformed { what } => write!(f, "malformed {what}"), + Self::UnexpectedValue { what, value } => { + write!(f, "{what}: unexpected value {value:#x}") + } + Self::HostStatus { code } => write!(f, "host answered with status {code}"), + Self::Crypto => f.write_str("cryptographic check failed"), + Self::PairingRefused => f.write_str("pairing refused"), + Self::Unsupported { what } => write!(f, "host does not support {what}"), + Self::State { expected } => write!(f, "not allowed now: needs state {expected}"), + Self::TimedOut => f.write_str("timed out"), + Self::Closed => f.write_str("session closed"), + } + } +} + +impl std::error::Error for Error {} diff --git a/crates/domistream/src/keys.rs b/crates/domistream/src/keys.rs new file mode 100644 index 0000000..6287337 --- /dev/null +++ b/crates/domistream/src/keys.rs @@ -0,0 +1,38 @@ +//! The per-session stream key handed to the host in `launch` / `resume` (`docs/PROTOCOL.md` 1.4, 7). + +use core::fmt; + +use crate::{RandomSource, Result}; + +/// Length of `rikey` in bytes (AES-128). +pub const RIKEY_LEN: usize = 16; + +/// The stream key pair of one session: `rikey` (AES-128 key for control, audio and video) and `rikeyid` +/// (feeds the audio IV). +/// +/// A fresh pair is generated for every launch and resume, because the GCM nonce counters restart per session. +/// `Debug` never prints the key. +#[derive(Clone, PartialEq, Eq)] +pub struct SessionKeys { + /// AES-128 key, sent hex-encoded as the `rikey` launch parameter. + pub rikey: [u8; RIKEY_LEN], + /// Key id, sent as the decimal `rikeyid` launch parameter. + pub rikeyid: u32, +} + +impl SessionKeys { + /// Draws a fresh key and key id from `rng`. + /// + /// # Errors + /// Phase A: always [`crate::Error::Unimplemented`]. + pub fn generate(rng: &mut dyn RandomSource) -> Result { + let _ = rng; + Err(crate::Error::Unimplemented) + } +} + +impl fmt::Debug for SessionKeys { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("SessionKeys").field("rikey", &"").field("rikeyid", &self.rikeyid).finish() + } +} diff --git a/crates/domistream/src/lib.rs b/crates/domistream/src/lib.rs new file mode 100644 index 0000000..8f3d8b8 --- /dev/null +++ b/crates/domistream/src/lib.rs @@ -0,0 +1,46 @@ +//! DomiStream: a sans-I/O GameStream client (the protocol Sunshine hosts and Moonlight clients speak), for +//! Arena Domi TV and the other Play remotely clients. +//! +//! **Phase A.** This crate holds the public API only. Every function that does protocol work returns +//! [`Error::Unimplemented`] (or `None` for `poll_*` functions); QA writes the mandatory tests against these +//! signatures before the logic lands. Plain data (constants, enum codes, field structs) is final. +//! +//! # Shape +//! +//! Nothing here opens a socket, reads a clock, spawns a thread or touches a file. The caller: +//! - feeds received bytes in (`handle_*` / `push` / `parse_*`, always `&[u8]`), +//! - passes the current time in as [`Instant`] and randomness as a [`RandomSource`], +//! - sends the bytes the state machines hand out (`poll_transmit`, [`http::HttpRequest`]), +//! - reacts to events (`poll_event`) and wakes the machine at its deadline (`poll_timeout` / `handle_timeout`). +//! +//! Every parser has a `MAX_*` constant and refuses larger input before it allocates. +//! +//! # Modules, in session order +//! +//! | Module | Protocol (see `docs/PROTOCOL.md`) | +//! | :-- | :-- | +//! | [`http`] | serverinfo, applist, launch/resume/cancel (section 1) | +//! | [`pairing`] | the PIN handshake and the client certificate (section 1.5) | +//! | [`rtsp`] | OPTIONS, DESCRIBE, SETUP, ANNOUNCE, PLAY and SDP (section 2) | +//! | [`enet`] | the ENet-compatible reliable UDP layer under the control stream (section 3.1) | +//! | [`control`] | AES-GCM framing and control messages (sections 3.2, 3.3) | +//! | [`input`] | input and gamepad packets (section 4) | +//! | [`rtp`] | the RTP header shared by video and audio | +//! | [`fec`] | Reed-Solomon erasure coding over GF(2^8) | +//! | [`video`] | video depacketizer with FEC recovery and decryption (section 5) | +//! | [`audio`] | audio depacketizer with FEC recovery and decryption (section 6) | +//! +//! Opus and video decoding are not here: they belong to the platform layer (ArenaTV). + +#![forbid(unsafe_code)] +#![deny(missing_docs)] + +pub mod entropy; +pub mod error; +pub mod keys; +pub mod time; + +pub use entropy::RandomSource; +pub use error::{Error, Result}; +pub use keys::SessionKeys; +pub use time::Instant; diff --git a/crates/domistream/src/time.rs b/crates/domistream/src/time.rs new file mode 100644 index 0000000..17b3b4c --- /dev/null +++ b/crates/domistream/src/time.rs @@ -0,0 +1,39 @@ +//! Time comes in from the caller as an [`Instant`]; DomiStream never reads a clock. + +use core::time::Duration; + +/// A point on the caller's monotonic clock, in microseconds since an epoch the caller chooses. +/// +/// Only differences between instants matter. The caller must never let it go backwards within one session. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Instant(u64); + +impl Instant { + /// The caller's epoch. + pub const ZERO: Self = Self(0); + + /// An instant `micros` microseconds after the caller's epoch. + #[must_use] + pub const fn from_micros(micros: u64) -> Self { + Self(micros) + } + + /// Microseconds since the caller's epoch. + #[must_use] + pub const fn as_micros(self) -> u64 { + self.0 + } + + /// `self + d`, saturating at the end of the clock. + #[must_use] + pub fn saturating_add(self, d: Duration) -> Self { + let micros = u64::try_from(d.as_micros()).unwrap_or(u64::MAX); + Self(self.0.saturating_add(micros)) + } + + /// `self - earlier`, or zero when `earlier` is later. + #[must_use] + pub fn saturating_duration_since(self, earlier: Self) -> Duration { + Duration::from_micros(self.0.saturating_sub(earlier.0)) + } +} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..c775577 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 120 +use_small_heuristics = "Max"