[FFmpeg-devel] [PATCH] tools/crypto_bench: add support for mbedcrypto

James Almer jamrial at gmail.com
Mon May 28 00:25:06 EEST 2018


Requires mbed TLS 2.7.0 or newer

Signed-off-by: James Almer <jamrial at gmail.com>
---
 tools/crypto_bench.c | 111 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 108 insertions(+), 3 deletions(-)

diff --git a/tools/crypto_bench.c b/tools/crypto_bench.c
index 203bffe198..29e7eeec6b 100644
--- a/tools/crypto_bench.c
+++ b/tools/crypto_bench.c
@@ -23,6 +23,7 @@
 #define USE_crypto           0x01    /* OpenSSL's libcrypto */
 #define USE_gcrypt           0x02    /* GnuTLS's libgcrypt */
 #define USE_tomcrypt         0x04    /* LibTomCrypt */
+#define USE_mbedcrypto       0x08    /* mbed TLS */
 
 #include <stdlib.h>
 #include <math.h>
@@ -445,6 +446,104 @@ static void run_tomcrypt_xtea(uint8_t *output,
 #define IMPL_USE_tomcrypt(...) /* ignore */
 #endif
 
+/***************************************************************************
+ * mbedcrypto: mbed TLS
+ ***************************************************************************/
+
+#if (USE_EXT_LIBS) & USE_mbedcrypto
+
+#include <mbedtls/aes.h>
+#include <mbedtls/blowfish.h>
+#include <mbedtls/camellia.h>
+#include <mbedtls/des.h>
+#include <mbedtls/md5.h>
+#include <mbedtls/ripemd160.h>
+#include <mbedtls/sha1.h>
+#include <mbedtls/sha256.h>
+#include <mbedtls/sha512.h>
+#include <mbedtls/xtea.h>
+
+#define DEFINE_MBEDCRYPTO_WRAPPER(suffix)                                  \
+static void run_mbedcrypto_ ## suffix(uint8_t *output,                     \
+                                      const uint8_t *input, unsigned size) \
+{                                                                          \
+    mbedtls_ ## suffix ## _ret(input, size, output);                       \
+}
+
+#define DEFINE_MBEDCRYPTO_WRAPPER_SHA2(suffix)                             \
+static void run_mbedcrypto_ ## suffix(uint8_t *output,                     \
+                                      const uint8_t *input, unsigned size) \
+{                                                                          \
+    mbedtls_ ## suffix ## _ret(input, size, output, 0);                    \
+}
+
+DEFINE_MBEDCRYPTO_WRAPPER(md5)
+DEFINE_MBEDCRYPTO_WRAPPER(ripemd160)
+DEFINE_MBEDCRYPTO_WRAPPER(sha1)
+DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha256)
+DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha512)
+
+
+#define DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(suffix, cypher, algo)                  \
+static void run_mbedcrypto_ ## suffix(uint8_t *output,                          \
+                                      const uint8_t *input, unsigned size)      \
+{                                                                               \
+    mbedtls_ ## cypher ## _context cypher;                                      \
+                                                                                \
+    mbedtls_ ## cypher ## _init(&cypher);                                       \
+    mbedtls_ ## cypher ## _setkey_enc(&cypher, hardcoded_key, 128);             \
+    for (int i = 0; i < size; i += 16)                                          \
+        mbedtls_ ## cypher ## _crypt_ecb(&cypher, MBEDTLS_ ## algo ## _ENCRYPT, \
+                                         input + i, output + i);                \
+    mbedtls_ ## cypher ## _free(&cypher);                                       \
+}
+
+DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(aes128, aes, AES)
+DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(camellia, camellia, CAMELLIA)
+
+static void run_mbedcrypto_blowfish(uint8_t *output,
+                                    const uint8_t *input, unsigned size)
+{
+    mbedtls_blowfish_context blowfish;
+
+    mbedtls_blowfish_init(&blowfish);
+    mbedtls_blowfish_setkey(&blowfish, hardcoded_key, 128);
+    for (int i = 0; i < size; i += 8)
+        mbedtls_blowfish_crypt_ecb(&blowfish, MBEDTLS_BLOWFISH_ENCRYPT,
+                                   input + i, output + i);
+    mbedtls_blowfish_free(&blowfish);
+}
+
+static void run_mbedcrypto_des(uint8_t *output,
+                               const uint8_t *input, unsigned size)
+{
+    mbedtls_des_context des;
+
+    mbedtls_des_init(&des);
+    mbedtls_des_setkey_enc(&des, hardcoded_key);
+    for (int i = 0; i < size; i += 8)
+        mbedtls_des_crypt_ecb(&des, input + i, output + i);
+    mbedtls_des_free(&des);
+}
+
+static void run_mbedcrypto_xtea(uint8_t *output,
+                                const uint8_t *input, unsigned size)
+{
+    mbedtls_xtea_context xtea;
+
+    mbedtls_xtea_init(&xtea);
+    mbedtls_xtea_setup(&xtea, hardcoded_key);
+    for (int i = 0; i < size; i += 8)
+        mbedtls_xtea_crypt_ecb(&xtea, MBEDTLS_XTEA_ENCRYPT,
+                               input + i, output + i);
+    mbedtls_xtea_free(&xtea);
+}
+
+#define IMPL_USE_mbedcrypto(...) IMPL_USE(__VA_ARGS__)
+#else
+#define IMPL_USE_mbedcrypto(...) /* ignore */
+#endif
+
 /***************************************************************************
  * Driver code
  ***************************************************************************/
@@ -512,6 +611,7 @@ static void run_implementation(const uint8_t *input, uint8_t *output,
     IMPL(lavu,       __VA_ARGS__) \
     IMPL(crypto,     __VA_ARGS__) \
     IMPL(gcrypt,     __VA_ARGS__) \
+    IMPL(mbedcrypto, __VA_ARGS__) \
     IMPL(tomcrypt,   __VA_ARGS__)
 
 struct hash_impl implementations[] = {
@@ -525,7 +625,10 @@ struct hash_impl implementations[] = {
     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(lavu,     "CAST-128", cast128, "crc:456aa584")
+    IMPL(crypto,   "CAST-128", cast128, "crc:456aa584")
+    IMPL(gcrypt,   "CAST-128", cast128, "crc:456aa584")
+    IMPL(tomcrypt, "CAST-128", cast128, "crc:456aa584")
     IMPL_ALL("BLOWFISH",   blowfish,  "crc:33e8aa74")
     IMPL_ALL("DES",        des,       "crc:31291e0b")
     IMPL(lavu,     "TWOFISH", twofish, "crc:9edbd5c1")
@@ -534,6 +637,7 @@ struct hash_impl implementations[] = {
     IMPL(lavu,     "RC4",     rc4,     "crc:538d37b2")
     IMPL(crypto,   "RC4",     rc4,     "crc:538d37b2")
     IMPL(lavu,     "XTEA",    xtea,    "crc:931fc270")
+    IMPL(mbedcrypto, "XTEA",  xtea,    "crc:931fc270")
     IMPL(tomcrypt, "XTEA",    xtea,    "crc:931fc270")
 };
 
@@ -561,15 +665,16 @@ int main(int argc, char **argv)
                     argv[0]);
             if ((USE_EXT_LIBS)) {
                 char buf[1024];
-                snprintf(buf, sizeof(buf), "%s%s%s",
+                snprintf(buf, sizeof(buf), "%s%s%s%s",
                          ((USE_EXT_LIBS) & USE_crypto)   ? "+crypto"   : "",
                          ((USE_EXT_LIBS) & USE_gcrypt)   ? "+gcrypt"   : "",
+                         ((USE_EXT_LIBS) & USE_mbedcrypto) ? "+mbedcrypto" : "",
                          ((USE_EXT_LIBS) & USE_tomcrypt) ? "+tomcrypt" : "");
                 fprintf(stderr, "Built with the following external libraries:\n"
                         "make VERSUS=%s\n", buf + 1);
             } else {
                 fprintf(stderr, "Built without external libraries; use\n"
-                        "make VERSUS=crypto+gcrypt+tomcrypt tools/crypto_bench\n"
+                        "make VERSUS=crypto+gcrypt+mbedcrypto+tomcrypt tools/crypto_bench\n"
                         "to enable them.\n");
             }
             exit(opt != 'h');
-- 
2.17.0



More information about the ffmpeg-devel mailing list