Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / src / benchmark-cipher.c
1 /*
2  * Copyright (C) 2009-2012 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuTLS.
5  *
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.
10  *
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.
15  *
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/>.
19  *
20  * Written by Nikos Mavrogiannopoulos <nmav@gnutls.org>.
21  */
22
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 "benchmark.h"
32
33 static unsigned char data[64 * 1024];
34
35
36 static void tls_log_func(int level, const char *str)
37 {
38         fprintf(stderr, "|<%d>| %s", level, str);
39 }
40
41 static void cipher_mac_bench(int algo, int mac_algo, int size)
42 {
43         int ret;
44         gnutls_cipher_hd_t ctx;
45         gnutls_hmac_hd_t mac_ctx;
46         void *_key, *_iv;
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;
52
53         _key = malloc(keysize);
54         if (_key == NULL)
55                 return;
56         memset(_key, 0xf0, keysize);
57
58         _iv = malloc(ivsize);
59         if (_iv == NULL) {
60                 if (_key)
61                         free(_key);
62                 return;
63         }
64         memset(_iv, 0xf0, ivsize);
65
66         iv.data = _iv;
67         iv.size = ivsize;
68
69         key.data = _key;
70         key.size = keysize;
71
72         printf("%16s-%s ", gnutls_cipher_get_name(algo),
73                gnutls_mac_get_name(mac_algo));
74         fflush(stdout);
75
76         start_benchmark(&st);
77
78         ret = gnutls_hmac_init(&mac_ctx, mac_algo, key.data, key.size);
79         if (ret < 0) {
80                 fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
81                 goto leave;
82         }
83
84         ret = gnutls_cipher_init(&ctx, algo, &key, &iv);
85         if (ret < 0) {
86                 fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
87                 goto leave;
88         }
89
90         gnutls_hmac(mac_ctx, data, 1024);
91
92         do {
93                 gnutls_hmac(mac_ctx, data, step);
94                 gnutls_cipher_encrypt2(ctx, data, step, data, step + 64);
95                 st.size += step;
96         }
97         while (benchmark_must_finish == 0);
98
99         gnutls_cipher_deinit(ctx);
100         gnutls_hmac_deinit(mac_ctx, NULL);
101
102         stop_benchmark(&st, NULL, 1);
103
104       leave:
105         free(_key);
106         free(_iv);
107 }
108
109
110 static void cipher_bench(int algo, int size, int aead)
111 {
112         int ret;
113         gnutls_cipher_hd_t ctx;
114         void *_key, *_iv;
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;
120
121         _key = malloc(keysize);
122         if (_key == NULL)
123                 return;
124         memset(_key, 0xf0, keysize);
125
126         _iv = malloc(ivsize);
127         if (_iv == NULL)
128                 return;
129         memset(_iv, 0xf0, ivsize);
130
131         iv.data = _iv;
132         if (aead)
133                 iv.size = 12;
134         else
135                 iv.size = ivsize;
136
137         key.data = _key;
138         key.size = keysize;
139
140         printf("%16s ", gnutls_cipher_get_name(algo));
141         fflush(stdout);
142
143         start_benchmark(&st);
144
145         ret = gnutls_cipher_init(&ctx, algo, &key, &iv);
146         if (ret < 0) {
147                 fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
148                 goto leave;
149         }
150
151         if (aead)
152                 gnutls_cipher_add_auth(ctx, data, 1024);
153
154         do {
155                 gnutls_cipher_encrypt2(ctx, data, step, data, step + 64);
156                 st.size += step;
157         }
158         while (benchmark_must_finish == 0);
159
160         gnutls_cipher_deinit(ctx);
161
162         stop_benchmark(&st, NULL, 1);
163
164       leave:
165         free(_key);
166         free(_iv);
167 }
168
169 static void mac_bench(int algo, int size)
170 {
171         void *_key;
172         int blocksize = gnutls_hmac_get_len(algo);
173         int step = size * 1024;
174         struct benchmark_st st;
175
176         _key = malloc(blocksize);
177         if (_key == NULL)
178                 return;
179         memset(_key, 0xf0, blocksize);
180
181         printf("%16s ", gnutls_mac_get_name(algo));
182         fflush(stdout);
183
184         start_benchmark(&st);
185
186         do {
187                 gnutls_hmac_fast(algo, _key, blocksize, data, step, _key);
188                 st.size += step;
189         }
190         while (benchmark_must_finish == 0);
191
192         stop_benchmark(&st, NULL, 1);
193
194         free(_key);
195 }
196
197 void benchmark_cipher(int debug_level)
198 {
199         int size = 16;
200         gnutls_global_set_log_function(tls_log_func);
201         gnutls_global_set_log_level(debug_level);
202
203         gnutls_rnd(GNUTLS_RND_NONCE, data, sizeof(data));
204
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,
209                          size);
210         cipher_bench(GNUTLS_CIPHER_AES_128_GCM, size, 1);
211
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);
216
217         printf("\nChecking ciphers, payload size: %u\n", size * 1024);
218         cipher_bench(GNUTLS_CIPHER_3DES_CBC, size, 0);
219
220         cipher_bench(GNUTLS_CIPHER_AES_128_CBC, size, 0);
221
222         cipher_bench(GNUTLS_CIPHER_ARCFOUR, size, 0);
223
224         cipher_bench(GNUTLS_CIPHER_SALSA20_256, size, 0);
225
226         gnutls_global_deinit();
227 }