1 //g++ test_sub_perm.cpp -o test_sub_perm -O3 -fpermissive
4 // ./test_sub_perm sizebuf512 nb1000
15 typedef unsigned char uchar;
22 double time_encrypt=0;
23 double time_decrypt=0;
27 struct timeval tstart;
28 gettimeofday(&tstart,0);
29 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
32 double TimeStop(double t)
36 gettimeofday(&tend,0);
37 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
44 void rc4key(uchar *key, uchar *sc, int size_DK) {
46 for(int i=0;i<256;i++) {
52 for(int i0=0; i0<256; i0++) {
53 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
60 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
66 for (int i=0;i<len;i++) {
69 for (int it = 0; it < rp; it++) {
71 for(int i0 = 0; i0<len; i0++) {
72 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
84 int main (int argc, char** argv)
86 /* Set up the key and iv. Do I need to say to not hard code these in a
87 * real application? :-)
94 for(int i=1; i<argc; i++){
95 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
96 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
103 for(int i=0;i<size;i++) {
104 DK[i]=lrand48()&0xFF;
118 rc4key(DK, Sbox1, 8);
119 int *Pbox=new int[imsize];
120 rc4keyperm(&DK[32], imsize, 1, Pbox, 32);
124 buffer=malloc(imsize*sizeof(uchar));
125 for(int i=0;i<imsize;i++) {
131 int oneD=width*height;
132 uchar *plaintext = malloc(imsize+1000); //add that for cbc
133 uchar *plaintext2 = malloc(imsize+1000); //add that for cbc
135 for(int i=0;i<oneD;i++) {
136 plaintext[i]=buffer[i];
137 plaintext2[i]=buffer[i];
142 uchar *ciphertext = malloc(imsize+1000); //add that for cbc
144 /* Buffer for the decrypted text */
145 uchar *decryptedtext = malloc(imsize+1000); //add that for cbc
147 int decryptedtext_len, ciphertext_len;
149 double t=TimeStart();
150 for(int i=0;i<nb_test;i++)
153 for(int j=0;j<imsize;j++)
154 plaintext[j]=Sbox1[plaintext[j]];
157 double time=TimeStop(t);
158 cout<<"Throughput Sbox \t";
159 cout<<(double)imsize*nb_test/time<<"\t";
162 for(int i=0;i<nb_test;i++)
165 for(int j=0;j<imsize;j++)
166 plaintext2[j]=plaintext[Pbox[j]];
170 cout<<"Throughput Pbox \t";
171 cout<<(double)imsize*nb_test/time<<"\t";
174 for(int i=0;i<nb_test;i++)
177 for(int j=0;j<imsize;j++)
178 plaintext2[j]=Sbox1[plaintext[Pbox[j]]];
182 cout<<"Throughput Sbox and Pbox \t";
183 cout<<(double)imsize*nb_test/time<<"\t";