tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

csa.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * CSA functions
  3. * Copyright (C) 2011-2012 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License (COPYING file) for more details.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include <sys/time.h>
  20. #include "libfuncs/libfuncs.h"
  21. #include "csa.h"
  22. csakey_t *csa_key_alloc(void) {
  23. struct csakey *key = calloc(1, sizeof(struct csakey));
  24. key->s_csakey[0] = dvbcsa_key_alloc();
  25. key->s_csakey[1] = dvbcsa_key_alloc();
  26. key->bs_csakey[0] = dvbcsa_bs_key_alloc();
  27. key->bs_csakey[1] = dvbcsa_bs_key_alloc();
  28. key->ff_csakey = ffdecsa_key_alloc();
  29. return (csakey_t *)key;
  30. }
  31. void csa_key_free(csakey_t **pcsakey) {
  32. struct csakey *key = *((struct csakey **)pcsakey);
  33. if (key) {
  34. dvbcsa_key_free(key->s_csakey[0]);
  35. dvbcsa_key_free(key->s_csakey[1]);
  36. dvbcsa_bs_key_free(key->bs_csakey[0]);
  37. dvbcsa_bs_key_free(key->bs_csakey[1]);
  38. ffdecsa_key_free(key->ff_csakey);
  39. FREE(*pcsakey);
  40. }
  41. }
  42. inline unsigned int csa_get_batch_size(void) {
  43. if (use_dvbcsa) {
  44. return dvbcsa_bs_batch_size(); // 32?
  45. }
  46. if (use_ffdecsa) {
  47. return ffdecsa_get_suggested_cluster_size() / 2;
  48. }
  49. return 0;
  50. }
  51. inline void csa_set_even_cw(csakey_t *csakey, uint8_t *even_cw) {
  52. struct csakey *key = (struct csakey *)csakey;
  53. dvbcsa_key_set(even_cw, key->s_csakey[0]);
  54. dvbcsa_bs_key_set(even_cw, key->bs_csakey[0]);
  55. ffdecsa_set_even_cw(key->ff_csakey, even_cw);
  56. }
  57. inline void csa_set_odd_cw(csakey_t *csakey, uint8_t *odd_cw) {
  58. struct csakey *key = (struct csakey *)csakey;
  59. dvbcsa_key_set(odd_cw, key->s_csakey[1]);
  60. dvbcsa_bs_key_set(odd_cw, key->bs_csakey[1]);
  61. ffdecsa_set_odd_cw(key->ff_csakey, odd_cw);
  62. }
  63. inline void csa_decrypt_single_packet(csakey_t *csakey, uint8_t *ts_packet) {
  64. struct csakey *key = (struct csakey *)csakey;
  65. if (use_dvbcsa) {
  66. unsigned int key_idx = ts_packet_get_scrambled(ts_packet) - 2;
  67. unsigned int payload_offset = ts_packet_get_payload_offset(ts_packet);
  68. ts_packet_set_not_scrambled(ts_packet);
  69. dvbcsa_decrypt(key->s_csakey[key_idx], ts_packet + payload_offset, 188 - payload_offset);
  70. }
  71. if (use_ffdecsa) {
  72. uint8_t *cluster[3] = { ts_packet, ts_packet + 188, NULL };
  73. ffdecsa_decrypt_packets(key->ff_csakey, cluster);
  74. }
  75. }
  76. inline void csa_decrypt_multiple_even(csakey_t *csakey, struct csa_batch *batch) {
  77. struct csakey *key = (struct csakey *)csakey;
  78. dvbcsa_bs_decrypt(key->bs_csakey[0], (struct dvbcsa_bs_batch_s *)batch, 184);
  79. }
  80. inline void csa_decrypt_multiple_odd(csakey_t *csakey, struct csa_batch *batch) {
  81. struct csakey *key = (struct csakey *)csakey;
  82. dvbcsa_bs_decrypt(key->bs_csakey[1], (struct dvbcsa_bs_batch_s *)batch, 184);
  83. }
  84. inline void csa_decrypt_multiple_ff(csakey_t *csakey, uint8_t **cluster) {
  85. struct csakey *key = (struct csakey *)csakey;
  86. ffdecsa_decrypt_packets(key->ff_csakey, cluster);
  87. }
  88. /* The following routine is taken from benchbitslice in libdvbcsa */
  89. void dvbcsa_benchmark(void) {
  90. struct timeval t0, t1;
  91. struct dvbcsa_bs_key_s *key = dvbcsa_bs_key_alloc();
  92. unsigned int n, i, npackets = 0;
  93. unsigned int batch_size = dvbcsa_bs_batch_size();
  94. uint8_t data[batch_size + 1][188];
  95. struct dvbcsa_bs_batch_s pcks[batch_size + 1];
  96. uint8_t cw[8] = { 0x12, 0x34, 0x56, 0x78, 0x89, 0xab, 0xcd, 0xef, };
  97. dvbcsa_bs_key_set (cw, key);
  98. printf("Batch size %d packets.\n\n", batch_size);
  99. if (!batch_size)
  100. return;
  101. for (i = 0; i < batch_size; i++) {
  102. pcks[i].data = data[i];
  103. pcks[i].len = 184;
  104. memset(data[i], rand(), pcks[i].len);
  105. }
  106. pcks[i].data = NULL;
  107. gettimeofday(&t0, NULL);
  108. for (n = (1 << 12) / batch_size; n < (1 << 19) / batch_size; n *= 2) {
  109. printf(" Decrypting %6u mpegts packets\r", n * batch_size);
  110. fflush(stdout);
  111. for (i = 0; i < n; i++) {
  112. dvbcsa_bs_decrypt(key, pcks, 184);
  113. }
  114. npackets += n * batch_size;
  115. }
  116. gettimeofday(&t1, NULL);
  117. unsigned long long usec = timeval_diff_usec(&t0, &t1);
  118. printf("DONE: %u packets (%u bytes) decrypted in %llu ms = %.1f Mbits/s\n\n",
  119. npackets,
  120. npackets * 188,
  121. usec / 1000,
  122. (double)(npackets * 188 * 8) / (double)usec
  123. );
  124. dvbcsa_bs_key_free(key);
  125. }
  126. void ffdecsa_benchmark(void) {
  127. struct timeval t0, t1;
  128. ffdecsa_key_t *key = ffdecsa_key_alloc();
  129. unsigned int n, i, d, npackets = 0;
  130. unsigned int batch_size = ffdecsa_get_suggested_cluster_size() / 2;
  131. uint8_t data[batch_size + 1][188];
  132. uint8_t *pcks[batch_size * 2 + 1];
  133. uint8_t ecw[8] = { 0x12, 0x34, 0x56, 0x78, 0x89, 0xab, 0xcd, 0xef, };
  134. uint8_t ocw[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, };
  135. ffdecsa_set_even_cw(key, ecw);
  136. ffdecsa_set_odd_cw (key, ocw);
  137. printf("Batch size %d packets.\n\n", batch_size);
  138. if (!batch_size)
  139. return;
  140. for (i = 0; i < batch_size; i++) {
  141. memset(data[i], rand(), 188);
  142. data[i][0] = 0x47;
  143. data[i][1] = 0x01;
  144. data[i][2] = 0x02;
  145. data[i][3] = i & 0x0f;
  146. }
  147. gettimeofday(&t0, NULL);
  148. for (n = (1 << 12) / batch_size; n < (1 << 18) / batch_size; n *= 2) {
  149. static unsigned int key_idx = 0;
  150. printf(" Decrypting %6u mpegts packets\r", n * batch_size);
  151. fflush(stdout);
  152. for (i = 0; i < n; i++) {
  153. // ffdecsa_decrypt function modifies data and pcks
  154. for (d = 0; d < batch_size; d++) {
  155. pcks[d * 2] = data[d];
  156. pcks[d * 2 + 1] = data[d] + 188;
  157. data[d][3] |= (key_idx == 0) ? (2 << 6) : (3 << 6);
  158. }
  159. pcks[d * 2] = NULL;
  160. key_idx = !!key_idx;
  161. ffdecsa_decrypt_packets(key, pcks);
  162. }
  163. npackets += n * batch_size;
  164. }
  165. gettimeofday(&t1, NULL);
  166. unsigned long long usec = timeval_diff_usec(&t0, &t1);
  167. printf("DONE: %u packets (%u bytes) decrypted in %llu ms = %.1f Mbits/s\n\n",
  168. npackets,
  169. npackets * 188,
  170. usec / 1000,
  171. (double)(npackets * 188 * 8) / (double)usec
  172. );
  173. dvbcsa_bs_key_free(key);
  174. }
  175. void csa_benchmark(void) {
  176. srand(time(0));
  177. printf("Single threaded CSA decoding benchmark : %s\n", DLIB);
  178. if (use_dvbcsa)
  179. dvbcsa_benchmark();
  180. if (use_ffdecsa)
  181. ffdecsa_benchmark();
  182. }