/src/openssl36/crypto/cpuid.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1998-2023 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 "internal/e_os.h" |
11 | | #include "crypto/cryptlib.h" |
12 | | |
13 | | #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) |
14 | | |
15 | | extern unsigned int OPENSSL_ia32cap_P[OPENSSL_IA32CAP_P_MAX_INDEXES]; |
16 | | |
17 | | #if defined(OPENSSL_CPUID_OBJ) |
18 | | |
19 | | /* |
20 | | * Purpose of these minimalistic and character-type-agnostic subroutines |
21 | | * is to break dependency on MSVCRT (on Windows) and locale. This makes |
22 | | * OPENSSL_cpuid_setup safe to use as "constructor". "Character-type- |
23 | | * agnostic" means that they work with either wide or 8-bit characters, |
24 | | * exploiting the fact that first 127 characters can be simply casted |
25 | | * between the sets, while the rest would be simply rejected by ossl_is* |
26 | | * subroutines. |
27 | | */ |
28 | | #ifdef _WIN32 |
29 | | typedef WCHAR variant_char; |
30 | | #define OPENSSL_IA32CAP_P_MAX_CHAR_SIZE 256 |
31 | | static variant_char *ossl_getenv(const char *name) |
32 | | { |
33 | | /* |
34 | | * Since we pull only one environment variable, it's simpler to |
35 | | * just ignore |name| and use equivalent wide-char L-literal. |
36 | | * As well as to ignore excessively long values... |
37 | | */ |
38 | | static WCHAR value[OPENSSL_IA32CAP_P_MAX_CHAR_SIZE]; |
39 | | DWORD len = GetEnvironmentVariableW(L"OPENSSL_ia32cap", value, OPENSSL_IA32CAP_P_MAX_CHAR_SIZE); |
40 | | |
41 | | return (len > 0 && len < OPENSSL_IA32CAP_P_MAX_CHAR_SIZE) ? value : NULL; |
42 | | } |
43 | | #else |
44 | | typedef char variant_char; |
45 | 166 | #define ossl_getenv getenv |
46 | | #endif |
47 | | |
48 | | #include "crypto/ctype.h" |
49 | | |
50 | | static int todigit(variant_char c) |
51 | 0 | { |
52 | 0 | if (ossl_isdigit(c)) |
53 | 0 | return c - '0'; |
54 | 0 | else if (ossl_isxdigit(c)) |
55 | 0 | return ossl_tolower(c) - 'a' + 10; |
56 | | |
57 | | /* return largest base value to make caller terminate the loop */ |
58 | 0 | return 16; |
59 | 0 | } |
60 | | |
61 | | static uint64_t ossl_strtouint64(const variant_char *str) |
62 | 0 | { |
63 | 0 | uint64_t ret = 0; |
64 | 0 | unsigned int digit, base = 10; |
65 | |
|
66 | 0 | if (*str == '0') { |
67 | 0 | base = 8, str++; |
68 | 0 | if (ossl_tolower(*str) == 'x') |
69 | 0 | base = 16, str++; |
70 | 0 | } |
71 | |
|
72 | 0 | while ((digit = todigit(*str++)) < base) |
73 | 0 | ret = ret * base + digit; |
74 | |
|
75 | 0 | return ret; |
76 | 0 | } |
77 | | |
78 | | static variant_char *ossl_strchr(const variant_char *str, char srch) |
79 | 0 | { |
80 | 0 | variant_char c; |
81 | |
|
82 | 0 | while ((c = *str)) { |
83 | 0 | if (c == srch) |
84 | 0 | return (variant_char *)str; |
85 | 0 | str++; |
86 | 0 | } |
87 | | |
88 | 0 | return NULL; |
89 | 0 | } |
90 | | |
91 | | #define OPENSSL_CPUID_SETUP |
92 | | typedef uint64_t IA32CAP; |
93 | | |
94 | | void OPENSSL_cpuid_setup(void) |
95 | 219 | { |
96 | 219 | static int trigger = 0; |
97 | 219 | IA32CAP OPENSSL_ia32_cpuid(unsigned int *); |
98 | 219 | IA32CAP vec; |
99 | 219 | const variant_char *env; |
100 | 219 | int index = 2; |
101 | | |
102 | 219 | if (trigger) |
103 | 53 | return; |
104 | | |
105 | 166 | trigger = 1; |
106 | 166 | if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) { |
107 | 0 | int off = (env[0] == '~') ? 1 : 0; |
108 | |
|
109 | 0 | vec = ossl_strtouint64(env + off); |
110 | |
|
111 | 0 | if (off) { |
112 | 0 | IA32CAP mask = vec; |
113 | 0 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask; |
114 | 0 | if (mask & (1 << 24)) { |
115 | | /* |
116 | | * User disables FXSR bit, mask even other capabilities |
117 | | * that operate exclusively on XMM, so we don't have to |
118 | | * double-check all the time. We mask PCLMULQDQ, AMD XOP, |
119 | | * AES-NI and AVX. Formally speaking we don't have to |
120 | | * do it in x86_64 case, but we can safely assume that |
121 | | * x86_64 users won't actually flip this flag. |
122 | | */ |
123 | 0 | vec &= ~((IA32CAP)(1 << 1 | 1 << 11 | 1 << 25 | 1 << 28) << 32); |
124 | 0 | } |
125 | 0 | } else if (env[0] == ':') { |
126 | 0 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P); |
127 | 0 | } |
128 | | |
129 | | /* Processed indexes 0, 1 */ |
130 | 0 | if ((env = ossl_strchr(env, ':')) != NULL) |
131 | 0 | env++; |
132 | 0 | for (; index < OPENSSL_IA32CAP_P_MAX_INDEXES; index += 2) { |
133 | 0 | if ((env != NULL) && (env[0] != '\0')) { |
134 | | /* if env[0] == ':' current index is skipped */ |
135 | 0 | if (env[0] != ':') { |
136 | 0 | IA32CAP vecx; |
137 | |
|
138 | 0 | off = (env[0] == '~') ? 1 : 0; |
139 | 0 | vecx = ossl_strtouint64(env + off); |
140 | 0 | if (off) { |
141 | 0 | OPENSSL_ia32cap_P[index] &= ~(unsigned int)vecx; |
142 | 0 | OPENSSL_ia32cap_P[index + 1] &= ~(unsigned int)(vecx >> 32); |
143 | 0 | } else { |
144 | 0 | OPENSSL_ia32cap_P[index] = (unsigned int)vecx; |
145 | 0 | OPENSSL_ia32cap_P[index + 1] = (unsigned int)(vecx >> 32); |
146 | 0 | } |
147 | 0 | } |
148 | | /* skip delimiter */ |
149 | 0 | if ((env = ossl_strchr(env, ':')) != NULL) |
150 | 0 | env++; |
151 | 0 | } else { /* zeroize the next two indexes */ |
152 | 0 | OPENSSL_ia32cap_P[index] = 0; |
153 | 0 | OPENSSL_ia32cap_P[index + 1] = 0; |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | /* If AVX10 is disabled, zero out its detailed cap bits */ |
158 | 0 | if (!(OPENSSL_ia32cap_P[6] & (1 << 19))) |
159 | 0 | OPENSSL_ia32cap_P[9] = 0; |
160 | 166 | } else { |
161 | 166 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P); |
162 | 166 | } |
163 | | |
164 | | /* |
165 | | * |(1<<10) sets a reserved bit to signal that variable |
166 | | * was initialized already... This is to avoid interference |
167 | | * with cpuid snippets in ELF .init segment. |
168 | | */ |
169 | 166 | OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10); |
170 | 166 | OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32); |
171 | 166 | } |
172 | | #else |
173 | | unsigned int OPENSSL_ia32cap_P[OPENSSL_IA32CAP_P_MAX_INDEXES]; |
174 | | #endif |
175 | | #endif |
176 | | |
177 | | #ifndef OPENSSL_CPUID_OBJ |
178 | | #ifndef OPENSSL_CPUID_SETUP |
179 | | void OPENSSL_cpuid_setup(void) |
180 | | { |
181 | | } |
182 | | #endif |
183 | | |
184 | | /* |
185 | | * The rest are functions that are defined in the same assembler files as |
186 | | * the CPUID functionality. |
187 | | */ |
188 | | |
189 | | /* |
190 | | * The volatile is used to ensure that the compiler generates code that reads |
191 | | * all values from the array and doesn't try to optimize this away. The standard |
192 | | * doesn't actually require this behavior if the original data pointed to is |
193 | | * not volatile, but compilers do this in practice anyway. |
194 | | * |
195 | | * There are also assembler versions of this function. |
196 | | */ |
197 | | #undef CRYPTO_memcmp |
198 | | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) |
199 | | { |
200 | | size_t i; |
201 | | const volatile unsigned char *a = in_a; |
202 | | const volatile unsigned char *b = in_b; |
203 | | unsigned char x = 0; |
204 | | |
205 | | for (i = 0; i < len; i++) |
206 | | x |= a[i] ^ b[i]; |
207 | | |
208 | | return x; |
209 | | } |
210 | | |
211 | | /* |
212 | | * For systems that don't provide an instruction counter register or equivalent. |
213 | | */ |
214 | | uint32_t OPENSSL_rdtsc(void) |
215 | | { |
216 | | return 0; |
217 | | } |
218 | | |
219 | | size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt) |
220 | | { |
221 | | return 0; |
222 | | } |
223 | | |
224 | | size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max) |
225 | | { |
226 | | return 0; |
227 | | } |
228 | | #endif |