2 * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib_int.h"
13 #include <openssl/safestack.h>
15 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
16 defined(__x86_64) || defined(__x86_64__) || \
17 defined(_M_AMD64) || defined(_M_X64)
19 extern unsigned int OPENSSL_ia32cap_P[4];
21 # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
24 * Purpose of these minimalistic and character-type-agnostic subroutines
25 * is to break dependency on MSVCRT (on Windows) and locale. This makes
26 * OPENSSL_cpuid_setup safe to use as "constructor". "Character-type-
27 * agnostic" means that they work with either wide or 8-bit characters,
28 * exploiting the fact that first 127 characters can be simply casted
29 * between the sets, while the rest would be simply rejected by ossl_is*
33 typedef WCHAR variant_char;
35 static variant_char *ossl_getenv(const char *name)
38 * Since we pull only one environment variable, it's simpler to
39 * to just ignore |name| and use equivalent wide-char L-literal.
40 * As well as to ignore excessively long values...
42 static WCHAR value[48];
43 DWORD len = GetEnvironmentVariableW(L"OPENSSL_ia32cap", value, 48);
45 return (len > 0 && len < 48) ? value : NULL;
48 typedef char variant_char;
49 # define ossl_getenv getenv
52 # include "internal/ctype.h"
54 static int todigit(variant_char c)
58 else if (ossl_isxdigit(c))
59 return ossl_tolower(c) - 'a' + 10;
61 /* return largest base value to make caller terminate the loop */
65 static uint64_t ossl_strtouint64(const variant_char *str)
68 unsigned int digit, base = 10;
72 if (ossl_tolower(*str) == 'x')
76 while((digit = todigit(*str++)) < base)
77 ret = ret * base + digit;
82 static variant_char *ossl_strchr(const variant_char *str, char srch)
87 return (variant_char *)str;
94 # define OPENSSL_CPUID_SETUP
95 typedef uint64_t IA32CAP;
97 void OPENSSL_cpuid_setup(void)
99 static int trigger = 0;
100 IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
102 const variant_char *env;
108 if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) {
109 int off = (env[0] == '~') ? 1 : 0;
111 vec = ossl_strtouint64(env + off);
115 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask;
116 if (mask & (1<<24)) {
118 * User disables FXSR bit, mask even other capabilities
119 * that operate exclusively on XMM, so we don't have to
120 * double-check all the time. We mask PCLMULQDQ, AMD XOP,
121 * AES-NI and AVX. Formally speaking we don't have to
122 * do it in x86_64 case, but we can safely assume that
123 * x86_64 users won't actually flip this flag.
125 vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32);
127 } else if (env[0] == ':') {
128 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
131 if ((env = ossl_strchr(env, ':')) != NULL) {
135 off = (env[0] == '~') ? 1 : 0;
136 vecx = ossl_strtouint64(env + off);
138 OPENSSL_ia32cap_P[2] &= ~(unsigned int)vecx;
139 OPENSSL_ia32cap_P[3] &= ~(unsigned int)(vecx >> 32);
141 OPENSSL_ia32cap_P[2] = (unsigned int)vecx;
142 OPENSSL_ia32cap_P[3] = (unsigned int)(vecx >> 32);
145 OPENSSL_ia32cap_P[2] = 0;
146 OPENSSL_ia32cap_P[3] = 0;
149 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
153 * |(1<<10) sets a reserved bit to signal that variable
154 * was initialized already... This is to avoid interference
155 * with cpuid snippets in ELF .init segment.
157 OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
158 OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
161 unsigned int OPENSSL_ia32cap_P[4];
164 int OPENSSL_NONPIC_relocated = 0;
165 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
166 void OPENSSL_cpuid_setup(void)
175 # if defined(_UNICODE) || defined(__UNICODE__)
176 # define _vsntprintf _vsnwprintf
178 # define _vsntprintf _vsnprintf
182 # define alloca _alloca
185 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
186 # ifdef OPENSSL_SYS_WIN_CORE
188 int OPENSSL_isservice(void)
190 /* OneCore API cannot interact with GUI */
194 int OPENSSL_isservice(void)
202 } _OPENSSL_isservice = {
206 if (_OPENSSL_isservice.p == NULL) {
207 HANDLE mod = GetModuleHandle(NULL);
211 f = GetProcAddress(mod, "_OPENSSL_isservice");
213 _OPENSSL_isservice.p = (void *)-1;
215 _OPENSSL_isservice.f = f;
218 if (_OPENSSL_isservice.p != (void *)-1)
219 return (*_OPENSSL_isservice.f) ();
221 h = GetProcessWindowStation();
225 if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
226 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
230 return -1; /* paranoia */
231 len++, len &= ~1; /* paranoia */
232 name = (WCHAR *)alloca(len + sizeof(WCHAR));
233 if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
236 len++, len &= ~1; /* paranoia */
237 name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
240 * This doesn't cover "interactive" services [working with real
241 * WinSta0's] nor programs started non-interactively by Task Scheduler
242 * [those are working with SAWinSta].
244 if (wcsstr(name, L"Service-0x"))
247 /* This covers all non-interactive programs such as services. */
248 if (!wcsstr(name, L"WinSta0"))
256 int OPENSSL_isservice(void)
262 void OPENSSL_showfatal(const char *fmta, ...)
268 * First check if it's a console application, in which case the
269 * error message would be printed to standard error.
270 * Windows CE does not have a concept of a console application,
271 * so we need to guard the check.
273 # ifdef STD_ERROR_HANDLE
276 if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
277 GetFileType(h) != FILE_TYPE_UNKNOWN) {
278 /* must be console application */
283 len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
284 WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
290 if (sizeof(TCHAR) == sizeof(char))
291 fmt = (const TCHAR *)fmta;
295 size_t len_0 = strlen(fmta) + 1, i;
298 fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
300 fmt = (const TCHAR *)L"no stack?";
303 if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
304 for (i = 0; i < len_0; i++)
305 fmtw[i] = (WCHAR)fmta[i];
306 for (i = 0; i < len_0; i++) {
310 switch (fmtw[i + 1]) {
342 fmt = (const TCHAR *)fmtw;
346 _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
347 buf[OSSL_NELEM(buf) - 1] = _T('\0');
350 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
351 # ifdef OPENSSL_SYS_WIN_CORE
352 /* ONECORE is always NONGUI and NT >= 0x0601 */
355 * TODO: (For non GUI and no std error cases)
356 * Add event logging feature here.
359 # if !defined(NDEBUG)
361 * We are in a situation where we tried to report a critical
362 * error and this failed for some reason. As a last resort,
363 * in debug builds, send output to the debugger or any other
364 * tool like DebugView which can monitor the output.
366 OutputDebugString(buf);
369 /* this -------------v--- guards NT-specific calls */
370 if (check_winnt() && OPENSSL_isservice() > 0) {
371 HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
373 if (hEventLog != NULL) {
374 const TCHAR *pmsg = buf;
376 if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
377 1, 0, &pmsg, NULL)) {
378 # if !defined(NDEBUG)
380 * We are in a situation where we tried to report a critical
381 * error and this failed for some reason. As a last resort,
382 * in debug builds, send output to the debugger or any other
383 * tool like DebugView which can monitor the output.
385 OutputDebugString(pmsg);
389 (void)DeregisterEventSource(hEventLog);
392 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
396 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
400 void OPENSSL_showfatal(const char *fmta, ...)
402 #ifndef OPENSSL_NO_STDIO
406 vfprintf(stderr, fmta, ap);
411 int OPENSSL_isservice(void)
417 void OPENSSL_die(const char *message, const char *file, int line)
419 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
420 file, line, message);
425 * Win32 abort() customarily shows a dialog, but we just did that...
427 # if !defined(_WIN32_WCE)
434 #if !defined(OPENSSL_CPUID_OBJ)
436 * The volatile is used to to ensure that the compiler generates code that reads
437 * all values from the array and doesn't try to optimize this away. The standard
438 * doesn't actually require this behavior if the original data pointed to is
439 * not volatile, but compilers do this in practice anyway.
441 * There are also assembler versions of this function.
443 # undef CRYPTO_memcmp
444 int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
447 const volatile unsigned char *a = in_a;
448 const volatile unsigned char *b = in_b;
451 for (i = 0; i < len; i++)
458 * For systems that don't provide an instruction counter register or equivalent.
460 uint32_t OPENSSL_rdtsc(void)