From: James Almer <jamrial@gmail.com> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] avutil/aes: use pthread_once to fill the static tables Date: Sun, 6 Apr 2025 21:00:04 -0300 Message-ID: <20250407000004.7306-1-jamrial@gmail.com> (raw) Signed-off-by: James Almer <jamrial@gmail.com> --- libavutil/aes.c | 53 ++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/libavutil/aes.c b/libavutil/aes.c index 1f4cceb700..5f31412149 100644 --- a/libavutil/aes.c +++ b/libavutil/aes.c @@ -29,6 +29,7 @@ #include "intreadwrite.h" #include "macros.h" #include "mem.h" +#include "thread.h" 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 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]; int KC = key_bits >> 5; int rounds = KC + 6; - uint8_t log8[256]; - uint8_t alog8[512]; 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; - 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); - } + ff_thread_once(&aes_static_init, aes_init_static); if (key_bits != 128 && key_bits != 192 && key_bits != 256) return AVERROR(EINVAL); -- 2.49.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2025-04-07 0:00 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-04-07 0:00 James Almer [this message] 2025-04-07 16:26 ` [FFmpeg-devel] [PATCH 2/2] avutil/x86/aes: remove a few branches James Almer
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250407000004.7306-1-jamrial@gmail.com \ --to=jamrial@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git