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 | # define ossl_getenv getenv
|
---|
48 | # endif
|
---|
49 |
|
---|
50 | # include "crypto/ctype.h"
|
---|
51 |
|
---|
52 | static int todigit(variant_char c)
|
---|
53 | {
|
---|
54 | if (ossl_isdigit(c))
|
---|
55 | return c - '0';
|
---|
56 | else if (ossl_isxdigit(c))
|
---|
57 | return ossl_tolower(c) - 'a' + 10;
|
---|
58 |
|
---|
59 | /* return largest base value to make caller terminate the loop */
|
---|
60 | return 16;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static uint64_t ossl_strtouint64(const variant_char *str)
|
---|
64 | {
|
---|
65 | uint64_t ret = 0;
|
---|
66 | unsigned int digit, base = 10;
|
---|
67 |
|
---|
68 | if (*str == '0') {
|
---|
69 | base = 8, str++;
|
---|
70 | if (ossl_tolower(*str) == 'x')
|
---|
71 | base = 16, str++;
|
---|
72 | }
|
---|
73 |
|
---|
74 | while((digit = todigit(*str++)) < base)
|
---|
75 | ret = ret * base + digit;
|
---|
76 |
|
---|
77 | return ret;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static variant_char *ossl_strchr(const variant_char *str, char srch)
|
---|
81 | { variant_char c;
|
---|
82 |
|
---|
83 | while((c = *str)) {
|
---|
84 | if (c == srch)
|
---|
85 | return (variant_char *)str;
|
---|
86 | str++;
|
---|
87 | }
|
---|
88 |
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 |
|
---|
92 | # define OPENSSL_CPUID_SETUP
|
---|
93 | typedef uint64_t IA32CAP;
|
---|
94 |
|
---|
95 | void OPENSSL_cpuid_setup(void)
|
---|
96 | {
|
---|
97 | static int trigger = 0;
|
---|
98 | IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
|
---|
99 | IA32CAP vec;
|
---|
100 | const variant_char *env;
|
---|
101 |
|
---|
102 | if (trigger)
|
---|
103 | return;
|
---|
104 |
|
---|
105 | trigger = 1;
|
---|
106 | if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) {
|
---|
107 | int off = (env[0] == '~') ? 1 : 0;
|
---|
108 |
|
---|
109 | vec = ossl_strtouint64(env + off);
|
---|
110 |
|
---|
111 | if (off) {
|
---|
112 | IA32CAP mask = vec;
|
---|
113 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask;
|
---|
114 | 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 | vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32);
|
---|
124 | }
|
---|
125 | } else if (env[0] == ':') {
|
---|
126 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if ((env = ossl_strchr(env, ':')) != NULL) {
|
---|
130 | IA32CAP vecx;
|
---|
131 |
|
---|
132 | env++;
|
---|
133 | off = (env[0] == '~') ? 1 : 0;
|
---|
134 | vecx = ossl_strtouint64(env + off);
|
---|
135 | if (off) {
|
---|
136 | OPENSSL_ia32cap_P[2] &= ~(unsigned int)vecx;
|
---|
137 | OPENSSL_ia32cap_P[3] &= ~(unsigned int)(vecx >> 32);
|
---|
138 | } else {
|
---|
139 | OPENSSL_ia32cap_P[2] = (unsigned int)vecx;
|
---|
140 | OPENSSL_ia32cap_P[3] = (unsigned int)(vecx >> 32);
|
---|
141 | }
|
---|
142 | } else {
|
---|
143 | OPENSSL_ia32cap_P[2] = 0;
|
---|
144 | OPENSSL_ia32cap_P[3] = 0;
|
---|
145 | }
|
---|
146 | } else {
|
---|
147 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
|
---|
148 | }
|
---|
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 | OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
|
---|
156 | OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
|
---|
157 | }
|
---|
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 | # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* VBox */
|
---|
184 | # undef CRYPTO_memcmp
|
---|
185 | # endif /* VBox */
|
---|
186 | int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
|
---|
187 | {
|
---|
188 | size_t i;
|
---|
189 | const volatile unsigned char *a = in_a;
|
---|
190 | const volatile unsigned char *b = in_b;
|
---|
191 | unsigned char x = 0;
|
---|
192 |
|
---|
193 | for (i = 0; i < len; i++)
|
---|
194 | x |= a[i] ^ b[i];
|
---|
195 |
|
---|
196 | return x;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * For systems that don't provide an instruction counter register or equivalent.
|
---|
201 | */
|
---|
202 | uint32_t OPENSSL_rdtsc(void)
|
---|
203 | {
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
|
---|
208 | {
|
---|
209 | return 0;
|
---|
210 | }
|
---|
211 |
|
---|
212 | size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
|
---|
213 | {
|
---|
214 | return 0;
|
---|
215 | }
|
---|
216 | #endif
|
---|