History

avutil/aes: use pthread_once to fill the static tables

James Almer committed 1 year ago

Commit aeed7471 parent 890b8da1 file · +30 −23Browse files

Unified · Split

libavutil/aes.c+30−23
@@ -29,6 +29,7 @@ · 28 unmodified lines
2929 #include "intreadwrite.h"
3030 #include "macros.h"
3131 #include "mem.h"
32+#include "thread.h"
3233
3334 const int av_aes_size= sizeof(AVAES);
3435
@@ -197,6 +198,34 @@ static void init_multbl2(uint32_t tbl[][256], const int c[4], · 162 unmodified lines
197198 }
198199 }
199200
201+static AVOnce aes_static_init = AV_ONCE_INIT;
202+
203+static void aes_init_static(void)
204+{
205+ uint8_t log8[256];
206+ uint8_t alog8[512];
207+ int i, j = 1;
208+
209+ for (i = 0; i < 255; i++) {
210+ alog8[i] = alog8[i + 255] = j;
211+ log8[j] = i;
212+ j ^= j + j;
213+ if (j > 255)
214+ j ^= 0x11B;
215+ }
216+ for (i = 0; i < 256; i++) {
217+ j = i ? alog8[255 - log8[i]] : 0;
218+ j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
219+ j = (j ^ (j >> 8) ^ 99) & 255;
220+ inv_sbox[j] = i;
221+ sbox[i] = j;
222+ }
223+ init_multbl2(dec_multbl, (const int[4]) { 0xe, 0x9, 0xd, 0xb },
224+ log8, alog8, inv_sbox);
225+ init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
226+ log8, alog8, sbox);
227+}
228+
200229 // this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
201230 int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
202231 {
@@ -204,34 +233,12 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt) · 1 unmodified line
204233 uint8_t tk[8][4];
205234 int KC = key_bits >> 5;
206235 int rounds = KC + 6;
207− uint8_t log8[256];
208− uint8_t alog8[512];
209236
210237 a->crypt = decrypt ? aes_decrypt : aes_encrypt;
211238 if (ARCH_X86)
212239 ff_init_aes_x86(a, decrypt);
213240
214− if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl) - 1][FF_ARRAY_ELEMS(enc_multbl[0]) - 1]) {
215− j = 1;
216− for (i = 0; i < 255; i++) {
217− alog8[i] = alog8[i + 255] = j;
218− log8[j] = i;
219− j ^= j + j;
220− if (j > 255)
221− j ^= 0x11B;
222− }
223− for (i = 0; i < 256; i++) {
224− j = i ? alog8[255 - log8[i]] : 0;
225− j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
226− j = (j ^ (j >> 8) ^ 99) & 255;
227− inv_sbox[j] = i;
228− sbox[i] = j;
229− }
230− init_multbl2(dec_multbl, (const int[4]) { 0xe, 0x9, 0xd, 0xb },
231− log8, alog8, inv_sbox);
232− init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
233− log8, alog8, sbox);
234− }
241+ ff_thread_once(&aes_static_init, aes_init_static);
235242
236243 if (key_bits != 128 && key_bits != 192 && key_bits != 256)
237244 return AVERROR(EINVAL);

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