s4:torture: Adapt KDC canon test to Heimdal upstream changes
[Samba.git] / source4 / heimdal / lib / hcrypto / test_bulk.c
blobe3737fd1e953f0b24fa0e978bb6cbf6c48110189
1 /*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <config.h>
35 #include <roken.h>
36 #include <assert.h>
37 #include <getarg.h>
39 #include <evp.h>
40 #include <evp-hcrypto.h>
41 #include <evp-cc.h>
42 #if defined(_WIN32)
43 #include <evp-w32.h>
44 #endif
45 #include <evp-pkcs11.h>
46 #include <hex.h>
47 #include <err.h>
49 #ifdef WIN32
50 #define STATS_START(M) \
51 LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds; \
52 LARGE_INTEGER Frequency; \
54 QueryPerformanceFrequency(&Frequency); \
55 QueryPerformanceCounter(&StartingTime);
57 #define STATS_END(M) \
58 QueryPerformanceCounter(&EndingTime); \
59 ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart; \
60 ElapsedMicroseconds.QuadPart *= 1000000; \
61 ElapsedMicroseconds.QuadPart /= Frequency.QuadPart; \
63 M += (ElapsedMicroseconds.QuadPart - M) / (i + 1);
64 #else
65 #define STATS_START(M) \
66 struct timeval StartingTime, EndingTime; \
68 gettimeofday(&StartingTime, NULL);
70 #define STATS_END(M) \
71 gettimeofday(&EndingTime, NULL); \
72 timevalsub(&EndingTime, &StartingTime); \
73 M += (EndingTime.tv_sec * 1000000 + EndingTime.tv_usec - M) / (i + 1);
74 #endif
76 static int version_flag;
77 static int help_flag;
78 static int len = 1;
79 static int loops = 20;
80 static char *provider = "hcrypto";
81 static unsigned char *d;
83 #ifdef __APPLE__
84 #define PROVIDER_USAGE "hcrypto|cc"
85 #elif defined(WIN32)
86 #define PROVIDER_USAGE "hcrypto|w32crypto"
87 #elif __sun || defined(PKCS11_MODULE_PATH)
88 #define PROVIDER_USAGE "hcrypto|pkcs11"
89 #else
90 #define PROVIDER_USAGE "hcrypto"
91 #endif
93 static struct getargs args[] = {
94 { "provider", 0, arg_string, &provider,
95 "crypto provider", PROVIDER_USAGE },
96 { "loops", 0, arg_integer, &loops,
97 "number of loops", "loops" },
98 { "size", 0, arg_integer, &len,
99 "size (KB)", NULL },
100 { "version", 0, arg_flag, &version_flag,
101 "print version", NULL },
102 { "help", 0, arg_flag, &help_flag,
103 NULL, NULL }
106 static void
107 usage (int ret)
109 arg_printusage (args,
110 sizeof(args)/sizeof(*args),
111 NULL,
112 "");
113 exit (ret);
116 static int
117 test_bulk_cipher(const char *cname, const EVP_CIPHER *c)
119 static unsigned char key[16];
120 static unsigned char iv[16];
121 int i;
122 int64_t M = 0;
124 if (c == NULL) {
125 printf("%s not supported\n", cname);
126 return 0;
129 for (i = 0; i < loops; i++) {
130 EVP_CIPHER_CTX ectx;
131 EVP_CIPHER_CTX dctx;
133 STATS_START(M)
135 EVP_CIPHER_CTX_init(&ectx);
136 EVP_CIPHER_CTX_init(&dctx);
138 if (EVP_CipherInit_ex(&ectx, c, NULL, NULL, NULL, 1) != 1)
139 errx(1, "can't init encrypt");
140 if (EVP_CipherInit_ex(&dctx, c, NULL, NULL, NULL, 0) != 1)
141 errx(1, "can't init decrypt");
143 EVP_CIPHER_CTX_set_key_length(&ectx, sizeof(key));
144 EVP_CIPHER_CTX_set_key_length(&dctx, sizeof(key));
146 if (EVP_CipherInit_ex(&ectx, NULL, NULL, key, iv, 1) != 1)
147 errx(1, "can't init encrypt");
148 if (EVP_CipherInit_ex(&dctx, NULL, NULL, key, iv, 0) != 1)
149 errx(1, "can't init decrypt");
151 if (!EVP_Cipher(&ectx, d, d, len))
152 errx(1, "can't encrypt");
153 if (!EVP_Cipher(&dctx, d, d, len))
154 errx(1, "can't decrypt");
156 EVP_CIPHER_CTX_cleanup(&ectx);
157 EVP_CIPHER_CTX_cleanup(&dctx);
159 STATS_END(M);
161 if (d[0] != 0x00 || d[len - 1] != ((len - 1) & 0xff))
162 errx(1, "encrypt/decrypt inconsistent");
165 printf("%s: mean time %llu usec%s\n", cname, (unsigned long long)M,
166 (M == 1) ? "" : "s");
168 return 0;
171 static int
172 test_bulk_digest(const char *cname, const EVP_MD *md)
174 char digest[EVP_MAX_MD_SIZE];
175 int i;
176 unsigned int tmp = sizeof(digest);
177 int64_t M = 0;
179 if (md == NULL) {
180 printf("%s not supported\n", cname);
181 return 0;
184 for (i = 0; i < loops; i++) {
185 STATS_START(M);
186 EVP_Digest(d, len, digest, &tmp, md, NULL);
187 STATS_END(M);
190 printf("%s: mean time %llu usec%s\n", cname, (unsigned long long)M,
191 (M == 1) ? "" : "s");
193 return 0;
196 static void
197 test_bulk_provider_hcrypto(void)
199 test_bulk_cipher("hcrypto_aes_256_cbc", EVP_hcrypto_aes_256_cbc());
200 #if 0
201 test_bulk_cipher("hcrypto_aes_256_cfb8", EVP_hcrypto_aes_256_cfb8());
202 #endif
203 test_bulk_cipher("hcrypto_rc4", EVP_hcrypto_rc4());
204 test_bulk_digest("hcrypto_md2", EVP_hcrypto_md2());
205 test_bulk_digest("hcrypto_md4", EVP_hcrypto_md4());
206 test_bulk_digest("hcrypto_md5", EVP_hcrypto_md5());
207 test_bulk_digest("hcrypto_sha1", EVP_hcrypto_sha1());
208 test_bulk_digest("hcrypto_sha256", EVP_hcrypto_sha256());
209 test_bulk_digest("hcrypto_sha384", EVP_hcrypto_sha384());
210 test_bulk_digest("hcrypto_sha512", EVP_hcrypto_sha512());
213 #ifdef __APPLE__
214 static void
215 test_bulk_provider_cc(void)
217 test_bulk_cipher("cc_aes_256_cbc", EVP_cc_aes_256_cbc());
218 #if 0
219 test_bulk_cipher("cc_aes_256_cfb8", EVP_cc_aes_256_cfb8());
220 #endif
221 test_bulk_cipher("cc_rc4", EVP_cc_rc4());
222 test_bulk_digest("cc_md2", EVP_cc_md2());
223 test_bulk_digest("cc_md4", EVP_cc_md4());
224 test_bulk_digest("cc_md5", EVP_cc_md5());
225 test_bulk_digest("cc_sha1", EVP_cc_sha1());
226 test_bulk_digest("cc_sha256", EVP_cc_sha256());
227 test_bulk_digest("cc_sha384", EVP_cc_sha384());
228 test_bulk_digest("cc_sha512", EVP_cc_sha512());
230 #endif /* __APPLE__ */
232 #ifdef WIN32
233 static void
234 test_bulk_provider_w32crypto(void)
236 test_bulk_cipher("w32crypto_aes_256_cbc", EVP_w32crypto_aes_256_cbc());
237 #if 0
238 test_bulk_cipher("w32crypto_aes_256_cfb8", EVP_w32crypto_aes_256_cfb8());
239 #endif
240 test_bulk_cipher("w32crypto_rc4", EVP_w32crypto_rc4());
241 test_bulk_digest("w32crypto_md2", EVP_w32crypto_md2());
242 test_bulk_digest("w32crypto_md4", EVP_w32crypto_md4());
243 test_bulk_digest("w32crypto_md5", EVP_w32crypto_md5());
244 test_bulk_digest("w32crypto_sha1", EVP_w32crypto_sha1());
245 test_bulk_digest("w32crypto_sha256", EVP_w32crypto_sha256());
246 test_bulk_digest("w32crypto_sha384", EVP_w32crypto_sha384());
247 test_bulk_digest("w32crypto_sha512", EVP_w32crypto_sha512());
249 #endif /* WIN32 */
251 #if __sun || defined(PKCS11_MODULE_PATH)
252 static void
253 test_bulk_provider_pkcs11(void)
255 test_bulk_cipher("pkcs11_aes_256_cbc", EVP_pkcs11_aes_256_cbc());
256 test_bulk_cipher("pkcs11_rc4", EVP_pkcs11_rc4());
257 test_bulk_digest("pkcs11_md5", EVP_pkcs11_md5());
258 test_bulk_digest("pkcs11_sha1", EVP_pkcs11_sha1());
259 test_bulk_digest("pkcs11_sha256", EVP_pkcs11_sha256());
260 test_bulk_digest("pkcs11_sha384", EVP_pkcs11_sha384());
261 test_bulk_digest("pkcs11_sha512", EVP_pkcs11_sha512());
263 #endif /* __sun || PKCS11_MODULE_PATH */
266 main(int argc, char **argv)
268 int ret = 0;
269 int idx = 0;
270 int i;
272 setprogname(argv[0]);
274 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
275 usage(1);
277 if (help_flag)
278 usage(0);
280 if(version_flag) {
281 print_version(NULL);
282 exit(0);
285 argc -= idx;
286 argv += idx;
288 len *= 1024;
290 d = emalloc(len);
291 for (i = 0; i < len; i++)
292 d[i] = i & 0xff;
294 if (strcmp(provider, "hcrypto") == 0)
295 test_bulk_provider_hcrypto();
296 #ifdef __APPLE__
297 else if (strcmp(provider, "cc") == 0)
298 test_bulk_provider_cc();
299 #endif
300 #ifdef WIN32
301 else if (strcmp(provider, "w32crypto") == 0)
302 test_bulk_provider_w32crypto();
303 #endif
304 #if __sun || defined(PKCS11_MODULE_PATH)
305 else if (strcmp(provider, "pkcs11") == 0)
306 test_bulk_provider_pkcs11();
307 #endif
308 else
309 usage(1);
311 free(d);
313 return ret;