1 |
/* |
2 |
* encspeed.c - originally written by einride |
3 |
* - modified for ircd-hybrid-7 by davidt |
4 |
* |
5 |
* $Id$ |
6 |
*/ |
7 |
|
8 |
#include "setup.h" |
9 |
|
10 |
#ifndef HAVE_LIBCRYPTO |
11 |
#include <stdio.h> |
12 |
|
13 |
int main(int argc, char *argv[]) |
14 |
{ |
15 |
printf("No OpenSSL support.\n"); |
16 |
return 0; |
17 |
} |
18 |
|
19 |
#else |
20 |
#include <stdio.h> |
21 |
#include <sys/time.h> |
22 |
#include <unistd.h> |
23 |
|
24 |
#include <openssl/evp.h> |
25 |
#include <openssl/rand.h> |
26 |
|
27 |
#define CIPHER_BF128 0 |
28 |
#define CIPHER_BF168 1 |
29 |
#define CIPHER_3DES168 2 |
30 |
#define CIPHER_DES56 3 |
31 |
#define CIPHER_CAST128 4 |
32 |
#define CIPHER_IDEA128 5 |
33 |
#define CIPHER_RC58128 6 |
34 |
#define CIPHER_RC512128 7 |
35 |
#define CIPHER_RC516128 8 |
36 |
|
37 |
#define NUM_CIPHERS 9 |
38 |
|
39 |
#define MAXKEYSIZE 64 |
40 |
#define BLOCKSIZE 1000 |
41 |
#define BLOCKCOUNT 20000 |
42 |
#define TICKINT 1000 |
43 |
|
44 |
struct timeval tvstart, tvstop; |
45 |
|
46 |
static void starttimer(void) { |
47 |
gettimeofday(&tvstart, 0); |
48 |
} |
49 |
|
50 |
static double stoptimer(void) { |
51 |
gettimeofday(&tvstop, 0); |
52 |
return (tvstop.tv_sec - tvstart.tv_sec) + ( (tvstop.tv_usec - tvstart.tv_usec) / 1000000.0); |
53 |
} |
54 |
|
55 |
#define DATASIZE ( (double) (BLOCKSIZE * BLOCKCOUNT) / (1024.0*1024.0)) |
56 |
|
57 |
int main(int argc, char * argv[]) { |
58 |
int i, n, skipped = 0; |
59 |
int bs = BLOCKSIZE; |
60 |
const char *cipherstr = NULL; |
61 |
char key[MAXKEYSIZE]; |
62 |
char iv[MAXKEYSIZE]; |
63 |
char plaintext[BLOCKSIZE]; |
64 |
char ciphertext[BLOCKSIZE]; |
65 |
double elapsed; |
66 |
|
67 |
EVP_CIPHER_CTX ctx; |
68 |
const EVP_CIPHER *cipher; |
69 |
|
70 |
printf("Encryption speed test\n\n"); |
71 |
|
72 |
for(n = 0; n < NUM_CIPHERS; n++) |
73 |
{ |
74 |
/* setup cipher */ |
75 |
if (skipped) |
76 |
{ |
77 |
printf("%20.20s encryption: not supported by OpenSSL\n", cipherstr); |
78 |
printf("%20.20s decryption: not supported by OpenSSL\n", cipherstr); |
79 |
} |
80 |
|
81 |
skipped = 1; |
82 |
switch(n) |
83 |
{ |
84 |
case CIPHER_BF168: |
85 |
cipherstr = "Blowfish 168-bit"; |
86 |
#ifdef HAVE_EVP_BF_CFB |
87 |
cipher = EVP_bf_cfb(); |
88 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
89 |
EVP_CIPHER_CTX_set_key_length(&ctx, 24); |
90 |
#else |
91 |
continue; |
92 |
#endif |
93 |
break; |
94 |
case CIPHER_BF128: |
95 |
cipherstr = "Blowfish 128-bit"; |
96 |
#ifdef HAVE_EVP_BF_CFB |
97 |
cipher = EVP_bf_cfb(); |
98 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
99 |
EVP_CIPHER_CTX_set_key_length(&ctx, 16); |
100 |
#else |
101 |
continue; |
102 |
#endif |
103 |
break; |
104 |
case CIPHER_CAST128: |
105 |
cipherstr = "CAST 128-bit"; |
106 |
#ifdef HAVE_EVP_CAST5_CFB |
107 |
cipher = EVP_cast5_cfb(); |
108 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
109 |
EVP_CIPHER_CTX_set_key_length(&ctx, 16); |
110 |
#else |
111 |
continue; |
112 |
#endif |
113 |
break; |
114 |
case CIPHER_IDEA128: |
115 |
cipherstr = "IDEA 128-bit"; |
116 |
#ifdef HAVE_EVP_IDEA_CFB |
117 |
cipher = EVP_idea_cfb(); |
118 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
119 |
EVP_CIPHER_CTX_set_key_length(&ctx, 16); |
120 |
#else |
121 |
continue; |
122 |
#endif |
123 |
break; |
124 |
case CIPHER_DES56: |
125 |
cipherstr = "DES 56-bit"; |
126 |
#ifdef HAVE_EVP_DES_CFB |
127 |
cipher = EVP_des_cfb(); |
128 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
129 |
EVP_CIPHER_CTX_set_key_length(&ctx, 8); |
130 |
#else |
131 |
continue; |
132 |
#endif |
133 |
break; |
134 |
case CIPHER_3DES168: |
135 |
cipherstr = "3DES 168-bit"; |
136 |
#ifdef HAVE_EVP_DES_EDE3_CFB |
137 |
cipher = EVP_des_ede3_cfb(); |
138 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
139 |
EVP_CIPHER_CTX_set_key_length(&ctx, 24); |
140 |
#else |
141 |
continue; |
142 |
#endif |
143 |
break; |
144 |
case CIPHER_RC58128: |
145 |
cipherstr = "RC5 8 round 128-bit"; |
146 |
#ifdef HAVE_EVP_RC5_32_12_16_CFB |
147 |
cipher = EVP_rc5_32_12_16_cfb(); |
148 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
149 |
EVP_CIPHER_CTX_set_key_length(&ctx, 8); |
150 |
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_SET_RC5_ROUNDS, 8, NULL); |
151 |
#else |
152 |
continue; |
153 |
#endif |
154 |
break; |
155 |
case CIPHER_RC512128: |
156 |
cipherstr = "RC5 12 round 128-bit"; |
157 |
#ifdef HAVE_EVP_RC5_32_12_16_CFB |
158 |
cipher = EVP_rc5_32_12_16_cfb(); |
159 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
160 |
EVP_CIPHER_CTX_set_key_length(&ctx, 12); |
161 |
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_SET_RC5_ROUNDS, 12, NULL); |
162 |
#else |
163 |
continue; |
164 |
#endif |
165 |
break; |
166 |
case CIPHER_RC516128: |
167 |
cipherstr = "RC5 16 round 128-bit"; |
168 |
#ifdef HAVE_EVP_RC5_32_12_16_CFB |
169 |
cipher = EVP_rc5_32_12_16_cfb(); |
170 |
EVP_EncryptInit(&ctx, cipher, NULL, NULL); |
171 |
EVP_CIPHER_CTX_set_key_length(&ctx, 16); |
172 |
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_SET_RC5_ROUNDS, 16, NULL); |
173 |
#else |
174 |
continue; |
175 |
#endif |
176 |
break; |
177 |
default: |
178 |
exit(1); |
179 |
} |
180 |
|
181 |
skipped = 0; |
182 |
|
183 |
RAND_pseudo_bytes((unsigned char *)key, MAXKEYSIZE); |
184 |
RAND_pseudo_bytes((unsigned char *)iv, MAXKEYSIZE); |
185 |
RAND_pseudo_bytes((unsigned char *)plaintext, BLOCKSIZE); |
186 |
RAND_pseudo_bytes((unsigned char *)ciphertext, BLOCKSIZE); |
187 |
|
188 |
EVP_EncryptInit(&ctx, NULL, (unsigned char *)key, (unsigned char *)iv); |
189 |
|
190 |
printf("%20.20s encryption: ", cipherstr); |
191 |
fflush(stdout); |
192 |
|
193 |
starttimer(); |
194 |
bs = BLOCKSIZE; |
195 |
for (i=1;i<=BLOCKCOUNT;i++) { |
196 |
if (!(i % TICKINT)) { |
197 |
printf("."); |
198 |
fflush(stdout); |
199 |
} |
200 |
EVP_EncryptUpdate(&ctx, (unsigned char *)ciphertext, &bs, |
201 |
(unsigned char *)plaintext, BLOCKSIZE); |
202 |
bs = BLOCKSIZE; |
203 |
} |
204 |
EVP_EncryptFinal(&ctx, (unsigned char *)ciphertext, &bs); |
205 |
elapsed=stoptimer(); |
206 |
printf(" done, %f MB/sec\n", DATASIZE / elapsed); |
207 |
|
208 |
EVP_DecryptInit(&ctx, NULL, (unsigned char *)key, (unsigned char *)iv); |
209 |
|
210 |
printf("%20.20s decryption: ", cipherstr); |
211 |
fflush(stdout); |
212 |
|
213 |
starttimer(); |
214 |
bs = BLOCKSIZE; |
215 |
for (i=1;i<=BLOCKCOUNT;i++) { |
216 |
if (!(i % TICKINT)) { |
217 |
printf("."); |
218 |
fflush(stdout); |
219 |
} |
220 |
EVP_DecryptUpdate(&ctx, (unsigned char *)ciphertext, &bs, |
221 |
(unsigned char *)plaintext, BLOCKSIZE); |
222 |
bs = BLOCKSIZE; |
223 |
} |
224 |
EVP_DecryptFinal(&ctx, (unsigned char *)ciphertext, &bs); |
225 |
elapsed=stoptimer(); |
226 |
printf(" done, %f MB/sec\n", DATASIZE / elapsed); |
227 |
} |
228 |
|
229 |
if (skipped) |
230 |
{ |
231 |
printf("%20.20s encryption: not supported by OpenSSL\n", cipherstr); |
232 |
printf("%20.20s decryption: not supported by OpenSSL\n", cipherstr); |
233 |
} |
234 |
|
235 |
return(0); |
236 |
} |
237 |
#endif |