From patchwork Sun May 27 21:25:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 9112 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:11c:0:0:0:0:0 with SMTP id c28-v6csp1265376jad; Sun, 27 May 2018 14:25:28 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIw/10nVEdKyWK0PtqS8BxxHVg5fw0gk0GiYin8CeucbYxysrwM1R5C5th2KsyBmyKlipKH X-Received: by 2002:a1c:1d10:: with SMTP id d16-v6mr2545241wmd.78.1527456328879; Sun, 27 May 2018 14:25:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527456328; cv=none; d=google.com; s=arc-20160816; b=HnoNK8yp20ZvwXQ2tqQQF/qrQKAwmtEAdA5O60ZQdbv+SY4xxObmCsiipAXr+r7vFD yiPyp2DsfYpFJWi/Cpu5cmbL1DXmA0XjESzTQ5CN7y2paGHwYymgQZCo6m5sSSM5Tis2 gK2/dERzHpn+Zsq6MBQYQpozITM1ek+/I1ZAOFxC1VKAo+6Ua8DRea3YajkWShF1Rs02 jY4zwwsInaPnHUkm3a8aNU20IPFFvKU+JAiFHCD2p1dWzQPTTbAHQLAkGzooJ2due8Xf OlA72c1vnx0fAuYQHuNdpjHTKdeAqjs3yo7PkL/Rs2qoJAGO4FcrIV3ntAOZPZpRlWPU 2FRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=+0HV++uzXYhH6ATUkQusnspmEYok6/77FFR5I/Ym3TA=; b=G36K/+hebLd95eCgHL5atsbZiFrMhMAddDSBrTBEnWFTQHLWsWj6CpZxCTGtzbvYKP ivCEWDr1pPCC/L2vVgnMBj0tYyU3OsJICG6RXr2sa+sdFRJhI7RbkxZJzlmV+DBnhkvi h08ygAD/Me5phpnnugcK/80CltKXLsn5vWgClb7jK0lY+YV3Qc3OQ0Wo2DqTa3/GQDhL rDqrOse5WtORVhNidf2/1ZXuu6h5Lxxd//A+6hqwjNuA9F6ydntDvanJPCRKh09RjU2t ku2rcZZaFn1FjemrtNxMXCtHt6+Kh8ulNp7VT6Iux9y2lEYPjn1UpEDe8RO8GhDdp+j6 ytcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Q+jLrkXj; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x1-v6si25519575wrc.414.2018.05.27.14.25.28; Sun, 27 May 2018 14:25:28 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Q+jLrkXj; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BBF1D68A2D6; Mon, 28 May 2018 00:24:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt0-f194.google.com (mail-qt0-f194.google.com [209.85.216.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 749A6680AD4 for ; Mon, 28 May 2018 00:24:37 +0300 (EEST) Received: by mail-qt0-f194.google.com with SMTP id f13-v6so12826940qtp.10 for ; Sun, 27 May 2018 14:25:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=SwMjsA+APCHedhtmOcrnTHT6ed6IPa9lFvLih36Vsl0=; b=Q+jLrkXjTXV90gorqrwz2CP1Ll7Ox/KbWAFq1XRWEdOljRmA5x7LVL63NnKxpCxpCq lyYCX6HtzMalLlLAqwEthkuW1ShUkMG/f1Yc9z5zhNWXn1PzGynSiAHDglx961fsSPT7 U/VO+bxbmACU1/KtoiH+biKd6C3mQlX0KsKqMMwl/Q0LeRQ7dtQ01iNfwDcG2GaTF8gP 0QD4tTVj8rinN3B7pElHfErSuoK4S49rs2MV9e7ttjbdIOolwEiXA6C+UeWx8NXxl6TB zrXPx/dF5c7mIRX8rP2TIkTbbybH1dDvmzsAErh+s4cPByll5+hAuUplv3tQYNiF61IM gI0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=SwMjsA+APCHedhtmOcrnTHT6ed6IPa9lFvLih36Vsl0=; b=cI+pKsy0X4PiScRiDftbuDgqx7CDATwg7Ou/r5kp38kjzgbwiEN2KpwdpLbJ4R0ThR 0PIMWKrPTurPzFLHS6sWtUnJnxrV/nhQMAtbh7E+MPvj99ra2ztLppVN6i5a6lgvD76o y5KUoklZxl/TbcRu2oF/4fI8xa/ikhAXObTRUj4dosGHWFAJS8gI41/isYw7qOIuFan+ gAgBZylJqWf+yPbZzZu/6d5GnWFoXON5KQaMzl4XhgF/02mt9YW3BPayCt6aqUDGmOf1 6DwT+d5nRujUK9H+PQZlRFu/EZOtiBKWwKByaB1lGQCDVhdei6mL+D+scZSZyLWdEirs 7A0Q== X-Gm-Message-State: ALKqPwdEJsD3ZZnWiLBn92rHq1DbIL47A4uS/aoXv7SPkCaY0l6r/L++ lN9tMU0ozeufoKegzqvG/vVNTQ== X-Received: by 2002:aed:3245:: with SMTP id y63-v6mr9894305qtd.7.1527456319705; Sun, 27 May 2018 14:25:19 -0700 (PDT) Received: from localhost.localdomain ([190.188.171.140]) by smtp.gmail.com with ESMTPSA id v26-v6sm1636200qtc.12.2018.05.27.14.25.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 27 May 2018 14:25:19 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sun, 27 May 2018 18:25:06 -0300 Message-Id: <20180527212506.8896-1-jamrial@gmail.com> X-Mailer: git-send-email 2.17.0 Subject: [FFmpeg-devel] [PATCH] tools/crypto_bench: add support for mbedcrypto X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Requires mbed TLS 2.7.0 or newer Signed-off-by: James Almer --- 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 #include @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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');