Forráskód Böngészése

ARM auto-detection (unfinished)

Adam Ierymenko 5 éve
szülő
commit
d5afba2610
4 módosított fájl, 46 hozzáadás és 11 törlés
  1. 1 4
      core/AES.cpp
  2. 3 5
      core/AES.hpp
  3. 28 2
      core/Utils.cpp
  4. 14 0
      core/Utils.hpp

+ 1 - 4
core/AES.cpp

@@ -745,7 +745,7 @@ void AES::CTR::crypt(const void *const input, unsigned int len) noexcept
 #endif // ZT_AES_AESNI
 #endif // ZT_AES_AESNI
 
 
 #ifdef ZT_AES_NEON
 #ifdef ZT_AES_NEON
-	if (s_hasNeonAes) {
+	if (Utils::ARMCAP.aes) {
 		uint8x16_t dd = vld1q_u8(reinterpret_cast<uint8_t *>(_ctr));
 		uint8x16_t dd = vld1q_u8(reinterpret_cast<uint8_t *>(_ctr));
 		const uint32x4_t one = {0,0,0,1};
 		const uint32x4_t one = {0,0,0,1};
 
 
@@ -1332,9 +1332,6 @@ void AES::_decrypt_aesni(const void *in, void *out) const noexcept
 
 
 #ifdef ZT_AES_NEON
 #ifdef ZT_AES_NEON
 
 
-const bool AES::s_hasNeonAes = true;
-const bool AES::s_hasNeonGcm = true;
-
 #define ZT_INIT_ARMNEON_CRYPTO_SUBWORD(w) ((uint32_t)s_sbox[w & 0xffU] + ((uint32_t)s_sbox[(w >> 8U) & 0xffU] << 8U) + ((uint32_t)s_sbox[(w >> 16U) & 0xffU] << 16U) + ((uint32_t)s_sbox[(w >> 24U) & 0xffU] << 24U))
 #define ZT_INIT_ARMNEON_CRYPTO_SUBWORD(w) ((uint32_t)s_sbox[w & 0xffU] + ((uint32_t)s_sbox[(w >> 8U) & 0xffU] << 8U) + ((uint32_t)s_sbox[(w >> 16U) & 0xffU] << 16U) + ((uint32_t)s_sbox[(w >> 24U) & 0xffU] << 24U))
 #define ZT_INIT_ARMNEON_CRYPTO_ROTWORD(w) (((w) << 8U) | ((w) >> 24U))
 #define ZT_INIT_ARMNEON_CRYPTO_ROTWORD(w) (((w) << 8U) | ((w) >> 24U))
 #define ZT_INIT_ARMNEON_CRYPTO_NK 8
 #define ZT_INIT_ARMNEON_CRYPTO_NK 8

+ 3 - 5
core/AES.hpp

@@ -79,7 +79,7 @@ public:
 		}
 		}
 #endif
 #endif
 #ifdef ZT_AES_NEON
 #ifdef ZT_AES_NEON
-		if (s_hasNeonAes) {
+		if (Utils::ARMCAP.aes) {
 			_init_armneon_crypto(reinterpret_cast<const uint8_t *>(key));
 			_init_armneon_crypto(reinterpret_cast<const uint8_t *>(key));
 			return;
 			return;
 		}
 		}
@@ -102,7 +102,7 @@ public:
 		}
 		}
 #endif
 #endif
 #ifdef ZT_AES_NEON
 #ifdef ZT_AES_NEON
-		if (s_hasNeonAes) {
+		if (Utils::ARMCAP.aes) {
 			_encrypt_armneon_crypto(in, out);
 			_encrypt_armneon_crypto(in, out);
 			return;
 			return;
 		}
 		}
@@ -125,7 +125,7 @@ public:
 		}
 		}
 #endif
 #endif
 #ifdef ZT_AES_NEON
 #ifdef ZT_AES_NEON
-		if (s_hasNeonAes) {
+		if (Utils::ARMCAP.aes) {
 			_decrypt_armneon_crypto(in, out);
 			_decrypt_armneon_crypto(in, out);
 			return;
 			return;
 		}
 		}
@@ -548,8 +548,6 @@ private:
 #endif
 #endif
 
 
 #ifdef ZT_AES_NEON
 #ifdef ZT_AES_NEON
-	static const bool s_hasNeonAes;
-	static const bool s_hasNeonGcm;
 	void _init_armneon_crypto(const uint8_t key[32]) noexcept;
 	void _init_armneon_crypto(const uint8_t key[32]) noexcept;
 	void _encrypt_armneon_crypto(const void *const in, void *const out) const 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 _decrypt_armneon_crypto(const void *const in, void *const out) const noexcept;

+ 28 - 2
core/Utils.cpp

@@ -17,11 +17,9 @@
 #include "SHA512.hpp"
 #include "SHA512.hpp"
 
 
 #ifdef __UNIX_LIKE__
 #ifdef __UNIX_LIKE__
-
 #include <unistd.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <fcntl.h>
 #include <sys/uio.h>
 #include <sys/uio.h>
-
 #endif
 #endif
 
 
 #include <time.h>
 #include <time.h>
@@ -31,10 +29,38 @@
 #include <wincrypt.h>
 #include <wincrypt.h>
 #endif
 #endif
 
 
+#if defined(ZT_ARCH_ARM_HAS_NEON) && defined(__LINUX__)
+#include <sys/auxv.h>
+#include <asm/hwcap.h>
+#endif
+
 namespace ZeroTier {
 namespace ZeroTier {
 
 
 namespace Utils {
 namespace Utils {
 
 
+#ifdef ZT_ARCH_ARM_HAS_NEON
+ARMCapabilities::ARMCapabilities() noexcept
+{
+	if (sizeof(void *) == 4) {
+		const long hwcaps2 = getauxval(AT_HWCAP2);
+		this->aes = (hwcaps2 & HWCAP2_AES) != 0;
+		this->crc32 = (hwcaps2 & HWCAP2_CRC32) != 0;
+		this->pmull = (hwcaps2 & HWCAP2_PMULL) != 0;
+		this->sha1 = (hwcaps2 & HWCAP2_SHA1) != 0;
+		this->sha2 = (hwcaps2 & HWCAP2_SHA2) != 0;
+	} else {
+		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;
+	}
+}
+
+const ARMCapabilities ARMCAP;
+#endif
+
 #ifdef ZT_ARCH_X64
 #ifdef ZT_ARCH_X64
 
 
 CPUIDRegisters::CPUIDRegisters() noexcept
 CPUIDRegisters::CPUIDRegisters() noexcept

+ 14 - 0
core/Utils.hpp

@@ -56,6 +56,20 @@ namespace Utils {
 #define ZT_ROR32(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
 #define ZT_ROR32(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
 #define ZT_ROL32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
 #define ZT_ROL32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
 
 
+#ifdef ZT_ARCH_ARM_HAS_NEON
+struct ARMCapabilities
+{
+	ARMCapabilities() noexcept;
+
+	bool aes;
+	bool crc32;
+	bool pmull;
+	bool sha1;
+	bool sha2;
+};
+extern const ARMCapabilities ARMCAP;
+#endif
+
 #ifdef ZT_ARCH_X64
 #ifdef ZT_ARCH_X64
 struct CPUIDRegisters
 struct CPUIDRegisters
 {
 {