1 // Copyright 2021 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "internal.h"
16 
17 #if defined(OPENSSL_AARCH64) && defined(OPENSSL_APPLE) && \
18     !defined(OPENSSL_STATIC_ARMCAP) && !defined(OPENSSL_NO_ASM)
19 
20 #include <sys/sysctl.h>
21 #include <sys/types.h>
22 
23 
has_hw_feature(const char * name)24 static int has_hw_feature(const char *name) {
25   int value;
26   size_t len = sizeof(value);
27   if (sysctlbyname(name, &value, &len, NULL, 0) != 0) {
28     return 0;
29   }
30   if (len != sizeof(int)) {
31     // This should not happen. All the values queried should be integer-valued.
32     assert(0);
33     return 0;
34   }
35 
36   // Per sys/sysctl.h:
37   //
38   //   Selectors that return errors are not support on the system. Supported
39   //   features will return 1 if they are recommended or 0 if they are supported
40   //   but are not expected to help performance. Future versions of these
41   //   selectors may return larger values as necessary so it is best to test for
42   //   non zero.
43   return value != 0;
44 }
45 
OPENSSL_cpuid_setup(void)46 void OPENSSL_cpuid_setup(void) {
47   // Apple ARM64 platforms have NEON and cryptography extensions available
48   // statically, so we do not need to query them. In particular, there sometimes
49   // are no sysctls corresponding to such features. See below.
50 #if !defined(__ARM_NEON) || !defined(__ARM_FEATURE_AES) || \
51     !defined(__ARM_FEATURE_SHA2)
52 #error "NEON and crypto extensions should be statically available."
53 #endif
54   OPENSSL_armcap_P =
55       ARMV7_NEON | ARMV8_AES | ARMV8_PMULL | ARMV8_SHA1 | ARMV8_SHA256;
56 
57   // See Apple's documentation for sysctl names:
58   // https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
59   //
60   // The new feature names, e.g. "hw.optional.arm.FEAT_SHA512", are only
61   // available in macOS 12. For compatibility with macOS 11, we also support
62   // the old names. The old names don't have values for features like FEAT_AES,
63   // so instead we detect them statically above.
64   //
65   // If querying new sysctls, update the Chromium sandbox definition. See
66   // https://crrev.com/c/4415225.
67   if (has_hw_feature("hw.optional.arm.FEAT_SHA512") ||
68       has_hw_feature("hw.optional.armv8_2_sha512")) {
69     OPENSSL_armcap_P |= ARMV8_SHA512;
70   }
71 }
72 
73 #endif  // OPENSSL_AARCH64 && OPENSSL_APPLE && !OPENSSL_STATIC_ARMCAP
74