History

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.

James Almer committed 1 year ago

Commit e4e57be1 parent 08a81b01 file · +16 −1Browse files

Unified · Split

libavutil/aes_ctr.c+16−1
@@ -34,6 +34,7 @@ · 33 unmodified lines
3434 typedef struct AVAESCTR {
3535 DECLARE_ALIGNED(8, uint8_t, counter)[AES_BLOCK_SIZE];
3636 DECLARE_ALIGNED(8, uint8_t, encrypted_counter)[AES_BLOCK_SIZE];
37+ int block_offset;
3738 AVAES aes;
3839 } AVAESCTR;
3940
@@ -46,11 +47,13 @@ void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv) · 6 unmodified lines
4647 {
4748 memcpy(a->counter, iv, AES_CTR_IV_SIZE);
4849 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
50+ a->block_offset = 0;
4951 }
5052
5153 void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
5254 {
5355 memcpy(a->counter, iv, sizeof(a->counter));
56+ a->block_offset = 0;
5457 }
5558
5659 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) · 16 unmodified lines
7376 av_aes_init(&a->aes, key, 128, 0);
7477
7578 memset(a->counter, 0, sizeof(a->counter));
79+ a->block_offset = 0;
7680
7781 return 0;
7882 }
@@ -92,10 +96,21 @@ void av_aes_ctr_increment_iv(struct AVAESCTR *a) · 13 unmodified lines
9296 {
9397 av_aes_ctr_increment_be64(a->counter);
9498 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
99+ a->block_offset = 0;
95100 }
96101
97102 void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
98103 {
104+ if (a->block_offset && count > 0) {
105+ int left = FFMIN(count, AES_BLOCK_SIZE - a->block_offset);
106+ for (int len = 0; len < left; len++)
107+ dst[len] = src[len] ^ a->encrypted_counter[a->block_offset++];
108+ a->block_offset &= AES_BLOCK_SIZE - 1;
109+ dst += left;
110+ src += left;
111+ count -= left;
112+ }
113+
99114 while (count >= AES_BLOCK_SIZE) {
100115 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
101116 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 · 13 unmodified lines
115130 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
116131 av_aes_ctr_increment_be64(a->counter + 8);
117132 for (int len = 0; len < count; len++)
118− dst[len] = src[len] ^ a->encrypted_counter[len];
133+ dst[len] = src[len] ^ a->encrypted_counter[a->block_offset++];
119134 }
120135 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t