Ejemplo n.º 1
0
static void
tvhcsa_csa_cbc_flush
  ( tvhcsa_t *csa, struct mpegts_service *s )
{
#if ENABLE_DVBCSA

  if(csa->csa_fill_even) {
    csa->csa_tsbbatch_even[csa->csa_fill_even].data = NULL;
    dvbcsa_bs_decrypt(csa->csa_key_even, csa->csa_tsbbatch_even, 184);
    csa->csa_fill_even = 0;
  }
  if(csa->csa_fill_odd) {
    csa->csa_tsbbatch_odd[csa->csa_fill_odd].data = NULL;
    dvbcsa_bs_decrypt(csa->csa_key_odd, csa->csa_tsbbatch_odd, 184);
    csa->csa_fill_odd = 0;
  }

  ts_recv_packet2(s, csa->csa_tsbcluster, csa->csa_fill * 188);

  csa->csa_fill = 0;

#else

  int r, l;
  unsigned char *vec[3];

  vec[0] = csa->csa_tsbcluster;
  vec[1] = csa->csa_tsbcluster + csa->csa_fill * 188;
  vec[2] = NULL;

  r = decrypt_packets(csa->csa_keys, vec);
  if(r > 0) {
    ts_recv_packet2(s, csa->csa_tsbcluster, r * 188);

    l = csa->csa_fill - r;
    assert(l >= 0);

    if(l > 0)
      memmove(csa->csa_tsbcluster, csa->csa_tsbcluster + r * 188, l * 188);
    csa->csa_fill = l;
  } else {
    csa->csa_fill = 0;
  }

#endif
}
Ejemplo n.º 2
0
static void
tvhcsa_des_flush
  ( tvhcsa_t *csa, struct mpegts_service *s )
{
#if ENABLE_DVBCSA

  int i;
  const uint8_t *t0;

  if(csa->csa_fill_even) {
    csa->csa_tsbbatch_even[csa->csa_fill_even].data = NULL;
    dvbcsa_bs_decrypt(csa->csa_key_even, csa->csa_tsbbatch_even, 184);
    csa->csa_fill_even = 0;
  }
  if(csa->csa_fill_odd) {
    csa->csa_tsbbatch_odd[csa->csa_fill_odd].data = NULL;
    dvbcsa_bs_decrypt(csa->csa_key_odd, csa->csa_tsbbatch_odd, 184);
    csa->csa_fill_odd = 0;
  }

  t0 = csa->csa_tsbcluster;

  for(i = 0; i < csa->csa_fill; i++) {
    ts_recv_packet2(s, t0);
    t0 += 188;
  }
  csa->csa_fill = 0;

#else

  int r;
  unsigned char *vec[3];

  while(1) {

    vec[0] = csa->csa_tsbcluster;
    vec[1] = csa->csa_tsbcluster + csa->csa_fill * 188;
    vec[2] = NULL;

    r = decrypt_packets(csa->csa_keys, vec);
    if(r > 0) {
      int i;
      const uint8_t *t0 = csa->csa_tsbcluster;

      for(i = 0; i < r; i++) {
        ts_recv_packet2(s, t0);
        t0 += 188;
      }

      r = csa->csa_fill - r;
      assert(r >= 0);

      if(r > 0)
        memmove(csa->csa_tsbcluster, t0, r * 188);
      csa->csa_fill = r;
    } else {
      csa->csa_fill = 0;
    }
    break;
  }

#endif
}
Ejemplo n.º 3
0
int main(void){
  int i;
  struct timeval tvs,tve;
  void *keys=get_key_struct();
  int ok=1;

  fprintf(stderr,"FFdecsa 1.0: testing correctness and speed\n");

/* begin correctness testing */

  set_control_words(keys,test_invalid_key,test_1_key);
  memcpy(onebuf,test_1_encrypted,188);
  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  decrypt_packets(keys,cluster);
  ok*=compare(onebuf,test_1_expected,188,0);

  set_control_words(keys,test_2_key,test_invalid_key);
  memcpy(onebuf,test_2_encrypted,188);
  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  decrypt_packets(keys,cluster);
  ok*=compare(onebuf,test_2_expected,188,0);

  set_control_words(keys,test_3_key,test_invalid_key);
  memcpy(onebuf,test_3_encrypted,188);
  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  decrypt_packets(keys,cluster);
  ok*=compare(onebuf,test_3_expected,188,0);

  set_control_words(keys,test_p_10_0_key,test_invalid_key);
  memcpy(onebuf,test_p_10_0_encrypted,188);
  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  decrypt_packets(keys,cluster);
  ok*=compare(onebuf,test_p_10_0_expected,188,0);

  set_control_words(keys,test_p_1_6_key,test_invalid_key);
  memcpy(onebuf,test_p_1_6_encrypted,188);
  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
  decrypt_packets(keys,cluster);
  ok*=compare(onebuf,test_p_1_6_expected,188,0);

/* begin speed testing */

#if 0
// test on short packets
#define s_encrypted test_p_1_6_encrypted
#define s_key_e     test_p_1_6_key
#define s_key_o     test_invalid_key
#define s_expected  test_p_1_6_expected

#else
//test on full packets
#define s_encrypted test_2_encrypted
#define s_key_e     test_2_key
#define s_key_o     test_invalid_key
#define s_expected  test_2_expected

#endif

  for(i=0;i<TS_PKTS_FOR_TEST;i++){
    memcpy(&megabuf[188*i],s_encrypted,188);
  }
// test that packets are not shuffled around
// so, let's put an undecryptable packet somewhere in the middle (we will use a wrong key)
#define noONE_POISONED_PACKET
#ifdef ONE_POISONED_PACKET
  memcpy(&megabuf[188*(TS_PKTS_FOR_TEST*2/3)],test_3_encrypted,188);
#endif

  // start decryption
  set_control_words(keys,s_key_e,s_key_o);
  gettimeofday(&tvs,NULL);
#if 0
// force one by one
  for(i=0;i<TS_PKTS_FOR_TEST;i++){
    cluster[0]=megabuf+188*i;cluster[1]=onebuf+188*i+188;cluster[2]=NULL;
    decrypt_packets(keys,cluster);
  }
#else
  {
    int done=0;
    while(done<TS_PKTS_FOR_TEST){
      //fprintf(stderr,"done=%i\n",done);
      cluster[0]=megabuf+188*done;cluster[1]=megabuf+188*TS_PKTS_FOR_TEST;cluster[2]=NULL;
      done+=decrypt_packets(keys,cluster);
    }
  }
#endif
  gettimeofday(&tve,NULL);
  //end decryption

  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);
  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)));

  // this packet couldn't be decrypted correctly
#ifdef ONE_POISONED_PACKET
  compare(megabuf+188*(TS_PKTS_FOR_TEST*2/3),test_3_expected,188,0); /* will fail because we used a wrong key */
#endif
  // these should be ok
  ok*=compare(megabuf,s_expected,188,0);
  ok*=compare(megabuf+188*511,s_expected,188,0);
  ok*=compare(megabuf+188*512,s_expected,188,0);
  ok*=compare(megabuf+188*319,s_expected,188,0);
  ok*=compare(megabuf+188*(TS_PKTS_FOR_TEST-1),s_expected,188,0);

  for(i=0;i<TS_PKTS_FOR_TEST;i++){
    if(!compare(megabuf+188*i,s_expected,188,1)){
      fprintf(stderr,"FAILED COMPARISON OF PACKET %10i\n",i);
      ok=0;
    };
  }

  return ok ? 0 : 10;
}