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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. for (i = 0; i < batch_size; i++) {
  100. pcks[i].data = data[i];
  101. pcks[i].len = 184;
  102. memset(data[i], rand(), pcks[i].len);
  103. }
  104. pcks[i].data = NULL;
  105. gettimeofday(&t0, NULL);
  106. for (n = (1 << 12) / batch_size; n < (1 << 19) / batch_size; n *= 2) {
  107. printf(" Decrypting %6u mpegts packets\r", n * batch_size);
  108. fflush(stdout);
  109. for (i = 0; i < n; i++) {
  110. dvbcsa_bs_decrypt(key, pcks, 184);
  111. }
  112. npackets += n * batch_size;
  113. }
  114. gettimeofday(&t1, NULL);
  115. unsigned long long usec = timeval_diff_usec(&t0, &t1);
  116. printf("DONE: %u packets (%u bytes) decrypted in %llu ms = %.1f Mbits/s\n\n",
  117. npackets,
  118. npackets * 188,
  119. usec / 1000,
  120. (double)(npackets * 188 * 8) / (double)usec
  121. );
  122. dvbcsa_bs_key_free(key);
  123. }
  124. void ffdecsa_benchmark(void) {
  125. struct timeval t0, t1;
  126. ffdecsa_key_t *key = ffdecsa_key_alloc();
  127. unsigned int n, i, d, npackets = 0;
  128. unsigned int batch_size = ffdecsa_get_suggested_cluster_size() / 2;
  129. uint8_t data[batch_size + 1][188];
  130. uint8_t *pcks[batch_size * 2 + 1];
  131. uint8_t ecw[8] = { 0x12, 0x34, 0x56, 0x78, 0x89, 0xab, 0xcd, 0xef, };
  132. uint8_t ocw[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, };
  133. ffdecsa_set_even_cw(key, ecw);
  134. ffdecsa_set_odd_cw (key, ocw);
  135. printf("Batch size %d packets.\n\n", batch_size);
  136. for (i = 0; i < batch_size; i++) {
  137. memset(data[i], rand(), 188);
  138. data[i][0] = 0x47;
  139. data[i][1] = 0x01;
  140. data[i][2] = 0x02;
  141. data[i][3] = i & 0x0f;
  142. }
  143. gettimeofday(&t0, NULL);
  144. for (n = (1 << 12) / batch_size; n < (1 << 18) / batch_size; n *= 2) {
  145. static unsigned int key_idx = 0;
  146. printf(" Decrypting %6u mpegts packets\r", n * batch_size);
  147. fflush(stdout);
  148. for (i = 0; i < n; i++) {
  149. // ffdecsa_decrypt function modifies data and pcks
  150. for (d = 0; d < batch_size; d++) {
  151. pcks[d * 2] = data[d];
  152. pcks[d * 2 + 1] = data[d] + 188;
  153. data[d][3] |= (key_idx == 0) ? (2 << 6) : (3 << 6);
  154. }
  155. pcks[d * 2] = NULL;
  156. key_idx = !!key_idx;
  157. ffdecsa_decrypt_packets(key, pcks);
  158. }
  159. npackets += n * batch_size;
  160. }
  161. gettimeofday(&t1, NULL);
  162. unsigned long long usec = timeval_diff_usec(&t0, &t1);
  163. printf("DONE: %u packets (%u bytes) decrypted in %llu ms = %.1f Mbits/s\n\n",
  164. npackets,
  165. npackets * 188,
  166. usec / 1000,
  167. (double)(npackets * 188 * 8) / (double)usec
  168. );
  169. dvbcsa_bs_key_free(key);
  170. }
  171. void csa_benchmark(void) {
  172. srand(time(0));
  173. printf("Single threaded CSA decoding benchmark : %s\n", DLIB);
  174. if (use_dvbcsa)
  175. dvbcsa_benchmark();
  176. if (use_ffdecsa)
  177. ffdecsa_benchmark();
  178. }