The branch, master has been updated
       via  799c133273a8c8455b21bbd9e2ab9e716f5e9bb1 (commit)
       via  335ba4a649044cd613c74df57e6f1144455aaf19 (commit)
      from  c1dc2e2b7cc8df8a40b616793d1204be0e71103c (commit)


- Log -----------------------------------------------------------------
commit 799c133273a8c8455b21bbd9e2ab9e716f5e9bb1
Author:     James Almer <[email protected]>
AuthorDate: Tue Sep 9 11:32:17 2025 -0300
Commit:     James Almer <[email protected]>
CommitDate: Thu Sep 11 18:32:24 2025 -0300

    avutil/tests/aes_ctr: extend the test to cover payloads smaller than a block
    
    Signed-off-by: James Almer <[email protected]>

diff --git a/libavutil/tests/aes_ctr.c b/libavutil/tests/aes_ctr.c
index 5af48428aa..6726b00cda 100644
--- a/libavutil/tests/aes_ctr.c
+++ b/libavutil/tests/aes_ctr.c
@@ -19,18 +19,23 @@
 #include <string.h>
 
 #include "libavutil/random_seed.h"
+#include "libavutil/lfg.h"
 #include "libavutil/log.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/aes_ctr.h"
 
 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {
+    0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f,
+    0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f,
     0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f,
     0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f
 };
 
 static const DECLARE_ALIGNED(8, uint8_t, encrypted)[] = {
     0x95, 0xcd, 0x9a, 0x8a, 0x83, 0xa2, 0x1a, 0x84, 0x92, 0xed,
-    0xd6, 0xf2, 0x57, 0x2f, 0x61, 0x98, 0xbc, 0x20, 0x98, 0xee
+    0xd6, 0xf2, 0x57, 0x2f, 0x61, 0x98, 0xbc, 0x20, 0x98, 0xee,
+    0x6c, 0xed, 0x53, 0xae, 0x2f, 0xc4, 0x18, 0x7c, 0xeb, 0x62,
+    0xbb, 0x3a, 0x71, 0x24, 0x22, 0x8c, 0xd9, 0xfa, 0xee, 0x10
 };
 
 static const DECLARE_ALIGNED(8, uint8_t, fixed_iv)[] = {
@@ -44,14 +49,17 @@ static const DECLARE_ALIGNED(8, uint8_t, fixed_key)[] = {
 
 static DECLARE_ALIGNED(8, uint32_t, key)[4];
 
-static DECLARE_ALIGNED(8, uint8_t, tmp)[20];
+static DECLARE_ALIGNED(8, uint8_t, tmp)[40];
 
 int main (void)
 {
     int ret = 1;
+    AVLFG lfg;
     struct AVAESCTR *ae, *ad;
     const uint8_t *iv, *k;
 
+    av_lfg_init(&lfg, av_get_random_seed());
+
     for (int i = 0; i < 2; i++) {
         ae = av_aes_ctr_alloc();
         ad = av_aes_ctr_alloc();
@@ -85,13 +93,30 @@ int main (void)
         iv =   av_aes_ctr_get_iv(ae);
         av_aes_ctr_set_full_iv(ad, iv);
 
-        av_aes_ctr_crypt(ae, tmp, plain, sizeof(tmp));
+        uint8_t *dst = tmp;
+        const uint8_t *src = plain;
+        int left = sizeof(plain);
+        while (left > 0) {
+            int count = (av_lfg_get(&lfg) % left) + 1;
+            av_aes_ctr_crypt(ae, dst, src, count);
+            dst += count;
+            src += count;
+            left -= count;
+        }
         if (i && memcmp(tmp, encrypted, sizeof(tmp)) != 0) {
             av_log(NULL, AV_LOG_ERROR, "test failed\n");
             goto ERROR;
         }
 
-        av_aes_ctr_crypt(ad, tmp, tmp,   sizeof(tmp));
+        dst = tmp;
+        left = sizeof(plain);
+        while (left > 0) {
+            int count = (av_lfg_get(&lfg) % left) + 1;
+            av_aes_ctr_crypt(ad, dst, dst, count);
+            dst += count;
+            left -= count;
+        }
+
         if (memcmp(tmp, plain, sizeof(tmp)) != 0){
             av_log(NULL, AV_LOG_ERROR, "test failed\n");
             goto ERROR;

commit 335ba4a649044cd613c74df57e6f1144455aaf19
Author:     James Almer <[email protected]>
AuthorDate: Tue Sep 9 11:31:45 2025 -0300
Commit:     James Almer <[email protected]>
CommitDate: Thu Sep 11 18:32:23 2025 -0300

    avutil/aes_ctr: reintroduce the block offset state
    
    Wrongly removed in fe73b84879a560d69affca88ce21e61108e7c38d, it's required 
for
    calls with a payload smaller than a full block.
    
    Fixes issue #20474.
    
    Signed-off-by: James Almer <[email protected]>

diff --git a/libavutil/aes_ctr.c b/libavutil/aes_ctr.c
index 63dcb20d3a..f653e54bd1 100644
--- a/libavutil/aes_ctr.c
+++ b/libavutil/aes_ctr.c
@@ -34,6 +34,7 @@
 typedef struct AVAESCTR {
     DECLARE_ALIGNED(8, uint8_t, counter)[AES_BLOCK_SIZE];
     DECLARE_ALIGNED(8, uint8_t, encrypted_counter)[AES_BLOCK_SIZE];
+    int block_offset;
     AVAES aes;
 } AVAESCTR;
 
@@ -46,11 +47,13 @@ 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)
@@ -73,6 +76,7 @@ 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;
 }
@@ -92,10 +96,21 @@ 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)
 {
+    if (a->block_offset && count > 0) {
+        int left = FFMIN(count, AES_BLOCK_SIZE - a->block_offset);
+        for (int len = 0; len < left; len++)
+            dst[len] = src[len] ^ a->encrypted_counter[a->block_offset++];
+        a->block_offset &= AES_BLOCK_SIZE - 1;
+        dst += left;
+        src += left;
+        count -= left;
+    }
+
     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);
@@ -115,6 +130,6 @@ void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, 
const uint8_t *src, int
         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];
+            dst[len] = src[len] ^ a->encrypted_counter[a->block_offset++];
     }
 }

-----------------------------------------------------------------------

Summary of changes:
 libavutil/aes_ctr.c       | 17 ++++++++++++++++-
 libavutil/tests/aes_ctr.c | 33 +++++++++++++++++++++++++++++----
 2 files changed, 45 insertions(+), 5 deletions(-)


hooks/post-receive
-- 

_______________________________________________
ffmpeg-cvslog mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to