1 | /*
|
---|
2 | * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (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 | #include "internal/cryptlib.h"
|
---|
17 |
|
---|
18 | #include "arm_arch.h"
|
---|
19 |
|
---|
20 | unsigned int OPENSSL_armcap_P = 0;
|
---|
21 |
|
---|
22 | #if __ARM_MAX_ARCH__<7
|
---|
23 | void OPENSSL_cpuid_setup(void)
|
---|
24 | {
|
---|
25 | }
|
---|
26 |
|
---|
27 | uint32_t OPENSSL_rdtsc(void)
|
---|
28 | {
|
---|
29 | return 0;
|
---|
30 | }
|
---|
31 | #else
|
---|
32 | static sigset_t all_masked;
|
---|
33 |
|
---|
34 | static sigjmp_buf ill_jmp;
|
---|
35 | static void ill_handler(int sig)
|
---|
36 | {
|
---|
37 | siglongjmp(ill_jmp, sig);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * Following subroutines could have been inlined, but it's not all
|
---|
42 | * ARM compilers support inline assembler...
|
---|
43 | */
|
---|
44 | void _armv7_neon_probe(void);
|
---|
45 | void _armv8_aes_probe(void);
|
---|
46 | void _armv8_sha1_probe(void);
|
---|
47 | void _armv8_sha256_probe(void);
|
---|
48 | void _armv8_pmull_probe(void);
|
---|
49 | # ifdef __aarch64__
|
---|
50 | void _armv8_sha512_probe(void);
|
---|
51 | # endif
|
---|
52 | uint32_t _armv7_tick(void);
|
---|
53 |
|
---|
54 | uint32_t OPENSSL_rdtsc(void)
|
---|
55 | {
|
---|
56 | if (OPENSSL_armcap_P & ARMV7_TICK)
|
---|
57 | return _armv7_tick();
|
---|
58 | else
|
---|
59 | return 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | # if defined(__GNUC__) && __GNUC__>=2
|
---|
63 | void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
|
---|
64 | # endif
|
---|
65 |
|
---|
66 | # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
|
---|
67 | # if __GLIBC_PREREQ(2, 16)
|
---|
68 | # include <sys/auxv.h>
|
---|
69 | # define OSSL_IMPLEMENT_GETAUXVAL
|
---|
70 | # endif
|
---|
71 | # elif defined(__ANDROID_API__)
|
---|
72 | /* see https://developer.android.google.cn/ndk/guides/cpu-features */
|
---|
73 | # if __ANDROID_API__ >= 18
|
---|
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 | * Android: according to https://developer.android.com/ndk/guides/cpu-features,
|
---|
98 | * getauxval is supported starting with API level 18
|
---|
99 | */
|
---|
100 | # if defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ >= 18
|
---|
101 | # include <sys/auxv.h>
|
---|
102 | # define OSSL_IMPLEMENT_GETAUXVAL
|
---|
103 | # endif
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
|
---|
107 | * AArch64 used AT_HWCAP.
|
---|
108 | */
|
---|
109 | # if defined(__arm__) || defined (__arm)
|
---|
110 | # define HWCAP 16
|
---|
111 | /* AT_HWCAP */
|
---|
112 | # define HWCAP_NEON (1 << 12)
|
---|
113 |
|
---|
114 | # define HWCAP_CE 26
|
---|
115 | /* AT_HWCAP2 */
|
---|
116 | # define HWCAP_CE_AES (1 << 0)
|
---|
117 | # define HWCAP_CE_PMULL (1 << 1)
|
---|
118 | # define HWCAP_CE_SHA1 (1 << 2)
|
---|
119 | # define HWCAP_CE_SHA256 (1 << 3)
|
---|
120 | # elif defined(__aarch64__)
|
---|
121 | # define HWCAP 16
|
---|
122 | /* AT_HWCAP */
|
---|
123 | # define HWCAP_NEON (1 << 1)
|
---|
124 |
|
---|
125 | # define HWCAP_CE HWCAP
|
---|
126 | # define HWCAP_CE_AES (1 << 3)
|
---|
127 | # define HWCAP_CE_PMULL (1 << 4)
|
---|
128 | # define HWCAP_CE_SHA1 (1 << 5)
|
---|
129 | # define HWCAP_CE_SHA256 (1 << 6)
|
---|
130 | # define HWCAP_CE_SHA512 (1 << 21)
|
---|
131 | # endif
|
---|
132 |
|
---|
133 | void OPENSSL_cpuid_setup(void)
|
---|
134 | {
|
---|
135 | const char *e;
|
---|
136 | struct sigaction ill_oact, ill_act;
|
---|
137 | sigset_t oset;
|
---|
138 | static int trigger = 0;
|
---|
139 |
|
---|
140 | if (trigger)
|
---|
141 | return;
|
---|
142 | trigger = 1;
|
---|
143 |
|
---|
144 | if ((e = getenv("OPENSSL_armcap"))) {
|
---|
145 | OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | # if defined(__APPLE__) && !defined(__aarch64__)
|
---|
150 | /*
|
---|
151 | * Capability probing by catching SIGILL appears to be problematic
|
---|
152 | * on iOS. But since Apple universe is "monocultural", it's actually
|
---|
153 | * possible to simply set pre-defined processor capability mask.
|
---|
154 | */
|
---|
155 | if (1) {
|
---|
156 | OPENSSL_armcap_P = ARMV7_NEON;
|
---|
157 | return;
|
---|
158 | }
|
---|
159 | /*
|
---|
160 | * One could do same even for __aarch64__ iOS builds. It's not done
|
---|
161 | * exclusively for reasons of keeping code unified across platforms.
|
---|
162 | * Unified code works because it never triggers SIGILL on Apple
|
---|
163 | * devices...
|
---|
164 | */
|
---|
165 | # endif
|
---|
166 |
|
---|
167 | OPENSSL_armcap_P = 0;
|
---|
168 |
|
---|
169 | # ifdef OSSL_IMPLEMENT_GETAUXVAL
|
---|
170 | if (getauxval(HWCAP) & HWCAP_NEON) {
|
---|
171 | unsigned long hwcap = getauxval(HWCAP_CE);
|
---|
172 |
|
---|
173 | OPENSSL_armcap_P |= ARMV7_NEON;
|
---|
174 |
|
---|
175 | if (hwcap & HWCAP_CE_AES)
|
---|
176 | OPENSSL_armcap_P |= ARMV8_AES;
|
---|
177 |
|
---|
178 | if (hwcap & HWCAP_CE_PMULL)
|
---|
179 | OPENSSL_armcap_P |= ARMV8_PMULL;
|
---|
180 |
|
---|
181 | if (hwcap & HWCAP_CE_SHA1)
|
---|
182 | OPENSSL_armcap_P |= ARMV8_SHA1;
|
---|
183 |
|
---|
184 | if (hwcap & HWCAP_CE_SHA256)
|
---|
185 | OPENSSL_armcap_P |= ARMV8_SHA256;
|
---|
186 |
|
---|
187 | # ifdef __aarch64__
|
---|
188 | if (hwcap & HWCAP_CE_SHA512)
|
---|
189 | OPENSSL_armcap_P |= ARMV8_SHA512;
|
---|
190 | # endif
|
---|
191 | }
|
---|
192 | # endif
|
---|
193 |
|
---|
194 | sigfillset(&all_masked);
|
---|
195 | sigdelset(&all_masked, SIGILL);
|
---|
196 | sigdelset(&all_masked, SIGTRAP);
|
---|
197 | sigdelset(&all_masked, SIGFPE);
|
---|
198 | sigdelset(&all_masked, SIGBUS);
|
---|
199 | sigdelset(&all_masked, SIGSEGV);
|
---|
200 |
|
---|
201 | memset(&ill_act, 0, sizeof(ill_act));
|
---|
202 | ill_act.sa_handler = ill_handler;
|
---|
203 | ill_act.sa_mask = all_masked;
|
---|
204 |
|
---|
205 | sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
|
---|
206 | sigaction(SIGILL, &ill_act, &ill_oact);
|
---|
207 |
|
---|
208 | /* If we used getauxval, we already have all the values */
|
---|
209 | # ifndef OSSL_IMPLEMENT_GETAUXVAL
|
---|
210 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
211 | _armv7_neon_probe();
|
---|
212 | OPENSSL_armcap_P |= ARMV7_NEON;
|
---|
213 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
214 | _armv8_pmull_probe();
|
---|
215 | OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
|
---|
216 | } else if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
217 | _armv8_aes_probe();
|
---|
218 | OPENSSL_armcap_P |= ARMV8_AES;
|
---|
219 | }
|
---|
220 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
221 | _armv8_sha1_probe();
|
---|
222 | OPENSSL_armcap_P |= ARMV8_SHA1;
|
---|
223 | }
|
---|
224 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
225 | _armv8_sha256_probe();
|
---|
226 | OPENSSL_armcap_P |= ARMV8_SHA256;
|
---|
227 | }
|
---|
228 | # if defined(__aarch64__) && !defined(__APPLE__)
|
---|
229 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
230 | _armv8_sha512_probe();
|
---|
231 | OPENSSL_armcap_P |= ARMV8_SHA512;
|
---|
232 | }
|
---|
233 | # endif
|
---|
234 | }
|
---|
235 | # endif
|
---|
236 |
|
---|
237 | /* Things that getauxval didn't tell us */
|
---|
238 | if (sigsetjmp(ill_jmp, 1) == 0) {
|
---|
239 | _armv7_tick();
|
---|
240 | OPENSSL_armcap_P |= ARMV7_TICK;
|
---|
241 | }
|
---|
242 |
|
---|
243 | sigaction(SIGILL, &ill_oact, NULL);
|
---|
244 | sigprocmask(SIG_SETMASK, &oset, NULL);
|
---|
245 | }
|
---|
246 | #endif
|
---|