Imported Debian version 2.4.3~trusty1
[deb_ffmpeg.git] / ffmpeg / tools / crypto_bench.c
CommitLineData
2ba45a60
DM
1/*
2 * Copyright (c) 2013 Nicolas George
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/* Optional external libraries; can be enabled using:
22 * make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench */
23#define USE_crypto 0x01 /* OpenSSL's libcrypto */
24#define USE_gcrypt 0x02 /* GnuTLS's libgcrypt */
25#define USE_tomcrypt 0x04 /* LibTomCrypt */
26
27#include <stdlib.h>
28#include <math.h>
29
30#include "libavutil/avutil.h"
31#include "libavutil/avstring.h"
32#include "libavutil/crc.h"
33#include "libavutil/intreadwrite.h"
34#include "libavutil/timer.h"
35
36#ifndef AV_READ_TIME
37#define AV_READ_TIME(x) 0
38#endif
39
40#if HAVE_UNISTD_H
41#include <unistd.h> /* for getopt */
42#endif
43#if !HAVE_GETOPT
44#include "compat/getopt.c"
45#endif
46
47#define MAX_INPUT_SIZE 1048576
48#define MAX_OUTPUT_SIZE 128
49
50static const char *enabled_libs;
51static const char *enabled_algos;
52static unsigned specified_runs;
53
54static const uint8_t *hardcoded_key = "FFmpeg is the best program ever.";
55
56static void fatal_error(const char *tag)
57{
58 av_log(NULL, AV_LOG_ERROR, "Fatal error: %s\n", tag);
59 exit(1);
60}
61
62struct hash_impl {
63 const char *lib;
64 const char *name;
65 void (*run)(uint8_t *output, const uint8_t *input, unsigned size);
66 const char *output;
67};
68
69/***************************************************************************
70 * lavu: libavutil
71 ***************************************************************************/
72
73#include "libavutil/md5.h"
74#include "libavutil/sha.h"
75#include "libavutil/sha512.h"
76#include "libavutil/ripemd.h"
77#include "libavutil/aes.h"
78
79#define IMPL_USE_lavu IMPL_USE
80
81static void run_lavu_md5(uint8_t *output,
82 const uint8_t *input, unsigned size)
83{
84 av_md5_sum(output, input, size);
85}
86
87#define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
88static void run_lavu_ ## suffix(uint8_t *output, \
89 const uint8_t *input, unsigned size) \
90{ \
91 static struct type *h; \
92 if (!h && !(h = av_ ## namespace ## _alloc())) \
93 fatal_error("out of memory"); \
94 av_ ## namespace ## _init(h, hsize); \
95 av_ ## namespace ## _update(h, input, size); \
96 av_ ## namespace ## _final(h, output); \
97}
98
99DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
100DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
101DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
102DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
103
104static void run_lavu_aes128(uint8_t *output,
105 const uint8_t *input, unsigned size)
106{
107 static struct AVAES *aes;
108 if (!aes && !(aes = av_aes_alloc()))
109 fatal_error("out of memory");
110 av_aes_init(aes, hardcoded_key, 128, 0);
111 av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
112}
113
114/***************************************************************************
115 * crypto: OpenSSL's libcrypto
116 ***************************************************************************/
117
118#if (USE_EXT_LIBS) & USE_crypto
119
120#include <openssl/md5.h>
121#include <openssl/sha.h>
122#include <openssl/ripemd.h>
123#include <openssl/aes.h>
124
125#define DEFINE_CRYPTO_WRAPPER(suffix, function) \
126static void run_crypto_ ## suffix(uint8_t *output, \
127 const uint8_t *input, unsigned size) \
128{ \
129 function(input, size, output); \
130}
131
132DEFINE_CRYPTO_WRAPPER(md5, MD5)
133DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
134DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
135DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
136DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
137
138static void run_crypto_aes128(uint8_t *output,
139 const uint8_t *input, unsigned size)
140{
141 AES_KEY aes;
142 unsigned i;
143
144 AES_set_encrypt_key(hardcoded_key, 128, &aes);
145 size -= 15;
146 for (i = 0; i < size; i += 16)
147 AES_encrypt(input + i, output + i, &aes);
148}
149
150#define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
151#else
152#define IMPL_USE_crypto(...) /* ignore */
153#endif
154
155/***************************************************************************
156 * gcrypt: GnuTLS's libgcrypt
157 ***************************************************************************/
158
159#if (USE_EXT_LIBS) & USE_gcrypt
160
161#include <gcrypt.h>
162
163#define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
164static void run_gcrypt_ ## suffix(uint8_t *output, \
165 const uint8_t *input, unsigned size) \
166{ \
167 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
168}
169
170DEFINE_GCRYPT_WRAPPER(md5, MD5)
171DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
172DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
173DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
174DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
175
176static void run_gcrypt_aes128(uint8_t *output,
177 const uint8_t *input, unsigned size)
178{
179 static gcry_cipher_hd_t aes;
180 if (!aes)
181 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
182 gcry_cipher_setkey(aes, hardcoded_key, 16);
183 gcry_cipher_encrypt(aes, output, size, input, size);
184}
185
186#define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
187#else
188#define IMPL_USE_gcrypt(...) /* ignore */
189#endif
190
191/***************************************************************************
192 * tomcrypt: LibTomCrypt
193 ***************************************************************************/
194
195#if (USE_EXT_LIBS) & USE_tomcrypt
196
197#include <tomcrypt.h>
198
199#define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
200static void run_tomcrypt_ ## suffix(uint8_t *output, \
201 const uint8_t *input, unsigned size) \
202{ \
203 hash_state md; \
204 namespace ## _init(&md); \
205 namespace ## _process(&md, input, size); \
206 namespace ## _done(&md, output); \
207}
208
209DEFINE_TOMCRYPT_WRAPPER(md5, md5, MD5)
210DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
211DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
212DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
213DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
214
215static void run_tomcrypt_aes128(uint8_t *output,
216 const uint8_t *input, unsigned size)
217{
218 symmetric_key aes;
219 unsigned i;
220
221 aes_setup(hardcoded_key, 16, 0, &aes);
222 size -= 15;
223 for (i = 0; i < size; i += 16)
224 aes_ecb_encrypt(input + i, output + i, &aes);
225}
226
227#define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
228#else
229#define IMPL_USE_tomcrypt(...) /* ignore */
230#endif
231
232/***************************************************************************
233 * Driver code
234 ***************************************************************************/
235
236static unsigned crc32(const uint8_t *data, unsigned size)
237{
238 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
239}
240
241static void run_implementation(const uint8_t *input, uint8_t *output,
242 struct hash_impl *impl, unsigned size)
243{
244 uint64_t t0, t1;
245 unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
246 unsigned outlen = 0, outcrc = 0;
247 unsigned i, j, val;
248 double mtime, ttime = 0, ttime2 = 0, stime;
249 uint8_t outref[MAX_OUTPUT_SIZE];
250
251 if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
252 enabled_algos && !av_stristr(enabled_algos, impl->name))
253 return;
254 if (!sscanf(impl->output, "crc:%x", &outcrc)) {
255 outlen = strlen(impl->output) / 2;
256 for (i = 0; i < outlen; i++) {
257 sscanf(impl->output + i * 2, "%02x", &val);
258 outref[i] = val;
259 }
260 }
261 for (i = 0; i < 8; i++) /* heat caches */
262 impl->run(output, input, size);
263 for (i = 0; i < nruns; i++) {
264 memset(output, 0, size); /* avoid leftovers from previous runs */
265 t0 = AV_READ_TIME();
266 impl->run(output, input, size);
267 t1 = AV_READ_TIME();
268 if (outlen ? memcmp(output, outref, outlen) :
269 crc32(output, size) != outcrc) {
270 fprintf(stderr, "Expected: ");
271 if (outlen)
272 for (j = 0; j < outlen; j++)
273 fprintf(stderr, "%02x", output[j]);
274 else
275 fprintf(stderr, "%08x", crc32(output, size));
276 fprintf(stderr, "\n");
277 fatal_error("output mismatch");
278 }
279 mtime = (double)(t1 - t0) / size;
280 ttime += mtime;
281 ttime2 += mtime * mtime;
282 }
283
284 ttime /= nruns;
285 ttime2 /= nruns;
286 stime = sqrt(ttime2 - ttime * ttime);
287 printf("%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
288 impl->lib, impl->name, size, nruns, ttime, stime);
289 fflush(stdout);
290}
291
292#define IMPL_USE(lib, name, symbol, output) \
293 { #lib, name, run_ ## lib ## _ ## symbol, output },
294#define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
295#define IMPL_ALL(...) \
296 IMPL(lavu, __VA_ARGS__) \
297 IMPL(crypto, __VA_ARGS__) \
298 IMPL(gcrypt, __VA_ARGS__) \
299 IMPL(tomcrypt, __VA_ARGS__)
300
301struct hash_impl implementations[] = {
302 IMPL_ALL("MD5", md5, "aa26ff5b895356bcffd9292ba9f89e66")
303 IMPL_ALL("SHA-1", sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
304 IMPL_ALL("SHA-256", sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
305 IMPL_ALL("SHA-512", sha512, "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
306 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
307 IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
308 IMPL_ALL("AES-128", aes128, "crc:ff6bc888")
309};
310
311int main(int argc, char **argv)
312{
313 uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
314 uint8_t *output = input + MAX_INPUT_SIZE;
315 unsigned i, impl, size;
316 int opt;
317
318 while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
319 switch (opt) {
320 case 'l':
321 enabled_libs = optarg;
322 break;
323 case 'a':
324 enabled_algos = optarg;
325 break;
326 case 'r':
327 specified_runs = strtol(optarg, NULL, 0);
328 break;
329 case 'h':
330 default:
331 fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
332 argv[0]);
333 if ((USE_EXT_LIBS)) {
334 char buf[1024];
335 snprintf(buf, sizeof(buf), "%s%s%s",
336 ((USE_EXT_LIBS) & USE_crypto) ? "+crypto" : "",
337 ((USE_EXT_LIBS) & USE_gcrypt) ? "+gcrypt" : "",
338 ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
339 fprintf(stderr, "Built with the following external libraries:\n"
340 "make VERSUS=%s\n", buf + 1);
341 } else {
342 fprintf(stderr, "Built without external libraries; use\n"
343 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
344 "to enable them.\n");
345 }
346 exit(opt != 'h');
347 }
348 }
349
350 if (!input)
351 fatal_error("out of memory");
352 for (i = 0; i < MAX_INPUT_SIZE; i += 4)
353 AV_WB32(input + i, i);
354
355 size = MAX_INPUT_SIZE;
356 for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
357 run_implementation(input, output, &implementations[impl], size);
358
359 av_free(input);
360
361 return 0;
362}