avutil/aes: use pthread_once to fill the static tables

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer
2025-04-06 20:57:58 -03:00
parent 890b8da1ce
commit aeed747f41

View File

@ -29,6 +29,7 @@
#include "intreadwrite.h"
#include "macros.h"
#include "mem.h"
#include "thread.h"
const int av_aes_size= sizeof(AVAES);
@ -197,22 +198,14 @@ static void init_multbl2(uint32_t tbl[][256], const int c[4],
}
}
// this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
static AVOnce aes_static_init = AV_ONCE_INIT;
static void aes_init_static(void)
{
int i, j, t, rconpointer = 0;
uint8_t tk[8][4];
int KC = key_bits >> 5;
int rounds = KC + 6;
uint8_t log8[256];
uint8_t alog8[512];
int i, j = 1;
a->crypt = decrypt ? aes_decrypt : aes_encrypt;
if (ARCH_X86)
ff_init_aes_x86(a, decrypt);
if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl) - 1][FF_ARRAY_ELEMS(enc_multbl[0]) - 1]) {
j = 1;
for (i = 0; i < 255; i++) {
alog8[i] = alog8[i + 255] = j;
log8[j] = i;
@ -233,6 +226,20 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
log8, alog8, sbox);
}
// this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
{
int i, j, t, rconpointer = 0;
uint8_t tk[8][4];
int KC = key_bits >> 5;
int rounds = KC + 6;
a->crypt = decrypt ? aes_decrypt : aes_encrypt;
if (ARCH_X86)
ff_init_aes_x86(a, decrypt);
ff_thread_once(&aes_static_init, aes_init_static);
if (key_bits != 128 && key_bits != 192 && key_bits != 256)
return AVERROR(EINVAL);