2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
40 #include <evp-hcrypto.h>
45 #include <evp-pkcs11.h>
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);
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);
76 static int version_flag
;
79 static int loops
= 20;
80 static char *provider
= "hcrypto";
81 static unsigned char *d
;
84 #define PROVIDER_USAGE "hcrypto|cc"
86 #define PROVIDER_USAGE "hcrypto|w32crypto"
87 #elif __sun || defined(PKCS11_MODULE_PATH)
88 #define PROVIDER_USAGE "hcrypto|pkcs11"
90 #define PROVIDER_USAGE "hcrypto"
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
,
100 { "version", 0, arg_flag
, &version_flag
,
101 "print version", NULL
},
102 { "help", 0, arg_flag
, &help_flag
,
109 arg_printusage (args
,
110 sizeof(args
)/sizeof(*args
),
117 test_bulk_cipher(const char *cname
, const EVP_CIPHER
*c
)
119 static unsigned char key
[16];
120 static unsigned char iv
[16];
125 printf("%s not supported\n", cname
);
129 for (i
= 0; i
< loops
; i
++) {
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
);
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");
172 test_bulk_digest(const char *cname
, const EVP_MD
*md
)
174 char digest
[EVP_MAX_MD_SIZE
];
176 unsigned int tmp
= sizeof(digest
);
180 printf("%s not supported\n", cname
);
184 for (i
= 0; i
< loops
; i
++) {
186 EVP_Digest(d
, len
, digest
, &tmp
, md
, NULL
);
190 printf("%s: mean time %llu usec%s\n", cname
, (unsigned long long)M
,
191 (M
== 1) ? "" : "s");
197 test_bulk_provider_hcrypto(void)
199 test_bulk_cipher("hcrypto_aes_256_cbc", EVP_hcrypto_aes_256_cbc());
201 test_bulk_cipher("hcrypto_aes_256_cfb8", EVP_hcrypto_aes_256_cfb8());
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());
215 test_bulk_provider_cc(void)
217 test_bulk_cipher("cc_aes_256_cbc", EVP_cc_aes_256_cbc());
219 test_bulk_cipher("cc_aes_256_cfb8", EVP_cc_aes_256_cfb8());
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__ */
234 test_bulk_provider_w32crypto(void)
236 test_bulk_cipher("w32crypto_aes_256_cbc", EVP_w32crypto_aes_256_cbc());
238 test_bulk_cipher("w32crypto_aes_256_cfb8", EVP_w32crypto_aes_256_cfb8());
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());
251 #if __sun || defined(PKCS11_MODULE_PATH)
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
)
272 setprogname(argv
[0]);
274 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &idx
))
291 for (i
= 0; i
< len
; i
++)
294 if (strcmp(provider
, "hcrypto") == 0)
295 test_bulk_provider_hcrypto();
297 else if (strcmp(provider
, "cc") == 0)
298 test_bulk_provider_cc();
301 else if (strcmp(provider
, "w32crypto") == 0)
302 test_bulk_provider_w32crypto();
304 #if __sun || defined(PKCS11_MODULE_PATH)
305 else if (strcmp(provider
, "pkcs11") == 0)
306 test_bulk_provider_pkcs11();