From cd3db0cfc51f9ea996ef0463d31ef9599d200e45 Mon Sep 17 00:00:00 2001 From: guoling Date: Wed, 3 Apr 2024 10:46:05 +0800 Subject: [PATCH] assume all ohos armv8 devices have aes+crc32 instr --- Core/MMKV.cpp | 6 +++--- Core/aes/openssl/openssl_aes.h | 11 +++++++++++ Core/aes/openssl/openssl_aes_core.cpp | 8 ++++---- Core/crc32/Checksum.h | 5 +++++ Core/crc32/crc32_armv8.cpp | 4 +++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Core/MMKV.cpp b/Core/MMKV.cpp index c71b06d0..3928efff 100644 --- a/Core/MMKV.cpp +++ b/Core/MMKV.cpp @@ -43,7 +43,7 @@ //#include #include -#if defined(__aarch64__) && defined(__linux) +#if defined(__aarch64__) && defined(__linux__) && !defined (MMKV_OHOS) # include # include #endif @@ -167,7 +167,7 @@ void initialize() { MMKVInfo("version %s, page size %d, arch %s", MMKV_VERSION, DEFAULT_MMAP_SIZE, MMKV_ABI); // get CPU status of ARMv8 extensions (CRC32, AES) -#if defined(__aarch64__) && defined(__linux__) +#if defined(__aarch64__) && defined(__linux__) && !defined (MMKV_OHOS) auto hwcaps = getauxval(AT_HWCAP); # ifndef MMKV_DISABLE_CRYPT if (hwcaps & HWCAP_AES) { @@ -188,7 +188,7 @@ void initialize() { MMKVInfo("armv8 CRC32 instructions is not supported"); } # endif // MMKV_USE_ARMV8_CRC32 -#endif // __aarch64__ && defined(__linux__) +#endif // __aarch64__ && defined(__linux__) && !defined (MMKV_OHOS) #if defined(MMKV_DEBUG) && !defined(MMKV_DISABLE_CRYPT) // AESCrypt::testAESCrypt(); diff --git a/Core/aes/openssl/openssl_aes.h b/Core/aes/openssl/openssl_aes.h index 50bb950c..73154617 100644 --- a/Core/aes/openssl/openssl_aes.h +++ b/Core/aes/openssl/openssl_aes.h @@ -77,6 +77,15 @@ extern "C" int openssl_aes_armv8_set_decrypt_key(const uint8_t *userKey, const i extern "C" void openssl_aes_armv8_encrypt(const uint8_t *in, uint8_t *out, const void *key); extern "C" void openssl_aes_armv8_decrypt(const uint8_t *in, uint8_t *out, const void *key); +#ifdef MMKV_OHOS + +#define AES_set_encrypt_key(userKey, bits, key) openssl_aes_armv8_set_encrypt_key(userKey, bits, key) +#define AES_set_decrypt_key(userKey, bits, key) openssl_aes_armv8_set_decrypt_key(userKey, bits, key) +#define AES_encrypt(in, out, key) openssl_aes_armv8_encrypt(in, out, key) +#define AES_decrypt(in, out, key) openssl_aes_armv8_decrypt(in, out, key) + +#else + typedef int (*aes_set_encrypt_t)(const uint8_t *userKey, const int bits, void *key); typedef int (*aes_set_decrypt_t)(const uint8_t *userKey, const int bits, void *key); typedef void (*aes_encrypt_t)(const uint8_t *in, uint8_t *out, const void *key); @@ -96,6 +105,8 @@ extern aes_decrypt_t AES_decrypt; } // namespace openssl +#endif // !MMKV_OHOS + #endif // __ARM_MAX_ARCH__ <= 7 #endif // __linux__ diff --git a/Core/aes/openssl/openssl_aes_core.cpp b/Core/aes/openssl/openssl_aes_core.cpp index 21406ada..7ebaf77c 100755 --- a/Core/aes/openssl/openssl_aes_core.cpp +++ b/Core/aes/openssl/openssl_aes_core.cpp @@ -45,16 +45,16 @@ namespace openssl { -#if (__ARM_MAX_ARCH__ > 7) && defined(__linux__) +#if (__ARM_MAX_ARCH__ > 7) && defined(__linux__) && !defined(MMKV_OHOS) aes_set_encrypt_t AES_set_encrypt_key = openssl::AES_C_set_encrypt_key; aes_set_decrypt_t AES_set_decrypt_key = openssl::AES_C_set_decrypt_key; aes_encrypt_t AES_encrypt = openssl::AES_C_encrypt; aes_encrypt_t AES_decrypt = openssl::AES_C_decrypt; -#endif // (__ARM_MAX_ARCH__ > 7 && defined(__linux__) +#endif // (__ARM_MAX_ARCH__ > 7 && defined(__linux__) && !defined(MMKV_OHOS) -#if (__ARM_MAX_ARCH__ <= 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__)) +#if (__ARM_MAX_ARCH__ <= 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__) && !defined(MMKV_OHOS)) /*- Te0[x] = S [x].[02, 01, 01, 03]; @@ -1037,7 +1037,7 @@ void AES_C_decrypt(const uint8_t *in, uint8_t *out, const void *k) { PUTU32(out + 12, s3); } -#endif // (__ARM_MAX_ARCH__ < 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__)) +#endif // (__ARM_MAX_ARCH__ < 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__) && !defined(MMKV_OHOS)) } // namespace openssl diff --git a/Core/crc32/Checksum.h b/Core/crc32/Checksum.h index 1416a8b3..cfafc840 100644 --- a/Core/crc32/Checksum.h +++ b/Core/crc32/Checksum.h @@ -53,9 +53,14 @@ namespace mmkv { uint32_t armv8_crc32(uint32_t crc, const uint8_t *buf, size_t len); } +# ifdef MMKV_OHOS +// getauxval(AT_HWCAP) in OHOS returns wrong value, we just assume all OHOS device have crc32 instr +# define CRC32 mmkv::armv8_crc32 +# else // have to check CPU's instruction set dynamically typedef uint32_t (*CRC32_Func_t)(uint32_t crc, const uint8_t *buf, size_t len); extern CRC32_Func_t CRC32; +# endif #else // defined(__aarch64__) && defined(__linux__) diff --git a/Core/crc32/crc32_armv8.cpp b/Core/crc32/crc32_armv8.cpp index 35d7c60b..81e7ff22 100644 --- a/Core/crc32/crc32_armv8.cpp +++ b/Core/crc32/crc32_armv8.cpp @@ -22,7 +22,9 @@ #ifdef MMKV_USE_ARMV8_CRC32 -# if MMKV_EMBED_ZLIB +# ifdef CRC32 +// nothing to do +# elif MMKV_EMBED_ZLIB static inline uint32_t _crc32Wrap(uint32_t crc, const uint8_t *buf, size_t len) { return static_cast(zlib::crc32(crc, buf, static_cast(len)));