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