2 * Copyright (C) 2009-2012 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 */
32 #include "benchmark.h"
34 static unsigned char data
[64 * 1024];
38 tls_log_func (int level
, const char *str
)
40 fprintf (stderr
, "|<%d>| %s", level
, str
);
44 cipher_mac_bench (int algo
, int mac_algo
, int size
)
47 gnutls_cipher_hd_t ctx
;
48 gnutls_hmac_hd_t mac_ctx
;
50 gnutls_datum_t key
, iv
;
51 int blocksize
= gnutls_cipher_get_block_size (algo
);
52 int keysize
= gnutls_cipher_get_key_size (algo
);
54 struct benchmark_st st
;
56 _key
= malloc (keysize
);
59 memset (_key
, 0xf0, keysize
);
61 _iv
= malloc (blocksize
);
64 memset (_iv
, 0xf0, blocksize
);
72 printf ("Checking %s with %s (%dkb payload)...\n", gnutls_cipher_get_name (algo
),
73 gnutls_mac_get_name(mac_algo
), size
);
78 ret
= gnutls_hmac_init(&mac_ctx
, mac_algo
, key
.data
, key
.size
);
81 fprintf (stderr
, "error: %s\n", gnutls_strerror (ret
));
85 ret
= gnutls_cipher_init (&ctx
, algo
, &key
, &iv
);
88 fprintf (stderr
, "error: %s\n", gnutls_strerror (ret
));
92 gnutls_hmac(mac_ctx
, data
, 1024);
96 gnutls_hmac(mac_ctx
, data
, step
);
97 gnutls_cipher_encrypt2 (ctx
, data
, step
, data
, step
+64);
100 while (benchmark_must_finish
== 0);
102 gnutls_cipher_deinit (ctx
);
103 gnutls_hmac_deinit(mac_ctx
, NULL
);
105 stop_benchmark (&st
, NULL
);
115 cipher_bench (int algo
, int size
, int aead
)
118 gnutls_cipher_hd_t ctx
;
120 gnutls_datum_t key
, iv
;
121 int blocksize
= gnutls_cipher_get_block_size (algo
);
122 int keysize
= gnutls_cipher_get_key_size (algo
);
123 int step
= size
*1024;
124 struct benchmark_st st
;
126 _key
= malloc (keysize
);
129 memset (_key
, 0xf0, keysize
);
131 _iv
= malloc (blocksize
);
134 memset (_iv
, 0xf0, blocksize
);
137 if (aead
) iv
.size
= 12;
138 else iv
.size
= blocksize
;
143 printf ("Checking %s (%dkb payload)...\n", gnutls_cipher_get_name (algo
),
147 start_benchmark(&st
);
149 ret
= gnutls_cipher_init (&ctx
, algo
, &key
, &iv
);
152 fprintf (stderr
, "error: %s\n", gnutls_strerror (ret
));
157 gnutls_cipher_add_auth (ctx
, data
, 1024);
161 gnutls_cipher_encrypt2 (ctx
, data
, step
, data
, step
+64);
164 while (benchmark_must_finish
== 0);
166 gnutls_cipher_deinit (ctx
);
168 stop_benchmark(&st
, NULL
);
176 mac_bench (int algo
, int size
)
179 int blocksize
= gnutls_hmac_get_len (algo
);
180 int step
= size
*1024;
181 struct benchmark_st st
;
183 _key
= malloc (blocksize
);
186 memset (_key
, 0xf0, blocksize
);
188 printf ("Checking %s (%dkb payload)...\n", gnutls_mac_get_name (algo
), size
);
191 start_benchmark(&st
);
195 gnutls_hmac_fast (algo
, _key
, blocksize
, data
, step
, _key
);
198 while (benchmark_must_finish
== 0);
200 stop_benchmark(&st
, NULL
);
205 void benchmark_cipher (int init
, int debug_level
)
207 gnutls_global_set_log_function (tls_log_func
);
208 gnutls_global_set_log_level (debug_level
);
211 gnutls_global_init ();
212 gnutls_rnd( GNUTLS_RND_NONCE
, data
, sizeof(data
));
215 cipher_mac_bench ( GNUTLS_CIPHER_AES_128_CBC
, GNUTLS_MAC_SHA1
, 16);
216 cipher_mac_bench ( GNUTLS_CIPHER_AES_128_CBC
, GNUTLS_MAC_SHA256
, 16);
217 cipher_bench ( GNUTLS_CIPHER_AES_128_GCM
, 16, 1);
219 mac_bench (GNUTLS_MAC_SHA1
, 16);
220 mac_bench (GNUTLS_MAC_SHA256
, 16);
221 mac_bench (GNUTLS_MAC_SHA512
, 16);
223 cipher_bench (GNUTLS_CIPHER_3DES_CBC
, 16, 0);
225 cipher_bench (GNUTLS_CIPHER_AES_128_CBC
, 16, 0);
227 cipher_bench (GNUTLS_CIPHER_ARCFOUR
, 16, 0);
229 gnutls_global_deinit();