MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / mxhwenp / test / test_ssl.c
bloba0ecc3c6be4323af07d4ed08ac554bfe1fc59fc9
1 #include <unistd.h>
2 #include <sys/types.h>
3 #ifdef MODULE
4 #define PTIME printk
5 #else
6 #include <time.h>
7 #include <sys/times.h>
8 #include <mxhw_crypto_wrapper.h>
9 #define PTIME printf
10 #endif
11 #include <test_ssl.h>
13 int
14 BIO_snprintf(char *buf, size_t size, const char *format, ...){return 0;}
16 static double timeSWT[16],timeHWU[16],timeHWS[16];
18 u_int
19 fill_data(u_char *buf, u_int size)
21 u_char *p = buf;
22 int i;
23 static int first=1;
25 if (first)
27 srand(time(0));
28 first=0;
30 if (size==0)
31 size = 160+((rand()%(MAX_BUF-160))&0xfffffff0);
33 for(i=0; i<size ;i++,p++)
34 *p = rand()&0xff;
36 return size;
40 void
41 times_reset(u_int numPktSizes)
43 memset(timeSWT, 0, 16*sizeof(unsigned int));
44 memset(timeHWU, 0, 16*sizeof(unsigned int));
45 memset(timeHWS, 0, 16*sizeof(unsigned int));
48 unsigned int ixOsServTimestampGet(void);
50 u_int
51 timestamps()
53 #ifdef MODULE
54 return ixOsServTimestampGet();
55 #else
56 return times(0);
57 #endif
60 void
61 timeswhw(double *swT, double *hwT, int idx)
63 *hwT = timeHWU[idx];
64 *swT = timeSWT[idx];
67 void
68 timereport(char *name, u_int *pktSizes, u_int max, u_int numPkt, u_int total)
70 unsigned int m;
71 double bytes;
73 if (max<2)
74 return;
76 PTIME("\n%ld/%ld\t%s*\t%s\t(M)\t(M)\tRATE\t%s*K\n",numPkt, total,name, name, name);
77 for(m=0; m < max; m++)
79 bytes = (double) pktSizes[m]*numPkt/1e6;
81 PTIME("%d\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n",
82 pktSizes[m], /* # of packets */
83 timeHWU[m], /* hw total time */
84 timeSWT[m], /* hw system time */
85 (timeHWU[m]!=0)? bytes/timeHWU[m]:0, /* hw in M bytes */
86 (timeSWT[m]!=0)? bytes/timeSWT[m]:0, /* sw in M bytes */
87 (timeHWU[m]!=0)? timeSWT[m]/timeHWU[m]:0, /* rate */
88 timeHWS[m]);
92 static unsigned int
93 timestick(unsigned int end, unsigned int start)
95 return (end<start)? (0xffffffff-start+end+1):(end-start);
98 void
99 timeit(u_int hwsw, u_int idx, int type, int add)
101 static struct tms tstart, tend;
102 static clock_t s, e;
104 if (idx>15)
105 return;
107 if (type==TSTART)
109 s = times(&tstart);
111 else
113 double d;
115 e = times(&tend);
117 if (hwsw==ISSW)
119 d = (double) timestick(e, s)/sysconf(_SC_CLK_TCK);
120 if (add)
121 timeSWT[idx] += d;
122 else
123 timeSWT[idx] = d;
125 else
127 d = (double) timestick(e, s)/sysconf(_SC_CLK_TCK);
128 if (add)
129 timeHWU[idx] += d;
130 else
131 timeHWU[idx] = d;
132 d = (double) timestick(tend.tms_stime,tstart.tms_stime)/sysconf(_SC_CLK_TCK);
133 if (add)
134 timeHWS[idx] += d;
135 else
136 timeHWS[idx] = d;
141 void
142 test_set_keys(TESTHW *t, u_char *keys, int IS_AES)
144 if (IS_AES)
146 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
147 if (AES_set_encrypt_key(keys, t->klen<<3, &t->AESKey1) ||
148 AES_set_decrypt_key(keys, t->klen<<3, &t->AESKey2))
149 return;
150 t->key1 = &t->AESKey1;
151 t->key2 = &t->AESKey2;
152 t->key3 = NULL;
153 #endif
155 else
157 DES_set_key_unchecked((DES_cblock*) keys, &t->DESks1);
158 DES_set_key_unchecked((DES_cblock*) (keys+8), &t->DESks2);
159 DES_set_key_unchecked((DES_cblock*) (keys+16), &t->DESks3);
160 t->key1 = &t->DESks1;
161 t->key2 = &t->DESks2;
162 t->key3 = &t->DESks3;
166 TCIPHER(test_DES_ecb_encrypt)
168 if (length < HWACC_PKTLEN_MIN_DES || mxcrypto_perfrom(MXCIPHER_ALGO_DES, MXCIPHER_MODE_ECB,
169 (const u_char *) input, output, length, ks1, NULL, NULL, NULL,0, enc))
171 u_int len=0;
172 while (len < length)
174 DES_ecb_encrypt((const_DES_cblock*) (input+len),(DES_cblock*)(output+len),
175 (DES_key_schedule*) ks1,enc);
176 len+=8;
181 TCIPHER(test_DES_cbc_encrypt)
183 DES_ncbc_encrypt(input,output, length,(DES_key_schedule*)ks1,(DES_cblock*)ivec, enc);
186 #ifdef CPE_ENGINE
187 TCIPHER(test_DES_cfb_encrypt)
189 DES_cfb_encrypt(input,output,8,length,(DES_key_schedule*)ks1,(DES_cblock*)ivec, enc);
192 TCIPHER(test_DES_ofb_encrypt)
194 int blks = 0;
195 DES_ofb64_encrypt(input,output,length,(DES_key_schedule*)ks1,(DES_cblock*)ivec, &blks);
198 /* not supported by CPE */
199 TCIPHER(test_DES_cfb64_encrypt)
201 int blks=0;
202 DES_cfb64_encrypt(input,output,length,(DES_key_schedule*)ks1,(DES_cblock*)ivec, &blks, enc);
205 #endif
207 TCIPHER(test_DES_ecb3_encrypt)
209 if (length < HWACC_PKTLEN_MIN || mxcrypto_perfrom(MXCIPHER_ALGO_3DES, MXCIPHER_MODE_ECB,
210 (const u_char *) input, output, length, ks1, ks2, ks3, NULL,0, enc))
212 u_int len=0;
214 while (len < length)
216 DES_ecb3_encrypt(input+len, output+len,
217 (DES_key_schedule*)ks1, (DES_key_schedule*)ks2, (DES_key_schedule*)ks3, enc);
218 len+=8;
223 TCIPHER(test_DES_ede3_cbc_encrypt)
225 DES_ede3_cbc_encrypt(input, output, length,
226 (DES_key_schedule*)ks1, (DES_key_schedule*)ks2, (DES_key_schedule*)ks3, (DES_cblock*)ivec, enc);
229 #ifdef CPE_ENGINE
230 TCIPHER(test_DES_ede3_ofb64_encrypt)
232 int blks = 0;
233 DES_ede3_ofb64_encrypt(input, output, length,
234 (DES_key_schedule*)ks1, (DES_key_schedule*)ks2, (DES_key_schedule*)ks3, (DES_cblock*)ivec, &blks);
237 TCIPHER(test_DES_ede3_cfb64_encrypt)
239 DES_ede3_cfb_encrypt(input, output, 8, length,
240 (DES_key_schedule*)ks1, (DES_key_schedule*)ks2, (DES_key_schedule*)ks3, (DES_cblock*)ivec, enc);
242 #endif
244 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
245 TCIPHER(test_AES_ecb_encrypt)
247 if (length < HWACC_PKTLEN_MIN || mxcrypto_perfrom(MXCIPHER_ALGO_AES, MXCIPHER_MODE_ECB,
248 (const u_char *) input, output, length, ks1, NULL, NULL, NULL, 0, enc)!=0)
250 u_int len=0;
251 while (len < length)
253 AES_ecb_encrypt(input+len, output+len, (const AES_KEY *) (enc? ks1:ks2), enc);
254 len+=AES_BLOCK_SIZE;
259 TCIPHER(test_AES_cbc_encrypt)
261 AES_cbc_encrypt(input, output, length, (const AES_KEY *) (enc? ks1:ks2), ivec, enc);
264 TCIPHER(test_AES_ctr_encrypt)
266 unsigned char ctr[AES_BLOCK_SIZE];
267 int blks=0;
269 memset(ctr, 0, AES_BLOCK_SIZE);
270 AES_ctr128_encrypt(input, output, length, (const AES_KEY *) ks1, ivec, ctr, &blks);
273 #ifdef CPE_ENGINE
274 TCIPHER(test_AES_cfb_encrypt)
276 int blks=0;
277 AES_cfb8_encrypt(input, output, length, (const AES_KEY *) ks1, ivec, &blks, enc);
280 TCIPHER(test_AES_cfb128_encrypt)
282 int blks=0;
283 AES_cfb128_encrypt(input, output, length, (const AES_KEY *) ks1, ivec, &blks, enc);
286 TCIPHER(test_AES_ofb_encrypt)
288 int blks=0;
289 AES_ofb128_encrypt(input, output, length, (const AES_KEY *) ks1, ivec, &blks);
291 #endif
292 #endif