Skip to main content

Crate dvb_simulcrypt

Crate dvb_simulcrypt 

Source
Expand description

§dvb-simulcrypt

Crates.io docs.rs

DVB SimulCrypt head-end CA message framing (ETSI TS 103 197): a codec for the messages a head-end’s conditional-access components exchange over TCP.

§What it does / what it does not

Does. A DVB SimulCrypt head-end lets several conditional-access systems share one multiplex. Its components (ECM generator, EMM generator, multiplexer, CA-specific SI generators) talk over TCP using one generic message layout. This crate parses and builds that layout and decodes the message_type / parameter_type values for three interfaces. Users are head-end software, test tools and protocol analysers.

Does not.

  • It does not open sockets or run the protocol state machines (channel and stream setup, test/status handshakes, bandwidth negotiation); the caller drives TCP.
  • It does not do cryptography: control words, ECMs, EMMs and private data are carried as opaque borrowed bytes, never decrypted or interpreted.
  • It does not type the individual parameter values (a parameter value is a byte slice; only the type codes and a few value tables are modelled).
  • Interfaces EIS-SCS, (P)SIG-MUX, ACG-EIS and SIMCOMP-MUXCONFIG share the same framing but their registries are not modelled, and the DVB-H 0x05 protocol version (annex N) is not modelled.
  • It does not scramble anything; see dvb-csa for the cipher.

§Standards supported

StandardEditionScopeStatusNotes
ETSI TS 103 197, DVB; Head-end implementation of DVB SimulCryptV1.5.1 (etsi_ts_103_197_v01.05.01_simulcrypt, in private/specs-md/INDEX.md)§4.4.1 Table 1b generic_message: 5-byte header plus TLV parameters; Table 2 protocol versionFullmessage_length and parameter_length recomputed on serialize; SimulcryptMessage::frame_len for stream reassembly
Clause 5 ECMG-SCS: message types (Table 3 incl. CW_provision 0x0201, ECM_response 0x0202), parameter types (Table 5), error status (Table 6)Typed-opaqueType codes typed; CW/ECM payloads opaque
Clause 6 EMMG/PDG-MUX: message types, parameter types (Table 7), error status (Table 8), DataType (§6.2.3), SectionTspktFlagTyped-opaquedatagram payload opaque
Clause 8 C(P)SIG-(P)SIG: message types 0x0301-0x0321, parameter types (Table 36)Typed-opaqueRegistry only
Other interfaces (EIS-SCS, (P)SIG-MUX, ACG-EIS, SIMCOMP-MUXCONFIG), XML-based protocols (Table 4), annex N DVB-HNot implementedRegistries not modelled; the generic framing is the same

§Interface scoping

The 16-bit message_type / parameter_type spaces are interface-scoped: the same value means different things on different interfaces, and the interface is not on the wire (it is fixed by which TCP connection a message arrived on). So SimulcryptMessage::parse_on takes an Interface hint and decodes into the matching interface-tagged MessageType / ParameterType enums. SimulcryptMessage deliberately has no Parse impl (a documented exception: Parse::parse(&[u8]) has nowhere to carry the hint). It implements Serialize, so the round-trip invariant is parse_on then serialize_into.

§Feature flags

FeatureDefaultEffect
stdyesLinks std. Without it the crate is #![no_std] + alloc.
serdenoserde::Serialize derives on public types.

§Quick start

use dvb_simulcrypt::{
    EcmgScsMessageType, EcmgScsParameterType, Interface, MessageType, Parameter,
    ParameterType, SimulcryptMessage,
};
use broadcast_common::traits::Serialize;

let ecm_channel_id = [0x00, 0x2A]; // 0x002A
let super_cas_id = [0x00, 0x01, 0x00, 0x02]; // CA_system_id | subsystem_id
let msg = SimulcryptMessage::new(
    Interface::EcmgScs.protocol_version(),
    MessageType::EcmgScs(EcmgScsMessageType::ChannelSetup),
    vec![
        Parameter::new(
            ParameterType::EcmgScs(EcmgScsParameterType::EcmChannelId),
            &ecm_channel_id,
        ),
        Parameter::new(
            ParameterType::EcmgScs(EcmgScsParameterType::SuperCasId),
            &super_cas_id,
        ),
    ],
);

let mut buf = vec![0u8; msg.serialized_len()];
msg.serialize_into(&mut buf).unwrap();
assert_eq!(SimulcryptMessage::parse_on(Interface::EcmgScs, &buf).unwrap(), msg);

Runnable examples: cargo run -p dvb-simulcrypt --example build_channel_setup and --example parse_cw_provision.

§What is implemented

  • Framing: SimulcryptMessage (new, parse_on, frame_len, find), Parameter, HEADER_LEN, PARAMETER_HEADER_LEN.
  • Interface dispatch: Interface (EcmgScs, EmmgPdgMux, CpSigPSig), MessageType, ParameterType.
  • ECMG-SCS: EcmgScsMessageType, EcmgScsParameterType, EcmgErrorStatus.
  • EMMG/PDG-MUX: EmmgMuxMessageType, EmmgMuxParameterType, EmmgErrorStatus, DataType, SectionTspktFlag.
  • C(P)SIG-(P)SIG: CpSigMessageType, CpSigParameterType.
  • Errors: Error / Result (#[non_exhaustive]).

Field-level transcriptions live in the crate’s docs/ directory (message-framing.md, ecmg-scs.md, emmg-pdg-mux.md, cpsig-psig.md, parameter-types.md).

§Design notes

  • Zero-copy: parameter values borrow from the input.
  • No raw passthrough: lengths are recomputed from the typed fields on serialize.
  • Round-trip tested against a committed capture of a CW_provision message (tests/it/fixture_cw_provision.rs) and truncation/length-overflow tests.
  • Public enums are #[non_exhaustive] with name() and Display.
  • no_std + alloc.

Depends only on broadcast-common. dvb-csa is the scrambling cipher the control words feed; dvb-ci / dvb-ci-runtime are the receiver-side CAM counterpart; dvb-si carries the CA descriptors and CAT/PMT signalling.

§MSRV and licence

Rust 1.95.0. MIT OR Apache-2.0.

§Runnable examples

Run with cargo run -p dvb-simulcrypt --example <name>.

§build_channel_setup

ⓘ
/// Build an ECMG⇔SCS `channel_setup` message from typed fields, serialize it
/// (recomputing message_length + each parameter_length), and dump the wire
/// bytes.
///
/// ```sh
/// cargo run -p dvb-simulcrypt --example build_channel_setup
/// ```
use broadcast_common::traits::Serialize;
use dvb_simulcrypt::{
    EcmgScsMessageType, EcmgScsParameterType, Interface, MessageType, Parameter, ParameterType,
    SimulcryptMessage,
};

fn main() {
    // channel_setup (Table, §5.4.1): ECM_channel_id (1) + Super_CAS_id (1).
    let ecm_channel_id = [0x00u8, 0x2A]; // ECM_channel_id = 0x002A
    let super_cas_id = [0x00u8, 0x01, 0x00, 0x02]; // CA_system_id | CA_subsystem_id

    let msg = SimulcryptMessage::new(
        Interface::EcmgScs.protocol_version(),
        MessageType::EcmgScs(EcmgScsMessageType::ChannelSetup),
        vec![
            Parameter::new(
                ParameterType::EcmgScs(EcmgScsParameterType::EcmChannelId),
                &ecm_channel_id,
            ),
            Parameter::new(
                ParameterType::EcmgScs(EcmgScsParameterType::SuperCasId),
                &super_cas_id,
            ),
        ],
    );

    let mut bytes = vec![0u8; msg.serialized_len()];
    let n = msg.serialize_into(&mut bytes).unwrap();

    println!("interface: {}", msg.interface());
    println!(
        "message_type: {} (0x{:04X})",
        msg.message_type,
        msg.message_type.to_u16()
    );
    println!("protocol_version: 0x{:02X}", msg.protocol_version);
    println!("message_length (body): {}", msg.body_len());
    println!("parameters: {}", msg.parameters.len());
    for p in &msg.parameters {
        print!("  {} (0x{:04X}) =", p.ptype, p.ptype.to_u16());
        for b in p.value {
            print!(" {b:02X}");
        }
        println!();
    }
    print!("wire bytes ({n}):");
    for b in &bytes {
        print!(" {b:02X}");
    }
    println!();

    // Round-trip: parse against the same interface, expect equality.
    assert_eq!(
        SimulcryptMessage::parse_on(Interface::EcmgScs, &bytes).unwrap(),
        msg
    );
    println!("round-trip: OK");
}

§parse_cw_provision

ⓘ
/// Read the committed `cw-provision.bin` fixture (an ECMG⇔SCS `CW_provision`
/// message), parse it against the ECMG⇔SCS interface, walk its parameters
/// (treating the CW inside `CP_CW_combination` as opaque), and byte-exact
/// round-trip it.
///
/// ```sh
/// cargo run -p dvb-simulcrypt --example parse_cw_provision
/// ```
use std::fs;

use broadcast_common::traits::Serialize;
use dvb_simulcrypt::{
    EcmgScsParameterType, Interface, MessageType, ParameterType, SimulcryptMessage,
};

fn main() {
    // Fixtures live in the workspace-shared `fixtures/` tree, not under the
    // crate. A committed fixture that cannot be read is a bug, not a reason
    // to skip — so a missing/unreadable fixture is a hard failure.
    let path = test_fixtures::path("simulcrypt/cw-provision.bin");
    let bytes = fs::read(&path).unwrap_or_else(|e| {
        panic!(
            "committed fixture {} could not be read: {e}",
            path.display()
        )
    });

    let msg = SimulcryptMessage::parse_on(Interface::EcmgScs, &bytes)
        .expect("CW_provision fixture must parse");

    println!("interface: {}", msg.interface());
    println!(
        "message_type: {} (0x{:04X})",
        msg.message_type,
        msg.message_type.to_u16()
    );
    assert!(matches!(
        msg.message_type,
        MessageType::EcmgScs(dvb_simulcrypt::EcmgScsMessageType::CwProvision)
    ));

    for p in &msg.parameters {
        print!(
            "  {} (0x{:04X}), {} bytes:",
            p.ptype,
            p.ptype.to_u16(),
            p.value.len()
        );
        for b in p.value {
            print!(" {b:02X}");
        }
        println!();
    }

    // The CP_CW_combination's CW is opaque — we only locate the parameter.
    // Guard: the parameter must carry at least 2 bytes for the CP number.
    if let Some(cpcw) = msg.find(ParameterType::EcmgScs(
        EcmgScsParameterType::CpCwCombination,
    )) {
        if let Some(cp_bytes) = cpcw.value.get(0..2) {
            let cp = u16::from_be_bytes([cp_bytes[0], cp_bytes[1]]);
            println!(
                "CP_CW_combination: CP={cp}, CW={} opaque bytes",
                cpcw.value.len() - 2
            );
        } else {
            println!(
                "CP_CW_combination: value too short ({} bytes, need ≥2)",
                cpcw.value.len()
            );
        }
    }

    // Byte-exact round-trip.
    let mut out = vec![0u8; msg.serialized_len()];
    let n = msg.serialize_into(&mut out).unwrap();
    assert_eq!(n, bytes.len());
    assert_eq!(
        out, bytes,
        "serialize must reproduce the fixture byte-for-byte"
    );
    println!("byte-exact round-trip: OK");
}

Structs§

Parameter
One TLV parameter of a SimulcryptMessage (Table 1b): a typed parameter_type plus a borrowed, opaque parameter_value.
SimulcryptMessage
A generic SimulCrypt message (TS 103 197 Table 1b): the 5-byte header plus an ordered list of TLV Parameters.

Enums§

CpSigMessageType
C(P)SIG⇔(P)SIG message_type values (TS 103 197 Table 3, §4.4.1 / §8).
CpSigParameterType
C(P)SIG⇔(P)SIG parameter_type values (TS 103 197 Table 36, §8).
DataType
data_type values (TS 103 197 §6.2.3 p. 42) — what a datagram carries.
EcmgErrorStatus
ECMG⇔SCS error_status values (TS 103 197 Table 6, §5.6 p. 39).
EcmgScsMessageType
ECMG⇔SCS message_type values (TS 103 197 Table 3, §4.4.1 pp. 27-28).
EcmgScsParameterType
ECMG⇔SCS parameter_type values (TS 103 197 Table 5, §5.2 p. 31).
EmmgErrorStatus
EMMG/PDG⇔MUX error_status values (TS 103 197 Table 8, §6.2.6 p. 47).
EmmgMuxMessageType
EMMG/PDG⇔MUX message_type values (TS 103 197 Table 3, §4.4.1 pp. 27-28).
EmmgMuxParameterType
EMMG/PDG⇔MUX parameter_type values (TS 103 197 Table 7, §6.2.2 p. 42).
Error
A SimulCrypt parse / serialize error.
Interface
The SimulCrypt connection-oriented interface a message belongs to.
MessageType
Interface-tagged message_type: decode a raw value once the Interface is known.
ParameterType
Interface-tagged parameter_type: decode a raw value once the Interface is known.
SectionTspktFlag
section_TSpkt_flag values (TS 103 197 §6.2.3 p. 43) — the datagram framing in datagram parameters. (The same flag, with the same meaning, is carried on the ECMG⇔SCS interface for ECM_datagram.)

Constants§

HEADER_LEN
Bytes of the fixed generic_message header: protocol_version (1) + message_type (2) + message_length (2).
PARAMETER_HEADER_LEN
Bytes of a parameter TLV’s fixed prefix: parameter_type (2) + parameter_length (2).

Type Aliases§

Result
Result alias for SimulCrypt parsing.