Unified · Split
| @@ -29,6 +29,7 @@ · 28 unmodified lines | ||
| 29 | 29 | #include "intreadwrite.h" |
| 30 | 30 | #include "macros.h" |
| 31 | 31 | #include "mem.h" |
| 32 | +#include "thread.h" | |
| 32 | 33 | |
| 33 | 34 | const 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 | |
| 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 | + | |
| 200 | 229 | // this is based on the reference AES code by Paulo Barreto and Vincent Rijmen |
| 201 | 230 | int 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 | 233 | uint8_t tk[8][4]; |
| 205 | 234 | int KC = key_bits >> 5; |
| 206 | 235 | int rounds = KC + 6; |
| 207 | − uint8_t log8[256]; | |
| 208 | − uint8_t alog8[512]; | |
| 209 | 236 | |
| 210 | 237 | a->crypt = decrypt ? aes_decrypt : aes_encrypt; |
| 211 | 238 | if (ARCH_X86) |
| 212 | 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]) { | |
| 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); | |
| 235 | 242 | |
| 236 | 243 | if (key_bits != 128 && key_bits != 192 && key_bits != 256) |
| 237 | 244 | return AVERROR(EINVAL); |