1 From b13160357e683b9d42ba513433b4c09456a8332b Mon Sep 17 00:00:00 2001
2 From: Alexe Radu <radu.alexe@nxp.com>
3 Date: Fri, 28 Oct 2016 13:39:50 +0300
4 Subject: [PATCH 081/104] add sync speed tests with the same format as async
7 The file speed.c was removed because has the same functionality
10 Signed-off-by: Alexe Radu <radu.alexe@nxp.com>
13 tests/speed.c | 265 -----------------------------------
14 tests/sync_speed.c | 399 +++++++++++++++++++++++++++++++++++++++++++++++++++++
15 3 files changed, 401 insertions(+), 267 deletions(-)
16 delete mode 100644 tests/speed.c
17 create mode 100644 tests/sync_speed.c
19 diff --git a/tests/Makefile b/tests/Makefile
20 index 23d67f9..400fb7a 100644
23 @@ -3,14 +3,14 @@ CFLAGS += -I.. $(CRYPTODEV_CFLAGS)
25 comp_progs := cipher_comp hash_comp hmac_comp
27 -hostprogs := cipher cipher-aead hmac speed async_cipher async_hmac \
28 +hostprogs := cipher cipher-aead hmac sync_speed async_cipher async_hmac \
29 async_speed sha_speed hashcrypt_speed fullspeed cipher-gcm \
30 cipher-aead-srtp $(comp_progs)
32 example-cipher-objs := cipher.o
33 example-cipher-aead-objs := cipher-aead.o
34 example-hmac-objs := hmac.o
35 -example-speed-objs := speed.c
36 +example-speed-objs := sync_speed.o
37 example-fullspeed-objs := fullspeed.c
38 example-sha-speed-objs := sha_speed.c
39 example-async-cipher-objs := async_cipher.o
40 diff --git a/tests/speed.c b/tests/speed.c
41 deleted file mode 100644
42 index 0e2bbc3..0000000
46 -/* cryptodev_test - simple benchmark tool for cryptodev
48 - * Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
50 - * This program is free software; you can redistribute it and/or modify
51 - * it under the terms of the GNU General Public License as published by
52 - * the Free Software Foundation; either version 2 of the License, or
53 - * (at your option) any later version.
55 - * This program is distributed in the hope that it will be useful,
56 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 - * GNU General Public License for more details.
60 - * You should have received a copy of the GNU General Public License
61 - * along with this program; if not, write to the Free Software
62 - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
68 -#include <sys/ioctl.h>
69 -#include <sys/time.h>
70 -#include <sys/types.h>
75 -#include <crypto/cryptodev.h>
77 -static int si = 1; /* SI by default */
79 -static double udifftimeval(struct timeval start, struct timeval end)
81 - return (double)(end.tv_usec - start.tv_usec) +
82 - (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
85 -static int must_finish = 0;
87 -static void alarm_handler(int signo)
92 -static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
93 -static char *si_units[] = { "", "K", "M", "G", "T", 0};
95 -static void value2human(int si, double bytes, double time, double* data, double* speed,char* metric)
102 - while (*data > 1000 && si_units[unit + 1]) {
106 - *speed = *data / time;
107 - sprintf(metric, "%sB", si_units[unit]);
109 - while (*data > 1024 && units[unit + 1]) {
113 - *speed = *data / time;
114 - sprintf(metric, "%sB", units[unit]);
118 -#define MAX(x,y) ((x)>(y)?(x):(y))
120 -int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
122 - struct crypt_op cop;
123 - char *buffer, iv[32];
124 - uint8_t mac[HASH_MAX_LEN];
125 - static int val = 23;
126 - struct timeval start, end;
128 - double secs, ddata, dspeed;
130 - int min_alignmask = sizeof(void*) - 1;
133 - alignmask = ((alignmask < min_alignmask) ? min_alignmask : alignmask);
134 - if (posix_memalign((void **)&buffer, MAX(alignmask + 1, sizeof(void*)), chunksize)) {
135 - printf("posix_memalign() failed! (mask %x, size: %d)\n", alignmask+1, chunksize);
139 - if (!(buffer = malloc(chunksize))) {
140 - perror("malloc()");
145 - memset(iv, 0x23, 32);
147 - printf("\tEncrypting in chunks of %d bytes: ", chunksize);
150 - memset(buffer, val++, chunksize);
155 - gettimeofday(&start, NULL);
157 - memset(&cop, 0, sizeof(cop));
158 - cop.ses = sess->ses;
159 - cop.len = chunksize;
160 - cop.iv = (unsigned char *)iv;
161 - cop.op = COP_ENCRYPT;
162 - cop.src = cop.dst = (unsigned char *)buffer;
165 - if (ioctl(fdc, CIOCCRYPT, &cop)) {
166 - perror("ioctl(CIOCCRYPT)");
170 - } while(must_finish==0);
171 - gettimeofday(&end, NULL);
173 - secs = udifftimeval(start, end)/ 1000000.0;
175 - value2human(si, total, secs, &ddata, &dspeed, metric);
176 - printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
177 - printf ("%.2f %s/sec\n", dspeed, metric);
183 -int main(int argc, char** argv)
185 - int fd, i, fdc = -1, alignmask = 0;
186 - struct session_op sess;
187 -#ifdef CIOCGSESSINFO
188 - struct session_info_op siop;
192 - signal(SIGALRM, alarm_handler);
195 - if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
196 - printf("Usage: speed [--kib]\n");
199 - if (strcmp(argv[1], "--kib") == 0) {
204 - if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
208 - if (ioctl(fd, CRIOGET, &fdc)) {
209 - perror("ioctl(CRIOGET)");
213 - fprintf(stderr, "Testing NULL cipher: \n");
214 - memset(&sess, 0, sizeof(sess));
215 - sess.cipher = CRYPTO_NULL;
217 - sess.key = (unsigned char *)keybuf;
218 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
219 - perror("ioctl(CIOCGSESSION)");
222 -#ifdef CIOCGSESSINFO
223 - siop.ses = sess.ses;
224 - if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
225 - perror("ioctl(CIOCGSESSINFO)");
228 - alignmask = siop.alignmask;
231 - for (i = 512; i <= (64 * 1024); i *= 2) {
232 - if (encrypt_data(&sess, fdc, i, alignmask))
236 - fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
237 - memset(&sess, 0, sizeof(sess));
238 - sess.cipher = CRYPTO_AES_CBC;
240 - memset(keybuf, 0x42, 16);
241 - sess.key = (unsigned char *)keybuf;
242 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
243 - perror("ioctl(CIOCGSESSION)");
246 -#ifdef CIOCGSESSINFO
247 - siop.ses = sess.ses;
248 - if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
249 - perror("ioctl(CIOCGSESSINFO)");
252 - alignmask = siop.alignmask;
255 - for (i = 512; i <= (64 * 1024); i *= 2) {
256 - if (encrypt_data(&sess, fdc, i, alignmask))
260 - fprintf(stderr, "\nTesting AES-256-XTS cipher: \n");
261 - memset(&sess, 0, sizeof(sess));
262 - sess.cipher = CRYPTO_AES_XTS;
264 - memset(keybuf, 0x42, sess.keylen);
265 - sess.key = (unsigned char *)keybuf;
266 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
267 - perror("ioctl(CIOCGSESSION)");
270 -#ifdef CIOCGSESSINFO
271 - siop.ses = sess.ses;
272 - if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
273 - perror("ioctl(CIOCGSESSINFO)");
276 - alignmask = siop.alignmask;
279 - for (i = 512; i <= (64 * 1024); i *= 2) {
280 - if (encrypt_data(&sess, fdc, i, alignmask))
284 - fprintf(stderr, "\nTesting CRC32C hash: \n");
285 - memset(&sess, 0, sizeof(sess));
286 - sess.mac = CRYPTO_CRC32C;
287 - if (ioctl(fdc, CIOCGSESSION, &sess)) {
288 - perror("ioctl(CIOCGSESSION)");
291 -#ifdef CIOCGSESSINFO
292 - siop.ses = sess.ses;
293 - if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
294 - perror("ioctl(CIOCGSESSINFO)");
297 - printf("requested hash CRYPTO_CRC32C, got %s with driver %s\n",
298 - siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
299 - alignmask = siop.alignmask;
302 - for (i = 512; i <= (64 * 1024); i *= 2) {
303 - if (encrypt_data(&sess, fdc, i, alignmask))
311 diff --git a/tests/sync_speed.c b/tests/sync_speed.c
313 index 0000000..b0cb9ad
315 +++ b/tests/sync_speed.c
317 +/* cryptodev_test - simple benchmark tool for cryptodev
319 + * Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
321 + * This program is free software; you can redistribute it and/or modify
322 + * it under the terms of the GNU General Public License as published by
323 + * the Free Software Foundation; either version 2 of the License, or
324 + * (at your option) any later version.
326 + * This program is distributed in the hope that it will be useful,
327 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
328 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
329 + * GNU General Public License for more details.
331 + * You should have received a copy of the GNU General Public License
332 + * along with this program; if not, write to the Free Software
333 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
341 +#include <sys/ioctl.h>
342 +#include <sys/time.h>
343 +#include <sys/types.h>
345 +#include <crypto/cryptodev.h>
346 +#include <stdbool.h>
349 +struct test_params {
356 +const char usage_str[] = "Usage: %s [OPTION]... <cipher>|<hash>\n"
357 + "Run benchmark test for cipher or hash\n\n"
358 + " -t <secs>\t" "time to run each test (default 10 secs)\n"
359 + " -n <bytes>\t" "size of the test buffer\n"
360 + " -h\t\t" "show this help\n"
363 +int run_null(int fdc, struct test_params tp);
364 +int run_aes_cbc(int fdc, struct test_params tp);
365 +int run_aes_xts(int fdc, struct test_params tp);
366 +int run_crc32c(int fdc, struct test_params tp);
367 +int run_sha1(int fdc, struct test_params tp);
368 +int run_sha256(int fdc, struct test_params tp);
369 +int get_alignmask(int fdc, struct session_op *sess);
374 + int (*func)(int, struct test_params);
375 +} ciphers[ALG_COUNT] = {
376 + {"null", run_null},
377 + {"aes-cbc", run_aes_cbc},
378 + {"aes-xts", run_aes_xts},
379 + {"crc32c", run_crc32c},
380 + {"sha1", run_sha1},
381 + {"sha256", run_sha256},
384 +static double udifftimeval(struct timeval start, struct timeval end)
386 + return (double)(end.tv_usec - start.tv_usec) +
387 + (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
390 +static int must_finish = 0;
391 +static int must_exit = 0;
393 +static void alarm_handler(int signo)
398 +static void exit_handler(int signo)
401 + printf("\nexit requested by user through ctrl+c \n");
404 +static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
406 +static void value2human(double bytes, double time, double* data, double* speed,char* metric)
411 + while (*data > 1024 && units[unit + 1]) {
415 + *speed = *data / time;
416 + sprintf(metric, "%sB", units[unit]);
419 +static int encrypt_data(int fdc, struct test_params tp, struct session_op *sess)
421 + struct crypt_op cop;
422 + char *buffer, iv[32];
423 + char mac[HASH_MAX_LEN];
424 + static int val = 23;
425 + struct timeval start, end;
427 + double secs, ddata, dspeed;
430 + int min_alignmask = sizeof(void*) - 1;
432 + memset(iv, 0x23, 32);
434 + printf("\tEncrypting in chunks of %d bytes: ", tp.nvalue);
437 + alignmask = get_alignmask(fdc, sess);
439 + alignmask = ((alignmask < min_alignmask) ? min_alignmask : alignmask);
440 + if (posix_memalign((void **)(&buffer), alignmask + 1, tp.nvalue)) {
441 + printf("posix_memalign() failed!\n");
445 + if (!(buffer = malloc(tp.nvalue))) {
446 + perror("malloc()");
450 + memset(buffer, val++, tp.nvalue);
455 + gettimeofday(&start, NULL);
457 + memset(&cop, 0, sizeof(cop));
458 + cop.ses = sess->ses;
459 + cop.len = tp.nvalue;
460 + cop.iv = (unsigned char *)iv;
461 + cop.op = COP_ENCRYPT;
462 + cop.src = cop.dst = (unsigned char *)buffer;
463 + cop.mac = (unsigned char *)mac;
465 + if (ioctl(fdc, CIOCCRYPT, &cop)) {
466 + perror("ioctl(CIOCCRYPT)");
470 + } while(!must_finish);
471 + gettimeofday(&end, NULL);
473 + secs = udifftimeval(start, end)/ 1000000.0;
475 + value2human(total, secs, &ddata, &dspeed, metric);
476 + printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
477 + printf ("%.2f %s/sec\n", dspeed, metric);
483 +void usage(char *cmd_name)
485 + printf(usage_str, cmd_name);
488 +int run_test(int id, struct test_params tp)
493 + fd = open("/dev/crypto", O_RDWR, 0);
498 + if (ioctl(fd, CRIOGET, &fdc)) {
499 + perror("ioctl(CRIOGET)");
503 + ciphers[id].func(fdc, tp);
511 +int get_alignmask(int fdc, struct session_op *sess)
515 +#ifdef CIOCGSESSINFO
516 + struct session_info_op siop;
518 + siop.ses = sess->ses;
519 + if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
520 + perror("ioctl(CIOCGSESSINFO)");
523 + alignmask = siop.alignmask;
531 +void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
536 + encrypt_data(fdc, tp, sess);
538 + for (i = 256; i <= (64 * 1024); i *= 2) {
543 + if (encrypt_data(fdc, tp, sess)) {
551 +int run_null(int fdc, struct test_params tp)
553 + struct session_op sess;
556 + fprintf(stderr, "Testing NULL cipher: \n");
557 + memset(&sess, 0, sizeof(sess));
558 + sess.cipher = CRYPTO_NULL;
560 + sess.key = (unsigned char *)keybuf;
561 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
562 + perror("ioctl(CIOCGSESSION)");
566 + do_test_vectors(fdc, tp, &sess);
570 +int run_aes_cbc(int fdc, struct test_params tp)
572 + struct session_op sess;
575 + fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
576 + memset(&sess, 0, sizeof(sess));
577 + sess.cipher = CRYPTO_AES_CBC;
579 + memset(keybuf, 0x42, 16);
580 + sess.key = (unsigned char *)keybuf;
581 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
582 + perror("ioctl(CIOCGSESSION)");
586 + do_test_vectors(fdc, tp, &sess);
590 +int run_aes_xts(int fdc, struct test_params tp)
592 + struct session_op sess;
595 + fprintf(stderr, "\nTesting AES-256-XTS cipher: \n");
596 + memset(&sess, 0, sizeof(sess));
597 + sess.cipher = CRYPTO_AES_XTS;
599 + memset(keybuf, 0x42, sess.keylen);
600 + sess.key = (unsigned char *)keybuf;
601 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
602 + perror("ioctl(CIOCGSESSION)");
606 + do_test_vectors(fdc, tp, &sess);
610 +int run_crc32c(int fdc, struct test_params tp)
612 + struct session_op sess;
614 + fprintf(stderr, "\nTesting CRC32C hash: \n");
615 + memset(&sess, 0, sizeof(sess));
616 + sess.mac = CRYPTO_CRC32C;
617 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
618 + perror("ioctl(CIOCGSESSION)");
622 + do_test_vectors(fdc, tp, &sess);
626 +int run_sha1(int fdc, struct test_params tp)
628 + struct session_op sess;
630 + fprintf(stderr, "\nTesting SHA-1 hash: \n");
631 + memset(&sess, 0, sizeof(sess));
632 + sess.mac = CRYPTO_SHA1;
633 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
634 + perror("ioctl(CIOCGSESSION)");
638 + do_test_vectors(fdc, tp, &sess);
642 +int run_sha256(int fdc, struct test_params tp)
644 + struct session_op sess;
646 + fprintf(stderr, "\nTesting SHA2-256 hash: \n");
647 + memset(&sess, 0, sizeof(sess));
648 + sess.mac = CRYPTO_SHA2_256;
649 + if (ioctl(fdc, CIOCGSESSION, &sess)) {
650 + perror("ioctl(CIOCGSESSION)");
654 + do_test_vectors(fdc, tp, &sess);
658 +int main(int argc, char **argv)
664 + struct test_params tp;
670 + while ((c = getopt(argc, argv, "hn:t:")) != -1) {
673 + tp.nvalue = atoi(optarg);
677 + tp.tvalue = atoi(optarg);
680 + case 'h': /* no break */
687 + /* the name of a specific test asked on the command line */
688 + if (optind < argc) {
689 + alg_name = argv[optind];
693 + /* default test time */
698 + signal(SIGALRM, alarm_handler);
699 + signal(SIGINT, exit_handler);
701 + for (i = 0; i < ALG_COUNT; i++) {
706 + if (strcmp(alg_name, ciphers[i].name) == 0) {