]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/armcap.c
Make EVP_PKEY_asn1_add0() stricter about its input
[thirdparty/openssl.git] / crypto / armcap.c
CommitLineData
d2e9e320 1/*
6244f531 2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
d2e9e320
RS
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
87873f43
AP
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <setjmp.h>
14#include <signal.h>
313e6ec1 15#include <openssl/crypto.h>
5fc89c1a 16#include <internal/cryptlib.h>
87873f43
AP
17
18#include "arm_arch.h"
19
0f113f3e 20unsigned int OPENSSL_armcap_P = 0;
87873f43 21
c1669e1c 22#if __ARM_MAX_ARCH__<7
0f113f3e
MC
23void OPENSSL_cpuid_setup(void)
24{
25}
26
27unsigned long OPENSSL_rdtsc(void)
28{
29 return 0;
30}
c1669e1c 31#else
87873f43
AP
32static sigset_t all_masked;
33
34static sigjmp_buf ill_jmp;
0f113f3e
MC
35static void ill_handler(int sig)
36{
37 siglongjmp(ill_jmp, sig);
38}
87873f43
AP
39
40/*
41 * Following subroutines could have been inlined, but it's not all
42 * ARM compilers support inline assembler...
43 */
44void _armv7_neon_probe(void);
4afa9f03
AP
45void _armv8_aes_probe(void);
46void _armv8_sha1_probe(void);
47void _armv8_sha256_probe(void);
48void _armv8_pmull_probe(void);
e8d93e34 49unsigned long _armv7_tick(void);
87873f43 50
e8d93e34 51unsigned long OPENSSL_rdtsc(void)
0f113f3e
MC
52{
53 if (OPENSSL_armcap_P & ARMV7_TICK)
54 return _armv7_tick();
55 else
56 return 0;
57}
87873f43 58
9b05cbc3
AP
59# if defined(__GNUC__) && __GNUC__>=2
60void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
61# endif
e8d93e34
AP
62/*
63 * Use a weak reference to getauxval() so we can use it if it is available but
64 * don't break the build if it is not.
65 */
9b05cbc3 66# if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
0f113f3e
MC
67extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
68# else
69static unsigned long (*getauxval) (unsigned long) = NULL;
70# endif
e8d93e34
AP
71
72/*
39184b24 73 * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
e8d93e34
AP
74 * AArch64 used AT_HWCAP.
75 */
0f113f3e
MC
76# if defined(__arm__) || defined (__arm)
77# define HWCAP 16
78 /* AT_HWCAP */
79# define HWCAP_NEON (1 << 12)
80
81# define HWCAP_CE 26
82 /* AT_HWCAP2 */
83# define HWCAP_CE_AES (1 << 0)
84# define HWCAP_CE_PMULL (1 << 1)
85# define HWCAP_CE_SHA1 (1 << 2)
86# define HWCAP_CE_SHA256 (1 << 3)
87# elif defined(__aarch64__)
88# define HWCAP 16
89 /* AT_HWCAP */
90# define HWCAP_NEON (1 << 1)
91
92# define HWCAP_CE HWCAP
93# define HWCAP_CE_AES (1 << 3)
94# define HWCAP_CE_PMULL (1 << 4)
95# define HWCAP_CE_SHA1 (1 << 5)
96# define HWCAP_CE_SHA256 (1 << 6)
97# endif
e8d93e34 98
87873f43 99void OPENSSL_cpuid_setup(void)
0f113f3e
MC
100{
101 char *e;
102 struct sigaction ill_oact, ill_act;
103 sigset_t oset;
104 static int trigger = 0;
105
106 if (trigger)
107 return;
108 trigger = 1;
109
110 if ((e = getenv("OPENSSL_armcap"))) {
111 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
112 return;
113 }
114
b763981b
AP
115# if defined(__APPLE__) && !defined(__aarch64__)
116 /*
117 * Capability probing by catching SIGILL appears to be problematic
118 * on iOS. But since Apple universe is "monocultural", it's actually
119 * possible to simply set pre-defined processor capability mask.
120 */
121 if (1) {
122 OPENSSL_armcap_P = ARMV7_NEON;
123 return;
124 }
125 /*
126 * One could do same even for __aarch64__ iOS builds. It's not done
127 * exclusively for reasons of keeping code unified across platforms.
128 * Unified code works because it never triggers SIGILL on Apple
129 * devices...
130 */
131# endif
132
0f113f3e
MC
133 sigfillset(&all_masked);
134 sigdelset(&all_masked, SIGILL);
135 sigdelset(&all_masked, SIGTRAP);
136 sigdelset(&all_masked, SIGFPE);
137 sigdelset(&all_masked, SIGBUS);
138 sigdelset(&all_masked, SIGSEGV);
139
140 OPENSSL_armcap_P = 0;
141
142 memset(&ill_act, 0, sizeof(ill_act));
143 ill_act.sa_handler = ill_handler;
144 ill_act.sa_mask = all_masked;
145
146 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
147 sigaction(SIGILL, &ill_act, &ill_oact);
148
149 if (getauxval != NULL) {
150 if (getauxval(HWCAP) & HWCAP_NEON) {
151 unsigned long hwcap = getauxval(HWCAP_CE);
152
153 OPENSSL_armcap_P |= ARMV7_NEON;
154
155 if (hwcap & HWCAP_CE_AES)
156 OPENSSL_armcap_P |= ARMV8_AES;
157
158 if (hwcap & HWCAP_CE_PMULL)
159 OPENSSL_armcap_P |= ARMV8_PMULL;
160
161 if (hwcap & HWCAP_CE_SHA1)
162 OPENSSL_armcap_P |= ARMV8_SHA1;
163
164 if (hwcap & HWCAP_CE_SHA256)
165 OPENSSL_armcap_P |= ARMV8_SHA256;
166 }
167 } else if (sigsetjmp(ill_jmp, 1) == 0) {
168 _armv7_neon_probe();
169 OPENSSL_armcap_P |= ARMV7_NEON;
170 if (sigsetjmp(ill_jmp, 1) == 0) {
171 _armv8_pmull_probe();
172 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
173 } else if (sigsetjmp(ill_jmp, 1) == 0) {
174 _armv8_aes_probe();
175 OPENSSL_armcap_P |= ARMV8_AES;
176 }
177 if (sigsetjmp(ill_jmp, 1) == 0) {
178 _armv8_sha1_probe();
179 OPENSSL_armcap_P |= ARMV8_SHA1;
180 }
181 if (sigsetjmp(ill_jmp, 1) == 0) {
182 _armv8_sha256_probe();
183 OPENSSL_armcap_P |= ARMV8_SHA256;
184 }
185 }
186 if (sigsetjmp(ill_jmp, 1) == 0) {
187 _armv7_tick();
188 OPENSSL_armcap_P |= ARMV7_TICK;
189 }
190
191 sigaction(SIGILL, &ill_oact, NULL);
192 sigprocmask(SIG_SETMASK, &oset, NULL);
193}
c1669e1c 194#endif