1 //version to test the performance of our code with message of size 16 to 240
2 //in order to make test with Duc
4 //the size of the message can be changed
20 struct timeval tstart;
21 gettimeofday(&tstart,0);
22 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
25 double TimeStop(double t)
29 gettimeofday(&tend,0);
30 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
37 typedef uint8_t uchar;
39 //typedef unsigned long uint;
49 //#define STATIC_KEY_256q
50 //#define STATIC_KEY_128
51 //#define STATIC_KEY_64
52 //#define STATIC_KEY_32
63 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
64 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
65 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
66 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
67 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
68 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
69 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
70 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
71 0x2B, 0x7E, 0x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E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
89 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
90 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
91 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
92 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
96 #elif defined STATIC_KEY_64
99 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
100 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
101 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
102 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
105 #elif defined STATIC_KEY_32
108 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C,
109 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
112 #elif defined STATIC_KEY_16
115 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
131 uint xorshift32(const uint t)
133 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
141 void rc4key(uchar *key, uchar *sc, int size_DK) {
143 for(int i=0;i<256;i+=4) {
151 for(int i0=0; i0<256; i0++) {
152 j0 = (j0 + sc[i0] + key[i0&(size_DK-1)] );
159 void rc4keyperm(uchar *key,int len, int rp,uchar *sc, int size_DK) {
163 for (int i=0;i<len;i++) {
167 for (int it = 0; it < rp; it++) {
169 for(int i0 = 0; i0<len; i0++) {
170 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
178 void prga(uchar *sc, int ldata, uchar *r) {
182 for (int it=0; it<ldata; it++) {
184 j0 = (j0 + sc[i0])&0xFF;
188 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
192 void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1, uchar *PboxRM, uchar *Sbox1, uchar *Sbox2,uint myrand,int enc) {
199 for(int a=0;a<h2;a+=4) {
203 X[a]=Sbox1[RM1[a]^(mm&255)]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
205 X[a+1]=Sbox2[RM1[a+1]^(mm&255)];
207 X[a+2]=Sbox1[RM1[a+2]^(mm&255)];
209 X[a+3]=Sbox2[RM1[a+3]^(mm&255)];
212 /* uint32_t val=*((uint32_t*)&RM1[a])^mm;
213 uchar *val2=(uchar*)&val;
215 X[a+1]=Sbox2[val2[1]];
216 X[a+2]=Sbox1[val2[2]];
217 X[a+3]=Sbox2[val2[3]];
224 for(int it=0;it<len;it++) {
226 for(int a=0;a<h2;a+=4) {
227 myrand=xorshift32(myrand);
229 X[a]=Sbox2[X[a]^RM1[a]^(mm&255)];
231 X[a+1]=Sbox1[X[a+1]^RM1[a+1]^(mm&255)];
233 X[a+2]=Sbox2[X[a+2]^RM1[a+2]^(mm&255)];
235 X[a+3]=Sbox1[X[a+3]^RM1[a+3]^(mm&255)];
237 /* uint32_t val=*((uint32_t*)&X[a])^*((uint32_t*)&RM1[a])^mm;
238 uchar *val2=(uchar*)&val;
240 X[a+1]=Sbox1[val2[1]];
241 X[a+2]=Sbox2[val2[2]];
242 X[a+3]=Sbox1[val2[3]];
248 for(int a=0;a<h2;a+=4) {
249 seq_out[ind+a]=X[a]^seq_in[ind+a];
250 seq_out[ind+a+1]=X[a+1]^seq_in[ind+a+1];
251 seq_out[ind+a+2]=X[a+2]^seq_in[ind+a+2];
252 seq_out[ind+a+3]=X[a+3]^seq_in[ind+a+3];
255 for(int a=0;a<h2;a+=4) {
256 /* RM1[a]=Sbox2[RM1[PboxRM[a]]];
257 RM1[a+1]=Sbox1[RM1[PboxRM[a+1]]];
258 RM1[a+2]=Sbox2[RM1[PboxRM[a+2]]];
259 RM1[a+3]=Sbox1[RM1[PboxRM[a+3]]];
261 uchar w0=Sbox2[RM1[PboxRM[a]]];
262 uchar w1=Sbox1[RM1[PboxRM[a+1]]];
263 uchar w2=Sbox2[RM1[PboxRM[a+2]]];
264 uchar w3=Sbox1[RM1[PboxRM[a+3]]];
279 void printArray(byte *mes, int n) {
280 for (byte i = 0; i < n; i++) {
281 printf("%d ",mes[i]);
286 int main (int argc, char ** argv)
295 for(int i=1; i<argc; i++){
296 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
297 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
301 size_buf=size_buf*size_buf;
303 int size_mesg=size_buf;
306 int len=(size_mesg+h2-1)/h2;
329 //initialization of the key
330 for(int i=0;i<SKEY;i+=4) {
331 seed=xorshift32(seed);
346 for(int i=0;i<256;i+=4) {
347 seed=xorshift32(seed);
361 for(int i=0;i<SKEY;i++) {
368 //computation of sbox and other dynamic tables
369 rc4key(&DK[0], sc, SKEY/4);
371 rc4keyperm(&DK[SKEY/4], h2, rp, PboxRM, SKEY/4);
372 rc4key(&DK[SKEY/2], Sbox1, SKEY/4);
373 rc4key(&DK[(SKEY/4)*3], Sbox2, SKEY/4);
378 for(int i=0;i<32;i++) {
383 /* plain = malloc(max_size_mesg*sizeof(byte));
384 cipher=malloc(max_size_mesg*sizeof(byte));
385 check=malloc(max_size_mesg*sizeof(byte));
388 // printf("len %d\n",len);
390 //we keep the same message or not
392 for(int i=0;i<size_mesg;i++) {
393 seed=xorshift32(seed);
398 for(int i=0;i<32;i++) {
404 //RM1 is changed in the encryption so we make a copy for the decryption
405 for(int i=0;i<h2;i++){
410 // unsigned long ms1 = micros ();
411 double t=TimeStart();
412 for(int i=0;i<nb_test;i++)
414 encrypt_ctr(plain, cipher,len,RM1,PboxRM,Sbox1,Sbox2,myrand,1);
416 double time_encrypt=TimeStop(t);
417 printf("%e \t",(double)size_buf*nb_test/time_encrypt);
420 for(int i=0;i<nb_test;i++)
422 encrypt_ctr(cipher, check,len,RM2,PboxRM,Sbox1,Sbox2,myrand,0);
424 double time_decrypt=TimeStop(t);
425 printf("%e \t",(double)size_buf*nb_test/time_decrypt);
431 for(int i=0;i<size_mesg;i++) {
433 if(check[i]!=plain[i]) {
438 printf("CHECK %d\n",equal);