simulcrypt 0.2.2

DVB SimulCrypt (ETSI TS 103 197) head-end protocol: ECMG client, EMMG server, control words
Documentation
/*
 * Independent implementation of the control word generator used to check CwGenerator
 * (src/cw/mod.rs): the same init and rotation scheme for the slot pair (current/next), the same
 * key byte order and the same checksum (sum of the first three bytes of each half of the 8-byte
 * slot).
 *
 *   - cw_init    - pre-fills the odd slot at init
 *   - cw_advance - generates a new key, checksum, alternates cp_number / cp_number+1 slots
 *                  and selects the slot that actually goes to the scrambler
 *
 * `rand_bytes` is replaced with a deterministic generator (sequential bytes from seed) so the
 * vectors are reproducible.
 *
 * Build and run:
 *   cc -O2 -o cw_reference cw_reference.c && ./cw_reference <seed> <rounds>
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

static uint8_t rng_state;

/* rand_bytes() replacement: deterministic sequence, same as StepRng in src/cw/mod.rs. */
static void fake_rand_bytes(uint8_t *dst, int n)
{
    for (int i = 0; i < n; i++) {
        dst[i] = rng_state;
        rng_state = (uint8_t)(rng_state + 1);
    }
}

static uint8_t cw_pair[16];
static uint16_t cw_number;

/* Init: fills only the odd slot - until the first rotation the pair holds a single valid CW. */
static void cw_init(void)
{
    uint8_t *key = &cw_pair[8];
    fake_rand_bytes(key, 8);
    key[3] = (uint8_t)(key[0] + key[1] + key[2]);
    key[7] = (uint8_t)(key[4] + key[5] + key[6]);

    cw_number = 0;
}

typedef struct {
    uint16_t cp_number;
    uint8_t active[8];
    uint8_t current[8];
    uint8_t next[8];
} cw_advance_t;

/* One rotation: generates a new key into the slot freed by the previous rotation and determines
 * which of the two slots (current) actually goes to the scrambler for this cp_number and which
 * (next) is already generated ahead for the following one. */
static cw_advance_t cw_advance(void)
{
    uint8_t *key = &cw_pair[(cw_number & 1) << 3];
    fake_rand_bytes(key, 8);
    key[3] = (uint8_t)(key[0] + key[1] + key[2]);
    key[7] = (uint8_t)(key[4] + key[5] + key[6]);

    cw_advance_t result;
    result.cp_number = cw_number;

    if (!(cw_number & 0x0001)) {
        /* current = cp_number   -> cw_pair[8..16] */
        /* next    = cp_number+1 -> cw_pair[0..8]  */
        for (int i = 0; i < 8; i++) {
            result.current[i] = cw_pair[8 + i];
            result.next[i] = cw_pair[i];
        }
        /* even cp_number -> active slot is the one at offset 8 */
        for (int i = 0; i < 8; i++) result.active[i] = cw_pair[8 + i];
    } else {
        for (int i = 0; i < 8; i++) {
            result.current[i] = cw_pair[i];
            result.next[i] = cw_pair[8 + i];
        }
        /* odd cp_number -> active slot is the one at offset 0 */
        for (int i = 0; i < 8; i++) result.active[i] = cw_pair[i];
    }

    cw_number += 1;
    return result;
}

static void print_hex(const char *label, const uint8_t *bytes)
{
    printf("%s=", label);
    for (int i = 0; i < 8; i++) printf("%02x", bytes[i]);
}

int main(int argc, char **argv)
{
    if (argc != 3) {
        fprintf(stderr, "usage: %s <seed 0-255> <rounds>\n", argv[0]);
        return 1;
    }

    rng_state = (uint8_t)atoi(argv[1]);
    int rounds = atoi(argv[2]);

    cw_init();

    for (int i = 0; i < rounds; i++) {
        cw_advance_t step = cw_advance();
        printf("cp=%u ", step.cp_number);
        print_hex("active", step.active);
        printf(" ");
        print_hex("current", step.current);
        printf(" ");
        print_hex("next", step.next);
        printf("\n");
    }

    return 0;
}