2 * Copyright 1998-2017 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;
67 vecx = strtoul(env + off, NULL, 0);
69 OPENSSL_ia32cap_P[2] &= ~vecx;
71 OPENSSL_ia32cap_P[2] = vecx;
73 OPENSSL_ia32cap_P[2] = 0;
76 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
80 * |(1<<10) sets a reserved bit to signal that variable
81 * was initialized already... This is to avoid interference
82 * with cpuid snippets in ELF .init segment.
84 OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
85 OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
88 unsigned int OPENSSL_ia32cap_P[4];
91 int OPENSSL_NONPIC_relocated = 0;
92 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
93 void OPENSSL_cpuid_setup(void)
98 #if defined(_WIN32) && !defined(__CYGWIN__)
102 # if defined(_UNICODE) || defined(__UNICODE__)
103 # define _vsntprintf _vsnwprintf
105 # define _vsntprintf _vsnprintf
109 # define alloca _alloca
112 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
113 int OPENSSL_isservice(void)
121 } _OPENSSL_isservice = {
125 if (_OPENSSL_isservice.p == NULL) {
126 HANDLE mod = GetModuleHandle(NULL);
128 _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
129 if (_OPENSSL_isservice.p == NULL)
130 _OPENSSL_isservice.p = (void *)-1;
133 if (_OPENSSL_isservice.p != (void *)-1)
134 return (*_OPENSSL_isservice.f) ();
136 h = GetProcessWindowStation();
140 if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
141 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
145 return -1; /* paranoia */
146 len++, len &= ~1; /* paranoia */
147 name = (WCHAR *)alloca(len + sizeof(WCHAR));
148 if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
151 len++, len &= ~1; /* paranoia */
152 name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
155 * This doesn't cover "interactive" services [working with real
156 * WinSta0's] nor programs started non-interactively by Task Scheduler
157 * [those are working with SAWinSta].
159 if (wcsstr(name, L"Service-0x"))
162 /* This covers all non-interactive programs such as services. */
163 if (!wcsstr(name, L"WinSta0"))
170 int OPENSSL_isservice(void)
176 void OPENSSL_showfatal(const char *fmta, ...)
182 * First check if it's a console application, in which case the
183 * error message would be printed to standard error.
184 * Windows CE does not have a concept of a console application,
185 * so we need to guard the check.
187 # ifdef STD_ERROR_HANDLE
190 if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
191 GetFileType(h) != FILE_TYPE_UNKNOWN) {
192 /* must be console application */
197 len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
198 WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
204 if (sizeof(TCHAR) == sizeof(char))
205 fmt = (const TCHAR *)fmta;
209 size_t len_0 = strlen(fmta) + 1, i;
212 fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
214 fmt = (const TCHAR *)L"no stack?";
217 if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
218 for (i = 0; i < len_0; i++)
219 fmtw[i] = (WCHAR)fmta[i];
220 for (i = 0; i < len_0; i++) {
224 switch (fmtw[i + 1]) {
256 fmt = (const TCHAR *)fmtw;
260 _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
261 buf[OSSL_NELEM(buf) - 1] = _T('\0');
264 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
265 /* this -------------v--- guards NT-specific calls */
266 if (check_winnt() && OPENSSL_isservice() > 0) {
267 HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
269 if (hEventLog != NULL) {
270 const TCHAR *pmsg = buf;
272 if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
273 1, 0, &pmsg, NULL)) {
276 * We are in a situation where we tried to report a critical
277 * error and this failed for some reason. As a last resort,
278 * in debug builds, send output to the debugger or any other
279 * tool like DebugView which can monitor the output.
281 OutputDebugString(pmsg);
285 (void)DeregisterEventSource(hEventLog);
289 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
292 void OPENSSL_showfatal(const char *fmta, ...)
294 #ifndef OPENSSL_NO_STDIO
298 vfprintf(stderr, fmta, ap);
303 int OPENSSL_isservice(void)
309 void OPENSSL_die(const char *message, const char *file, int line)
311 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
312 file, line, message);
313 #if !defined(_WIN32) || defined(__CYGWIN__)
317 * Win32 abort() customarily shows a dialog, but we just did that...
319 # if !defined(_WIN32_WCE)
326 #if !defined(OPENSSL_CPUID_OBJ)
328 * The volatile is used to to ensure that the compiler generates code that reads
329 * all values from the array and doesn't try to optimize this away. The standard
330 * doesn't actually require this behavior if the original data pointed to is
331 * not volatile, but compilers do this in practice anyway.
333 * There are also assembler versions of this function.
335 # undef CRYPTO_memcmp
336 int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
339 const volatile unsigned char *a = in_a;
340 const volatile unsigned char *b = in_b;
343 for (i = 0; i < len; i++)