assume all ohos armv8 devices have aes+crc32 instr

This commit is contained in:
guoling
2024-04-03 10:46:05 +08:00
parent c33213f327
commit cd3db0cfc5
5 changed files with 26 additions and 8 deletions
+3 -3
View File
@@ -43,7 +43,7 @@
//#include <unistd.h>
#include <cassert>
#if defined(__aarch64__) && defined(__linux)
#if defined(__aarch64__) && defined(__linux__) && !defined (MMKV_OHOS)
# include <asm/hwcap.h>
# include <sys/auxv.h>
#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();
+11
View File
@@ -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__
+4 -4
View File
@@ -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
+5
View File
@@ -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__)
+3 -1
View File
@@ -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<uint32_t>(zlib::crc32(crc, buf, static_cast<uInt>(len)));