| 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 |
#define ossl_getenv getenv |
| 46 |
#endif |
| 47 |
|
| 48 |
#include "crypto/ctype.h" |
| 49 |
|
| 50 |
static int todigit(variant_char c) |
| 51 |
{ |
| 52 |
if (ossl_isdigit(c)) |
| 53 |
return c - '0'; |
| 54 |
else if (ossl_isxdigit(c)) |
| 55 |
return ossl_tolower(c) - 'a' + 10; |
| 56 |
|
| 57 |
/* return largest base value to make caller terminate the loop */ |
| 58 |
return 16; |
| 59 |
} |
| 60 |
|
| 61 |
static uint64_t ossl_strtouint64(const variant_char *str) |
| 62 |
{ |
| 63 |
uint64_t ret = 0; |
| 64 |
unsigned int digit, base = 10; |
| 65 |
|
| 66 |
if (*str == '0') { |
| 67 |
base = 8, str++; |
| 68 |
if (ossl_tolower(*str) == 'x') |
| 69 |
base = 16, str++; |
| 70 |
} |
| 71 |
|
| 72 |
while ((digit = todigit(*str++)) < base) |
| 73 |
ret = ret * base + digit; |
| 74 |
|
| 75 |
return ret; |
| 76 |
} |
| 77 |
|
| 78 |
static variant_char *ossl_strchr(const variant_char *str, char srch) |
| 79 |
{ |
| 80 |
variant_char c; |
| 81 |
|
| 82 |
while ((c = *str)) { |
| 83 |
if (c == srch) |
| 84 |
return (variant_char *)str; |
| 85 |
str++; |
| 86 |
} |
| 87 |
|
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
#define OPENSSL_CPUID_SETUP |
| 92 |
typedef uint64_t IA32CAP; |
| 93 |
|
| 94 |
void OPENSSL_cpuid_setup(void) |
| 95 |
{ |
| 96 |
static int trigger = 0; |
| 97 |
IA32CAP OPENSSL_ia32_cpuid(unsigned int *); |
| 98 |
IA32CAP vec; |
| 99 |
const variant_char *env; |
| 100 |
int index = 2; |
| 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 |
/* Processed indexes 0, 1 */ |
| 130 |
if ((env = ossl_strchr(env, ':')) != NULL) |
| 131 |
env++; |
| 132 |
for (; index < OPENSSL_IA32CAP_P_MAX_INDEXES; index += 2) { |
| 133 |
if ((env != NULL) && (env[0] != '\0')) { |
| 134 |
/* if env[0] == ':' current index is skipped */ |
| 135 |
if (env[0] != ':') { |
| 136 |
IA32CAP vecx; |
| 137 |
|
| 138 |
off = (env[0] == '~') ? 1 : 0; |
| 139 |
vecx = ossl_strtouint64(env + off); |
| 140 |
if (off) { |
| 141 |
OPENSSL_ia32cap_P[index] &= ~(unsigned int)vecx; |
| 142 |
OPENSSL_ia32cap_P[index + 1] &= ~(unsigned int)(vecx >> 32); |
| 143 |
} else { |
| 144 |
OPENSSL_ia32cap_P[index] = (unsigned int)vecx; |
| 145 |
OPENSSL_ia32cap_P[index + 1] = (unsigned int)(vecx >> 32); |
| 146 |
} |
| 147 |
} |
| 148 |
/* skip delimiter */ |
| 149 |
if ((env = ossl_strchr(env, ':')) != NULL) |
| 150 |
env++; |
| 151 |
} else { /* zeroize the next two indexes */ |
| 152 |
OPENSSL_ia32cap_P[index] = 0; |
| 153 |
OPENSSL_ia32cap_P[index + 1] = 0; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/* If AVX10 is disabled, zero out its detailed cap bits */ |
| 158 |
if (!(OPENSSL_ia32cap_P[6] & (1 << 19))) |
| 159 |
OPENSSL_ia32cap_P[9] = 0; |
| 160 |
} else { |
| 161 |
vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P); |
| 162 |
} |
| 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 |
OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10); |
| 170 |
OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32); |
| 171 |
} |
| 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 |