Botan 3.7.1
Crypto and TLS for C&
cpuid_arm32.cpp
Go to the documentation of this file.
1/*
2* Runtime CPU detection for 32-bit ARM
3* (C) 2009,2010,2013,2017,2024 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/cpuid.h>
9
10#if defined(BOTAN_HAS_OS_UTILS)
11 #include <botan/internal/os_utils.h>
12#endif
13
14namespace Botan {
15
16#if defined(BOTAN_TARGET_ARCH_IS_ARM32)
17
18uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
19 uint32_t feat = 0;
20
21 #if defined(BOTAN_HAS_OS_UTILS)
22
23 if(auto auxval = OS::get_auxval_hwcap()) {
24 const auto [hwcap_neon, hwcap_crypto] = *auxval;
25
26 /*
27 * On systems with getauxval these bits should normally be defined
28 * in bits/auxv.h but some buggy? glibc installs seem to miss them.
29 * These following values are all fixed, for the Linux ELF format,
30 * so we just hardcode them in ARM_hwcap_bit enum.
31 */
32
33 enum class ARM_hwcap_bit : uint64_t {
34 NEON_bit = (1 << 12),
35 AES_bit = (1 << 0),
36 PMULL_bit = (1 << 1),
37 SHA1_bit = (1 << 2),
38 SHA2_bit = (1 << 3),
39 };
40
41 feat |= if_set(hwcap_neon, ARM_hwcap_bit::NEON_bit, CPUID::CPUID_ARM_NEON_BIT, allowed);
42
43 if(feat & CPUID::CPUID_ARM_NEON_BIT) {
44 feat |= if_set(hwcap_crypto, ARM_hwcap_bit::AES_bit, CPUID::CPUID_ARM_AES_BIT, allowed);
45 feat |= if_set(hwcap_crypto, ARM_hwcap_bit::PMULL_bit, CPUID::CPUID_ARM_PMULL_BIT, allowed);
46 feat |= if_set(hwcap_crypto, ARM_hwcap_bit::SHA1_bit, CPUID::CPUID_ARM_SHA1_BIT, allowed);
47 feat |= if_set(hwcap_crypto, ARM_hwcap_bit::SHA2_bit, CPUID::CPUID_ARM_SHA2_BIT, allowed);
48 }
49 }
50 #endif
51
52 return feat;
53}
54
55#endif
56
57} // namespace Botan
static uint32_t if_set(uint64_t cpuid, T flag, CPUID::CPUID_bits bit, uint32_t allowed)
Definition cpuid.h:402
std::optional< std::pair< unsigned long, unsigned long > > get_auxval_hwcap()
Definition os_utils.cpp:150