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
25 #include <sys/ioctl.h>
27 #include <sys/types.h>
29 #include <crypto/cryptodev.h>
31 static double udifftimeval(struct timeval start
, struct timeval end
)
33 return (double)(end
.tv_usec
- start
.tv_usec
) +
34 (double)(end
.tv_sec
- start
.tv_sec
) * 1000 * 1000;
37 static int must_finish
= 0;
38 static struct pollfd pfd
;
40 static void alarm_handler(int signo
)
46 static char *units
[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
48 static void value2human(double bytes
, double time
, double* data
, double* speed
,char* metric
)
53 while (*data
> 1024 && units
[unit
+ 1]) {
57 *speed
= *data
/ time
;
58 sprintf(metric
, "%sB", units
[unit
]);
62 int encrypt_data(struct session_op
*sess
, int fdc
, int chunksize
, int alignmask
)
65 char *buffer
[64], iv
[32];
67 struct timeval start
, end
;
69 double secs
, ddata
, dspeed
;
71 int rc
, wqueue
= 0, bufidx
= 0;
75 printf("\tEncrypting in chunks of %d bytes: ", chunksize
);
78 for (rc
= 0; rc
< 64; rc
++) {
80 if (posix_memalign((void **)(buffer
+ rc
), alignmask
+ 1, chunksize
)) {
81 printf("posix_memalign() failed!\n");
85 if (!(buffer
[rc
] = malloc(chunksize
))) {
90 memset(buffer
[rc
], val
++, chunksize
);
93 pfd
.events
= POLLOUT
| POLLIN
;
98 gettimeofday(&start
, NULL
);
100 if ((rc
= poll(&pfd
, 1, 100)) < 0) {
101 if (errno
& (ERESTART
| EINTR
))
103 fprintf(stderr
, "errno = %d ", errno
);
108 if (pfd
.revents
& POLLOUT
) {
109 memset(&cop
, 0, sizeof(cop
));
112 cop
.iv
= (unsigned char *)iv
;
113 cop
.op
= COP_ENCRYPT
;
114 cop
.src
= cop
.dst
= (unsigned char *)buffer
[bufidx
];
115 bufidx
= (bufidx
+ 1) % 64;
117 if (ioctl(fdc
, CIOCASYNCCRYPT
, &cop
)) {
118 perror("ioctl(CIOCASYNCCRYPT)");
123 if (pfd
.revents
& POLLIN
) {
124 if (ioctl(fdc
, CIOCASYNCFETCH
, &cop
)) {
125 perror("ioctl(CIOCASYNCFETCH)");
131 } while(!must_finish
|| wqueue
);
132 gettimeofday(&end
, NULL
);
134 secs
= udifftimeval(start
, end
)/ 1000000.0;
136 value2human(total
, secs
, &ddata
, &dspeed
, metric
);
137 printf ("done. %.2f %s in %.2f secs: ", ddata
, metric
, secs
);
138 printf ("%.2f %s/sec\n", dspeed
, metric
);
140 for (rc
= 0; rc
< 64; rc
++)
147 int fd
, i
, fdc
= -1, alignmask
= 0;
148 struct session_op sess
;
150 struct session_info_op siop
;
154 signal(SIGALRM
, alarm_handler
);
156 if ((fd
= open("/dev/crypto", O_RDWR
, 0)) < 0) {
160 if (ioctl(fd
, CRIOGET
, &fdc
)) {
161 perror("ioctl(CRIOGET)");
165 fprintf(stderr
, "Testing NULL cipher: \n");
166 memset(&sess
, 0, sizeof(sess
));
167 sess
.cipher
= CRYPTO_NULL
;
169 sess
.key
= (unsigned char *)keybuf
;
170 if (ioctl(fdc
, CIOCGSESSION
, &sess
)) {
171 perror("ioctl(CIOCGSESSION)");
176 if (ioctl(fdc
, CIOCGSESSINFO
, &siop
)) {
177 perror("ioctl(CIOCGSESSINFO)");
180 alignmask
= siop
.alignmask
;
183 for (i
= 256; i
<= (64 * 4096); i
*= 2) {
184 if (encrypt_data(&sess
, fdc
, i
, alignmask
))
188 fprintf(stderr
, "\nTesting AES-128-CBC cipher: \n");
189 memset(&sess
, 0, sizeof(sess
));
190 sess
.cipher
= CRYPTO_AES_CBC
;
192 memset(keybuf
, 0x42, 16);
193 sess
.key
= (unsigned char *)keybuf
;
194 if (ioctl(fdc
, CIOCGSESSION
, &sess
)) {
195 perror("ioctl(CIOCGSESSION)");
200 if (ioctl(fdc
, CIOCGSESSINFO
, &siop
)) {
201 perror("ioctl(CIOCGSESSINFO)");
204 alignmask
= siop
.alignmask
;
207 for (i
= 256; i
<= (64 * 1024); i
*= 2) {
208 if (encrypt_data(&sess
, fdc
, i
, alignmask
))