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.2KB

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