X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/e5b61a8dec3586f96d25bd3ef454176526ff0f69..HEAD:/symm/rijndael-base.c diff --git a/symm/rijndael-base.c b/symm/rijndael-base.c index bfab63a2..2f651918 100644 --- a/symm/rijndael-base.c +++ b/symm/rijndael-base.c @@ -27,12 +27,15 @@ /*----- Header files ------------------------------------------------------*/ +#include "config.h" + #include #include #include #include "blkc.h" +#include "dispatch.h" #include "gcipher.h" #include "rijndael.h" #include "rijndael-base.h" @@ -55,25 +58,14 @@ const octet rijndael_keysz[] = { KSZ_RANGE, RIJNDAEL_KEYSZ, 4, 32, 4 }; * Use: Low-level key-scheduling. */ -void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz) +static void simple_setup(rijndael_ctx *k, unsigned nb, + const void *buf, unsigned nk) { - unsigned nk, nr, nw; + unsigned nr = k->nr, nw; unsigned i, j, jj; const octet *p; uint32 ww; - /* --- Sort out the key size --- */ - - KSZ_ASSERT(rijndael, sz); - nk = sz / 4; - - /* --- Select the number of rounds --- */ - - nr = (nk > nb ? nk : nb) + 6; - if (nr < 10) - nr = 10; - k->nr = nr; - /* --- Fetch the first key words out --- */ p = buf; @@ -120,4 +112,60 @@ void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz) k->wi[i] = k->w[j + jj++]; } +CPU_DISPATCH(static, EMPTY, void, setup, + (rijndael_ctx *k, unsigned nb, const void *buf, unsigned nk), + (k, nb, buf, nk), pick_setup, simple_setup) + +#if CPUFAM_X86 || CPUFAM_AMD64 +extern setup__functype rijndael_setup_x86ish_aesni; +extern setup__functype rijndael_setup_x86ish_aesni_avx; +#endif +#if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO +extern setup__functype rijndael_setup_arm_crypto; +#endif +#if CPUFAM_ARM64 +extern setup__functype rijndael_setup_arm64_crypto; +#endif + +static setup__functype *pick_setup(void) +{ +#if CPUFAM_X86 || CPUFAM_AMD64 + DISPATCH_PICK_COND(rijndael_setup, rijndael_setup_x86ish_aesni_avx, + cpu_feature_p(CPUFEAT_X86_AVX) && + cpu_feature_p(CPUFEAT_X86_AESNI)); + DISPATCH_PICK_COND(rijndael_setup, rijndael_setup_x86ish_aesni, + cpu_feature_p(CPUFEAT_X86_AESNI)); +#endif +#if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO + DISPATCH_PICK_COND(rijndael_setup, rijndael_setup_arm_crypto, + cpu_feature_p(CPUFEAT_ARM_AES)); +#endif +#if CPUFAM_ARM64 + DISPATCH_PICK_COND(rijndael_setup, rijndael_setup_arm64_crypto, + cpu_feature_p(CPUFEAT_ARM_AES)); +#endif + DISPATCH_PICK_FALLBACK(rijndael_setup, simple_setup); +} + +void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz) +{ + unsigned nk, nr; + + /* --- Sort out the key size --- */ + + KSZ_ASSERT(rijndael, sz); + nk = sz / 4; + + /* --- Select the number of rounds --- */ + + nr = (nk > nb ? nk : nb) + 6; + if (nr < 10) + nr = 10; + k->nr = nr; + + /* --- Do the main setup --- */ + + setup(k, nb, buf, nk); +} + /*----- That's all, folks -------------------------------------------------*/