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