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
34typedef struct AVAESCTR {34typedef struct AVAESCTR {
35 DECLARE_ALIGNED(8, uint8_t, counter)[AES_BLOCK_SIZE];35 DECLARE_ALIGNED(8, uint8_t, counter)[AES_BLOCK_SIZE];
36 DECLARE_ALIGNED(8, uint8_t, encrypted_counter)[AES_BLOCK_SIZE];36 DECLARE_ALIGNED(8, uint8_t, encrypted_counter)[AES_BLOCK_SIZE];
37 int block_offset;
37 AVAES aes;38 AVAES aes;
38} AVAESCTR;39} AVAESCTR;
39 40
@@ -46,11 +47,13 @@ void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv) · 6 unmodified lines
46{47{
47 memcpy(a->counter, iv, AES_CTR_IV_SIZE);48 memcpy(a->counter, iv, AES_CTR_IV_SIZE);
48 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);49 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
50 a->block_offset = 0;
49}51}
50 52
51void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)53void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
52{54{
53 memcpy(a->counter, iv, sizeof(a->counter));55 memcpy(a->counter, iv, sizeof(a->counter));
56 a->block_offset = 0;
54}57}
55 58
56const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a)59const 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
73 av_aes_init(&a->aes, key, 128, 0);76 av_aes_init(&a->aes, key, 128, 0);
74 77
75 memset(a->counter, 0, sizeof(a->counter));78 memset(a->counter, 0, sizeof(a->counter));
79 a->block_offset = 0;
76 80
77 return 0;81 return 0;
78}82}
@@ -92,10 +96,21 @@ void av_aes_ctr_increment_iv(struct AVAESCTR *a) · 13 unmodified lines
92{96{
93 av_aes_ctr_increment_be64(a->counter);97 av_aes_ctr_increment_be64(a->counter);
94 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);98 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
99 a->block_offset = 0;
95}100}
96 101
97void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)102void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
98{103{
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
99 while (count >= AES_BLOCK_SIZE) {114 while (count >= AES_BLOCK_SIZE) {
100 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);115 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
101 av_aes_ctr_increment_be64(a->counter + 8);116 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
115 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);130 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
116 av_aes_ctr_increment_be64(a->counter + 8);131 av_aes_ctr_increment_be64(a->counter + 8);
117 for (int len = 0; len < count; len++)132 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++];
119 }134 }
120}135}

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