| 1 |
/* |
| 2 |
* Copyright (c) 2006 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 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. |
| 16 |
* |
| 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. |
| 20 |
* |
| 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. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
#include <roken.h> |
| 36 |
#include <assert.h> |
| 37 |
#include <getarg.h> |
| 38 |
|
| 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> |
| 48 |
|
| 49 |
#ifdef WIN32 |
| 50 |
#define STATS_START(M) \ |
| 51 |
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds; \ |
| 52 |
LARGE_INTEGER Frequency; \ |
| 53 |
\ |
| 54 |
QueryPerformanceFrequency(&Frequency); \ |
| 55 |
QueryPerformanceCounter(&StartingTime); |
| 56 |
|
| 57 |
#define STATS_END(M) \ |
| 58 |
QueryPerformanceCounter(&EndingTime); \ |
| 59 |
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart; \ |
| 60 |
ElapsedMicroseconds.QuadPart *= 1000000; \ |
| 61 |
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart; \ |
| 62 |
\ |
| 63 |
M += (ElapsedMicroseconds.QuadPart - M) / (i + 1); |
| 64 |
#else |
| 65 |
#define STATS_START(M) \ |
| 66 |
struct timeval StartingTime, EndingTime; \ |
| 67 |
\ |
| 68 |
gettimeofday(&StartingTime, NULL); |
| 69 |
|
| 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 |
| 75 |
|
| 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; |
| 82 |
|
| 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 |
| 92 |
|
| 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 } |
| 104 |
}; |
| 105 |
|
| 106 |
static void |
| 107 |
usage (int ret) |
| 108 |
{ |
| 109 |
arg_printusage (args, |
| 110 |
sizeof(args)/sizeof(*args), |
| 111 |
NULL, |
| 112 |
""); |
| 113 |
exit (ret); |
| 114 |
} |
| 115 |
|
| 116 |
static int |
| 117 |
test_bulk_cipher(const char *cname, const EVP_CIPHER *c) |
| 118 |
{ |
| 119 |
static unsigned char key[16]; |
| 120 |
static unsigned char iv[16]; |
| 121 |
int i; |
| 122 |
int64_t M = 0; |
| 123 |
|
| 124 |
if (c == NULL) { |
| 125 |
printf("%s not supported\n", cname); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
for (i = 0; i < loops; i++) { |
| 130 |
EVP_CIPHER_CTX ectx; |
| 131 |
EVP_CIPHER_CTX dctx; |
| 132 |
|
| 133 |
STATS_START(M) |
| 134 |
|
| 135 |
EVP_CIPHER_CTX_init(&ectx); |
| 136 |
EVP_CIPHER_CTX_init(&dctx); |
| 137 |
|
| 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"); |
| 142 |
|
| 143 |
EVP_CIPHER_CTX_set_key_length(&ectx, sizeof(key)); |
| 144 |
EVP_CIPHER_CTX_set_key_length(&dctx, sizeof(key)); |
| 145 |
|
| 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"); |
| 150 |
|
| 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"); |
| 155 |
|
| 156 |
EVP_CIPHER_CTX_cleanup(&ectx); |
| 157 |
EVP_CIPHER_CTX_cleanup(&dctx); |
| 158 |
|
| 159 |
STATS_END(M); |
| 160 |
|
| 161 |
if (d[0] != 0x00 || d[len - 1] != ((len - 1) & 0xff)) |
| 162 |
errx(1, "encrypt/decrypt inconsistent"); |
| 163 |
} |
| 164 |
|
| 165 |
printf("%s: mean time %llu usec%s\n", cname, (unsigned long long)M, |
| 166 |
(M == 1) ? "" : "s"); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
static int |
| 172 |
test_bulk_digest(const char *cname, const EVP_MD *md) |
| 173 |
{ |
| 174 |
char digest[EVP_MAX_MD_SIZE]; |
| 175 |
int i; |
| 176 |
unsigned int tmp = sizeof(digest); |
| 177 |
int64_t M = 0; |
| 178 |
|
| 179 |
if (md == NULL) { |
| 180 |
printf("%s not supported\n", cname); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
for (i = 0; i < loops; i++) { |
| 185 |
STATS_START(M); |
| 186 |
EVP_Digest(d, len, digest, &tmp, md, NULL); |
| 187 |
STATS_END(M); |
| 188 |
} |
| 189 |
|
| 190 |
printf("%s: mean time %llu usec%s\n", cname, (unsigned long long)M, |
| 191 |
(M == 1) ? "" : "s"); |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
static void |
| 197 |
test_bulk_provider_hcrypto(void) |
| 198 |
{ |
| 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_md4", EVP_hcrypto_md4()); |
| 205 |
test_bulk_digest("hcrypto_md5", EVP_hcrypto_md5()); |
| 206 |
test_bulk_digest("hcrypto_sha1", EVP_hcrypto_sha1()); |
| 207 |
test_bulk_digest("hcrypto_sha256", EVP_hcrypto_sha256()); |
| 208 |
test_bulk_digest("hcrypto_sha384", EVP_hcrypto_sha384()); |
| 209 |
test_bulk_digest("hcrypto_sha512", EVP_hcrypto_sha512()); |
| 210 |
} |
| 211 |
|
| 212 |
#ifdef __APPLE__ |
| 213 |
static void |
| 214 |
test_bulk_provider_cc(void) |
| 215 |
{ |
| 216 |
test_bulk_cipher("cc_aes_256_cbc", EVP_cc_aes_256_cbc()); |
| 217 |
#if 0 |
| 218 |
test_bulk_cipher("cc_aes_256_cfb8", EVP_cc_aes_256_cfb8()); |
| 219 |
#endif |
| 220 |
test_bulk_cipher("cc_rc4", EVP_cc_rc4()); |
| 221 |
test_bulk_digest("cc_md4", EVP_cc_md4()); |
| 222 |
test_bulk_digest("cc_md5", EVP_cc_md5()); |
| 223 |
test_bulk_digest("cc_sha1", EVP_cc_sha1()); |
| 224 |
test_bulk_digest("cc_sha256", EVP_cc_sha256()); |
| 225 |
test_bulk_digest("cc_sha384", EVP_cc_sha384()); |
| 226 |
test_bulk_digest("cc_sha512", EVP_cc_sha512()); |
| 227 |
} |
| 228 |
#endif /* __APPLE__ */ |
| 229 |
|
| 230 |
#ifdef WIN32 |
| 231 |
static void |
| 232 |
test_bulk_provider_w32crypto(void) |
| 233 |
{ |
| 234 |
test_bulk_cipher("w32crypto_aes_256_cbc", EVP_w32crypto_aes_256_cbc()); |
| 235 |
#if 0 |
| 236 |
test_bulk_cipher("w32crypto_aes_256_cfb8", EVP_w32crypto_aes_256_cfb8()); |
| 237 |
#endif |
| 238 |
test_bulk_cipher("w32crypto_rc4", EVP_w32crypto_rc4()); |
| 239 |
test_bulk_digest("w32crypto_md4", EVP_w32crypto_md4()); |
| 240 |
test_bulk_digest("w32crypto_md5", EVP_w32crypto_md5()); |
| 241 |
test_bulk_digest("w32crypto_sha1", EVP_w32crypto_sha1()); |
| 242 |
test_bulk_digest("w32crypto_sha256", EVP_w32crypto_sha256()); |
| 243 |
test_bulk_digest("w32crypto_sha384", EVP_w32crypto_sha384()); |
| 244 |
test_bulk_digest("w32crypto_sha512", EVP_w32crypto_sha512()); |
| 245 |
} |
| 246 |
#endif /* WIN32 */ |
| 247 |
|
| 248 |
#if __sun || defined(PKCS11_MODULE_PATH) |
| 249 |
static void |
| 250 |
test_bulk_provider_pkcs11(void) |
| 251 |
{ |
| 252 |
test_bulk_cipher("pkcs11_aes_256_cbc", EVP_pkcs11_aes_256_cbc()); |
| 253 |
test_bulk_cipher("pkcs11_rc4", EVP_pkcs11_rc4()); |
| 254 |
test_bulk_digest("pkcs11_md5", EVP_pkcs11_md5()); |
| 255 |
test_bulk_digest("pkcs11_sha1", EVP_pkcs11_sha1()); |
| 256 |
test_bulk_digest("pkcs11_sha256", EVP_pkcs11_sha256()); |
| 257 |
test_bulk_digest("pkcs11_sha384", EVP_pkcs11_sha384()); |
| 258 |
test_bulk_digest("pkcs11_sha512", EVP_pkcs11_sha512()); |
| 259 |
} |
| 260 |
#endif /* __sun || PKCS11_MODULE_PATH */ |
| 261 |
|
| 262 |
int |
| 263 |
main(int argc, char **argv) |
| 264 |
{ |
| 265 |
int ret = 0; |
| 266 |
int idx = 0; |
| 267 |
int i; |
| 268 |
|
| 269 |
setprogname(argv[0]); |
| 270 |
|
| 271 |
if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx)) |
| 272 |
usage(1); |
| 273 |
|
| 274 |
if (help_flag) |
| 275 |
usage(0); |
| 276 |
|
| 277 |
if(version_flag) { |
| 278 |
print_version(NULL); |
| 279 |
exit(0); |
| 280 |
} |
| 281 |
|
| 282 |
argc -= idx; |
| 283 |
argv += idx; |
| 284 |
|
| 285 |
len *= 1024; |
| 286 |
|
| 287 |
d = emalloc(len); |
| 288 |
for (i = 0; i < len; i++) |
| 289 |
d[i] = i & 0xff; |
| 290 |
|
| 291 |
if (strcmp(provider, "hcrypto") == 0) |
| 292 |
test_bulk_provider_hcrypto(); |
| 293 |
#ifdef __APPLE__ |
| 294 |
else if (strcmp(provider, "cc") == 0) |
| 295 |
test_bulk_provider_cc(); |
| 296 |
#endif |
| 297 |
#ifdef WIN32 |
| 298 |
else if (strcmp(provider, "w32crypto") == 0) |
| 299 |
test_bulk_provider_w32crypto(); |
| 300 |
#endif |
| 301 |
#if __sun || defined(PKCS11_MODULE_PATH) |
| 302 |
else if (strcmp(provider, "pkcs11") == 0) |
| 303 |
test_bulk_provider_pkcs11(); |
| 304 |
#endif |
| 305 |
else |
| 306 |
usage(1); |
| 307 |
|
| 308 |
free(d); |
| 309 |
|
| 310 |
return ret; |
| 311 |
} |