PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/FFdecsa/FFdecsa_test.c

https://bitbucket.org/a84/gs_public/
C | 176 lines | 115 code | 28 blank | 33 comment | 9 complexity | 0d77f10f14006646ff0c6c7dab0fdf12 MD5 | raw file
  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
 20
 21#include <string.h>
 22#include <stdio.h>
 23#include <sys/time.h>
 24
 25#include "FFdecsa.h"
 26
 27#ifndef NULL
 28#define NULL 0
 29#endif
 30
 31#include "FFdecsa_test_testcases.h"
 32
 33int compare(unsigned char *p1, unsigned char *p2, int n, int silently){
 34  int i;
 35  int ok=1;
 36  for(i=0;i<n;i++){
 37    if(i==3) continue; // tolerate this
 38    if(p1[i]!=p2[i]){
 39//      fprintf(stderr,"at pos 0x%02x, got 0x%02x instead of 0x%02x\n",i,p1[i],p2[i]);
 40      ok=0;
 41    }
 42  }
 43  if(!silently){
 44    if(ok){
 45       fprintf(stderr,"CORRECT!\n");
 46    }
 47    else{
 48       fprintf(stderr,"FAILED!\n");
 49    }
 50  }
 51  return ok;
 52}
 53
 54
 55//MAIN
 56
 57#define TS_PKTS_FOR_TEST 30*1000
 58//#define TS_PKTS_FOR_TEST 1000*1000
 59unsigned char megabuf[188*TS_PKTS_FOR_TEST];
 60unsigned char onebuf[188];
 61
 62unsigned char *cluster[10];
 63
 64int main(void){
 65  int i;
 66  struct timeval tvs,tve;
 67  void *keys=get_key_struct();
 68  int ok=1;
 69
 70  fprintf(stderr,"FFdecsa 1.0: testing correctness and speed\n");
 71
 72/* begin correctness testing */
 73
 74  set_control_words(keys,test_invalid_key,test_1_key);
 75  memcpy(onebuf,test_1_encrypted,188);
 76  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
 77  decrypt_packets(keys,cluster);
 78  ok*=compare(onebuf,test_1_expected,188,0);
 79
 80  set_control_words(keys,test_2_key,test_invalid_key);
 81  memcpy(onebuf,test_2_encrypted,188);
 82  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
 83  decrypt_packets(keys,cluster);
 84  ok*=compare(onebuf,test_2_expected,188,0);
 85
 86  set_control_words(keys,test_3_key,test_invalid_key);
 87  memcpy(onebuf,test_3_encrypted,188);
 88  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
 89  decrypt_packets(keys,cluster);
 90  ok*=compare(onebuf,test_3_expected,188,0);
 91
 92  set_control_words(keys,test_p_10_0_key,test_invalid_key);
 93  memcpy(onebuf,test_p_10_0_encrypted,188);
 94  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
 95  decrypt_packets(keys,cluster);
 96  ok*=compare(onebuf,test_p_10_0_expected,188,0);
 97
 98  set_control_words(keys,test_p_1_6_key,test_invalid_key);
 99  memcpy(onebuf,test_p_1_6_encrypted,188);
100  cluster[0]=onebuf;cluster[1]=onebuf+188;cluster[2]=NULL;
101  decrypt_packets(keys,cluster);
102  ok*=compare(onebuf,test_p_1_6_expected,188,0);
103
104/* begin speed testing */
105
106#if 0
107// test on short packets
108#define s_encrypted test_p_1_6_encrypted
109#define s_key_e     test_p_1_6_key
110#define s_key_o     test_invalid_key
111#define s_expected  test_p_1_6_expected
112
113#else
114//test on full packets
115#define s_encrypted test_2_encrypted
116#define s_key_e     test_2_key
117#define s_key_o     test_invalid_key
118#define s_expected  test_2_expected
119
120#endif
121
122  for(i=0;i<TS_PKTS_FOR_TEST;i++){
123    memcpy(&megabuf[188*i],s_encrypted,188);
124  }
125// test that packets are not shuffled around
126// so, let's put an undecryptable packet somewhere in the middle (we will use a wrong key)
127#define noONE_POISONED_PACKET
128#ifdef ONE_POISONED_PACKET
129  memcpy(&megabuf[188*(TS_PKTS_FOR_TEST*2/3)],test_3_encrypted,188);
130#endif
131
132  // start decryption
133  set_control_words(keys,s_key_e,s_key_o);
134  gettimeofday(&tvs,NULL);
135#if 0
136// force one by one
137  for(i=0;i<TS_PKTS_FOR_TEST;i++){
138    cluster[0]=megabuf+188*i;cluster[1]=onebuf+188*i+188;cluster[2]=NULL;
139    decrypt_packets(keys,cluster);
140  }
141#else
142  {
143    int done=0;
144    while(done<TS_PKTS_FOR_TEST){
145      //fprintf(stderr,"done=%i\n",done);
146      cluster[0]=megabuf+188*done;cluster[1]=megabuf+188*TS_PKTS_FOR_TEST;cluster[2]=NULL;
147      done+=decrypt_packets(keys,cluster);
148    }
149  }
150#endif
151  gettimeofday(&tve,NULL);
152  //end decryption
153
154  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);
155  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)));
156
157  // this packet couldn't be decrypted correctly
158#ifdef ONE_POISONED_PACKET
159  compare(megabuf+188*(TS_PKTS_FOR_TEST*2/3),test_3_expected,188,0); /* will fail because we used a wrong key */
160#endif
161  // these should be ok
162  ok*=compare(megabuf,s_expected,188,0);
163  ok*=compare(megabuf+188*511,s_expected,188,0);
164  ok*=compare(megabuf+188*512,s_expected,188,0);
165  ok*=compare(megabuf+188*319,s_expected,188,0);
166  ok*=compare(megabuf+188*(TS_PKTS_FOR_TEST-1),s_expected,188,0);
167
168  for(i=0;i<TS_PKTS_FOR_TEST;i++){
169    if(!compare(megabuf+188*i,s_expected,188,1)){
170      fprintf(stderr,"FAILED COMPARISON OF PACKET %10i\n",i);
171      ok=0;
172    };
173  }
174
175  return ok ? 0 : 10;
176}