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