1 // Copyright (c) 2022, Robert Nagy <robert.nagy@gmail.com>
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 <openssl/cpu.h>
16 
17 #if defined(OPENSSL_AARCH64) && defined(OPENSSL_OPENBSD) && \
18     !defined(OPENSSL_STATIC_ARMCAP) && !defined(OPENSSL_NO_ASM)
19 
20 #include <machine/armreg.h>
21 #include <machine/cpu.h>
22 #include <sys/sysctl.h>
23 
24 #include "internal.h"
25 
26 
OPENSSL_cpuid_setup(void)27 void OPENSSL_cpuid_setup(void) {
28   int isar0_mib[] = {CTL_MACHDEP, CPU_ID_AA64ISAR0};
29   uint64_t cpu_id = 0;
30   size_t len = sizeof(cpu_id);
31 
32   if (sysctl(isar0_mib, 2, &cpu_id, &len, NULL, 0) < 0) {
33     return;
34   }
35 
36   OPENSSL_armcap_P |= ARMV7_NEON;
37 
38   if (ID_AA64ISAR0_AES(cpu_id) >= ID_AA64ISAR0_AES_BASE) {
39     OPENSSL_armcap_P |= ARMV8_AES;
40   }
41 
42   if (ID_AA64ISAR0_AES(cpu_id) >= ID_AA64ISAR0_AES_PMULL) {
43     OPENSSL_armcap_P |= ARMV8_PMULL;
44   }
45 
46   if (ID_AA64ISAR0_SHA1(cpu_id) >= ID_AA64ISAR0_SHA1_BASE) {
47     OPENSSL_armcap_P |= ARMV8_SHA1;
48   }
49 
50   if (ID_AA64ISAR0_SHA2(cpu_id) >= ID_AA64ISAR0_SHA2_BASE) {
51     OPENSSL_armcap_P |= ARMV8_SHA256;
52   }
53 
54   if (ID_AA64ISAR0_SHA2(cpu_id) >= ID_AA64ISAR0_SHA2_512) {
55     OPENSSL_armcap_P |= ARMV8_SHA512;
56   }
57 }
58 
59 #endif  // OPENSSL_AARCH64 && OPENSSL_OPENBSD && !OPENSSL_STATIC_ARMCAP
60