1 /* sha_speed - simple SHA benchmark tool for cryptodev
3 * Copyright (C) 2011 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
24 #include <sys/ioctl.h>
26 #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;
39 static void alarm_handler(int signo
)
44 static char *units
[] = { "", "Ki", "Mi", "Gi", "Ti", 0};
45 static char *si_units
[] = { "", "K", "M", "G", "T", 0};
47 static void value2human(int si
, double bytes
, double time
, double* data
, double* speed
,char* metric
)
54 while (*data
> 1000 && si_units
[unit
+ 1]) {
58 *speed
= *data
/ time
;
59 sprintf(metric
, "%sB", si_units
[unit
]);
61 while (*data
> 1024 && units
[unit
+ 1]) {
65 *speed
= *data
/ time
;
66 sprintf(metric
, "%sB", units
[unit
]);
71 int hash_data(struct session_op
*sess
, int fdc
, int chunksize
, int alignmask
)
76 struct timeval start
, end
;
78 double secs
, ddata
, dspeed
;
80 uint8_t mac
[AALG_MAX_RESULT_LEN
];
83 if (posix_memalign((void **)&buffer
, alignmask
+ 1, chunksize
)) {
84 printf("posix_memalign() failed!\n");
88 if (!(buffer
= malloc(chunksize
))) {
94 printf("\tEncrypting in chunks of %d bytes: ", chunksize
);
97 memset(buffer
, val
++, chunksize
);
102 gettimeofday(&start
, NULL
);
104 memset(&cop
, 0, sizeof(cop
));
107 cop
.op
= COP_ENCRYPT
;
108 cop
.src
= (unsigned char *)buffer
;
111 if (ioctl(fdc
, CIOCCRYPT
, &cop
)) {
112 perror("ioctl(CIOCCRYPT)");
116 } while(must_finish
==0);
117 gettimeofday(&end
, NULL
);
119 secs
= udifftimeval(start
, end
)/ 1000000.0;
121 value2human(1, total
, secs
, &ddata
, &dspeed
, metric
);
122 printf ("done. %.2f %s in %.2f secs: ", ddata
, metric
, secs
);
123 printf ("%.2f %s/sec\n", dspeed
, metric
);
131 int fd
, i
, fdc
= -1, alignmask
= 0;
132 struct session_op sess
;
135 struct session_info_op siop
;
138 signal(SIGALRM
, alarm_handler
);
140 if ((fd
= open("/dev/crypto", O_RDWR
, 0)) < 0) {
144 if (ioctl(fd
, CRIOGET
, &fdc
)) {
145 perror("ioctl(CRIOGET)");
149 fprintf(stderr
, "Testing SHA1 Hash: \n");
150 memset(&sess
, 0, sizeof(sess
));
151 sess
.mac
= CRYPTO_SHA1
;
152 if (ioctl(fdc
, CIOCGSESSION
, &sess
)) {
153 perror("ioctl(CIOCGSESSION)");
158 if (ioctl(fdc
, CIOCGSESSINFO
, &siop
)) {
159 perror("ioctl(CIOCGSESSINFO)");
162 printf("requested hash CRYPTO_SHA1, got %s with driver %s\n",
163 siop
.hash_info
.cra_name
, siop
.hash_info
.cra_driver_name
);
164 alignmask
= siop
.alignmask
;
167 for (i
= 256; i
<= (64 * 1024); i
*= 4) {
168 if (hash_data(&sess
, fdc
, i
, alignmask
))
172 fprintf(stderr
, "\nTesting SHA256 Hash: \n");
173 memset(&sess
, 0, sizeof(sess
));
174 sess
.mac
= CRYPTO_SHA2_256
;
175 if (ioctl(fdc
, CIOCGSESSION
, &sess
)) {
176 perror("ioctl(CIOCGSESSION)");
181 if (ioctl(fdc
, CIOCGSESSINFO
, &siop
)) {
182 perror("ioctl(CIOCGSESSINFO)");
185 printf("requested hash CRYPTO_SHA2_256, got %s with driver %s\n",
186 siop
.hash_info
.cra_name
, siop
.hash_info
.cra_driver_name
);
187 alignmask
= siop
.alignmask
;
190 for (i
= 256; i
<= (64 * 1024); i
*= 4) {
191 if (hash_data(&sess
, fdc
, i
, alignmask
))