You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-08-04 22:03:09 +02:00
avutil/aes: use pthread_once to fill the static tables
Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
@@ -29,6 +29,7 @@
|
|||||||
#include "intreadwrite.h"
|
#include "intreadwrite.h"
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
|
#include "thread.h"
|
||||||
|
|
||||||
const int av_aes_size= sizeof(AVAES);
|
const int av_aes_size= sizeof(AVAES);
|
||||||
|
|
||||||
@@ -197,6 +198,34 @@ static void init_multbl2(uint32_t tbl[][256], const int c[4],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static AVOnce aes_static_init = AV_ONCE_INIT;
|
||||||
|
|
||||||
|
static void aes_init_static(void)
|
||||||
|
{
|
||||||
|
uint8_t log8[256];
|
||||||
|
uint8_t alog8[512];
|
||||||
|
int i, j = 1;
|
||||||
|
|
||||||
|
for (i = 0; i < 255; i++) {
|
||||||
|
alog8[i] = alog8[i + 255] = j;
|
||||||
|
log8[j] = i;
|
||||||
|
j ^= j + j;
|
||||||
|
if (j > 255)
|
||||||
|
j ^= 0x11B;
|
||||||
|
}
|
||||||
|
for (i = 0; i < 256; i++) {
|
||||||
|
j = i ? alog8[255 - log8[i]] : 0;
|
||||||
|
j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
|
||||||
|
j = (j ^ (j >> 8) ^ 99) & 255;
|
||||||
|
inv_sbox[j] = i;
|
||||||
|
sbox[i] = j;
|
||||||
|
}
|
||||||
|
init_multbl2(dec_multbl, (const int[4]) { 0xe, 0x9, 0xd, 0xb },
|
||||||
|
log8, alog8, inv_sbox);
|
||||||
|
init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
|
||||||
|
log8, alog8, sbox);
|
||||||
|
}
|
||||||
|
|
||||||
// this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
|
// 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 av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
|
||||||
{
|
{
|
||||||
@@ -204,34 +233,12 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
|
|||||||
uint8_t tk[8][4];
|
uint8_t tk[8][4];
|
||||||
int KC = key_bits >> 5;
|
int KC = key_bits >> 5;
|
||||||
int rounds = KC + 6;
|
int rounds = KC + 6;
|
||||||
uint8_t log8[256];
|
|
||||||
uint8_t alog8[512];
|
|
||||||
|
|
||||||
a->crypt = decrypt ? aes_decrypt : aes_encrypt;
|
a->crypt = decrypt ? aes_decrypt : aes_encrypt;
|
||||||
if (ARCH_X86)
|
if (ARCH_X86)
|
||||||
ff_init_aes_x86(a, decrypt);
|
ff_init_aes_x86(a, decrypt);
|
||||||
|
|
||||||
if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl) - 1][FF_ARRAY_ELEMS(enc_multbl[0]) - 1]) {
|
ff_thread_once(&aes_static_init, aes_init_static);
|
||||||
j = 1;
|
|
||||||
for (i = 0; i < 255; i++) {
|
|
||||||
alog8[i] = alog8[i + 255] = j;
|
|
||||||
log8[j] = i;
|
|
||||||
j ^= j + j;
|
|
||||||
if (j > 255)
|
|
||||||
j ^= 0x11B;
|
|
||||||
}
|
|
||||||
for (i = 0; i < 256; i++) {
|
|
||||||
j = i ? alog8[255 - log8[i]] : 0;
|
|
||||||
j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
|
|
||||||
j = (j ^ (j >> 8) ^ 99) & 255;
|
|
||||||
inv_sbox[j] = i;
|
|
||||||
sbox[i] = j;
|
|
||||||
}
|
|
||||||
init_multbl2(dec_multbl, (const int[4]) { 0xe, 0x9, 0xd, 0xb },
|
|
||||||
log8, alog8, inv_sbox);
|
|
||||||
init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
|
|
||||||
log8, alog8, sbox);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key_bits != 128 && key_bits != 192 && key_bits != 256)
|
if (key_bits != 128 && key_bits != 192 && key_bits != 256)
|
||||||
return AVERROR(EINVAL);
|
return AVERROR(EINVAL);
|
||||||
|
Reference in New Issue
Block a user