2 * Copyright (c) 2004 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
15 * 3. Neither the names of the above-listed copyright holders nor the names
16 * of any contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
25 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGES.
36 * Simple tool for testing hardware/system crypto support.
38 * cryptotest [-czsbv] [-a algorithm] [count] [size ...]
40 * Run count iterations of a crypt+decrypt or mac operation on a buffer of
41 * size bytes. A random key and iv are used. Options:
42 * -c check the results
43 * -d dev pin work on device dev
44 * -z run all available algorithms on a variety of buffer sizes
46 * -b mark operations for batching
47 * -p profile kernel crypto operations (must be root)
48 * -t n fork n threads and run tests concurrently
49 * Known algorithms are:
56 * aes rijndael/aes 128-bit cbc
57 * aes192 rijndael/aes 192-bit cbc
58 * aes256 rijndael/aes 256-bit cbc
63 * sha256 256-bit sha2 hmac
64 * sha384 384-bit sha2 hmac
65 * sha512 512--bit sha2 hmac
67 * For a test of how fast a crypto card is, use something like:
69 * This will run a series of tests using the available crypto/cipher
70 * algorithms over a variety of buffer sizes. The 1024 says to do 1024
71 * iterations. Extra arguments can be used to specify one or more buffer
72 * sizes to use in doing tests.
74 * To fork multiple processes all doing the same work, specify -t X on the
75 * command line to get X "threads" running simultaneously. No effort is made
76 * to synchronize the threads or otherwise maximize load.
78 * If the kernel crypto code is built with CRYPTO_TIMING and you run as root,
79 * then you can specify the -p option to get a "profile" of the time spent
80 * processing crypto operations. At present this data is only meaningful for
81 * symmetric operations. To get meaningful numbers you must run on an idle
84 * Expect ~400 Mb/s for a Broadcom 582x for 8K buffers on a reasonable CPU
85 * (64-bit PCI helps). Hifn 7811 parts top out at ~110 Mb/s.
88 #include <sys/param.h>
89 #include <sys/cpuset.h>
90 #include <sys/ioctl.h>
92 #include <sys/sysctl.h>
102 #include <sysexits.h>
105 #include <crypto/cryptodev.h>
107 #define CHUNK 64 /* how much to display */
108 #define streq(a,b) (strcasecmp(a,b) == 0)
110 void hexdump(char *, int);
115 int crid = CRYPTO_FLAG_HARDWARE;
125 #ifdef CRYPTO_NULL_CBC
126 { "null", 0, 8, 1, 256, CRYPTO_NULL_CBC },
128 { "des", 0, 8, 8, 8, CRYPTO_DES_CBC },
129 { "3des", 0, 8, 24, 24, CRYPTO_3DES_CBC },
130 { "blf", 0, 8, 5, 56, CRYPTO_BLF_CBC },
131 { "cast", 0, 8, 5, 16, CRYPTO_CAST_CBC },
132 { "skj", 0, 8, 10, 10, CRYPTO_SKIPJACK_CBC },
133 { "rij", 0, 16, 16, 16, CRYPTO_RIJNDAEL128_CBC},
134 { "aes", 0, 16, 16, 16, CRYPTO_AES_CBC},
135 { "aes192", 0, 16, 24, 24, CRYPTO_AES_CBC},
136 { "aes256", 0, 16, 32, 32, CRYPTO_AES_CBC},
137 { "blake2b", 1, 128, 64, 64, CRYPTO_BLAKE2B },
138 { "blake2s", 1, 64, 32, 32, CRYPTO_BLAKE2S },
139 { "md5", 1, 8, 16, 16, CRYPTO_MD5_HMAC },
140 { "sha1", 1, 8, 20, 20, CRYPTO_SHA1_HMAC },
141 { "sha256", 1, 8, 32, 32, CRYPTO_SHA2_256_HMAC },
142 { "sha384", 1, 8, 48, 48, CRYPTO_SHA2_384_HMAC },
143 { "sha512", 1, 8, 64, 64, CRYPTO_SHA2_512_HMAC },
147 usage(const char* cmd)
149 printf("usage: %s [-czsbv] [-d dev] [-a algorithm] [count] [size ...]\n",
151 printf("where algorithm is one of:\n");
152 printf(" null des 3des (default) blowfish cast skipjack rij\n");
153 printf(" aes aes192 aes256 md5 sha1 sha256 sha384 sha512\n");
154 printf(" blake2b blake2s\n");
155 printf(" or an encryption algorithm concatented with authentication\n");
156 printf(" algorithm with '+' in the middle, e.g., aes+sha1.\n");
157 printf("count is the number of encrypt/decrypt ops to do\n");
158 printf("size is the number of bytes of text to encrypt+decrypt\n");
160 printf("-c check the results (slows timing)\n");
161 printf("-d use specific device, specify 'soft' for testing software implementations\n");
162 printf("\tNOTE: to use software you must set:\n\t sysctl kern.cryptodevallowsoft=1\n");
163 printf("-z run all available algorithms on a variety of sizes\n");
164 printf("-v be verbose\n");
165 printf("-b mark operations for batching\n");
166 printf("-p profile kernel crypto operation (must be root)\n");
167 printf("-t n for n threads and run tests concurrently\n");
172 getalgbycode(int cipher)
176 for (i = 0; i < nitems(algorithms); i++)
177 if (cipher == algorithms[i].code)
178 return &algorithms[i];
183 getalgbyname(const char* name)
187 for (i = 0; i < nitems(algorithms); i++)
188 if (streq(name, algorithms[i].name))
189 return &algorithms[i];
199 fd = open(_PATH_DEV "crypto", O_RDWR, 0);
201 err(1, _PATH_DEV "crypto");
202 if (fcntl(fd, F_SETFD, 1) == -1)
203 err(1, "fcntl(F_SETFD) (devcrypto)");
209 crlookup(const char *devname)
211 struct crypt_find_op find;
213 if (strncmp(devname, "soft", 4) == 0)
214 return CRYPTO_FLAG_SOFTWARE;
217 strlcpy(find.name, devname, sizeof(find.name));
218 if (ioctl(devcrypto(), CIOCFINDDEV, &find) == -1)
219 err(1, "ioctl(CIOCFINDDEV)");
226 static struct crypt_find_op find;
228 bzero(&find, sizeof(find));
230 if (ioctl(devcrypto(), CRIOFINDDEV, &find) == -1)
231 err(1, "ioctl(CIOCFINDDEV): crid %d", crid);
240 if (ioctl(devcrypto(), CRIOGET, &fd) == -1)
241 err(1, "ioctl(CRIOGET)");
242 if (fcntl(fd, F_SETFD, 1) == -1)
243 err(1, "fcntl(F_SETFD) (crget)");
251 0x10,0x54,0x11,0x48,0x45,0x12,0x4f,0x13,0x49,0x53,0x14,0x41,
252 0x15,0x16,0x4e,0x55,0x54,0x17,0x18,0x4a,0x4f,0x42,0x19,0x01
254 return 0x20+a[random()%nitems(a)];
258 runtest(struct alg *ealg, struct alg *alg, int count, int size, u_long cmd, struct timeval *tv)
261 struct timeval start, stop, dt;
262 char *cleartext, *ciphertext, *originaltext, *key;
263 struct session2_op sop;
265 char iv[EALG_MAX_BLOCK_LEN];
268 /* Canonicalize 'ealg' to crypt alg and 'alg' to authentication alg. */
269 if (ealg == NULL && !alg->ishash) {
274 bzero(&sop, sizeof(sop));
276 sop.keylen = (ealg->minkeylen + ealg->maxkeylen)/2;
277 key = (char *) malloc(sop.keylen);
279 err(1, "malloc (key)");
280 for (i = 0; i < sop.keylen; i++)
283 sop.cipher = ealg->code;
286 sop.mackeylen = (alg->minkeylen + alg->maxkeylen)/2;
287 key = (char *) malloc(sop.mackeylen);
289 err(1, "malloc (mac)");
290 for (i = 0; i < sop.mackeylen; i++)
297 if (ioctl(fd, cmd, &sop) < 0) {
298 if (cmd == CIOCGSESSION || cmd == CIOCGSESSION2) {
301 printf("cipher %s%s%s", ealg? ealg->name : "",
302 (ealg && alg) ? "+" : "",
303 alg? alg->name : "");
306 printf(" mackeylen %u\n", sop.mackeylen);
308 printf(" keylen %u\n", sop.keylen);
309 perror("CIOCGSESSION");
311 /* hardware doesn't support algorithm; skip it */
314 printf("cipher %s%s%s keylen %u mackeylen %u\n",
315 ealg? ealg->name : "", (ealg && alg) ? "+" : "",
316 alg? alg->name : "", sop.keylen, sop.mackeylen);
317 err(1, "CIOCGSESSION");
320 originaltext = malloc(3*size);
321 if (originaltext == NULL)
322 err(1, "malloc (text)");
323 cleartext = originaltext+size;
324 ciphertext = cleartext+size;
325 for (i = 0; i < size; i++)
326 cleartext[i] = rdigit();
327 memcpy(originaltext, cleartext, size);
328 for (i = 0; i < nitems(iv); i++)
332 printf("session = 0x%x\n", sop.ses);
333 printf("device = %s\n", crfind(sop.crid));
334 printf("count = %d, size = %d\n", count, size);
337 hexdump(iv, sizeof iv);
339 printf("cleartext:");
340 hexdump(cleartext, MIN(size, CHUNK));
343 gettimeofday(&start, NULL);
345 for (i = 0; i < count; i++) {
347 cop.op = COP_ENCRYPT;
348 cop.flags = opflags | COP_F_CIPHER_FIRST;
351 cop.dst = ciphertext;
358 if (ioctl(fd, CIOCCRYPT, &cop) < 0)
359 err(1, "ioctl(CIOCCRYPT)");
361 if (verify && bcmp(ciphertext, cleartext, size) == 0) {
362 printf("cipher text unchanged:");
363 hexdump(ciphertext, size);
366 memset(cleartext, 'x', MIN(size, CHUNK));
368 cop.op = COP_DECRYPT;
371 cop.src = ciphertext;
379 if (ioctl(fd, CIOCCRYPT, &cop) < 0)
380 err(1, "ioctl(CIOCCRYPT)");
382 if (verify && bcmp(cleartext, originaltext, size) != 0) {
383 printf("decrypt mismatch:\n");
385 hexdump(originaltext, size);
386 printf("cleartext:");
387 hexdump(cleartext, size);
391 for (i = 0; i < count; i++) {
398 cop.mac = ciphertext;
401 if (ioctl(fd, CIOCCRYPT, &cop) < 0)
402 err(1, "ioctl(CIOCCRYPT)");
405 gettimeofday(&stop, NULL);
407 if (ioctl(fd, CIOCFSESSION, &sop.ses) < 0)
408 perror("ioctl(CIOCFSESSION)");
411 printf("cleartext:");
412 hexdump(cleartext, MIN(size, CHUNK));
414 timersub(&stop, &start, tv);
425 struct cryptostats stats;
428 slen = sizeof (stats);
429 if (sysctlbyname("kern.crypto_stats", &stats, &slen, NULL, 0) < 0) {
430 perror("kern.crypto_stats");
433 bzero(&stats.cs_invoke, sizeof (stats.cs_invoke));
434 bzero(&stats.cs_done, sizeof (stats.cs_done));
435 bzero(&stats.cs_cb, sizeof (stats.cs_cb));
436 bzero(&stats.cs_finis, sizeof (stats.cs_finis));
437 stats.cs_invoke.min.tv_sec = 10000;
438 stats.cs_done.min.tv_sec = 10000;
439 stats.cs_cb.min.tv_sec = 10000;
440 stats.cs_finis.min.tv_sec = 10000;
441 if (sysctlbyname("kern.crypto_stats", NULL, NULL, &stats, sizeof (stats)) < 0)
442 perror("kern.cryptostats");
446 printt(const char* tag, struct cryptotstat *ts)
448 uint64_t avg, min, max;
452 avg = (1000000000LL*ts->acc.tv_sec + ts->acc.tv_nsec) / ts->count;
453 min = 1000000000LL*ts->min.tv_sec + ts->min.tv_nsec;
454 max = 1000000000LL*ts->max.tv_sec + ts->max.tv_nsec;
455 printf("%16.16s: avg %6llu ns : min %6llu ns : max %7llu ns [%u samps]\n",
456 tag, avg, min, max, ts->count);
461 runtests(struct alg *ealg, struct alg *alg, int count, int size, u_long cmd, int threads, int profile)
467 struct timeval total;
470 if (size % alg->blocksize || (ealg && size % ealg->blocksize)) {
472 printf("skipping blocksize %u 'cuz not a multiple of "
473 "%s blocksize %u (or %s blocksize %u)\n",
474 size, alg->name, alg->blocksize,
475 ealg ? ealg->name : "n/a",
476 ealg ? ealg->blocksize : 0);
480 region = mmap(NULL, threads * sizeof (struct timeval),
481 PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
482 if (region == MAP_FAILED) {
486 tvp = (struct timeval *) region;
489 size_t tlen = sizeof (otiming);
493 if (sysctlbyname("debug.crypto_timing", &otiming, &tlen,
494 &timing, sizeof (timing)) < 0)
495 perror("debug.crypto_timing");
500 for (i = 0; i < threads; i++)
505 cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
506 -1, sizeof(mask), &mask);
507 runtest(ealg, alg, count, size, cmd, &tvp[i]);
510 while (waitpid(WAIT_MYPGRP, &status, 0) != -1)
513 runtest(ealg, alg, count, size, cmd, tvp);
516 for (i = 0; i < threads; i++)
517 t += (((double)tvp[i].tv_sec * 1000000 + tvp[i].tv_usec) / 1000000);
519 int nops = alg->ishash ? count : 2*count;
522 printf("%8.3lf sec, %7d %6s%s%6s crypts, %7d bytes, %8.0lf byte/sec, %7.1lf Mb/sec\n",
523 t, nops, alg->name, ealg? "+" : "", ealg? ealg->name : "",
524 size, (double)nops*size / t,
525 (double)nops*size / t * 8 / 1024 / 1024);
529 struct cryptostats stats;
530 size_t slen = sizeof (stats);
532 if (sysctlbyname("debug.crypto_timing", NULL, NULL,
533 &otiming, sizeof (otiming)) < 0)
534 perror("debug.crypto_timing");
535 if (sysctlbyname("kern.crypto_stats", &stats, &slen, NULL, 0) < 0)
536 perror("kern.cryptostats");
537 if (stats.cs_invoke.count) {
538 printt("dispatch->invoke", &stats.cs_invoke);
539 printt("invoke->done", &stats.cs_done);
540 printt("done->cb", &stats.cs_cb);
541 printt("cb->finis", &stats.cs_finis);
549 main(int argc, char **argv)
551 struct alg *alg = NULL, *ealg = NULL;
554 int sizes[128], nsizes = 0;
555 u_long cmd = CIOCGSESSION2;
561 while ((ch = getopt(argc, argv, "cpzsva:bd:t:")) != -1) {
572 tmp = strchr(optarg, '+');
575 ealg = getalgbyname(optarg);
576 if (ealg == NULL || ealg->ishash)
581 alg = getalgbyname(optarg);
583 if (streq(optarg, "rijndael"))
584 alg = getalgbyname("aes");
587 } else if (ealg != NULL && !alg->ishash)
591 crid = crlookup(optarg);
594 maxthreads = atoi(optarg);
603 opflags |= COP_F_BATCH;
612 argc -= optind, argv += optind;
614 count = atoi(argv[0]);
616 int s = atoi(argv[1]);
617 if (nsizes < nitems(sizes)) {
620 printf("Too many sizes, ignoring %u\n", s);
624 if (maxthreads > CPU_SETSIZE)
625 errx(EX_USAGE, "Too many threads, %d, choose fewer.", maxthreads);
629 sizes[nsizes++] = alg->blocksize;
633 while (sizes[nsizes-1] < 8*1024) {
634 sizes[nsizes] = sizes[nsizes-1]<<1;
641 for (i = 0; i < nitems(algorithms); i++) {
643 alg = &algorithms[i];
644 for (j = 0; j < nsizes; j++)
645 runtests(ealg, alg, count, sizes[j], cmd, maxthreads, profile);
649 alg = getalgbycode(CRYPTO_3DES_CBC);
650 for (i = 0; i < nsizes; i++)
651 runtests(ealg, alg, count, sizes[i], cmd, maxthreads, profile);
658 hexdump(char *p, int n)
662 for (off = 0; n > 0; off += 16, n -= 16) {
663 printf("%s%04x:", off == 0 ? "\n" : "", off);
664 i = (n >= 16 ? 16 : n);
666 printf(" %02x", *p++ & 0xff);