From 064cb6dad740cee044302252013f020caeb33352 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 30 Jul 2020 03:45:40 +0000 Subject: [PATCH] AES-GMAC for ARM crypto extensions (pmull) --- core/AES.cpp | 159 +++++++++++++++++++++++++++++++++++++++---------- core/AES.hpp | 9 ++- core/Tests.cpp | 17 +++++- core/Utils.cpp | 4 ++ 4 files changed, 155 insertions(+), 34 deletions(-) diff --git a/core/AES.cpp b/core/AES.cpp index d0109d013..12a20736c 100644 --- a/core/AES.cpp +++ b/core/AES.cpp @@ -20,6 +20,35 @@ namespace ZeroTier { namespace { +#ifdef ZT_AES_NEON + +ZT_INLINE uint8x16_t s_clmul_armneon_crypto(uint8x16_t a8, const uint8x16_t y, const uint8_t b[16]) noexcept +{ + const uint8x16_t p = vreinterpretq_u8_u64(vdupq_n_u64(0x0000000000000087)); + const uint8x16_t z = vdupq_n_u8(0); + uint8x16_t b8 = vrbitq_u8(veorq_u8(vld1q_u8(b), y)); + uint8x16_t r0, r1, t0, t1; + __asm__ __volatile__("pmull %0.1q, %1.1d, %2.1d \n\t" : "=w" (r0) : "w" (a8), "w" (b8)); + __asm__ __volatile__("pmull2 %0.1q, %1.2d, %2.2d \n\t" :"=w" (r1) : "w" (a8), "w" (b8)); + t0 = vextq_u8(b8, b8, 8); + __asm__ __volatile__("pmull %0.1q, %1.1d, %2.1d \n\t" : "=w" (t1) : "w" (a8), "w" (t0)); + __asm__ __volatile__("pmull2 %0.1q, %1.2d, %2.2d \n\t" :"=w" (t0) : "w" (a8), "w" (t0)); + t0 = veorq_u8(t0, t1); + t1 = vextq_u8(z, t0, 8); + r0 = veorq_u8(r0, t1); + t1 = vextq_u8(t0, z, 8); + r1 = veorq_u8(r1, t1); + __asm__ __volatile__("pmull2 %0.1q, %1.2d, %2.2d \n\t" :"=w" (t0) : "w" (r1), "w" (p)); + t1 = vextq_u8(t0, z, 8); + r1 = veorq_u8(r1, t1); + t1 = vextq_u8(z, t0, 8); + r0 = veorq_u8(r0, t1); + __asm__ __volatile__("pmull %0.1q, %1.1d, %2.1d \n\t" : "=w" (t0) : "w" (r1), "w" (p)); + return vrbitq_u8(veorq_u8(r0, t0)); +} + +#endif + #ifdef ZT_HAVE_UINT128 ZT_INLINE void s_bmul64(const uint64_t x, const uint64_t y, uint64_t &r_high, uint64_t &r_low) noexcept @@ -264,7 +293,41 @@ void AES::GMAC::update(const void *const data, unsigned int len) noexcept return; } -#endif +#endif // ZT_AES_AESNI + +#ifdef ZT_AES_NEON + if (Utils::ARMCAP.pmull) { + uint8x16_t y = vld1q_u8(reinterpret_cast(_y)); + const uint8x16_t h = _aes._k.neon.h; + + if (_rp) { + for(;;) { + if (!len) + return; + --len; + _r[_rp++] = *(in++); + if (_rp == 16) { + y = s_clmul_armneon_crypto(h, y, _r); + break; + } + } + } + + while (len >= 16) { + y = s_clmul_armneon_crypto(h, y, in); + in += 16; + len -= 16; + } + + vst1q_u8(reinterpret_cast(_y), y); + + for (unsigned int i = 0; i < len; ++i) + _r[i] = in[i]; + _rp = len; // len is always less than 16 here + + return; + } +#endif // ZT_AES_NEON const uint64_t h0 = _aes._k.sw.h[0]; const uint64_t h1 = _aes._k.sw.h[1]; @@ -294,12 +357,12 @@ void AES::GMAC::update(const void *const data, unsigned int len) noexcept len -= 16; } + _y[0] = y0; + _y[1] = y1; + for (unsigned int i = 0; i < len; ++i) _r[i] = in[i]; _rp = len; // len is always less than 16 here - - _y[0] = y0; - _y[1] = y1; } void AES::GMAC::finish(uint8_t tag[16]) noexcept @@ -375,7 +438,39 @@ void AES::GMAC::finish(uint8_t tag[16]) noexcept return; } +#endif // ZT_AES_AESNI + +#ifdef ZT_AES_NEON + if (Utils::ARMCAP.pmull) { + uint64_t tmp[2]; + uint8x16_t y = vld1q_u8(reinterpret_cast(_y)); + const uint8x16_t h = _aes._k.neon.h; + + if (_rp) { + while (_rp < 16) + _r[_rp++] = 0; + y = s_clmul_armneon_crypto(h, y, _r); + } + + tmp[0] = Utils::hton((uint64_t)_len << 3U); + tmp[1] = 0; + y = s_clmul_armneon_crypto(h, y, reinterpret_cast(tmp)); + + Utils::copy< 12 >(tmp, _iv); +#if __BYTE_ORDER == __BIG_ENDIAN + reinterpret_cast(tmp)[3] = 0x00000001; +#else + reinterpret_cast(tmp)[3] = 0x01000000; #endif + _aes.encrypt(tmp, tmp); + + uint8x16_t yy = y; + Utils::storeMachineEndian< uint64_t >(tag, tmp[0] ^ reinterpret_cast(&yy)[0]); + Utils::storeMachineEndian< uint64_t >(tag + 8, tmp[1] ^ reinterpret_cast(&yy)[1]); + + return; + } +#endif // ZT_AES_NEON const uint64_t h0 = _aes._k.sw.h[0]; const uint64_t h1 = _aes._k.sw.h[1]; @@ -400,7 +495,7 @@ void AES::GMAC::finish(uint8_t tag[16]) noexcept #else reinterpret_cast(iv2)[3] = 0x01000000; #endif - _aes._encryptSW(reinterpret_cast(iv2), reinterpret_cast(iv2)); + _aes.encrypt(iv2, iv2); Utils::storeMachineEndian< uint64_t >(tag, iv2[0] ^ y0); Utils::storeMachineEndian< uint64_t >(tag + 8, iv2[1] ^ y1); @@ -829,8 +924,8 @@ void AES::CTR::crypt(const void *const input, unsigned int len) noexcept uint8x16_t pt = vld1q_u8(reinterpret_cast(in)); vst1q_u8(reinterpret_cast(out), veorq_u8(pt, tmp)); in += 16; - len -= 16; - out += 16; + len -= 16; + out += 16; } // Any remaining input is placed in _out. This will be picked up and crypted @@ -1024,10 +1119,7 @@ void AES::_initSW(const uint8_t key[32]) noexcept rk += 8; } - uint64_t zero[2]; - zero[0] = 0; - zero[1] = 0; - _encryptSW((const uint8_t *)zero, (uint8_t *)_k.sw.h); + _encryptSW((const uint8_t *)Utils::ZERO256, (uint8_t *)_k.sw.h); _k.sw.h[0] = Utils::ntoh(_k.sw.h[0]); _k.sw.h[1] = Utils::ntoh(_k.sw.h[1]); @@ -1342,33 +1434,38 @@ void AES::_init_armneon_crypto(const uint8_t key[32]) noexcept { static const uint8_t s_sbox[256] = {0x63, 0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65, 0x7a, 0xae, 0x08,0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,0xb0, 0x54, 0xbb, 0x16}; - { - uint32_t *const w = reinterpret_cast(_k.neon.ek); + uint64_t h[2]; + uint32_t *const w = reinterpret_cast(_k.neon.ek); - for (unsigned int i=0;i(&(_k.neon.h), h); + _k.neon.h = vrbitq_u8(_k.neon.h); + _k.sw.h[0] = Utils::ntoh(h[0]); + _k.sw.h[1] = Utils::ntoh(h[1]); } void AES::_encrypt_armneon_crypto(const void *const in, void *const out) const noexcept diff --git a/core/AES.hpp b/core/AES.hpp index 765b0dcc2..8a2ef811f 100644 --- a/core/AES.hpp +++ b/core/AES.hpp @@ -43,8 +43,12 @@ public: { #ifdef ZT_AES_AESNI return Utils::CPUID.aes; +#else +#ifdef ZT_AES_NEON + return Utils::ARMCAP.aes; #else return false; +#endif #endif } @@ -527,6 +531,7 @@ private: #ifdef ZT_AES_NEON struct { + uint64_t hsw[2]; // in case it has AES but not PMULL, not sure if that ever happens uint8x16_t ek[15]; uint8x16_t dk[15]; uint8x16_t h; @@ -549,8 +554,8 @@ private: #ifdef ZT_AES_NEON void _init_armneon_crypto(const uint8_t key[32]) noexcept; - void _encrypt_armneon_crypto(const void *const in, void *const out) const noexcept; - void _decrypt_armneon_crypto(const void *const in, void *const out) const noexcept; + void _encrypt_armneon_crypto(const void *in, void *out) const noexcept; + void _decrypt_armneon_crypto(const void *in, void *out) const noexcept; #endif }; diff --git a/core/Tests.cpp b/core/Tests.cpp index 34fcf975e..a5c569739 100644 --- a/core/Tests.cpp +++ b/core/Tests.cpp @@ -452,6 +452,15 @@ extern "C" const char *ZTT_general() Utils::CPUID.vpclmulqdq); #endif +#ifdef ZT_ARCH_ARM_HAS_NEON + ZT_T_PRINTF("[general] ARM capabilities: aes=%d crc32=%d pmull=%d sha1=%d sha2=%d" ZT_EOL_S, + Utils::ARMCAP.aes, + Utils::ARMCAP.crc32, + Utils::ARMCAP.pmull, + Utils::ARMCAP.sha1, + Utils::ARMCAP.sha2); +#endif + { ZT_T_PRINTF("[general] Testing Utils::countBits() functions... "); uint32_t i32 = 0; @@ -1114,7 +1123,13 @@ extern "C" const char *ZTT_crypto() gmac.update(AES_GMAC_VECTOR_0_IN, sizeof(AES_GMAC_VECTOR_0_IN)); gmac.finish(tag); if (memcmp(tag, AES_GMAC_VECTOR_0_OUT, 16) != 0) { - ZT_T_PRINTF("FAILED (test vector 0)" ZT_EOL_S); + ZT_T_PRINTF("FAILED (test vector 0, "); + for(int i=0;i<16;++i) + ZT_T_PRINTF("%.2x",(unsigned int)AES_GMAC_VECTOR_0_OUT[i]); + ZT_T_PRINTF(" != "); + for(int i=0;i<16;++i) + ZT_T_PRINTF("%.2x",(unsigned int)tag[i]); + ZT_T_PRINTF(")" ZT_EOL_S); return "AES-GMAC test vector 0 failed"; } } diff --git a/core/Utils.cpp b/core/Utils.cpp index 02daf6cd4..1d8f9d9fb 100644 --- a/core/Utils.cpp +++ b/core/Utils.cpp @@ -41,6 +41,7 @@ namespace Utils { #ifdef ZT_ARCH_ARM_HAS_NEON ARMCapabilities::ARMCapabilities() noexcept { +#ifdef HWCAP2_AES if (sizeof(void *) == 4) { const long hwcaps2 = getauxval(AT_HWCAP2); this->aes = (hwcaps2 & HWCAP2_AES) != 0; @@ -49,13 +50,16 @@ ARMCapabilities::ARMCapabilities() noexcept this->sha1 = (hwcaps2 & HWCAP2_SHA1) != 0; this->sha2 = (hwcaps2 & HWCAP2_SHA2) != 0; } else { +#endif const long hwcaps = getauxval(AT_HWCAP); this->aes = (hwcaps & HWCAP_AES) != 0; this->crc32 = (hwcaps & HWCAP_CRC32) != 0; this->pmull = (hwcaps & HWCAP_PMULL) != 0; this->sha1 = (hwcaps & HWCAP_SHA1) != 0; this->sha2 = (hwcaps & HWCAP_SHA2) != 0; +#ifdef HWCAP2_AES } +#endif } const ARMCapabilities ARMCAP;