updated makefiles
[gnutls.git] / src / benchmark-cipher.c
blobc0695153a53ba244a6956b0cc0d4adfb50779054
1 /*
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>.
23 #include <config.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <gnutls/gnutls.h>
29 #include <gnutls/crypto.h>
30 #include <time.h>
31 #include "timespec.h" /* gnulib gettime */
32 #include "benchmark.h"
34 static unsigned char data[64 * 1024];
37 static void
38 tls_log_func (int level, const char *str)
40 fprintf (stderr, "|<%d>| %s", level, str);
43 static void
44 cipher_mac_bench (int algo, int mac_algo, int size)
46 int ret;
47 gnutls_cipher_hd_t ctx;
48 gnutls_hmac_hd_t mac_ctx;
49 void *_key, *_iv;
50 gnutls_datum_t key, iv;
51 int blocksize = gnutls_cipher_get_block_size (algo);
52 int keysize = gnutls_cipher_get_key_size (algo);
53 int step = size*1024;
54 struct benchmark_st st;
56 _key = malloc (keysize);
57 if (_key == NULL)
58 return;
59 memset (_key, 0xf0, keysize);
61 _iv = malloc (blocksize);
62 if (_iv == NULL)
63 return;
64 memset (_iv, 0xf0, blocksize);
66 iv.data = _iv;
67 iv.size = blocksize;
69 key.data = _key;
70 key.size = keysize;
72 printf ("Checking %s with %s (%dkb payload)... ", gnutls_cipher_get_name (algo),
73 gnutls_mac_get_name(mac_algo), size);
74 fflush (stdout);
76 start_benchmark(&st);
78 ret = gnutls_hmac_init(&mac_ctx, mac_algo, key.data, key.size);
79 if (ret < 0)
81 fprintf (stderr, "error: %s\n", gnutls_strerror (ret));
82 goto leave;
85 ret = gnutls_cipher_init (&ctx, algo, &key, &iv);
86 if (ret < 0)
88 fprintf (stderr, "error: %s\n", gnutls_strerror (ret));
89 goto leave;
92 gnutls_hmac(mac_ctx, data, 1024);
96 gnutls_hmac(mac_ctx, data, step);
97 gnutls_cipher_encrypt2 (ctx, data, step, data, step+64);
98 st.size += step;
100 while (benchmark_must_finish == 0);
102 gnutls_cipher_deinit (ctx);
103 gnutls_hmac_deinit(mac_ctx, NULL);
105 stop_benchmark (&st, NULL);
107 leave:
108 free (_key);
109 free (_iv);
114 static void
115 cipher_bench (int algo, int size, int aead)
117 int ret;
118 gnutls_cipher_hd_t ctx;
119 void *_key, *_iv;
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);
127 if (_key == NULL)
128 return;
129 memset (_key, 0xf0, keysize);
131 _iv = malloc (blocksize);
132 if (_iv == NULL)
133 return;
134 memset (_iv, 0xf0, blocksize);
136 iv.data = _iv;
137 if (aead) iv.size = 12;
138 else iv.size = blocksize;
140 key.data = _key;
141 key.size = keysize;
143 printf ("Checking %s (%dkb payload)... ", gnutls_cipher_get_name (algo),
144 size);
145 fflush (stdout);
147 start_benchmark(&st);
149 ret = gnutls_cipher_init (&ctx, algo, &key, &iv);
150 if (ret < 0)
152 fprintf (stderr, "error: %s\n", gnutls_strerror (ret));
153 goto leave;
156 if (aead)
157 gnutls_cipher_add_auth (ctx, data, 1024);
161 gnutls_cipher_encrypt2 (ctx, data, step, data, step+64);
162 st.size += step;
164 while (benchmark_must_finish == 0);
166 gnutls_cipher_deinit (ctx);
168 stop_benchmark(&st, NULL);
170 leave:
171 free (_key);
172 free (_iv);
175 static void
176 mac_bench (int algo, int size)
178 void *_key;
179 int blocksize = gnutls_hmac_get_len (algo);
180 int step = size*1024;
181 struct benchmark_st st;
183 _key = malloc (blocksize);
184 if (_key == NULL)
185 return;
186 memset (_key, 0xf0, blocksize);
188 printf ("Checking %s (%dkb payload)... ", gnutls_mac_get_name (algo), size);
189 fflush (stdout);
191 start_benchmark(&st);
195 gnutls_hmac_fast (algo, _key, blocksize, data, step, _key);
196 st.size += step;
198 while (benchmark_must_finish == 0);
200 stop_benchmark(&st, NULL);
202 free (_key);
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);
209 if (init)
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();