2 * Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 * This file is part of GnuTLS.
6 * GnuTLS is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuTLS is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
20 * Written by Nikos Mavrogiannopoulos <nmav@gnutls.org>.
28 #include <gnutls/gnutls.h>
29 #include <gnutls/crypto.h>
32 #include "timespec.h" /* gnulib gettime */
34 static unsigned char data
[64 * 1024];
36 static int must_finish
= 0;
40 alarm_handler (int signo
)
46 DWORD WINAPI
alarm_handler (LPVOID lpParameter
);
48 alarm_handler (LPVOID lpParameter
)
50 HANDLE wtimer
= *((HANDLE
*) lpParameter
);
51 WaitForSingleObject (wtimer
, INFINITE
);
56 #define W32_ALARM_VARIABLES HANDLE wtimer = NULL, wthread = NULL; \
57 LARGE_INTEGER alarm_timeout
58 #define W32_ALARM_TRIGGER(timeout, leave) { \
59 wtimer = CreateWaitableTimer (NULL, TRUE, NULL); \
62 fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ()); \
65 wthread = CreateThread (NULL, 0, alarm_handler, &wtimer, 0, NULL); \
66 if (wthread == NULL) \
68 fprintf (stderr, "error: CreateThread %u\n", GetLastError ()); \
71 alarm_timeout.QuadPart = timeout * 10000000; \
72 if (SetWaitableTimer (wtimer, &alarm_timeout, 0, NULL, NULL, FALSE) == 0) \
74 fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ()); \
78 #define W32_ALARM_CLEANUP { \
80 CloseHandle (wtimer); \
81 if (wthread != NULL) \
82 CloseHandle (wthread);}
86 tls_log_func (int level
, const char *str
)
88 fprintf (stderr
, "|<%d>| %s", level
, str
);
92 value2human (double bytes
, double time
, double *data
, double *speed
,
95 if (bytes
> 1000 && bytes
< 1000 * 1000)
97 *data
= ((double) bytes
) / 1000;
98 *speed
= *data
/ time
;
99 strcpy (metric
, "Kb");
102 else if (bytes
>= 1000 * 1000 && bytes
< 1000 * 1000 * 1000)
104 *data
= ((double) bytes
) / (1000 * 1000);
105 *speed
= *data
/ time
;
106 strcpy (metric
, "Mb");
109 else if (bytes
>= 1000 * 1000 * 1000)
111 *data
= ((double) bytes
) / (1000 * 1000 * 1000);
112 *speed
= *data
/ time
;
113 strcpy (metric
, "Gb");
118 *data
= (double) bytes
;
119 *speed
= *data
/ time
;
120 strcpy (metric
, "bytes");
126 cipher_bench (int algo
, int size
)
129 gnutls_cipher_hd_t ctx
;
131 gnutls_datum_t key
, iv
;
132 struct timespec start
, stop
;
134 double data_size
= 0;
135 double dspeed
, ddata
;
136 int blocksize
= gnutls_cipher_get_block_size (algo
);
137 int keysize
= gnutls_cipher_get_key_size (algo
);
143 _key
= malloc (keysize
);
146 memset (_key
, 0xf0, keysize
);
148 _iv
= malloc (blocksize
);
151 memset (_iv
, 0xf0, blocksize
);
159 printf ("Checking %s (%dkb payload)... ", gnutls_cipher_get_name (algo
),
167 W32_ALARM_TRIGGER(5, goto leave
);
172 ret
= gnutls_cipher_init (&ctx
, algo
, &key
, &iv
);
175 fprintf (stderr
, "error: %s\n", gnutls_strerror (ret
));
181 gnutls_cipher_encrypt (ctx
, data
, size
* 1024);
182 data_size
+= size
* 1024;
184 while (must_finish
== 0);
186 gnutls_cipher_deinit (ctx
);
190 secs
= (stop
.tv_sec
* 1000 + stop
.tv_nsec
/ (1000 * 1000) -
191 (start
.tv_sec
* 1000 + start
.tv_nsec
/ (1000 * 1000)));
194 value2human (data_size
, secs
, &ddata
, &dspeed
, metric
);
195 printf ("Encrypted %.2f %s in %.2f secs: ", ddata
, metric
, secs
);
196 printf ("%.2f %s/sec\n", dspeed
, metric
);
207 mac_bench (int algo
, int size
)
210 struct timespec start
, stop
;
212 double data_size
= 0;
213 double ddata
, dspeed
;
214 int blocksize
= gnutls_hmac_get_len (algo
);
220 _key
= malloc (blocksize
);
223 memset (_key
, 0xf0, blocksize
);
225 printf ("Checking %s (%dkb payload)... ", gnutls_mac_get_name (algo
), size
);
232 W32_ALARM_TRIGGER(5, goto leave
);
239 gnutls_hmac_fast (algo
, _key
, blocksize
, data
, size
* 1024, _key
);
240 data_size
+= size
* 1024;
242 while (must_finish
== 0);
247 (stop
.tv_sec
* 1000 + stop
.tv_nsec
/ (1000 * 1000) -
248 (start
.tv_sec
* 1000 + start
.tv_nsec
/ (1000 * 1000)));
251 value2human (data_size
, secs
, &ddata
, &dspeed
, metric
);
253 printf ("Hashed %.2f %s in %.2f secs: ", ddata
, metric
, secs
);
254 printf ("%.2f %s/sec\n", dspeed
, metric
);
263 main (int argc
, char **argv
)
271 signal (SIGALRM
, alarm_handler
);
274 gnutls_global_set_log_function (tls_log_func
);
275 gnutls_global_set_log_level (debug_level
);
276 gnutls_global_init ();
278 mac_bench (GNUTLS_MAC_SHA1
, 4);
279 mac_bench (GNUTLS_MAC_SHA1
, 8);
280 mac_bench (GNUTLS_MAC_SHA1
, 16);
282 mac_bench (GNUTLS_MAC_SHA256
, 4);
283 mac_bench (GNUTLS_MAC_SHA256
, 8);
284 mac_bench (GNUTLS_MAC_SHA256
, 16);
286 cipher_bench (GNUTLS_CIPHER_3DES_CBC
, 4);
287 cipher_bench (GNUTLS_CIPHER_3DES_CBC
, 8);
288 cipher_bench (GNUTLS_CIPHER_3DES_CBC
, 16);
290 cipher_bench (GNUTLS_CIPHER_AES_128_CBC
, 4);
291 cipher_bench (GNUTLS_CIPHER_AES_128_CBC
, 8);
292 cipher_bench (GNUTLS_CIPHER_AES_128_CBC
, 16);
294 cipher_bench (GNUTLS_CIPHER_ARCFOUR
, 4);
295 cipher_bench (GNUTLS_CIPHER_ARCFOUR
, 8);
296 cipher_bench (GNUTLS_CIPHER_ARCFOUR
, 16);