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