From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id 6C8AE4CBB4
	for <ffmpegdev@gitmailbox.com>; Wed, 11 Jun 2025 01:55:07 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 07F8068C026;
	Wed, 11 Jun 2025 04:54:46 +0300 (EEST)
Received: from mail-vk1-f182.google.com (mail-vk1-f182.google.com
 [209.85.221.182])
 by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 0C66268C05F
 for <ffmpeg-devel@ffmpeg.org>; Wed, 11 Jun 2025 04:54:38 +0300 (EEST)
Received: by mail-vk1-f182.google.com with SMTP id
 71dfb90a1353d-53124b5d255so90904e0c.1
 for <ffmpeg-devel@ffmpeg.org>; Tue, 10 Jun 2025 18:54:37 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1749606876; x=1750211676; darn=ffmpeg.org;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:to:from:from:to:cc:subject:date:message-id
 :reply-to; bh=6pqJ+lhWU2xUF9Xb0wSgH16XO0yGkYuPU/oT7atp3kE=;
 b=EHg5u+qhOPk5KRdHJFiNSdmlzme7M0faS0NHLbnIWK+ZOuYuj1HWQlp60zgjj/xPbZ
 Js2TylbOZwSQKBlUdqu/okaOqO9X0DgjICJh0afMauSRUERQiIVzVZEjrpqNo0pPrY2f
 U4mEWXp3r9xKAYIwKLoAejfNBFVDe/4O/Drdcd1bZhmAM48PeOyyxxYrICaNDEgBzJXk
 vDpzG9RI0jiZJQ8h5Bef0Hqy65fD1njogUy+w3FamOipfY0Tj/HYQZCzPWxd46Z2GjfU
 q823BvVRrXmUIaWIV90R8+hmIwOcgmKNqwyi+hU7YUDxsaJQqp0XzuFcnxwTFNe3CIGI
 BwRg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1749606876; x=1750211676;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=6pqJ+lhWU2xUF9Xb0wSgH16XO0yGkYuPU/oT7atp3kE=;
 b=m08j53ICtFhaJTxK2WTiWqEl3hY1H/MDaZqBasozLLjTWlMQwj9BTIbLbb5/gEi/dS
 5ZEonmxr9JSxfX/TYOWef/HlQwE/tJCnd+sf8pYK4XnVDdrcTuyatJbx2M5fiMBSeVY0
 8zrXwfsWOPOCpQy1Frz1Z4TVPp55n4aMX/mecxtv7BIgy6m+tWV+IAjq1JxQD5yyr0mB
 BtYRqGuW6qaUhKsMGlp0wq/b7Azauugboxyxqo2gbPPjTnH8WY9dmpwCOfSB1/LunDGd
 0Yb6tgZ1kajT3w47t9mBFxPZtfRmNl1X/2tA14wY6TdUSE12RMD9m4EOrr8tQuFyfZdp
 s1hA==
X-Gm-Message-State: AOJu0Yy3/YTpRn+qoqF1lwi5zQICOvTzJ9loO9OEkphDSZcJBpVQsJnu
 Jr8SAzr0ZKgyrETlWk36dDTbYOQimjafHAxP5Lj1rMW6dtEqd81lzGwB6qQyRw==
X-Gm-Gg: ASbGncvOkRR6kqLhibWM1hYHq2c0ZyGfzfgli8a9m7ADZKeXSONK3zA358EdcEHIrEc
 sbFzaUDmB70QvoM8iq9VADBSr7V4pJUV6ITPTIA7l534xyGC8edIxOlnj+BBz4rSubPewv1/hry
 dsYY9qf93F9+gMxUEqud5Dn41OTygjH5hf4dcRQuMM90utdAINFifCzv1PBZkSlqC9JPFt1YDcI
 R/42vHe4RRrJXG+ncZpUiNCFG7qNB/so8QOD+hrmF9NogjVK6WUc77X4XeyV1smvRoGsQYtRR23
 QFm+vp9Vh/5fNj3JNud0upMzvPf4idZi/bKKUafCZoM6z1P/QQdfDSucuc76zJE/bRescWZvOLZ
 /04s=
X-Google-Smtp-Source: AGHT+IEW3jxE+XypXi3VG/RJpIs9GLnwhgfDy2/6jbT1G2FX+QpnTE8HJCsrqneWG0eTMjXjoBTZXQ==
X-Received: by 2002:a05:6102:c52:b0:4db:e01:f2db with SMTP id
 ada2fe7eead31-4e7bac51164mr1859311137.0.1749606876045; 
 Tue, 10 Jun 2025 18:54:36 -0700 (PDT)
Received: from localhost.localdomain ([2800:2121:b000:82e:4125:db3:37dc:a9ca])
 by smtp.gmail.com with ESMTPSA id
 a1e0cc1a2514c-87eeaf6e05bsm2151838241.28.2025.06.10.18.54.34
 for <ffmpeg-devel@ffmpeg.org>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Tue, 10 Jun 2025 18:54:35 -0700 (PDT)
From: James Almer <jamrial-at-gmail.com@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Date: Tue, 10 Jun 2025 22:54:15 -0300
Message-ID: <20250611015415.19575-3-jamrial@gmail.com>
X-Mailer: git-send-email 2.49.0
In-Reply-To: <20250611015415.19575-1-jamrial@gmail.com>
References: <20250611015415.19575-1-jamrial@gmail.com>
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH 3/3] avutil/aes: add an OpenSSL-libcrypto
 backed implementation
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250611015415.19575-3-jamrial@gmail.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

OpenSSL has optimizations for more architectures than x86-AESNI, so use it if
libavutil is configured with libcrypto explicitly (Enabling OpenSSL alone will
not make use of it).

Signed-off-by: James Almer <jamrial@gmail.com>
---
 configure                |  8 +++++++-
 libavutil/aes.c          | 37 ++++++++++++++++++++++++++++++++++---
 libavutil/aes_internal.h | 11 ++++++++++-
 libavutil/x86/aes_init.c |  2 ++
 4 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index 534b443f7d..c7b4fbb2fe 100755
--- a/configure
+++ b/configure
@@ -223,6 +223,7 @@ External library support:
   --enable-libcelt         enable CELT decoding via libcelt [no]
   --enable-libcdio         enable audio CD grabbing with libcdio [no]
   --enable-libcodec2       enable codec2 en/decoding using libcodec2 [no]
+  --enable-libcrypto       enable crypto via libcrypto (AES only) [no]
   --enable-libdav1d        enable AV1 decoding via libdav1d [no]
   --enable-libdavs2        enable AVS2 decoding via libdavs2 [no]
   --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
@@ -1931,6 +1932,7 @@ EXTERNAL_LIBRARY_LIST="
     libcaca
     libcelt
     libcodec2
+    libcrypto
     libdav1d
     libdc1394
     libflite
@@ -4091,7 +4093,7 @@ avfilter_deps="avutil"
 avfilter_suggest="libm stdatomic spirv_compiler"
 avformat_deps="avcodec avutil"
 avformat_suggest="libm network zlib stdatomic"
-avutil_suggest="clock_gettime ffnvcodec gcrypt libm libdrm libmfx opencl openssl user32 vaapi vulkan videotoolbox corefoundation corevideo coremedia bcrypt stdatomic"
+avutil_suggest="clock_gettime ffnvcodec gcrypt libcrypto libm libdrm libmfx opencl openssl user32 vaapi vulkan videotoolbox corefoundation corevideo coremedia bcrypt stdatomic"
 swresample_deps="avutil"
 swresample_suggest="libm libsoxr stdatomic"
 swscale_deps="avutil"
@@ -7193,6 +7195,10 @@ enabled openssl           && { { check_pkg_config openssl "openssl >= 3.0.0" ope
                                check_lib openssl openssl/ssl.h SSL_library_init -lssl32 -leay32 ||
                                check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 ||
                                die "ERROR: openssl not found"; }
+enabled libcrypto         && { { check_pkg_config libcrypto "libcrypto >= 3.0.0" openssl/aes.h AES_set_encrypt_key &&
+                                 { enabled gplv3 || ! enabled gpl || enabled nonfree || die "ERROR: OpenSSL-libcrypto >= 3.0.0 requires --enable-version3"; }; } ||
+                               { enabled gpl && ! enabled nonfree && die "ERROR: OpenSSL-libcrypto < 3.0.0 is incompatible with the gpl"; } ||
+                               require_pkg_config libcrypto libcrypto openssl/aes.h AES_set_encrypt_key; }
 enabled pocketsphinx      && require_pkg_config pocketsphinx pocketsphinx pocketsphinx/pocketsphinx.h ps_init
 enabled rkmpp             && { require_pkg_config rkmpp rockchip_mpp  rockchip/rk_mpi.h mpp_create &&
                                require_pkg_config rockchip_mpp "rockchip_mpp >= 1.3.7" rockchip/rk_mpi.h mpp_create &&
diff --git a/libavutil/aes.c b/libavutil/aes.c
index 7fe42a5548..3a0d4e1385 100644
--- a/libavutil/aes.c
+++ b/libavutil/aes.c
@@ -39,6 +39,7 @@ struct AVAES *av_aes_alloc(void)
     return av_mallocz(sizeof(struct AVAES));
 }
 
+#if !CONFIG_LIBCRYPTO
 static const uint8_t rcon[10] = {
     0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
 };
@@ -137,7 +138,7 @@ static inline void aes_crypt(AVAES *a, int s, const uint8_t *sbox,
 }
 
 static void aes_encrypt(AVAES *a, uint8_t *dst, const uint8_t *src,
-                        int count, uint8_t *iv)
+                        int count, uint8_t *iv, int decrypt)
 {
     while (count--) {
         addkey_s(&a->state[1], src, &a->round_key[a->rounds]);
@@ -153,7 +154,7 @@ static void aes_encrypt(AVAES *a, uint8_t *dst, const uint8_t *src,
 }
 
 static void aes_decrypt(AVAES *a, uint8_t *dst, const uint8_t *src,
-                        int count, uint8_t *iv)
+                        int count, uint8_t *iv, int decrypt)
 {
     while (count--) {
         addkey_s(&a->state[1], src, &a->round_key[a->rounds]);
@@ -168,12 +169,32 @@ static void aes_decrypt(AVAES *a, uint8_t *dst, const uint8_t *src,
     }
 }
 
+#else
+
+static void aes_libcrypto(AVAES *a, uint8_t *dst, const uint8_t *src,
+                          int count, uint8_t *iv, int decrypt)
+{
+    if (iv)
+        AES_cbc_encrypt((const unsigned char *)src,
+                        (unsigned char *)dst,
+                        count, &a->key,
+                        (unsigned char *)iv, !decrypt);
+    else {
+        for (int i = 0; i < count; i++)
+            AES_ecb_encrypt((const unsigned char *)&src[i*16],
+                            (unsigned char *)&dst[i*16],
+                            &a->key, !decrypt);
+    }
+}
+#endif
+
 void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src,
                   int count, uint8_t *iv, int decrypt)
 {
-    a->crypt(a, dst, src, count, iv);
+    a->crypt(a, dst, src, count, iv, decrypt);
 }
 
+#if !CONFIG_LIBCRYPTO
 static void init_multbl2(uint32_t tbl[][256], const int c[4],
                          const uint8_t *log8, const uint8_t *alog8,
                          const uint8_t *sbox)
@@ -226,10 +247,19 @@ static av_cold void aes_init_static(void)
     init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
                  log8, alog8, sbox);
 }
+#endif
 
 // this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
 int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
 {
+#if CONFIG_LIBCRYPTO
+    int ret = decrypt ? AES_set_decrypt_key(key, key_bits, &a->key) :
+                        AES_set_encrypt_key(key, key_bits, &a->key);
+    if (ret < 0)
+        return AVERROR_EXTERNAL;
+
+    a->crypt = aes_libcrypto;
+#else
     int i, j, t, rconpointer = 0;
     uint8_t tk[8][4];
     int KC = key_bits >> 5;
@@ -278,6 +308,7 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
         for (i = 0; i < (rounds + 1) >> 1; i++)
             FFSWAP(av_aes_block, a->round_key[i], a->round_key[rounds - i]);
     }
+#endif
 
     return 0;
 }
diff --git a/libavutil/aes_internal.h b/libavutil/aes_internal.h
index e2de382f26..6713798ce2 100644
--- a/libavutil/aes_internal.h
+++ b/libavutil/aes_internal.h
@@ -21,8 +21,13 @@
 #ifndef AVUTIL_AES_INTERNAL_H
 #define AVUTIL_AES_INTERNAL_H
 
+#include "config.h"
+
 #include "mem_internal.h"
 #include <stdint.h>
+#if CONFIG_LIBCRYPTO
+#include <openssl/aes.h>
+#endif
 
 typedef union {
     uint64_t u64[2];
@@ -32,12 +37,16 @@ typedef union {
 } av_aes_block;
 
 typedef struct AVAES {
+#if CONFIG_LIBCRYPTO
+    AES_KEY key;
+#else
     // Note: round_key[16] is accessed in the init code, but this only
     // overwrites state, which does not matter (see also commit ba554c0).
     DECLARE_ALIGNED(16, av_aes_block, round_key)[15];
     DECLARE_ALIGNED(16, av_aes_block, state)[2];
     int rounds;
-    void (*crypt)(struct AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv);
+#endif
+    void (*crypt)(struct AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decript);
 } AVAES;
 
 void ff_init_aes_x86(AVAES *a, int decrypt);
diff --git a/libavutil/x86/aes_init.c b/libavutil/x86/aes_init.c
index f825e0799c..54487e8832 100644
--- a/libavutil/x86/aes_init.c
+++ b/libavutil/x86/aes_init.c
@@ -37,6 +37,7 @@ void ff_aes_encrypt_14_aesni(AVAES *a, uint8_t *dst, const uint8_t *src,
 
 void ff_init_aes_x86(AVAES *a, int decrypt)
 {
+#if !CONFIG_LIBCRYPTO
     int cpu_flags = av_get_cpu_flags();
 
     if (EXTERNAL_AESNI(cpu_flags)) {
@@ -47,4 +48,5 @@ void ff_init_aes_x86(AVAES *a, int decrypt)
         else if (a->rounds == 14)
             a->crypt = decrypt ? ff_aes_decrypt_14_aesni : ff_aes_encrypt_14_aesni;
     }
+#endif
 }
-- 
2.49.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".