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
29#include "intreadwrite.h"29#include "intreadwrite.h"
30#include "macros.h"30#include "macros.h"
31#include "mem.h"31#include "mem.h"
32#include "thread.h"
32 33
33const int av_aes_size= sizeof(AVAES);34const int av_aes_size= sizeof(AVAES);
34 35
@@ -197,6 +198,34 @@ static void init_multbl2(uint32_t tbl[][256], const int c[4], · 162 unmodified lines
197 }198 }
198}199}
199 200
201static AVOnce aes_static_init = AV_ONCE_INIT;
202
203static 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
200// this is based on the reference AES code by Paulo Barreto and Vincent Rijmen229// this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
201int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)230int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
202{231{
@@ -204,34 +233,12 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt) · 1 unmodified line
204 uint8_t tk[8][4];233 uint8_t tk[8][4];
205 int KC = key_bits >> 5;234 int KC = key_bits >> 5;
206 int rounds = KC + 6;235 int rounds = KC + 6;
207 uint8_t log8[256];
208 uint8_t alog8[512];
209 236
210 a->crypt = decrypt ? aes_decrypt : aes_encrypt;237 a->crypt = decrypt ? aes_decrypt : aes_encrypt;
211 if (ARCH_X86)238 if (ARCH_X86)
212 ff_init_aes_x86(a, decrypt);239 ff_init_aes_x86(a, decrypt);
213 240
214 if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl) - 1][FF_ARRAY_ELEMS(enc_multbl[0]) - 1]) {241 ff_thread_once(&aes_static_init, aes_init_static);
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 }
235 242
236 if (key_bits != 128 && key_bits != 192 && key_bits != 256)243 if (key_bits != 128 && key_bits != 192 && key_bits != 256)
237 return AVERROR(EINVAL);244 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