crypto_bench: add support for xtea
[ffmpeg.git] / tools / crypto_bench.c
index 6037ead..fb749ac 100644 (file)
@@ -75,7 +75,12 @@ struct hash_impl {
 #include "libavutil/sha512.h"
 #include "libavutil/ripemd.h"
 #include "libavutil/aes.h"
 #include "libavutil/sha512.h"
 #include "libavutil/ripemd.h"
 #include "libavutil/aes.h"
+#include "libavutil/blowfish.h"
+#include "libavutil/camellia.h"
 #include "libavutil/cast5.h"
 #include "libavutil/cast5.h"
+#include "libavutil/twofish.h"
+#include "libavutil/rc4.h"
+#include "libavutil/xtea.h"
 
 #define IMPL_USE_lavu IMPL_USE
 
 
 #define IMPL_USE_lavu IMPL_USE
 
@@ -112,6 +117,26 @@ static void run_lavu_aes128(uint8_t *output,
     av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
 }
 
     av_aes_crypt(aes, output, input, size >> 4, NULL, 0);
 }
 
+static void run_lavu_blowfish(uint8_t *output,
+                              const uint8_t *input, unsigned size)
+{
+    static struct AVBlowfish *blowfish;
+    if (!blowfish && !(blowfish = av_blowfish_alloc()))
+        fatal_error("out of memory");
+    av_blowfish_init(blowfish, hardcoded_key, 16);
+    av_blowfish_crypt(blowfish, output, input, size >> 3, NULL, 0);
+}
+
+static void run_lavu_camellia(uint8_t *output,
+                              const uint8_t *input, unsigned size)
+{
+    static struct AVCAMELLIA *camellia;
+    if (!camellia && !(camellia = av_camellia_alloc()))
+        fatal_error("out of memory");
+    av_camellia_init(camellia, hardcoded_key, 128);
+    av_camellia_crypt(camellia, output, input, size >> 4, NULL, 0);
+}
+
 static void run_lavu_cast128(uint8_t *output,
                              const uint8_t *input, unsigned size)
 {
 static void run_lavu_cast128(uint8_t *output,
                              const uint8_t *input, unsigned size)
 {
@@ -122,6 +147,36 @@ static void run_lavu_cast128(uint8_t *output,
     av_cast5_crypt(cast, output, input, size >> 3, 0);
 }
 
     av_cast5_crypt(cast, output, input, size >> 3, 0);
 }
 
+static void run_lavu_twofish(uint8_t *output,
+                              const uint8_t *input, unsigned size)
+{
+    static struct AVTWOFISH *twofish;
+    if (!twofish && !(twofish = av_twofish_alloc()))
+        fatal_error("out of memory");
+    av_twofish_init(twofish, hardcoded_key, 128);
+    av_twofish_crypt(twofish, output, input, size >> 4, NULL, 0);
+}
+
+static void run_lavu_rc4(uint8_t *output,
+                              const uint8_t *input, unsigned size)
+{
+    static struct AVRC4 *rc4;
+    if (!rc4 && !(rc4 = av_rc4_alloc()))
+        fatal_error("out of memory");
+    av_rc4_init(rc4, hardcoded_key, 128, 0);
+    av_rc4_crypt(rc4, output, input, size, NULL, 0);
+}
+
+static void run_lavu_xtea(uint8_t *output,
+                              const uint8_t *input, unsigned size)
+{
+    static struct AVXTEA *xtea;
+    if (!xtea && !(xtea = av_xtea_alloc()))
+        fatal_error("out of memory");
+    av_xtea_init(xtea, hardcoded_key);
+    av_xtea_crypt(xtea, output, input, size >> 3, NULL, 0);
+}
+
 /***************************************************************************
  * crypto: OpenSSL's libcrypto
  ***************************************************************************/
 /***************************************************************************
  * crypto: OpenSSL's libcrypto
  ***************************************************************************/
@@ -132,7 +187,10 @@ static void run_lavu_cast128(uint8_t *output,
 #include <openssl/sha.h>
 #include <openssl/ripemd.h>
 #include <openssl/aes.h>
 #include <openssl/sha.h>
 #include <openssl/ripemd.h>
 #include <openssl/aes.h>
+#include <openssl/blowfish.h>
+#include <openssl/camellia.h>
 #include <openssl/cast.h>
 #include <openssl/cast.h>
+#include <openssl/rc4.h>
 
 #define DEFINE_CRYPTO_WRAPPER(suffix, function)                              \
 static void run_crypto_ ## suffix(uint8_t *output,                           \
 
 #define DEFINE_CRYPTO_WRAPPER(suffix, function)                              \
 static void run_crypto_ ## suffix(uint8_t *output,                           \
@@ -159,6 +217,29 @@ static void run_crypto_aes128(uint8_t *output,
         AES_encrypt(input + i, output + i, &aes);
 }
 
         AES_encrypt(input + i, output + i, &aes);
 }
 
+static void run_crypto_blowfish(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    BF_KEY blowfish;
+    unsigned i;
+
+    BF_set_key(&blowfish, 16, hardcoded_key);
+    for (i = 0; i < size; i += 8)
+        BF_ecb_encrypt(input + i, output + i, &blowfish, 1);
+}
+
+static void run_crypto_camellia(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    CAMELLIA_KEY camellia;
+    unsigned i;
+
+    Camellia_set_key(hardcoded_key, 128, &camellia);
+    size -= 15;
+    for (i = 0; i < size; i += 16)
+        Camellia_ecb_encrypt(input + i, output + i, &camellia, 1);
+}
+
 static void run_crypto_cast128(uint8_t *output,
                                const uint8_t *input, unsigned size)
 {
 static void run_crypto_cast128(uint8_t *output,
                                const uint8_t *input, unsigned size)
 {
@@ -170,6 +251,15 @@ static void run_crypto_cast128(uint8_t *output,
         CAST_ecb_encrypt(input + i, output + i, &cast, 1);
 }
 
         CAST_ecb_encrypt(input + i, output + i, &cast, 1);
 }
 
+static void run_crypto_rc4(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    RC4_KEY rc4;
+
+    RC4_set_key(&rc4, 16, hardcoded_key);
+    RC4(&rc4, size, input, output);
+}
+
 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
 #else
 #define IMPL_USE_crypto(...) /* ignore */
 #define IMPL_USE_crypto(...) IMPL_USE(__VA_ARGS__)
 #else
 #define IMPL_USE_crypto(...) /* ignore */
@@ -206,6 +296,26 @@ static void run_gcrypt_aes128(uint8_t *output,
     gcry_cipher_encrypt(aes, output, size, input, size);
 }
 
     gcry_cipher_encrypt(aes, output, size, input, size);
 }
 
+static void run_gcrypt_blowfish(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    static gcry_cipher_hd_t blowfish;
+    if (!blowfish)
+        gcry_cipher_open(&blowfish, GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_ECB, 0);
+    gcry_cipher_setkey(blowfish, hardcoded_key, 16);
+    gcry_cipher_encrypt(blowfish, output, size, input, size);
+}
+
+static void run_gcrypt_camellia(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    static gcry_cipher_hd_t camellia;
+    if (!camellia)
+        gcry_cipher_open(&camellia, GCRY_CIPHER_CAMELLIA128, GCRY_CIPHER_MODE_ECB, 0);
+    gcry_cipher_setkey(camellia, hardcoded_key, 16);
+    gcry_cipher_encrypt(camellia, output, size, input, size);
+}
+
 static void run_gcrypt_cast128(uint8_t *output,
                               const uint8_t *input, unsigned size)
 {
 static void run_gcrypt_cast128(uint8_t *output,
                               const uint8_t *input, unsigned size)
 {
@@ -216,6 +326,16 @@ static void run_gcrypt_cast128(uint8_t *output,
     gcry_cipher_encrypt(cast, output, size, input, size);
 }
 
     gcry_cipher_encrypt(cast, output, size, input, size);
 }
 
+static void run_gcrypt_twofish(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    static gcry_cipher_hd_t twofish;
+    if (!twofish)
+        gcry_cipher_open(&twofish, GCRY_CIPHER_TWOFISH128, GCRY_CIPHER_MODE_ECB, 0);
+    gcry_cipher_setkey(twofish, hardcoded_key, 16);
+    gcry_cipher_encrypt(twofish, output, size, input, size);
+}
+
 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
 #else
 #define IMPL_USE_gcrypt(...) /* ignore */
 #define IMPL_USE_gcrypt(...) IMPL_USE(__VA_ARGS__)
 #else
 #define IMPL_USE_gcrypt(...) /* ignore */
@@ -257,6 +377,29 @@ static void run_tomcrypt_aes128(uint8_t *output,
         aes_ecb_encrypt(input + i, output + i, &aes);
 }
 
         aes_ecb_encrypt(input + i, output + i, &aes);
 }
 
+static void run_tomcrypt_blowfish(uint8_t *output,
+                                  const uint8_t *input, unsigned size)
+{
+    symmetric_key blowfish;
+    unsigned i;
+
+    blowfish_setup(hardcoded_key, 16, 0, &blowfish);
+    for (i = 0; i < size; i += 8)
+        blowfish_ecb_encrypt(input + i, output + i, &blowfish);
+}
+
+static void run_tomcrypt_camellia(uint8_t *output,
+                                  const uint8_t *input, unsigned size)
+{
+    symmetric_key camellia;
+    unsigned i;
+
+    camellia_setup(hardcoded_key, 16, 0, &camellia);
+    size -= 15;
+    for (i = 0; i < size; i += 16)
+        camellia_ecb_encrypt(input + i, output + i, &camellia);
+}
+
 static void run_tomcrypt_cast128(uint8_t *output,
                                 const uint8_t *input, unsigned size)
 {
 static void run_tomcrypt_cast128(uint8_t *output,
                                 const uint8_t *input, unsigned size)
 {
@@ -268,6 +411,30 @@ static void run_tomcrypt_cast128(uint8_t *output,
         cast5_ecb_encrypt(input + i, output + i, &cast);
 }
 
         cast5_ecb_encrypt(input + i, output + i, &cast);
 }
 
+static void run_tomcrypt_twofish(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    symmetric_key twofish;
+    unsigned i;
+
+    twofish_setup(hardcoded_key, 16, 0, &twofish);
+    size -= 15;
+    for (i = 0; i < size; i += 16)
+        twofish_ecb_encrypt(input + i, output + i, &twofish);
+}
+
+static void run_tomcrypt_xtea(uint8_t *output,
+                              const uint8_t *input, unsigned size)
+{
+    symmetric_key xtea;
+    unsigned i;
+
+    xtea_setup(hardcoded_key, 16, 0, &xtea);
+    for (i = 0; i < size; i += 8)
+        xtea_ecb_encrypt(input + i, output + i, &xtea);
+}
+
+
 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
 #else
 #define IMPL_USE_tomcrypt(...) /* ignore */
 #define IMPL_USE_tomcrypt(...) IMPL_USE(__VA_ARGS__)
 #else
 #define IMPL_USE_tomcrypt(...) /* ignore */
@@ -350,7 +517,16 @@ struct hash_impl implementations[] = {
                                       "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
     IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
     IMPL_ALL("AES-128",    aes128,    "crc:ff6bc888")
                                       "7c25b9e118c200a189fcd5a01ef106a4e200061f3e97dbf50ba065745fd46bef")
     IMPL_ALL("RIPEMD-160", ripemd160, "62a5321e4fc8784903bb43ab7752c75f8b25af00")
     IMPL_ALL("AES-128",    aes128,    "crc:ff6bc888")
+    IMPL_ALL("CAMELLIA",   camellia,  "crc:7abb59a7")
     IMPL_ALL("CAST-128",   cast128,   "crc:456aa584")
     IMPL_ALL("CAST-128",   cast128,   "crc:456aa584")
+    IMPL_ALL("BLOWFISH",   blowfish,  "crc:33e8aa74")
+    IMPL(lavu,     "TWOFISH", twofish, "crc:9edbd5c1")
+    IMPL(gcrypt,   "TWOFISH", twofish, "crc:9edbd5c1")
+    IMPL(tomcrypt, "TWOFISH", twofish, "crc:9edbd5c1")
+    IMPL(lavu,     "RC4",     rc4,     "crc:538d37b2")
+    IMPL(crypto,   "RC4",     rc4,     "crc:538d37b2")
+    IMPL(lavu,     "XTEA",    xtea,    "crc:931fc270")
+    IMPL(tomcrypt, "XTEA",    xtea,    "crc:931fc270")
 };
 
 int main(int argc, char **argv)
 };
 
 int main(int argc, char **argv)