1 /* Copyright (c) 2021, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include "internal.h"
16
17 #if defined(OPENSSL_AARCH64) && defined(OPENSSL_APPLE) && \
18 !defined(OPENSSL_STATIC_ARMCAP)
19
20 #include <sys/sysctl.h>
21 #include <sys/types.h>
22
23 #include <openssl/arm_arch.h>
24
25
has_hw_feature(const char * name)26 static int has_hw_feature(const char *name) {
27 int value;
28 size_t len = sizeof(value);
29 if (sysctlbyname(name, &value, &len, NULL, 0) != 0) {
30 return 0;
31 }
32 if (len != sizeof(int)) {
33 // This should not happen. All the values queried should be integer-valued.
34 assert(0);
35 return 0;
36 }
37
38 // Per sys/sysctl.h:
39 //
40 // Selectors that return errors are not support on the system. Supported
41 // features will return 1 if they are recommended or 0 if they are supported
42 // but are not expected to help performance. Future versions of these
43 // selectors may return larger values as necessary so it is best to test for
44 // non zero.
45 return value != 0;
46 }
47
OPENSSL_cpuid_setup(void)48 void OPENSSL_cpuid_setup(void) {
49 // Apple ARM64 platforms have NEON and cryptography extensions available
50 // statically, so we do not need to query them. In particular, there sometimes
51 // are no sysctls corresponding to such features. See below.
52 #if !defined(__ARM_NEON) || !defined(__ARM_FEATURE_AES) || \
53 !defined(__ARM_FEATURE_SHA2)
54 #error "NEON and crypto extensions should be statically available."
55 #endif
56 OPENSSL_armcap_P =
57 ARMV7_NEON | ARMV8_AES | ARMV8_PMULL | ARMV8_SHA1 | ARMV8_SHA256;
58
59 // See Apple's documentation for sysctl names:
60 // https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
61 //
62 // The new feature names, e.g. "hw.optional.arm.FEAT_SHA512", are only
63 // available in macOS 12. For compatibility with macOS 11, we also support
64 // the old names. The old names don't have values for features like FEAT_AES,
65 // so instead we detect them statically above.
66 //
67 // If querying new sysctls, update the Chromium sandbox definition. See
68 // https://crrev.com/c/4415225.
69 if (has_hw_feature("hw.optional.arm.FEAT_SHA512") ||
70 has_hw_feature("hw.optional.armv8_2_sha512")) {
71 OPENSSL_armcap_P |= ARMV8_SHA512;
72 }
73 }
74
75 #endif // OPENSSL_AARCH64 && OPENSSL_APPLE && !OPENSSL_STATIC_ARMCAP
76