From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <ffmpeg-devel-bounces@ffmpeg.org> Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 4B3F14BA87 for <ffmpegdev@gitmailbox.com>; Tue, 1 Apr 2025 15:17:03 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CF8DC687D3B; Tue, 1 Apr 2025 18:16:58 +0300 (EEST) Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D544C687B54 for <ffmpeg-devel@ffmpeg.org>; Tue, 1 Apr 2025 18:16:51 +0300 (EEST) Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2260c915749so79425605ad.3 for <ffmpeg-devel@ffmpeg.org>; Tue, 01 Apr 2025 08:16:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743520609; x=1744125409; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=gAWvF66biOZ2B1HstQQoH9QO1LbOLUsRkYSmPVbG0xA=; b=FL7Wb0Y/AVFAs0TpDlgTxSN/EsqPVQAfyBMxNIjROUBjCHL+uwB0Ib/QLTldWEGGoZ eeEnAB228co5pLL4/3vsbKOymiILbdkjbWY67ywTHhthz9aPWfXvWioxQmG+VfQ3lxu3 P4miJj2IWDnqH2vMSwsP0tGbW3ogB7x2bBzRDJW3YovvC3kpEou5UgbMhUw4oG0qUaEN n1T+flytYNQ/vytG8YZl51ABuLxALvQ13NeIiX8t6oYVERstRhc6vsJvllqYYvJL1GKo qX/An8zqu+zLh7NDBfZflE5oQWViW7ssHDgcY7e5Bug4JySjCkxZt2982t+bxLQacio+ fqaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743520609; x=1744125409; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gAWvF66biOZ2B1HstQQoH9QO1LbOLUsRkYSmPVbG0xA=; b=r2/WrZXdYBLa4WSwH+2L5t3g+z3WdEiki+DiIwFZwIScFQrqB/0cYNTkPzDHDY5QtL fliCMIKd0IoVvDlM3BjruoqTqq8RwNdtQ61ugq00ig7b9YDwSCHuNlTmBROHe9q6gPlk NKLYJRsPMGgk1vo1fJjblwCTLKpQiMbxvjwAoZj4sy5qAAX2WTJfLNILmibzzETnl9/e XZJo/cebKyU2CyOpe2zPX3j6GY/cpW44M+M7SmlM/NikQc+9koVO1a3I9BzfIgg17rxV NoCDv/WMal1BUGopNEVW8Dv9c7tnorUEhGfuWOuqMDhtJE33if7H15UukweB0xmmvcoX 7Sag== X-Gm-Message-State: AOJu0YzRJfcmQrTEd/OJXTuP9n9QqDZmYpph6mm2Z5V4M+nIL3GP1kBH dsfYzvz4eJCUlcSWQ15RQJQDKQ0HE4mECtC/JDoS24LPc/Wfagy+LJ1vdw== X-Gm-Gg: ASbGnctW+QK44gv7e26O2Z1g5KhODqyyfY61CxF3gnnfzVXUwgBoW0a92iZdfY+lX5H ah8rpLcjYkpng/lKjcKiV5rqothX82or0i4uw77v8Y5zKokffiHRzYgbrATNPpT/ekKSzbafo8E KVplvhFGppETOowXA88cSY4z+dnCtetiIevVn2/ycndyrFJ7NKv9IrtTJ+u4r8QWbDQQ+2/qmr9 XkOFrWrTBImQoDVLjrO3YmS10fWWYv/6QwR8/Gfmsk5mzweKu00LM3ORMgqO4cyYn5Dms4c7YpW VtBGrkplffPDhM0shwSrTZ12SHH25Em3IafDsRnRmwQztFRbBZmLBGXxK+A6mHjIbb2d+iEPSjY = X-Google-Smtp-Source: AGHT+IEXBmz+eqDwvw5IfOgH59dUe3RUY4ZcbZaVeiI4E9x0GsL5xIzlNfk4wOdAjJLzSZrZ2Zi9ig== X-Received: by 2002:a17:902:f549:b0:224:a74:28cd with SMTP id d9443c01a7336-2292f9767camr202492705ad.31.1743520609333; Tue, 01 Apr 2025 08:16:49 -0700 (PDT) Received: from localhost.localdomain ([2800:2121:b000:82e:6c84:2143:301b:3529]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2295aacc9f1sm18443325ad.89.2025.04.01.08.16.48 for <ffmpeg-devel@ffmpeg.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Apr 2025 08:16:48 -0700 (PDT) From: James Almer <jamrial@gmail.com> To: ffmpeg-devel@ffmpeg.org Date: Tue, 1 Apr 2025 12:16:35 -0300 Message-ID: <20250401151637.2021-1-jamrial@gmail.com> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avutil/aes_ctr: simplify and optimize av_aes_ctr_crypt() 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/20250401151637.2021-1-jamrial@gmail.com/> List-Archive: <https://master.gitmailbox.com/ffmpegdev/> List-Post: <mailto:ffmpegdev@gitmailbox.com> Process data in chunks of four or eight bytes, depending on host, instead of one at a time. Signed-off-by: James Almer <jamrial@gmail.com> --- libavutil/aes_ctr.c | 47 ++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/libavutil/aes_ctr.c b/libavutil/aes_ctr.c index c2d6d570e7..d720aa6aaf 100644 --- a/libavutil/aes_ctr.c +++ b/libavutil/aes_ctr.c @@ -24,6 +24,7 @@ #include "aes_ctr.h" #include "aes.h" #include "aes_internal.h" +#include "intreadwrite.h" #include "macros.h" #include "mem.h" #include "random_seed.h" @@ -32,8 +33,7 @@ typedef struct AVAESCTR { uint8_t counter[AES_BLOCK_SIZE]; - uint8_t encrypted_counter[AES_BLOCK_SIZE]; - int block_offset; + DECLARE_ALIGNED(8, uint8_t, encrypted_counter)[AES_BLOCK_SIZE]; AVAES aes; } AVAESCTR; @@ -46,13 +46,11 @@ void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv) { memcpy(a->counter, iv, AES_CTR_IV_SIZE); memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE); - a->block_offset = 0; } void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv) { memcpy(a->counter, iv, sizeof(a->counter)); - a->block_offset = 0; } const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a) @@ -75,7 +73,6 @@ int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key) av_aes_init(&a->aes, key, 128, 0); memset(a->counter, 0, sizeof(a->counter)); - a->block_offset = 0; return 0; } @@ -101,31 +98,29 @@ void av_aes_ctr_increment_iv(struct AVAESCTR *a) { av_aes_ctr_increment_be64(a->counter); memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE); - a->block_offset = 0; } void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count) { - const uint8_t* src_end = src + count; - const uint8_t* cur_end_pos; - uint8_t* encrypted_counter_pos; - - while (src < src_end) { - if (a->block_offset == 0) { - av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0); - - av_aes_ctr_increment_be64(a->counter + 8); - } - - encrypted_counter_pos = a->encrypted_counter + a->block_offset; - cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset; - cur_end_pos = FFMIN(cur_end_pos, src_end); - - a->block_offset += cur_end_pos - src; - a->block_offset &= (AES_BLOCK_SIZE - 1); + while (count >= AES_BLOCK_SIZE) { + av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0); + av_aes_ctr_increment_be64(a->counter + 8); +#if HAVE_FAST_64BIT + for (int len = 0; len < AES_BLOCK_SIZE; len += 8) + AV_WN64(&dst[len], AV_RN64(&src[len]) ^ AV_RN64A(&a->encrypted_counter[len])); +#else + for (int len = 0; len < AES_BLOCK_SIZE; len += 4) + AV_WN32(&dst[len], AV_RN32(&src[len]) ^ AV_RN32A(&a->encrypted_counter[len])); +#endif + dst += AES_BLOCK_SIZE; + src += AES_BLOCK_SIZE; + count -= AES_BLOCK_SIZE; + } - while (src < cur_end_pos) { - *dst++ = *src++ ^ *encrypted_counter_pos++; - } + if (count > 0) { + av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0); + av_aes_ctr_increment_be64(a->counter + 8); + for (int len = 0; len < count; len++) + dst[len] = src[len] ^ a->encrypted_counter[len]; } } -- 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".