armcap: fix Mac M1 SHA512 support.
[openssl.git] / crypto / armcap.c
1 /*
2  * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <setjmp.h>
14 #include <signal.h>
15 #include <openssl/crypto.h>
16 #ifdef __APPLE__
17 #include <sys/sysctl.h>
18 #endif
19 #include "internal/cryptlib.h"
20
21 #include "arm_arch.h"
22
23 unsigned int OPENSSL_armcap_P = 0;
24 unsigned int OPENSSL_arm_midr = 0;
25 unsigned int OPENSSL_armv8_rsa_neonized = 0;
26
27 #if __ARM_MAX_ARCH__<7
28 void OPENSSL_cpuid_setup(void)
29 {
30 }
31
32 uint32_t OPENSSL_rdtsc(void)
33 {
34     return 0;
35 }
36 #else
37 static sigset_t all_masked;
38
39 static sigjmp_buf ill_jmp;
40 static void ill_handler(int sig)
41 {
42     siglongjmp(ill_jmp, sig);
43 }
44
45 /*
46  * Following subroutines could have been inlined, but it's not all
47  * ARM compilers support inline assembler...
48  */
49 void _armv7_neon_probe(void);
50 void _armv8_aes_probe(void);
51 void _armv8_sha1_probe(void);
52 void _armv8_sha256_probe(void);
53 void _armv8_pmull_probe(void);
54 # ifdef __aarch64__
55 void _armv8_sha512_probe(void);
56 unsigned int _armv8_cpuid_probe(void);
57 # endif
58 uint32_t _armv7_tick(void);
59
60 uint32_t OPENSSL_rdtsc(void)
61 {
62     if (OPENSSL_armcap_P & ARMV7_TICK)
63         return _armv7_tick();
64     else
65         return 0;
66 }
67
68 # if defined(__GNUC__) && __GNUC__>=2
69 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
70 # endif
71
72 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
73 #  if __GLIBC_PREREQ(2, 16)
74 #   include <sys/auxv.h>
75 #   define OSSL_IMPLEMENT_GETAUXVAL
76 #  endif
77 # endif
78 # if defined(__FreeBSD__)
79 #  include <sys/param.h>
80 #  if __FreeBSD_version >= 1200000
81 #   include <sys/auxv.h>
82 #   define OSSL_IMPLEMENT_GETAUXVAL
83
84 static unsigned long getauxval(unsigned long key)
85 {
86   unsigned long val = 0ul;
87
88   if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
89     return 0ul;
90
91   return val;
92 }
93 #  endif
94 # endif
95
96 /*
97  * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
98  * AArch64 used AT_HWCAP.
99  */
100 # if defined(__arm__) || defined (__arm)
101 #  define HWCAP                  16
102                                   /* AT_HWCAP */
103 #  define HWCAP_NEON             (1 << 12)
104
105 #  define HWCAP_CE               26
106                                   /* AT_HWCAP2 */
107 #  define HWCAP_CE_AES           (1 << 0)
108 #  define HWCAP_CE_PMULL         (1 << 1)
109 #  define HWCAP_CE_SHA1          (1 << 2)
110 #  define HWCAP_CE_SHA256        (1 << 3)
111 # elif defined(__aarch64__)
112 #  define HWCAP                  16
113                                   /* AT_HWCAP */
114 #  define HWCAP_NEON             (1 << 1)
115
116 #  define HWCAP_CE               HWCAP
117 #  define HWCAP_CE_AES           (1 << 3)
118 #  define HWCAP_CE_PMULL         (1 << 4)
119 #  define HWCAP_CE_SHA1          (1 << 5)
120 #  define HWCAP_CE_SHA256        (1 << 6)
121 #  define HWCAP_CPUID            (1 << 11)
122 #  define HWCAP_CE_SHA512        (1 << 21)
123 # endif
124
125 void OPENSSL_cpuid_setup(void)
126 {
127     const char *e;
128     struct sigaction ill_oact, ill_act;
129     sigset_t oset;
130     static int trigger = 0;
131
132     if (trigger)
133         return;
134     trigger = 1;
135
136     if ((e = getenv("OPENSSL_armcap"))) {
137         OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
138         return;
139     }
140
141 # if defined(__APPLE__)
142 #   if !defined(__aarch64__)
143     /*
144      * Capability probing by catching SIGILL appears to be problematic
145      * on iOS. But since Apple universe is "monocultural", it's actually
146      * possible to simply set pre-defined processor capability mask.
147      */
148     if (1) {
149         OPENSSL_armcap_P = ARMV7_NEON;
150         return;
151     }
152     /*
153      * One could do same even for __aarch64__ iOS builds. It's not done
154      * exclusively for reasons of keeping code unified across platforms.
155      * Unified code works because it never triggers SIGILL on Apple
156      * devices...
157      */
158 #   else
159     {
160         unsigned int sha512;
161         size_t len = sizeof(sha512);
162
163         if (sysctlbyname("hw.optional.armv8_2_sha512", &sha512, &len, NULL, 0) == 0 && sha512 == 1)
164             OPENSSL_armcap_P |= ARMV8_SHA512;
165     }
166 #   endif
167 # endif
168
169     OPENSSL_armcap_P = 0;
170
171 # ifdef OSSL_IMPLEMENT_GETAUXVAL
172     if (getauxval(HWCAP) & HWCAP_NEON) {
173         unsigned long hwcap = getauxval(HWCAP_CE);
174
175         OPENSSL_armcap_P |= ARMV7_NEON;
176
177         if (hwcap & HWCAP_CE_AES)
178             OPENSSL_armcap_P |= ARMV8_AES;
179
180         if (hwcap & HWCAP_CE_PMULL)
181             OPENSSL_armcap_P |= ARMV8_PMULL;
182
183         if (hwcap & HWCAP_CE_SHA1)
184             OPENSSL_armcap_P |= ARMV8_SHA1;
185
186         if (hwcap & HWCAP_CE_SHA256)
187             OPENSSL_armcap_P |= ARMV8_SHA256;
188
189 #  ifdef __aarch64__
190         if (hwcap & HWCAP_CE_SHA512)
191             OPENSSL_armcap_P |= ARMV8_SHA512;
192
193         if (hwcap & HWCAP_CPUID)
194             OPENSSL_armcap_P |= ARMV8_CPUID;
195 #  endif
196     }
197 # endif
198
199     sigfillset(&all_masked);
200     sigdelset(&all_masked, SIGILL);
201     sigdelset(&all_masked, SIGTRAP);
202     sigdelset(&all_masked, SIGFPE);
203     sigdelset(&all_masked, SIGBUS);
204     sigdelset(&all_masked, SIGSEGV);
205
206     memset(&ill_act, 0, sizeof(ill_act));
207     ill_act.sa_handler = ill_handler;
208     ill_act.sa_mask = all_masked;
209
210     sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
211     sigaction(SIGILL, &ill_act, &ill_oact);
212
213     /* If we used getauxval, we already have all the values */
214 # ifndef OSSL_IMPLEMENT_GETAUXVAL
215     if (sigsetjmp(ill_jmp, 1) == 0) {
216         _armv7_neon_probe();
217         OPENSSL_armcap_P |= ARMV7_NEON;
218         if (sigsetjmp(ill_jmp, 1) == 0) {
219             _armv8_pmull_probe();
220             OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
221         } else if (sigsetjmp(ill_jmp, 1) == 0) {
222             _armv8_aes_probe();
223             OPENSSL_armcap_P |= ARMV8_AES;
224         }
225         if (sigsetjmp(ill_jmp, 1) == 0) {
226             _armv8_sha1_probe();
227             OPENSSL_armcap_P |= ARMV8_SHA1;
228         }
229         if (sigsetjmp(ill_jmp, 1) == 0) {
230             _armv8_sha256_probe();
231             OPENSSL_armcap_P |= ARMV8_SHA256;
232         }
233 #  if defined(__aarch64__) && !defined(__APPLE__)
234         if (sigsetjmp(ill_jmp, 1) == 0) {
235             _armv8_sha512_probe();
236             OPENSSL_armcap_P |= ARMV8_SHA512;
237         }
238 #  endif
239     }
240 # endif
241
242     /* Things that getauxval didn't tell us */
243     if (sigsetjmp(ill_jmp, 1) == 0) {
244         _armv7_tick();
245         OPENSSL_armcap_P |= ARMV7_TICK;
246     }
247
248     sigaction(SIGILL, &ill_oact, NULL);
249     sigprocmask(SIG_SETMASK, &oset, NULL);
250
251 # ifdef __aarch64__
252     if (OPENSSL_armcap_P & ARMV8_CPUID)
253         OPENSSL_arm_midr = _armv8_cpuid_probe();
254
255     if ((MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A72) ||
256          MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_N1)) &&
257         (OPENSSL_armcap_P & ARMV7_NEON)) {
258             OPENSSL_armv8_rsa_neonized = 1;
259     }
260 # endif
261 }
262 #endif