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)
23 # define OPENSSL_CPUID_SETUP
24 typedef uint64_t IA32CAP;
25 void OPENSSL_cpuid_setup(void)
27 static int trigger = 0;
28 IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
36 if ((env = getenv("OPENSSL_ia32cap"))) {
37 int off = (env[0] == '~') ? 1 : 0;
39 if (!sscanf(env + off, "%I64i", &vec))
40 vec = strtoul(env + off, NULL, 0);
42 if (!sscanf(env + off, "%lli", (long long *)&vec))
43 vec = strtoul(env + off, NULL, 0);
47 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask;
50 * User disables FXSR bit, mask even other capabilities
51 * that operate exclusively on XMM, so we don't have to
52 * double-check all the time. We mask PCLMULQDQ, AMD XOP,
53 * AES-NI and AVX. Formally speaking we don't have to
54 * do it in x86_64 case, but we can safely assume that
55 * x86_64 users won't actually flip this flag.
57 vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32);
59 } else if (env[0] == ':') {
60 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
63 if ((env = strchr(env, ':'))) {
66 off = (env[0] == '~') ? 1 : 0;
68 if (!sscanf(env + off, "%I64i", &vecx))
69 vecx = strtoul(env + off, NULL, 0);
71 if (!sscanf(env + off, "%lli", (long long *)&vecx))
72 vecx = strtoul(env + off, NULL, 0);
75 OPENSSL_ia32cap_P[2] &= ~(unsigned int)vecx;
76 OPENSSL_ia32cap_P[3] &= ~(unsigned int)(vecx >> 32);
78 OPENSSL_ia32cap_P[2] = (unsigned int)vecx;
79 OPENSSL_ia32cap_P[3] = (unsigned int)(vecx >> 32);
82 OPENSSL_ia32cap_P[2] = 0;
83 OPENSSL_ia32cap_P[3] = 0;
86 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
90 * |(1<<10) sets a reserved bit to signal that variable
91 * was initialized already... This is to avoid interference
92 * with cpuid snippets in ELF .init segment.
94 OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
95 OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
98 unsigned int OPENSSL_ia32cap_P[4];
101 int OPENSSL_NONPIC_relocated = 0;
102 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
103 void OPENSSL_cpuid_setup(void)
112 # if defined(_UNICODE) || defined(__UNICODE__)
113 # define _vsntprintf _vsnwprintf
115 # define _vsntprintf _vsnprintf
119 # define alloca _alloca
122 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
123 # ifdef OPENSSL_SYS_WIN_CORE
125 int OPENSSL_isservice(void)
127 /* OneCore API cannot interact with GUI */
131 int OPENSSL_isservice(void)
139 } _OPENSSL_isservice = {
143 if (_OPENSSL_isservice.p == NULL) {
144 HANDLE mod = GetModuleHandle(NULL);
146 _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
147 if (_OPENSSL_isservice.p == NULL)
148 _OPENSSL_isservice.p = (void *)-1;
151 if (_OPENSSL_isservice.p != (void *)-1)
152 return (*_OPENSSL_isservice.f) ();
154 h = GetProcessWindowStation();
158 if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
159 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
163 return -1; /* paranoia */
164 len++, len &= ~1; /* paranoia */
165 name = (WCHAR *)alloca(len + sizeof(WCHAR));
166 if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
169 len++, len &= ~1; /* paranoia */
170 name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
173 * This doesn't cover "interactive" services [working with real
174 * WinSta0's] nor programs started non-interactively by Task Scheduler
175 * [those are working with SAWinSta].
177 if (wcsstr(name, L"Service-0x"))
180 /* This covers all non-interactive programs such as services. */
181 if (!wcsstr(name, L"WinSta0"))
189 int OPENSSL_isservice(void)
195 void OPENSSL_showfatal(const char *fmta, ...)
201 * First check if it's a console application, in which case the
202 * error message would be printed to standard error.
203 * Windows CE does not have a concept of a console application,
204 * so we need to guard the check.
206 # ifdef STD_ERROR_HANDLE
209 if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
210 GetFileType(h) != FILE_TYPE_UNKNOWN) {
211 /* must be console application */
216 len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
217 WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
223 if (sizeof(TCHAR) == sizeof(char))
224 fmt = (const TCHAR *)fmta;
228 size_t len_0 = strlen(fmta) + 1, i;
231 fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
233 fmt = (const TCHAR *)L"no stack?";
236 if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
237 for (i = 0; i < len_0; i++)
238 fmtw[i] = (WCHAR)fmta[i];
239 for (i = 0; i < len_0; i++) {
243 switch (fmtw[i + 1]) {
275 fmt = (const TCHAR *)fmtw;
279 _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
280 buf[OSSL_NELEM(buf) - 1] = _T('\0');
283 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
284 # ifdef OPENSSL_SYS_WIN_CORE
285 /* ONECORE is always NONGUI and NT >= 0x0601 */
288 * TODO: (For non GUI and no std error cases)
289 * Add event logging feature here.
292 # if !defined(NDEBUG)
294 * We are in a situation where we tried to report a critical
295 * error and this failed for some reason. As a last resort,
296 * in debug builds, send output to the debugger or any other
297 * tool like DebugView which can monitor the output.
299 OutputDebugString(buf);
302 /* this -------------v--- guards NT-specific calls */
303 if (check_winnt() && OPENSSL_isservice() > 0) {
304 HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
306 if (hEventLog != NULL) {
307 const TCHAR *pmsg = buf;
309 if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
310 1, 0, &pmsg, NULL)) {
311 # if !defined(NDEBUG)
313 * We are in a situation where we tried to report a critical
314 * error and this failed for some reason. As a last resort,
315 * in debug builds, send output to the debugger or any other
316 * tool like DebugView which can monitor the output.
318 OutputDebugString(pmsg);
322 (void)DeregisterEventSource(hEventLog);
325 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
329 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
333 void OPENSSL_showfatal(const char *fmta, ...)
335 #ifndef OPENSSL_NO_STDIO
339 vfprintf(stderr, fmta, ap);
344 int OPENSSL_isservice(void)
350 void OPENSSL_die(const char *message, const char *file, int line)
352 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
353 file, line, message);
358 * Win32 abort() customarily shows a dialog, but we just did that...
360 # if !defined(_WIN32_WCE)
367 #if !defined(OPENSSL_CPUID_OBJ)
369 * The volatile is used to to ensure that the compiler generates code that reads
370 * all values from the array and doesn't try to optimize this away. The standard
371 * doesn't actually require this behavior if the original data pointed to is
372 * not volatile, but compilers do this in practice anyway.
374 * There are also assembler versions of this function.
376 # undef CRYPTO_memcmp
377 int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
380 const volatile unsigned char *a = in_a;
381 const volatile unsigned char *b = in_b;
384 for (i = 0; i < len; i++)
391 * For systems that don't provide an instruction counter register or equivalent.
393 uint32_t OPENSSL_rdtsc(void)