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 "benchmark.h"
33 static unsigned char data[64 * 1024];
36 static void tls_log_func(int level, const char *str)
38 fprintf(stderr, "|<%d>| %s", level, str);
41 static void cipher_mac_bench(int algo, int mac_algo, int size)
44 gnutls_cipher_hd_t ctx;
45 gnutls_hmac_hd_t mac_ctx;
47 gnutls_datum_t key, iv;
48 int ivsize = gnutls_cipher_get_iv_size(algo);
49 int keysize = gnutls_cipher_get_key_size(algo);
50 int step = size * 1024;
51 struct benchmark_st st;
53 _key = malloc(keysize);
56 memset(_key, 0xf0, keysize);
64 memset(_iv, 0xf0, ivsize);
72 printf("%16s-%s ", gnutls_cipher_get_name(algo),
73 gnutls_mac_get_name(mac_algo));
78 ret = gnutls_hmac_init(&mac_ctx, mac_algo, key.data, key.size);
80 fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
84 ret = gnutls_cipher_init(&ctx, algo, &key, &iv);
86 fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
90 gnutls_hmac(mac_ctx, data, 1024);
93 gnutls_hmac(mac_ctx, data, step);
94 gnutls_cipher_encrypt2(ctx, data, step, data, step + 64);
97 while (benchmark_must_finish == 0);
99 gnutls_cipher_deinit(ctx);
100 gnutls_hmac_deinit(mac_ctx, NULL);
102 stop_benchmark(&st, NULL, 1);
110 static void cipher_bench(int algo, int size, int aead)
113 gnutls_cipher_hd_t ctx;
115 gnutls_datum_t key, iv;
116 int ivsize = gnutls_cipher_get_iv_size(algo);
117 int keysize = gnutls_cipher_get_key_size(algo);
118 int step = size * 1024;
119 struct benchmark_st st;
121 _key = malloc(keysize);
124 memset(_key, 0xf0, keysize);
126 _iv = malloc(ivsize);
129 memset(_iv, 0xf0, ivsize);
140 printf("%16s ", gnutls_cipher_get_name(algo));
143 start_benchmark(&st);
145 ret = gnutls_cipher_init(&ctx, algo, &key, &iv);
147 fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
152 gnutls_cipher_add_auth(ctx, data, 1024);
155 gnutls_cipher_encrypt2(ctx, data, step, data, step + 64);
158 while (benchmark_must_finish == 0);
160 gnutls_cipher_deinit(ctx);
162 stop_benchmark(&st, NULL, 1);
169 static void mac_bench(int algo, int size)
172 int blocksize = gnutls_hmac_get_len(algo);
173 int step = size * 1024;
174 struct benchmark_st st;
176 _key = malloc(blocksize);
179 memset(_key, 0xf0, blocksize);
181 printf("%16s ", gnutls_mac_get_name(algo));
184 start_benchmark(&st);
187 gnutls_hmac_fast(algo, _key, blocksize, data, step, _key);
190 while (benchmark_must_finish == 0);
192 stop_benchmark(&st, NULL, 1);
197 void benchmark_cipher(int debug_level)
200 gnutls_global_set_log_function(tls_log_func);
201 gnutls_global_set_log_level(debug_level);
203 gnutls_rnd(GNUTLS_RND_NONCE, data, sizeof(data));
205 printf("Checking cipher-MAC combinations, payload size: %u\n", size * 1024);
206 cipher_mac_bench(GNUTLS_CIPHER_SALSA20_256, GNUTLS_MAC_SHA1, size);
207 cipher_mac_bench(GNUTLS_CIPHER_AES_128_CBC, GNUTLS_MAC_SHA1, size);
208 cipher_mac_bench(GNUTLS_CIPHER_AES_128_CBC, GNUTLS_MAC_SHA256,
210 cipher_bench(GNUTLS_CIPHER_AES_128_GCM, size, 1);
212 printf("\nChecking MAC algorithms, payload size: %u\n", size * 1024);
213 mac_bench(GNUTLS_MAC_SHA1, size);
214 mac_bench(GNUTLS_MAC_SHA256, size);
215 mac_bench(GNUTLS_MAC_SHA512, size);
217 printf("\nChecking ciphers, payload size: %u\n", size * 1024);
218 cipher_bench(GNUTLS_CIPHER_3DES_CBC, size, 0);
220 cipher_bench(GNUTLS_CIPHER_AES_128_CBC, size, 0);
222 cipher_bench(GNUTLS_CIPHER_ARCFOUR, size, 0);
224 cipher_bench(GNUTLS_CIPHER_SALSA20_256, size, 0);
226 gnutls_global_deinit();