### Decrypt TS Packet Payload with dvbcsa_decrypt Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Decrypts a single MPEG-TS packet payload in-place using the DVB CSA algorithm. The data pointer should point to the start of the payload (184 bytes for standard TS packets), not the header. ```c #include #include #include int main(void) { dvbcsa_cw_t cw = { 0x07, 0xe0, 0x1b, 0x02, 0xc9, 0xe0, 0x45, 0xee }; /* Simulated scrambled TS packet payload (184 bytes) */ unsigned char payload[184]; memset(payload, 0xAB, sizeof(payload)); /* placeholder scrambled data */ struct dvbcsa_key_s *key = dvbcsa_key_alloc(); dvbcsa_key_set(cw, key); /* Decrypt in place */ dvbcsa_decrypt(key, payload, 184); printf("Decryption complete, first byte: 0x%02x\n", payload[0]); dvbcsa_key_free(key); return 0; } ``` -------------------------------- ### dvbcsa_decrypt Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Decrypts a single TS packet payload. Decrypts `len` bytes of in-place data using the DVB CSA block and stream cipher combination. The `data` pointer should point to the start of the TS packet payload (not the 4-byte header). ```APIDOC ## dvbcsa_decrypt ### Description Decrypts a single TS packet payload. Decrypts `len` bytes of in-place data using the DVB CSA block and stream cipher combination. The `data` pointer should point to the start of the TS packet payload (not the 4-byte header). For standard MPEG-TS packets the payload is 184 bytes. Operates on a single packet per call. ### Function Signature ```c void dvbcsa_decrypt(struct dvbcsa_key_s *key, unsigned char *data, size_t len); ``` ### Parameters * **key** (`struct dvbcsa_key_s *`): The key context containing the expanded key schedule. * **data** (`unsigned char *`): Pointer to the start of the TS packet payload to be decrypted. * **len** (`size_t`): The number of bytes in the payload to decrypt. ### Example Usage ```c #include #include #include int main(void) { dvbcsa_cw_t cw = { 0x07, 0xe0, 0x1b, 0x02, 0xc9, 0xe0, 0x45, 0xee }; /* Simulated scrambled TS packet payload (184 bytes) */ unsigned char payload[184]; memset(payload, 0xAB, sizeof(payload)); /* placeholder scrambled data */ struct dvbcsa_key_s *key = dvbcsa_key_alloc(); dvbcsa_key_set(cw, key); /* Decrypt in place */ dvbcsa_decrypt(key, payload, 184); printf("Decryption complete, first byte: 0x%02x\n", payload[0]); dvbcsa_key_free(key); return 0; } ``` ``` -------------------------------- ### Build libdvbcsa with SIMD Acceleration Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Configure the build process to enable specific SIMD instruction sets for performance optimization. Ensure your hardware supports the chosen acceleration. ```sh ./configure make make install ``` ```sh ./configure --enable-sse2 make ``` ```sh ./configure --enable-avx2 make ``` ```sh ./configure --enable-neon make ``` ```sh ./configure --enable-altivec make ``` ```sh ./configure --enable-debug make ``` -------------------------------- ### dvbcsa_bs_key_alloc Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Allocates a key context for the parallel bitslice CSA implementation. This context holds pre-expanded SIMD-ready key material. ```APIDOC ## dvbcsa_bs_key_alloc — Allocate a bitslice key context ### Description Allocates a key context for the parallel bitslice CSA implementation. The bitslice context holds pre-expanded SIMD-ready key material for both the block and stream cipher components. Returns `NULL` on failure. ### Returns A pointer to the allocated bitslice key context, or `NULL` on failure. ``` -------------------------------- ### Load Control Word into Bitslice Key Context Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Expands an 8-byte control word into the bitslice key schedule for DVB CSA. This prepares subkeys and initial states in a SIMD-friendly transposed form, making the key ready for batch processing. ```c #include int main(void) { dvbcsa_cw_t cw = { 0x12, 0x34, 0x56, 0x78, 0x1e, 0xd4, 0xf6, 0xab }; struct dvbcsa_bs_key_s *key = dvbcsa_bs_key_alloc(); dvbcsa_bs_key_set(cw, key); /* key is now ready for batch encrypt/decrypt */ dvbcsa_bs_key_free(key); return 0; } ``` -------------------------------- ### Allocate a Key Context with dvbcsa_key_alloc Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Allocates a new key context for the classical CSA implementation. The context must be freed using dvbcsa_key_free. Returns NULL on allocation failure. ```c #include #include #include int main(void) { struct dvbcsa_key_s *key = dvbcsa_key_alloc(); assert(key != NULL); /* key is ready to be configured with a control word */ printf("Key context allocated successfully\n"); dvbcsa_key_free(key); return 0; } ``` -------------------------------- ### dvbcsa_key_alloc Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Allocates and returns a new opaque key context for the classical (single-packet) CSA implementation. The context holds the expanded key schedule derived from the control word and must be freed with `dvbcsa_key_free` when no longer needed. ```APIDOC ## dvbcsa_key_alloc ### Description Allocates and returns a new opaque key context for the classical (single-packet) CSA implementation. Returns `NULL` on allocation failure. The context holds the expanded key schedule derived from the control word and must be freed with `dvbcsa_key_free` when no longer needed. ### Function Signature ```c struct dvbcsa_key_s *dvbcsa_key_alloc(void); ``` ### Example Usage ```c #include #include #include int main(void) { struct dvbcsa_key_s *key = dvbcsa_key_alloc(); assert(key != NULL); /* key is ready to be configured with a control word */ printf("Key context allocated successfully\n"); dvbcsa_key_free(key); return 0; } ``` ``` -------------------------------- ### dvbcsa_bs_key_free Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Releases all memory associated with a bitslice key context. Must be paired with every `dvbcsa_bs_key_alloc` call. ```APIDOC ## dvbcsa_bs_key_free ### Description Releases all memory associated with a bitslice key context. ### Parameters - `key` (*dvbcsa_bs_key_t*): A pointer to the bitslice key context to be freed. ### Request Example ```c #include int main(void) { struct dvbcsa_bs_key_s *key = dvbcsa_bs_key_alloc(); dvbcsa_cw_t cw = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11 }; dvbcsa_bs_key_set(cw, key); /* ... process batches ... */ dvbcsa_bs_key_free(key); return 0; } ``` ``` -------------------------------- ### dvbcsa_bs_key_set Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Loads a control word into a bitslice key context, expanding it for SIMD processing. ```APIDOC ## dvbcsa_bs_key_set — Load a control word into a bitslice key context ### Description Expands the 8-byte control word into the bitslice key schedule, preparing both the block cipher subkeys and the stream cipher initial state in SIMD-friendly transposed form. ### Parameters - **cw** (*dvbcsa_cw_t*) - The 8-byte control word. - **key** (*dvbcsa_bs_key_t*) - A pointer to the bitslice key context. ``` -------------------------------- ### Allocate Parallel Bitslice Key Context Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Allocates a key context for the parallel bitslice DVB CSA implementation. This context is used for pre-expanded SIMD-ready key material. Remember to free the context using `dvbcsa_bs_key_free`. ```c #include #include int main(void) { struct dvbcsa_bs_key_s *key = dvbcsa_bs_key_alloc(); assert(key != NULL); /* configure and use ... */ dvbcsa_bs_key_free(key); return 0; } ``` -------------------------------- ### dvbcsa_key_set Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Loads a control word into a key context. Expands the 8-byte control word (CW) into the internal key schedule stored within the key context. Must be called before any encrypt/decrypt operation, and can be called again any time the CW changes. ```APIDOC ## dvbcsa_key_set ### Description Loads a control word into a key context. Expands the 8-byte control word (CW) into the internal key schedule stored within the key context. Must be called before any encrypt/decrypt operation, and can be called again any time the CW changes (e.g., on each key period boundary in a conditional-access system). ### Function Signature ```c void dvbcsa_key_set(dvbcsa_cw_t cw, struct dvbcsa_key_s *key); ``` ### Parameters * **cw** (`dvbcsa_cw_t`): The 8-byte control word. * **key** (`struct dvbcsa_key_s *`): The key context allocated by `dvbcsa_key_alloc`. ### Example Usage ```c #include #include #include int main(void) { /* 64-bit control word (8 bytes) */ dvbcsa_cw_t cw = { 0x07, 0xe0, 0x1b, 0x02, 0xc9, 0xe0, 0x45, 0xee }; struct dvbcsa_key_s *key = dvbcsa_key_alloc(); dvbcsa_key_set(cw, key); printf("Control word loaded into key context\n"); /* Update to a new control word at the next key period */ dvbcsa_cw_t new_cw = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; dvbcsa_key_set(new_cw, key); printf("Control word updated\n"); dvbcsa_key_free(key); return 0; } ``` ``` -------------------------------- ### Encrypt TS Packets in Batch (Bitslice) Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Encrypts a batch of TS packet payloads in parallel. Aim to fill the batch completely for maximum throughput, as partial batches take the same time as full ones. Verifies encryption by cross-API decryption. ```c #include #include #include #include #include #define TS_PAYLOAD_LEN 184 int main(void) { dvbcsa_cw_t cw = { 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a }; struct dvbcsa_bs_key_s *bs_key = dvbcsa_bs_key_alloc(); struct dvbcsa_key_s *ref_key = dvbcsa_key_alloc(); assert(bs_key != NULL && ref_key != NULL); dvbcsa_bs_key_set(cw, bs_key); dvbcsa_key_set(cw, ref_key); unsigned int gs = dvbcsa_bs_batch_size(); uint8_t **pkt_data = malloc(gs * sizeof(uint8_t *)); struct dvbcsa_bs_batch_s *batch = malloc((gs + 1) * sizeof(struct dvbcsa_bs_batch_s)); /* Fill packets with known cleartext */ for (unsigned int i = 0; i < gs; i++) { pkt_data[i] = malloc(TS_PAYLOAD_LEN); memset(pkt_data[i], (uint8_t)i, TS_PAYLOAD_LEN); batch[i].data = pkt_data[i]; batch[i].len = TS_PAYLOAD_LEN; } batch[gs].data = NULL; /* Encrypt the entire batch in one parallel call */ dvbcsa_bs_encrypt(bs_key, batch, TS_PAYLOAD_LEN); printf("Batch of %u packets encrypted\n", gs); /* Verify by decrypting each packet with the classical API */ for (unsigned int i = 0; i < gs; i++) { dvbcsa_decrypt(ref_key, batch[i].data, TS_PAYLOAD_LEN); assert(batch[i].data[0] == (uint8_t)i); } printf("All packets verified after cross-API round-trip\n"); for (unsigned int i = 0; i < gs; i++) free(pkt_data[i]); free(pkt_data); free(batch); dvbcsa_bs_key_free(bs_key); dvbcsa_key_free(ref_key); return 0; } ``` -------------------------------- ### Set Control Word with dvbcsa_key_set Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Loads an 8-byte control word (CW) into the key context, expanding it into an internal key schedule. This must be called before encryption/decryption and can be called again to update the CW. ```c #include #include #include int main(void) { /* 64-bit control word (8 bytes) */ dvbcsa_cw_t cw = { 0x07, 0xe0, 0x1b, 0x02, 0xc9, 0xe0, 0x45, 0xee }; struct dvbcsa_key_s *key = dvbcsa_key_alloc(); dvbcsa_key_set(cw, key); printf("Control word loaded into key context\n"); /* Update to a new control word at the next key period */ dvbcsa_cw_t new_cw = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; dvbcsa_key_set(new_cw, key); printf("Control word updated\n"); dvbcsa_key_free(key); return 0; } ``` -------------------------------- ### Free Bitslice Key Context Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Releases memory associated with a bitslice key context. This function must be called to pair every `dvbcsa_bs_key_alloc` call. ```c #include int main(void) { struct dvbcsa_bs_key_s *key = dvbcsa_bs_key_alloc(); dvbcsa_cw_t cw = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11 }; dvbcsa_bs_key_set(cw, key); /* ... process batches ... */ dvbcsa_bs_key_free(key); return 0; } ``` -------------------------------- ### dvbcsa_key_free Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Releases all memory associated with a classical key context. This should always be paired with dvbcsa_key_alloc. ```APIDOC ## dvbcsa_key_free — Free a key context ### Description Releases all memory associated with a classical key context. Always pair with `dvbcsa_key_alloc`. ### Parameters - **key** (*dvbcsa_key_t*) - A pointer to the key context to be freed. ``` -------------------------------- ### dvbcsa_bs_encrypt Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Encrypts a batch of TS packet payloads in parallel. The batch layout and `maxlen` semantics are identical to `dvbcsa_bs_decrypt`. Partial batches (fewer entries than `dvbcsa_bs_batch_size()`) take the same time as a full batch, so callers should aim to fill the batch completely for maximum throughput. ```APIDOC ## dvbcsa_bs_encrypt ### Description Encrypts a batch of TS packet payloads in parallel. ### Parameters - `bs_key` (*dvbcsa_bs_key_t*): A pointer to the bitslice key context. - `batch` (*struct dvbcsa_bs_batch_s*): An array of batch descriptors, terminated by an entry with a NULL data pointer. - `maxlen` (unsigned int): The maximum payload length to process, must be a multiple of 8. ### Request Example ```c #include #include #include #include #include #define TS_PAYLOAD_LEN 184 int main(void) { dvbcsa_cw_t cw = { 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a }; struct dvbcsa_bs_key_s *bs_key = dvbcsa_bs_key_alloc(); struct dvbcsa_key_s *ref_key = dvbcsa_key_alloc(); assert(bs_key != NULL && ref_key != NULL); dvbcsa_bs_key_set(cw, bs_key); dvbcsa_key_set(cw, ref_key); unsigned int gs = dvbcsa_bs_batch_size(); uint8_t **pkt_data = malloc(gs * sizeof(uint8_t *)); struct dvbcsa_bs_batch_s *batch = malloc((gs + 1) * sizeof(struct dvbcsa_bs_batch_s)); for (unsigned int i = 0; i < gs; i++) { pkt_data[i] = malloc(TS_PAYLOAD_LEN); memset(pkt_data[i], (uint8_t)i, TS_PAYLOAD_LEN); batch[i].data = pkt_data[i]; batch[i].len = TS_PAYLOAD_LEN; } batch[gs].data = NULL; dvbcsa_bs_encrypt(bs_key, batch, TS_PAYLOAD_LEN); printf("Batch of %u packets encrypted\n", gs); for (unsigned int i = 0; i < gs; i++) { dvbcsa_decrypt(ref_key, batch[i].data, TS_PAYLOAD_LEN); assert(batch[i].data[0] == (uint8_t)i); } printf("All packets verified after cross-API round-trip\n"); for (unsigned int i = 0; i < gs; i++) free(pkt_data[i]); free(pkt_data); free(batch); dvbcsa_bs_key_free(bs_key); dvbcsa_key_free(ref_key); return 0; } ``` ``` -------------------------------- ### Decrypt TS Packets in Batch (Bitslice) Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Decrypts a batch of TS packet payloads in parallel using the bitslice algorithm. Ensure the `pcks` array is NULL-terminated and `maxlen` is a multiple of 8 (184 for standard TS packets). ```c #include #include #include #include #include #define TS_PAYLOAD_LEN 184 int main(void) { dvbcsa_cw_t cw = { 0x12, 0x34, 0x56, 0x78, 0x1e, 0xd4, 0xf6, 0xab }; struct dvbcsa_bs_key_s *key = dvbcsa_bs_key_alloc(); assert(key != NULL); dvbcsa_bs_key_set(cw, key); unsigned int gs = dvbcsa_bs_batch_size(); /* Allocate per-packet buffers and batch descriptor */ uint8_t **pkt_data = malloc(gs * sizeof(uint8_t *)); struct dvbcsa_bs_batch_s *batch = malloc((gs + 1) * sizeof(struct dvbcsa_bs_batch_s)); for (unsigned int i = 0; i < gs; i++) { pkt_data[i] = malloc(TS_PAYLOAD_LEN); memset(pkt_data[i], 0xA5, TS_PAYLOAD_LEN); /* scrambled payload */ batch[i].data = pkt_data[i]; batch[i].len = TS_PAYLOAD_LEN; } batch[gs].data = NULL; /* NULL-terminate the batch */ /* Decrypt all gs packets in one parallel call */ dvbcsa_bs_decrypt(key, batch, TS_PAYLOAD_LEN); printf("Batch of %u packets decrypted\n", gs); printf("First byte of first packet: 0x%02x\n", batch[0].data[0]); for (unsigned int i = 0; i < gs; i++) free(pkt_data[i]); free(pkt_data); free(batch); dvbcsa_bs_key_free(key); return 0; } ``` -------------------------------- ### Query Maximum Bitslice Batch Size Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Returns the maximum number of packets that can be processed in a single bitslice batch operation. Callers must allocate batch arrays of at least this size plus one extra NULL-terminator entry. ```c #include #include #include int main(void) { unsigned int batch_size = dvbcsa_bs_batch_size(); printf("Maximum batch size: %u packets\n", batch_size); /* Allocate batch array: batch_size entries + 1 NULL terminator */ struct dvbcsa_bs_batch_s *batch = malloc((batch_size + 1) * sizeof(struct dvbcsa_bs_batch_s)); /* ... fill and process batch ... */ free(batch); return 0; } ``` -------------------------------- ### dvbcsa_bs_batch_size Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Queries the maximum number of packets that can be processed in a single bitslice batch. ```APIDOC ## dvbcsa_bs_batch_size — Query maximum batch size ### Description Returns the number of packets that can be processed in a single bitslice batch. This value is determined at compile time by the selected SIMD word width (e.g., 64 for uint64, 128 for SSE2, 256 for AVX2). Callers must allocate batch arrays of at least this size plus one extra NULL-terminator entry. ### Returns The maximum batch size as an unsigned integer. ``` -------------------------------- ### Encrypt and Decrypt TS Packet Payload with DVB CSA Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Demonstrates encrypting and then decrypting a TS packet payload using the DVB CSA algorithm. Ensure the key context is allocated and set before encryption/decryption. The data is modified in-place. ```c #include #include #include #include int main(void) { dvbcsa_cw_t cw = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; unsigned char original[184]; unsigned char data[184]; /* Fill with known pattern */ for (int i = 0; i < 184; i++) original[i] = (unsigned char)i; memcpy(data, original, 184); struct dvbcsa_key_s *key = dvbcsa_key_alloc(); assert(key != NULL); dvbcsa_key_set(cw, key); /* Encrypt */ dvbcsa_encrypt(key, data, 184); printf("Encrypted, first byte: 0x%02x\n", data[0]); /* e.g. 0x2d */ /* Decrypt back */ dvbcsa_decrypt(key, data, 184); assert(memcmp(data, original, 184) == 0); printf("Round-trip encrypt/decrypt successful\n"); dvbcsa_key_free(key); return 0; } ``` -------------------------------- ### Free DVB CSA Key Context Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Releases memory associated with a classical DVB CSA key context. This function must be called to prevent memory leaks, typically after using the key for encryption or decryption operations. ```c #include int main(void) { struct dvbcsa_key_s *key = dvbcsa_key_alloc(); dvbcsa_cw_t cw = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; dvbcsa_key_set(cw, key); /* ... use key for encrypt/decrypt ... */ dvbcsa_key_free(key); /* must be called to avoid memory leak */ return 0; } ``` -------------------------------- ### dvbcsa_bs_decrypt Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Decrypts a batch of TS packet payloads in parallel using the bitslice algorithm. The `pcks` array must be terminated by an entry with a `NULL` data pointer. `maxlen` specifies the maximum payload length to process and must be a multiple of 8; use 184 for standard MPEG-TS packets. Achieves 80–200 Mbits/s depending on SIMD backend. ```APIDOC ## dvbcsa_bs_decrypt ### Description Decrypts a batch of TS packet payloads in parallel using the bitslice algorithm. ### Parameters - `key` (*dvbcsa_bs_key_t*): A pointer to the bitslice key context. - `batch` (*struct dvbcsa_bs_batch_s*): An array of batch descriptors, terminated by an entry with a NULL data pointer. - `maxlen` (unsigned int): The maximum payload length to process, must be a multiple of 8. ### Request Example ```c #include #include #include #include #include #define TS_PAYLOAD_LEN 184 int main(void) { dvbcsa_cw_t cw = { 0x12, 0x34, 0x56, 0x78, 0x1e, 0xd4, 0xf6, 0xab }; struct dvbcsa_bs_key_s *key = dvbcsa_bs_key_alloc(); assert(key != NULL); dvbcsa_bs_key_set(cw, key); unsigned int gs = dvbcsa_bs_batch_size(); uint8_t **pkt_data = malloc(gs * sizeof(uint8_t *)); struct dvbcsa_bs_batch_s *batch = malloc((gs + 1) * sizeof(struct dvbcsa_bs_batch_s)); for (unsigned int i = 0; i < gs; i++) { pkt_data[i] = malloc(TS_PAYLOAD_LEN); memset(pkt_data[i], 0xA5, TS_PAYLOAD_LEN); /* scrambled payload */ batch[i].data = pkt_data[i]; batch[i].len = TS_PAYLOAD_LEN; } batch[gs].data = NULL; /* NULL-terminate the batch */ dvbcsa_bs_decrypt(key, batch, TS_PAYLOAD_LEN); printf("Batch of %u packets decrypted\n", gs); printf("First byte of first packet: 0x%02x\n", batch[0].data[0]); for (unsigned int i = 0; i < gs; i++) free(pkt_data[i]); free(pkt_data); free(batch); dvbcsa_bs_key_free(key); return 0; } ``` ``` -------------------------------- ### dvbcsa_encrypt Source: https://context7.com/glenvt18/libdvbcsa/llms.txt Encrypts a single TS packet payload using the DVB CSA algorithm. This function is symmetric with dvbcsa_decrypt. ```APIDOC ## dvbcsa_encrypt — Encrypt a single TS packet payload ### Description Encrypts `len` bytes of in-place data using the DVB CSA algorithm. Symmetric with `dvbcsa_decrypt`; applying encrypt then decrypt (or vice versa) with the same CW recovers the original data. Throughput is 20–50 Mbits/s on modern hardware. ### Parameters - **key** (*dvbcsa_key_t*) - A pointer to the key context. - **data** (*unsigned char*\*) - A pointer to the data buffer to be encrypted. - **len** (*size_t*) - The number of bytes to encrypt. ### Returns This function does not return a value. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.