Imported Debian version 2.5.0~trusty1.1
[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"
f6fa7814 78#include "libavutil/cast5.h"
2ba45a60
DM
79
80#define IMPL_USE_lavu IMPL_USE
81
82static void run_lavu_md5(uint8_t *output,
83 const uint8_t *input, unsigned size)
84{
85 av_md5_sum(output, input, size);
86}
87
88#define DEFINE_LAVU_MD(suffix, type, namespace, hsize) \
89static void run_lavu_ ## suffix(uint8_t *output, \
90 const uint8_t *input, unsigned size) \
91{ \
92 static struct type *h; \
93 if (!h && !(h = av_ ## namespace ## _alloc())) \
94 fatal_error("out of memory"); \
95 av_ ## namespace ## _init(h, hsize); \
96 av_ ## namespace ## _update(h, input, size); \
97 av_ ## namespace ## _final(h, output); \
98}
99
100DEFINE_LAVU_MD(sha1, AVSHA, sha, 160);
101DEFINE_LAVU_MD(sha256, AVSHA, sha, 256);
102DEFINE_LAVU_MD(sha512, AVSHA512, sha512, 512);
103DEFINE_LAVU_MD(ripemd160, AVRIPEMD, ripemd, 160);
104
105static void run_lavu_aes128(uint8_t *output,
106 const uint8_t *input, unsigned size)
107{
108 static struct AVAES *aes;
109 if (!aes && !(aes = av_aes_alloc()))
110 fatal_error("out of memory");
111 av_aes_init(aes, hardcoded_key, 128, 0);
112 av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
113}
114
f6fa7814
DM
115static void run_lavu_cast128(uint8_t *output,
116 const uint8_t *input, unsigned size)
117{
118 static struct AVCAST5 *cast;
119 if (!cast && !(cast = av_cast5_alloc()))
120 fatal_error("out of memory");
121 av_cast5_init(cast, hardcoded_key, 128);
122 av_cast5_crypt(cast, output, input, size >> 3, 0);
123}
124
2ba45a60
DM
125/***************************************************************************
126 * crypto: OpenSSL's libcrypto
127 ***************************************************************************/
128
129#if (USE_EXT_LIBS) & USE_crypto
130
131#include <openssl/md5.h>
132#include <openssl/sha.h>
133#include <openssl/ripemd.h>
134#include <openssl/aes.h>
f6fa7814 135#include <openssl/cast.h>
2ba45a60
DM
136
137#define DEFINE_CRYPTO_WRAPPER(suffix, function) \
138static void run_crypto_ ## suffix(uint8_t *output, \
139 const uint8_t *input, unsigned size) \
140{ \
141 function(input, size, output); \
142}
143
144DEFINE_CRYPTO_WRAPPER(md5, MD5)
145DEFINE_CRYPTO_WRAPPER(sha1, SHA1)
146DEFINE_CRYPTO_WRAPPER(sha256, SHA256)
147DEFINE_CRYPTO_WRAPPER(sha512, SHA512)
148DEFINE_CRYPTO_WRAPPER(ripemd160, RIPEMD160)
149
150static void run_crypto_aes128(uint8_t *output,
151 const uint8_t *input, unsigned size)
152{
153 AES_KEY aes;
154 unsigned i;
155
156 AES_set_encrypt_key(hardcoded_key, 128, &aes);
157 size -= 15;
158 for (i = 0; i < size; i += 16)
159 AES_encrypt(input + i, output + i, &aes);
160}
161
f6fa7814
DM
162static void run_crypto_cast128(uint8_t *output,
163 const uint8_t *input, unsigned size)
164{
165 CAST_KEY cast;
166 unsigned i;
167
168 CAST_set_key(&cast, 16, hardcoded_key);
169 for (i = 0; i < size; i += 8)
170 CAST_ecb_encrypt(input + i, output + i, &cast, 1);
171}
172
2ba45a60
DM
173#define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
174#else
175#define IMPL_USE_crypto(...) /* ignore */
176#endif
177
178/***************************************************************************
179 * gcrypt: GnuTLS's libgcrypt
180 ***************************************************************************/
181
182#if (USE_EXT_LIBS) & USE_gcrypt
183
184#include <gcrypt.h>
185
186#define DEFINE_GCRYPT_WRAPPER(suffix, algo) \
187static void run_gcrypt_ ## suffix(uint8_t *output, \
188 const uint8_t *input, unsigned size) \
189{ \
190 gcry_md_hash_buffer(GCRY_MD_ ## algo, output, input, size); \
191}
192
193DEFINE_GCRYPT_WRAPPER(md5, MD5)
194DEFINE_GCRYPT_WRAPPER(sha1, SHA1)
195DEFINE_GCRYPT_WRAPPER(sha256, SHA256)
196DEFINE_GCRYPT_WRAPPER(sha512, SHA512)
197DEFINE_GCRYPT_WRAPPER(ripemd160, RMD160)
198
199static void run_gcrypt_aes128(uint8_t *output,
200 const uint8_t *input, unsigned size)
201{
202 static gcry_cipher_hd_t aes;
203 if (!aes)
204 gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
205 gcry_cipher_setkey(aes, hardcoded_key, 16);
206 gcry_cipher_encrypt(aes, output, size, input, size);
207}
208
f6fa7814
DM
209static void run_gcrypt_cast128(uint8_t *output,
210 const uint8_t *input, unsigned size)
211{
212 static gcry_cipher_hd_t cast;
213 if (!cast)
214 gcry_cipher_open(&cast, GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_ECB, 0);
215 gcry_cipher_setkey(cast, hardcoded_key, 16);
216 gcry_cipher_encrypt(cast, output, size, input, size);
217}
218
2ba45a60
DM
219#define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
220#else
221#define IMPL_USE_gcrypt(...) /* ignore */
222#endif
223
224/***************************************************************************
225 * tomcrypt: LibTomCrypt
226 ***************************************************************************/
227
228#if (USE_EXT_LIBS) & USE_tomcrypt
229
230#include <tomcrypt.h>
231
232#define DEFINE_TOMCRYPT_WRAPPER(suffix, namespace, algo) \
233static void run_tomcrypt_ ## suffix(uint8_t *output, \
234 const uint8_t *input, unsigned size) \
235{ \
236 hash_state md; \
237 namespace ## _init(&md); \
238 namespace ## _process(&md, input, size); \
239 namespace ## _done(&md, output); \
240}
241
242DEFINE_TOMCRYPT_WRAPPER(md5, md5, MD5)
243DEFINE_TOMCRYPT_WRAPPER(sha1, sha1, SHA1)
244DEFINE_TOMCRYPT_WRAPPER(sha256, sha256, SHA256)
245DEFINE_TOMCRYPT_WRAPPER(sha512, sha512, SHA512)
246DEFINE_TOMCRYPT_WRAPPER(ripemd160, rmd160, RIPEMD160)
247
248static void run_tomcrypt_aes128(uint8_t *output,
249 const uint8_t *input, unsigned size)
250{
251 symmetric_key aes;
252 unsigned i;
253
254 aes_setup(hardcoded_key, 16, 0, &aes);
255 size -= 15;
256 for (i = 0; i < size; i += 16)
257 aes_ecb_encrypt(input + i, output + i, &aes);
258}
259
f6fa7814
DM
260static void run_tomcrypt_cast128(uint8_t *output,
261 const uint8_t *input, unsigned size)
262{
263 symmetric_key cast;
264 unsigned i;
265
266 cast5_setup(hardcoded_key, 16, 0, &cast);
267 for (i = 0; i < size; i += 8)
268 cast5_ecb_encrypt(input + i, output + i, &cast);
269}
270
2ba45a60
DM
271#define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
272#else
273#define IMPL_USE_tomcrypt(...) /* ignore */
274#endif
275
276/***************************************************************************
277 * Driver code
278 ***************************************************************************/
279
280static unsigned crc32(const uint8_t *data, unsigned size)
281{
282 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, data, size);
283}
284
285static void run_implementation(const uint8_t *input, uint8_t *output,
286 struct hash_impl *impl, unsigned size)
287{
288 uint64_t t0, t1;
289 unsigned nruns = specified_runs ? specified_runs : (1 << 30) / size;
290 unsigned outlen = 0, outcrc = 0;
291 unsigned i, j, val;
292 double mtime, ttime = 0, ttime2 = 0, stime;
293 uint8_t outref[MAX_OUTPUT_SIZE];
294
295 if (enabled_libs && !av_stristr(enabled_libs, impl->lib) ||
296 enabled_algos && !av_stristr(enabled_algos, impl->name))
297 return;
298 if (!sscanf(impl->output, "crc:%x", &outcrc)) {
299 outlen = strlen(impl->output) / 2;
300 for (i = 0; i < outlen; i++) {
301 sscanf(impl->output + i * 2, "%02x", &val);
302 outref[i] = val;
303 }
304 }
305 for (i = 0; i < 8; i++) /* heat caches */
306 impl->run(output, input, size);
307 for (i = 0; i < nruns; i++) {
308 memset(output, 0, size); /* avoid leftovers from previous runs */
309 t0 = AV_READ_TIME();
310 impl->run(output, input, size);
311 t1 = AV_READ_TIME();
312 if (outlen ? memcmp(output, outref, outlen) :
313 crc32(output, size) != outcrc) {
314 fprintf(stderr, "Expected: ");
315 if (outlen)
316 for (j = 0; j < outlen; j++)
317 fprintf(stderr, "%02x", output[j]);
318 else
319 fprintf(stderr, "%08x", crc32(output, size));
320 fprintf(stderr, "\n");
321 fatal_error("output mismatch");
322 }
323 mtime = (double)(t1 - t0) / size;
324 ttime += mtime;
325 ttime2 += mtime * mtime;
326 }
327
328 ttime /= nruns;
329 ttime2 /= nruns;
330 stime = sqrt(ttime2 - ttime * ttime);
331 printf("%-10s %-12s size: %7d runs: %6d time: %8.3f +- %.3f\n",
332 impl->lib, impl->name, size, nruns, ttime, stime);
333 fflush(stdout);
334}
335
336#define IMPL_USE(lib, name, symbol, output) \
337 { #lib, name, run_ ## lib ## _ ## symbol, output },
338#define IMPL(lib, ...) IMPL_USE_ ## lib(lib, __VA_ARGS__)
339#define IMPL_ALL(...) \
340 IMPL(lavu, __VA_ARGS__) \
341 IMPL(crypto, __VA_ARGS__) \
342 IMPL(gcrypt, __VA_ARGS__) \
343 IMPL(tomcrypt, __VA_ARGS__)
344
345struct hash_impl implementations[] = {
346 IMPL_ALL("MD5", md5, "aa26ff5b895356bcffd9292ba9f89e66")
347 IMPL_ALL("SHA-1", sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744")
348 IMPL_ALL("SHA-256", sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5")
349 IMPL_ALL("SHA-512", sha512, "3afdd44a80d99af15c87bd724cb717243193767835ce866dd5d58c02d674bb57"
350 "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
351 IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
352 IMPL_ALL("AES-128", aes128, "crc:ff6bc888")
f6fa7814 353 IMPL_ALL("CAST-128", cast128, "crc:456aa584")
2ba45a60
DM
354};
355
356int main(int argc, char **argv)
357{
358 uint8_t *input = av_malloc(MAX_INPUT_SIZE * 2);
359 uint8_t *output = input + MAX_INPUT_SIZE;
360 unsigned i, impl, size;
361 int opt;
362
363 while ((opt = getopt(argc, argv, "hl:a:r:")) != -1) {
364 switch (opt) {
365 case 'l':
366 enabled_libs = optarg;
367 break;
368 case 'a':
369 enabled_algos = optarg;
370 break;
371 case 'r':
372 specified_runs = strtol(optarg, NULL, 0);
373 break;
374 case 'h':
375 default:
376 fprintf(stderr, "Usage: %s [-l libs] [-a algos] [-r runs]\n",
377 argv[0]);
378 if ((USE_EXT_LIBS)) {
379 char buf[1024];
380 snprintf(buf, sizeof(buf), "%s%s%s",
381 ((USE_EXT_LIBS) & USE_crypto) ? "+crypto" : "",
382 ((USE_EXT_LIBS) & USE_gcrypt) ? "+gcrypt" : "",
383 ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
384 fprintf(stderr, "Built with the following external libraries:\n"
385 "make VERSUS=%s\n", buf + 1);
386 } else {
387 fprintf(stderr, "Built without external libraries; use\n"
388 "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
389 "to enable them.\n");
390 }
391 exit(opt != 'h');
392 }
393 }
394
395 if (!input)
396 fatal_error("out of memory");
397 for (i = 0; i < MAX_INPUT_SIZE; i += 4)
398 AV_WB32(input + i, i);
399
400 size = MAX_INPUT_SIZE;
401 for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
402 run_implementation(input, output, &implementations[impl], size);
403
404 av_free(input);
405
406 return 0;
407}