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;
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)
108 #if defined(_WIN32) && !defined(__CYGWIN__)
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 int OPENSSL_isservice(void)
131 } _OPENSSL_isservice = {
135 if (_OPENSSL_isservice.p == NULL) {
136 HANDLE mod = GetModuleHandle(NULL);
138 _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
139 if (_OPENSSL_isservice.p == NULL)
140 _OPENSSL_isservice.p = (void *)-1;
143 if (_OPENSSL_isservice.p != (void *)-1)
144 return (*_OPENSSL_isservice.f) ();
146 h = GetProcessWindowStation();
150 if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
151 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
155 return -1; /* paranoia */
156 len++, len &= ~1; /* paranoia */
157 name = (WCHAR *)alloca(len + sizeof(WCHAR));
158 if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
161 len++, len &= ~1; /* paranoia */
162 name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
165 * This doesn't cover "interactive" services [working with real
166 * WinSta0's] nor programs started non-interactively by Task Scheduler
167 * [those are working with SAWinSta].
169 if (wcsstr(name, L"Service-0x"))
172 /* This covers all non-interactive programs such as services. */
173 if (!wcsstr(name, L"WinSta0"))
180 int OPENSSL_isservice(void)
186 void OPENSSL_showfatal(const char *fmta, ...)
192 * First check if it's a console application, in which case the
193 * error message would be printed to standard error.
194 * Windows CE does not have a concept of a console application,
195 * so we need to guard the check.
197 # ifdef STD_ERROR_HANDLE
200 if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
201 GetFileType(h) != FILE_TYPE_UNKNOWN) {
202 /* must be console application */
207 len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
208 WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
214 if (sizeof(TCHAR) == sizeof(char))
215 fmt = (const TCHAR *)fmta;
219 size_t len_0 = strlen(fmta) + 1, i;
222 fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
224 fmt = (const TCHAR *)L"no stack?";
227 if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
228 for (i = 0; i < len_0; i++)
229 fmtw[i] = (WCHAR)fmta[i];
230 for (i = 0; i < len_0; i++) {
234 switch (fmtw[i + 1]) {
266 fmt = (const TCHAR *)fmtw;
270 _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
271 buf[OSSL_NELEM(buf) - 1] = _T('\0');
274 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
275 /* this -------------v--- guards NT-specific calls */
276 if (check_winnt() && OPENSSL_isservice() > 0) {
277 HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
279 if (hEventLog != NULL) {
280 const TCHAR *pmsg = buf;
282 if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
283 1, 0, &pmsg, NULL)) {
286 * We are in a situation where we tried to report a critical
287 * error and this failed for some reason. As a last resort,
288 * in debug builds, send output to the debugger or any other
289 * tool like DebugView which can monitor the output.
291 OutputDebugString(pmsg);
295 (void)DeregisterEventSource(hEventLog);
299 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
302 void OPENSSL_showfatal(const char *fmta, ...)
304 #ifndef OPENSSL_NO_STDIO
308 vfprintf(stderr, fmta, ap);
313 int OPENSSL_isservice(void)
319 void OPENSSL_die(const char *message, const char *file, int line)
321 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
322 file, line, message);
323 #if !defined(_WIN32) || defined(__CYGWIN__)
327 * Win32 abort() customarily shows a dialog, but we just did that...
329 # if !defined(_WIN32_WCE)
336 #if !defined(OPENSSL_CPUID_OBJ)
338 * The volatile is used to to ensure that the compiler generates code that reads
339 * all values from the array and doesn't try to optimize this away. The standard
340 * doesn't actually require this behavior if the original data pointed to is
341 * not volatile, but compilers do this in practice anyway.
343 * There are also assembler versions of this function.
345 # undef CRYPTO_memcmp
346 int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
349 const volatile unsigned char *a = in_a;
350 const volatile unsigned char *b = in_b;
353 for (i = 0; i < len; i++)