TLS mode flag is being ignored for AEAD ciphers.
[cryptodev-linux.git] / examples / fullspeed.c
blob611859d32ded7f771efd1a0e8a484066c0d0ed08
1 /* cryptodev_test - simple benchmark tool for cryptodev
3 * Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <unistd.h>
29 #include <crypto/cryptodev.h>
31 static int si = 1; /* SI by default */
33 static double udifftimeval(struct timeval start, struct timeval end)
35 return (double)(end.tv_usec - start.tv_usec) +
36 (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
39 static int must_finish = 0;
41 static void alarm_handler(int signo)
43 must_finish = 1;
46 static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
47 static char *si_units[] = { "", "K", "M", "G", "T", 0};
49 static void value2human(int si, double bytes, double time, double* data, double* speed,char* metric)
51 int unit = 0;
53 *data = bytes;
55 if (si) {
56 while (*data > 1000 && si_units[unit + 1]) {
57 *data /= 1000;
58 unit++;
60 *speed = *data / time;
61 sprintf(metric, "%sB", si_units[unit]);
62 } else {
63 while (*data > 1024 && units[unit + 1]) {
64 *data /= 1024;
65 unit++;
67 *speed = *data / time;
68 sprintf(metric, "%sB", units[unit]);
72 #define MAX(x,y) ((x)>(y)?(x):(y))
74 int encrypt_data(int algo, void* keybuf, int key_size, int fdc, int chunksize)
76 struct crypt_op cop;
77 char *buffer, iv[32];
78 static int val = 23;
79 struct timeval start, end;
80 double total = 0;
81 double secs, ddata, dspeed;
82 char metric[16];
83 struct session_op sess;
85 if (posix_memalign((void **)&buffer, 16, chunksize)) {
86 printf("posix_memalign() failed! (mask %x, size: %d)\n", 16, chunksize);
87 return 1;
90 memset(iv, 0x23, 32);
92 printf("\tEncrypting in chunks of %d bytes: ", chunksize);
93 fflush(stdout);
95 memset(buffer, val++, chunksize);
97 must_finish = 0;
98 alarm(5);
100 gettimeofday(&start, NULL);
101 do {
102 memset(&sess, 0, sizeof(sess));
103 sess.cipher = algo;
104 sess.keylen = key_size;
105 sess.key = keybuf;
106 if (ioctl(fdc, CIOCGSESSION, &sess)) {
107 perror("ioctl(CIOCGSESSION)");
108 return 1;
111 memset(&cop, 0, sizeof(cop));
112 cop.ses = sess.ses;
113 cop.len = chunksize;
114 cop.iv = (unsigned char *)iv;
115 cop.op = COP_ENCRYPT;
116 cop.src = (unsigned char *)buffer;
117 cop.dst = buffer;
119 if (ioctl(fdc, CIOCCRYPT, &cop)) {
120 perror("ioctl(CIOCCRYPT)");
121 return 1;
124 ioctl(fdc, CIOCFSESSION, &sess.ses);
126 total+=chunksize;
127 } while(must_finish==0);
128 gettimeofday(&end, NULL);
130 secs = udifftimeval(start, end)/ 1000000.0;
132 value2human(si, total, secs, &ddata, &dspeed, metric);
133 printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
134 printf ("%.2f %s/sec\n", dspeed, metric);
136 free(buffer);
137 return 0;
140 int main(int argc, char** argv)
142 int fd, i, fdc = -1;
143 char keybuf[32];
145 signal(SIGALRM, alarm_handler);
147 if (argc > 1) {
148 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
149 printf("Usage: speed [--kib]\n");
150 exit(0);
152 if (strcmp(argv[1], "--kib") == 0) {
153 si = 0;
157 if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
158 perror("open()");
159 return 1;
161 if (ioctl(fd, CRIOGET, &fdc)) {
162 perror("ioctl(CRIOGET)");
163 return 1;
166 fprintf(stderr, "Testing NULL cipher: \n");
168 for (i = 512; i <= (64 * 1024); i *= 2) {
169 if (encrypt_data(CRYPTO_NULL, keybuf, 0, fdc, i))
170 break;
173 fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
174 memset(keybuf, 0x42, 16);
176 for (i = 512; i <= (64 * 1024); i *= 2) {
177 if (encrypt_data(CRYPTO_AES_CBC, keybuf, 16, fdc, i))
178 break;
181 close(fdc);
182 close(fd);
183 return 0;