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>
31 #include "timespec.h" /* gnulib gettime */
33 static unsigned char data
[64 * 1024];
35 #define TOTAL_ITER 8*1024
38 cipher_bench (int algo
, int size
)
41 gnutls_cipher_hd_t ctx
;
43 gnutls_datum_t key
, iv
;
44 struct timespec start
, stop
;
48 int blocksize
= gnutls_cipher_get_block_size (algo
);
49 int keysize
= gnutls_cipher_get_key_size (algo
);
51 _key
= malloc (keysize
);
54 memset (_key
, 0xf0, keysize
);
56 _iv
= malloc (blocksize
);
59 memset (_iv
, 0xf0, blocksize
);
67 gnutls_global_init ();
69 printf ("Checking %s (%dkb payload)... ", gnutls_cipher_get_name (algo
),
74 ret
= gnutls_cipher_init (&ctx
, algo
, &key
, &iv
);
77 fprintf (stderr
, "error: %s\n", gnutls_strerror (ret
));
81 for (i
= 0; i
< TOTAL_ITER
; i
++)
83 gnutls_cipher_encrypt (ctx
, data
, size
* 1024);
84 data_size
+= size
* 1024;
87 gnutls_cipher_deinit (ctx
);
91 secs
= (stop
.tv_sec
* 1000 + stop
.tv_nsec
/ (1000 * 1000) -
92 (start
.tv_sec
* 1000 + start
.tv_nsec
/ (1000 * 1000)));
94 dd
= (((double) data_size
/ (double) secs
)) / 1000;
95 printf ("Encrypted %ld kb in %.2f secs: ", data_size
/ 1000, secs
);
96 printf ("%.2f kbyte/sec\n", dd
);
105 mac_bench (int algo
, int size
)
109 struct timespec start
, stop
;
113 int blocksize
= gnutls_hmac_get_len (algo
);
115 _key
= malloc (blocksize
);
118 memset (_key
, 0xf0, blocksize
);
120 gnutls_global_init ();
122 printf ("Checking %s (%dkb payload)... ", gnutls_mac_get_name (algo
), size
);
126 for (i
= 0; i
< TOTAL_ITER
; i
++)
128 gnutls_hmac_fast (algo
, _key
, blocksize
, data
, size
* 1024, _key
);
129 data_size
+= size
* 1024;
135 (stop
.tv_sec
* 1000 + stop
.tv_nsec
/ (1000 * 1000) -
136 (start
.tv_sec
* 1000 + start
.tv_nsec
/ (1000 * 1000)));
138 dd
= (((double) data_size
/ (double) secs
)) / 1000;
139 printf ("Hashed %ld kb in %.2f secs: ", data_size
/ 1000, secs
);
140 printf ("%.2f kbyte/sec\n", dd
);
148 mac_bench (GNUTLS_MAC_SHA1
, 4);
149 mac_bench (GNUTLS_MAC_SHA1
, 8);
150 mac_bench (GNUTLS_MAC_SHA1
, 16);
152 mac_bench (GNUTLS_MAC_SHA256
, 4);
153 mac_bench (GNUTLS_MAC_SHA256
, 8);
154 mac_bench (GNUTLS_MAC_SHA256
, 16);
156 cipher_bench (GNUTLS_CIPHER_3DES_CBC
, 4);
157 cipher_bench (GNUTLS_CIPHER_3DES_CBC
, 8);
158 cipher_bench (GNUTLS_CIPHER_3DES_CBC
, 16);
160 cipher_bench (GNUTLS_CIPHER_AES_128_CBC
, 4);
161 cipher_bench (GNUTLS_CIPHER_AES_128_CBC
, 8);
162 cipher_bench (GNUTLS_CIPHER_AES_128_CBC
, 16);
164 cipher_bench (GNUTLS_CIPHER_ARCFOUR
, 4);
165 cipher_bench (GNUTLS_CIPHER_ARCFOUR
, 8);
166 cipher_bench (GNUTLS_CIPHER_ARCFOUR
, 16);