SASC
DVBSoftCAM

FFdecsa_test.c

Go to the documentation of this file.
00001 /* FFdecsa -- fast decsa algorithm
00002  *
00003  * Copyright (C) 2003-2004  fatih89r
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  */
00019 
00020 
00021 #include <string.h>
00022 #include <stdio.h>
00023 #include <sys/time.h>
00024 
00025 #include "FFdecsa.h"
00026 
00027 #ifndef NULL
00028 #define NULL 0
00029 #endif
00030 
00031 #include "FFdecsa_test_testcases.h"
00032 
00033 int compare(unsigned char *p1, unsigned char *p2, int n, int silently){
00034   int i;
00035   int ok=1;
00036   for(i=0;i<n;i++){
00037     if(i==3) continue; // tolerate this
00038     if(p1[i]!=p2[i]){
00039 //      fprintf(stderr,"at pos 0x%02x, got 0x%02x instead of 0x%02x\n",i,p1[i],p2[i]);
00040       ok=0;
00041     }
00042   }
00043   if(!silently){
00044     if(ok){
00045        fprintf(stderr,"CORRECT!\n");
00046     }
00047     else{
00048        fprintf(stderr,"FAILED!\n");
00049     }
00050   }
00051   return ok;
00052 }
00053 
00054 
00055 //MAIN
00056 
00057 #define TS_PKTS_FOR_TEST 30*1000
00058 //#define TS_PKTS_FOR_TEST 1000*1000
00059 unsigned char megabuf[188*TS_PKTS_FOR_TEST];
00060 unsigned char onebuf[188];
00061 
00062 unsigned char *cluster[10];
00063 
00064 int main(void){
00065   int i;
00066   struct timeval tvs,tve;
00067   void *keys=get_key_struct();
00068   int ok=1;
00069 
00070   fprintf(stderr,"FFdecsa 1.0: testing correctness and speed\n");
00071 
00072 /* begin correctness testing */
00073 
00074   set_control_words(keys,test_invalid_key,test_1_key);
00075   memcpy(onebuf,test_1_encrypted,188);
00076   cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
00077   decrypt_packets(keys,cluster);
00078   ok*=compare(onebuf,test_1_expected,188,0);
00079 
00080   set_control_words(keys,test_2_key,test_invalid_key);
00081   memcpy(onebuf,test_2_encrypted,188);
00082   cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
00083   decrypt_packets(keys,cluster);
00084   ok*=compare(onebuf,test_2_expected,188,0);
00085 
00086   set_control_words(keys,test_3_key,test_invalid_key);
00087   memcpy(onebuf,test_3_encrypted,188);
00088   cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
00089   decrypt_packets(keys,cluster);
00090   ok*=compare(onebuf,test_3_expected,188,0);
00091 
00092   set_control_words(keys,test_p_10_0_key,test_invalid_key);
00093   memcpy(onebuf,test_p_10_0_encrypted,188);
00094   cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
00095   decrypt_packets(keys,cluster);
00096   ok*=compare(onebuf,test_p_10_0_expected,188,0);
00097 
00098   set_control_words(keys,test_p_1_6_key,test_invalid_key);
00099   memcpy(onebuf,test_p_1_6_encrypted,188);
00100   cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
00101   decrypt_packets(keys,cluster);
00102   ok*=compare(onebuf,test_p_1_6_expected,188,0);
00103 
00104 /* begin speed testing */
00105 
00106 #if 0
00107 // test on short packets
00108 #define s_encrypted test_p_1_6_encrypted
00109 #define s_key_e     test_p_1_6_key
00110 #define s_key_o     test_invalid_key
00111 #define s_expected  test_p_1_6_expected
00112 
00113 #else
00114 //test on full packets
00115 #define s_encrypted test_2_encrypted
00116 #define s_key_e     test_2_key
00117 #define s_key_o     test_invalid_key
00118 #define s_expected  test_2_expected
00119 
00120 #endif
00121 
00122   for(i=0;i<TS_PKTS_FOR_TEST;i++){
00123     memcpy(&megabuf[188*i],s_encrypted,188);
00124   }
00125 // test that packets are not shuffled around
00126 // so, let's put an undecryptable packet somewhere in the middle (we will use a wrong key)
00127 #define noONE_POISONED_PACKET
00128 #ifdef ONE_POISONED_PACKET
00129   memcpy(&megabuf[188*(TS_PKTS_FOR_TEST*2/3)],test_3_encrypted,188);
00130 #endif
00131 
00132   // start decryption
00133   set_control_words(keys,s_key_e,s_key_o);
00134   gettimeofday(&tvs,NULL);
00135 #if 0
00136 // force one by one
00137   for(i=0;i<TS_PKTS_FOR_TEST;i++){
00138     cluster[0]=megabuf+188*i;cluster[1]=onebuf+188*i+188;cluster[2]=NULL;
00139     decrypt_packets(keys,cluster);
00140   }
00141 #else
00142   {
00143     int done=0;
00144     while(done<TS_PKTS_FOR_TEST){
00145       //fprintf(stderr,"done=%i\n",done);
00146       cluster[0]=megabuf+188*done;cluster[1]=megabuf+188*TS_PKTS_FOR_TEST;cluster[2]=NULL;
00147       done+=decrypt_packets(keys,cluster);
00148     }
00149   }
00150 #endif
00151   gettimeofday(&tve,NULL);
00152   //end decryption
00153 
00154   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);
00155   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)));
00156 
00157   // this packet couldn't be decrypted correctly
00158 #ifdef ONE_POISONED_PACKET
00159   compare(megabuf+188*(TS_PKTS_FOR_TEST*2/3),test_3_expected,188,0); /* will fail because we used a wrong key */
00160 #endif
00161   // these should be ok
00162   ok*=compare(megabuf,s_expected,188,0);
00163   ok*=compare(megabuf+188*511,s_expected,188,0);
00164   ok*=compare(megabuf+188*512,s_expected,188,0);
00165   ok*=compare(megabuf+188*319,s_expected,188,0);
00166   ok*=compare(megabuf+188*(TS_PKTS_FOR_TEST-1),s_expected,188,0);
00167 
00168   for(i=0;i<TS_PKTS_FOR_TEST;i++){
00169     if(!compare(megabuf+188*i,s_expected,188,1)){
00170       fprintf(stderr,"FAILED COMPARISON OF PACKET %10i\n",i);
00171       ok=0;
00172     };
00173   }
00174 
00175   return ok ? 0 : 10;
00176 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines