broadcast-core 0.1.0

Frozen wire-format contract for the rust-broadcast crates: symmetric Parse/Serialize traits, checked field-width helpers, MSB-first bit I/O, reserved-bit fields, media timescales, the shared error kinds and the CENC scheme identity. no_std + alloc.
Documentation
//! Common Encryption (CENC) scheme identity — Common Encryption (de facto)
//!
//! [`CencScheme`] names the protection scheme a protected track is encrypted
//! under: the `schm.scheme_type` four-CC of ISO/IEC 14496-12:2026 §13.4.6.
//!
//! # Why this lives in `broadcast-core`
//!
//! CENC is *Common* Encryption — a container-independent scheme identity, not
//! an ISOBMFF or an HLS concept. It is referenced from at least three places
//! that must agree on one type:
//!
//! - the **decrypt** path, which recovers it from a protected file's `schm`;
//! - the **encrypt** path, which is given it by the caller;
//! - **manifest/playlist signalling** (DASH `ContentProtection`, HLS
//!   `#EXT-X-KEY`), which renders a different tag per scheme.
//!
//! Those live in different crates (`transmux` owns the ISOBMFF crypto boxes,
//! `broadcast-hls` owns the M3U8 playlist syntax), so the scheme identity
//! belongs *below* both — in `broadcast-core`. Defining it once here
//! is what stops "two names for one thing" reappearing every time a new
//! container crate needs to name a scheme (issue #564 consolidated three
//! definitions into one; issue #878 keeps it that way across the
//! `transmux`/`broadcast-hls` split).
//!
//! The *box layouts* that carry this scheme (`tenc`, `senc`, `sinf`, `schm`,
//! …) are ISOBMFF-specific and stay in `transmux`.

use crate::wire::FOURCC_LEN;

/// Four-CC identifying the `cenc` (AES-128 full-block counter, CTR)
/// protection scheme, as it appears in `schm.scheme_type`.
const SCHEME_CENC: [u8; FOURCC_LEN] = *b"cenc";

/// Four-CC identifying the `cbcs` (AES-128 pattern cipher-block-chaining)
/// protection scheme, as it appears in `schm.scheme_type`.
const SCHEME_CBCS: [u8; FOURCC_LEN] = *b"cbcs";

/// Four-CC identifying the `cens` (AES-128 pattern counter, CTR) protection
/// scheme, as it appears in `schm.scheme_type`.
const SCHEME_CENS: [u8; FOURCC_LEN] = *b"cens";

/// Four-CC identifying the `cbc1` (AES-128 full-block cipher-block-chaining)
/// protection scheme, as it appears in `schm.scheme_type`.
const SCHEME_CBC1: [u8; FOURCC_LEN] = *b"cbc1";

/// A CENC protection scheme (`schm.scheme_type`).
///
/// The four schemes are `cenc`, `cens`, `cbc1` and `cbcs`. Their cipher rules
/// are verified against MPEG's file-format conformance files and two
/// independent tools, not against a held page of ISO/IEC 23001-7; the evidence
/// is recorded in `private/specs-verified/de-facto/isobmff/cenc.md`.
///
/// `#[non_exhaustive]`: the 2023 edition registers further identifiers (`sve1`),
/// which this workspace holds untyped; adding one must stay additive.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum CencScheme {
    /// `cenc` — AES-128 full-block counter (CTR) mode.
    Cenc,
    /// `cbcs` — AES-128 pattern cipher-block-chaining mode.
    Cbcs,
    /// `cens` — AES-128 counter (CTR) mode over a crypt/skip block pattern.
    Cens,
    /// `cbc1` — AES-128 cipher-block-chaining mode over whole blocks, no pattern.
    Cbc1,
}

impl CencScheme {
    /// The scheme's four-CC token as it appears in `schm` (`"cenc"` / `"cbcs"`).
    pub fn name(&self) -> &'static str {
        match self {
            CencScheme::Cenc => "cenc",
            CencScheme::Cbcs => "cbcs",
            CencScheme::Cens => "cens",
            CencScheme::Cbc1 => "cbc1",
        }
    }

    /// The scheme's `schm.scheme_type` four-CC bytes, the inverse of
    /// [`CencScheme::from_four_cc`].
    pub fn to_four_cc(&self) -> [u8; FOURCC_LEN] {
        match self {
            CencScheme::Cenc => SCHEME_CENC,
            CencScheme::Cbcs => SCHEME_CBCS,
            CencScheme::Cens => SCHEME_CENS,
            CencScheme::Cbc1 => SCHEME_CBC1,
        }
    }

    /// Map a `schm.scheme_type` four-CC to a known scheme, or `None` if it is
    /// one this workspace does not type (e.g. `sve1`).
    pub fn from_four_cc(four_cc: &[u8; FOURCC_LEN]) -> Option<Self> {
        match *four_cc {
            SCHEME_CENC => Some(CencScheme::Cenc),
            SCHEME_CBCS => Some(CencScheme::Cbcs),
            SCHEME_CENS => Some(CencScheme::Cens),
            SCHEME_CBC1 => Some(CencScheme::Cbc1),
            _ => None,
        }
    }
}

crate::impl_spec_display!(CencScheme);

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::ToString;

    const ALL: [CencScheme; 4] = [
        CencScheme::Cenc,
        CencScheme::Cens,
        CencScheme::Cbc1,
        CencScheme::Cbcs,
    ];

    #[test]
    fn name_matches_the_spec_token() {
        assert_eq!(CencScheme::Cenc.name(), "cenc");
        assert_eq!(CencScheme::Cens.name(), "cens");
        assert_eq!(CencScheme::Cbc1.name(), "cbc1");
        assert_eq!(CencScheme::Cbcs.name(), "cbcs");
    }

    #[test]
    fn display_delegates_to_name() {
        for scheme in ALL {
            assert_eq!(scheme.to_string(), scheme.name());
        }
    }

    #[test]
    fn four_cc_round_trips() {
        for scheme in ALL {
            assert_eq!(CencScheme::from_four_cc(&scheme.to_four_cc()), Some(scheme));
        }
    }

    #[test]
    fn four_cc_is_the_name_bytes() {
        for scheme in ALL {
            assert_eq!(scheme.to_four_cc().as_slice(), scheme.name().as_bytes());
        }
    }

    #[test]
    fn unregistered_schemes_are_rejected_not_guessed() {
        assert_eq!(CencScheme::from_four_cc(b"sve1"), None);
        assert_eq!(CencScheme::from_four_cc(b"\0\0\0\0"), None);
    }
}