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.

FFdecsa_test.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* FFdecsa -- fast decsa algorithm
  2. *
  3. * Copyright (C) 2003-2004 fatih89r
  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 as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <sys/time.h>
  22. #include "FFdecsa.h"
  23. #ifndef NULL
  24. #define NULL 0
  25. #endif
  26. #include "FFdecsa_test_testcases.h"
  27. int compare(unsigned char *p1, unsigned char *p2, int n, int silently){
  28. int i;
  29. int ok=1;
  30. for(i=0;i<n;i++){
  31. if(i==3) continue; // tolerate this
  32. if(p1[i]!=p2[i]){
  33. // fprintf(stderr,"at pos 0x%02x, got 0x%02x instead of 0x%02x\n",i,p1[i],p2[i]);
  34. ok=0;
  35. }
  36. }
  37. if(!silently){
  38. if(ok){
  39. fprintf(stderr,"CORRECT!\n");
  40. }
  41. else{
  42. fprintf(stderr,"FAILED!\n");
  43. }
  44. }
  45. return ok;
  46. }
  47. //MAIN
  48. #define TS_PKTS_FOR_TEST 30*1000
  49. //#define TS_PKTS_FOR_TEST 1000*1000
  50. unsigned char megabuf[188*TS_PKTS_FOR_TEST];
  51. unsigned char onebuf[188];
  52. unsigned char *cluster[10];
  53. int main(void){
  54. int i;
  55. struct timeval tvs,tve;
  56. void *keys=get_key_struct();
  57. int ok=1;
  58. fprintf(stderr,"FFdecsa 1.0: testing correctness and speed\n");
  59. /* begin correctness testing */
  60. set_control_words(keys,test_invalid_key,test_1_key);
  61. memcpy(onebuf,test_1_encrypted,188);
  62. cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  63. decrypt_packets(keys,cluster);
  64. ok*=compare(onebuf,test_1_expected,188,0);
  65. set_control_words(keys,test_2_key,test_invalid_key);
  66. memcpy(onebuf,test_2_encrypted,188);
  67. cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  68. decrypt_packets(keys,cluster);
  69. ok*=compare(onebuf,test_2_expected,188,0);
  70. set_control_words(keys,test_3_key,test_invalid_key);
  71. memcpy(onebuf,test_3_encrypted,188);
  72. cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  73. decrypt_packets(keys,cluster);
  74. ok*=compare(onebuf,test_3_expected,188,0);
  75. set_control_words(keys,test_p_10_0_key,test_invalid_key);
  76. memcpy(onebuf,test_p_10_0_encrypted,188);
  77. cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  78. decrypt_packets(keys,cluster);
  79. ok*=compare(onebuf,test_p_10_0_expected,188,0);
  80. set_control_words(keys,test_p_1_6_key,test_invalid_key);
  81. memcpy(onebuf,test_p_1_6_encrypted,188);
  82. cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  83. decrypt_packets(keys,cluster);
  84. ok*=compare(onebuf,test_p_1_6_expected,188,0);
  85. /* begin speed testing */
  86. #if 0
  87. // test on short packets
  88. #define s_encrypted test_p_1_6_encrypted
  89. #define s_key_e test_p_1_6_key
  90. #define s_key_o test_invalid_key
  91. #define s_expected test_p_1_6_expected
  92. #else
  93. //test on full packets
  94. #define s_encrypted test_2_encrypted
  95. #define s_key_e test_2_key
  96. #define s_key_o test_invalid_key
  97. #define s_expected test_2_expected
  98. #endif
  99. for(i=0;i<TS_PKTS_FOR_TEST;i++){
  100. memcpy(&megabuf[188*i],s_encrypted,188);
  101. }
  102. // test that packets are not shuffled around
  103. // so, let's put an undecryptable packet somewhere in the middle (we will use a wrong key)
  104. #define noONE_POISONED_PACKET
  105. #ifdef ONE_POISONED_PACKET
  106. memcpy(&megabuf[188*(TS_PKTS_FOR_TEST*2/3)],test_3_encrypted,188);
  107. #endif
  108. // start decryption
  109. set_control_words(keys,s_key_e,s_key_o);
  110. gettimeofday(&tvs,NULL);
  111. #if 0
  112. // force one by one
  113. for(i=0;i<TS_PKTS_FOR_TEST;i++){
  114. cluster[0]=megabuf+188*i;cluster[1]=onebuf+188*i+188;cluster[2]=NULL;
  115. decrypt_packets(keys,cluster);
  116. }
  117. #else
  118. {
  119. int done=0;
  120. while(done<TS_PKTS_FOR_TEST){
  121. //fprintf(stderr,"done=%i\n",done);
  122. cluster[0]=megabuf+188*done;cluster[1]=megabuf+188*TS_PKTS_FOR_TEST;cluster[2]=NULL;
  123. done+=decrypt_packets(keys,cluster);
  124. }
  125. }
  126. #endif
  127. gettimeofday(&tve,NULL);
  128. //end decryption
  129. fprintf(stderr,"speed=%f Mbit/s\n",(184*TS_PKTS_FOR_TEST*8)/((tve.tv_sec-tvs.tv_sec)+1e-6*(tve.tv_usec-tvs.tv_usec))/1000000);
  130. fprintf(stderr,"speed=%f pkts/s\n",TS_PKTS_FOR_TEST/((tve.tv_sec-tvs.tv_sec)+1e-6*(tve.tv_usec-tvs.tv_usec)));
  131. // this packet couldn't be decrypted correctly
  132. #ifdef ONE_POISONED_PACKET
  133. compare(megabuf+188*(TS_PKTS_FOR_TEST*2/3),test_3_expected,188,0); /* will fail because we used a wrong key */
  134. #endif
  135. // these should be ok
  136. ok*=compare(megabuf,s_expected,188,0);
  137. ok*=compare(megabuf+188*511,s_expected,188,0);
  138. ok*=compare(megabuf+188*512,s_expected,188,0);
  139. ok*=compare(megabuf+188*319,s_expected,188,0);
  140. ok*=compare(megabuf+188*(TS_PKTS_FOR_TEST-1),s_expected,188,0);
  141. for(i=0;i<TS_PKTS_FOR_TEST;i++){
  142. if(!compare(megabuf+188*i,s_expected,188,1)){
  143. fprintf(stderr,"FAILED COMPARISON OF PACKET %10i\n",i);
  144. ok=0;
  145. };
  146. }
  147. return ok ? 0 : 10;
  148. }