aaa3fce4fbcfea17aa46179088e4695a7dbf9f31
[openssl.git] / crypto / cryptlib.c
1 /* ====================================================================
2  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@openssl.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This product includes cryptographic software written by Eric Young
50  * (eay@cryptsoft.com).  This product includes software written by Tim
51  * Hudson (tjh@cryptsoft.com).
52  *
53  */
54 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
55  * All rights reserved.
56  *
57  * This package is an SSL implementation written
58  * by Eric Young (eay@cryptsoft.com).
59  * The implementation was written so as to conform with Netscapes SSL.
60  *
61  * This library is free for commercial and non-commercial use as long as
62  * the following conditions are aheared to.  The following conditions
63  * apply to all code found in this distribution, be it the RC4, RSA,
64  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
65  * included with this distribution is covered by the same copyright terms
66  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
67  *
68  * Copyright remains Eric Young's, and as such any Copyright notices in
69  * the code are not to be removed.
70  * If this package is used in a product, Eric Young should be given attribution
71  * as the author of the parts of the library used.
72  * This can be in the form of a textual message at program startup or
73  * in documentation (online or textual) provided with the package.
74  *
75  * Redistribution and use in source and binary forms, with or without
76  * modification, are permitted provided that the following conditions
77  * are met:
78  * 1. Redistributions of source code must retain the copyright
79  *    notice, this list of conditions and the following disclaimer.
80  * 2. Redistributions in binary form must reproduce the above copyright
81  *    notice, this list of conditions and the following disclaimer in the
82  *    documentation and/or other materials provided with the distribution.
83  * 3. All advertising materials mentioning features or use of this software
84  *    must display the following acknowledgement:
85  *    "This product includes cryptographic software written by
86  *     Eric Young (eay@cryptsoft.com)"
87  *    The word 'cryptographic' can be left out if the rouines from the library
88  *    being used are not cryptographic related :-).
89  * 4. If you include any Windows specific code (or a derivative thereof) from
90  *    the apps directory (application code) you must include an acknowledgement:
91  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
92  *
93  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
94  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
97  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
99  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103  * SUCH DAMAGE.
104  *
105  * The licence and distribution terms for any publically available version or
106  * derivative of this code cannot be changed.  i.e. this code cannot simply be
107  * copied and put under another distribution licence
108  * [including the GNU Public Licence.]
109  */
110 /* ====================================================================
111  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112  * ECDH support in OpenSSL originally developed by
113  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114  */
115
116 #include "internal/cryptlib_int.h"
117 #include <openssl/safestack.h>
118
119 #if     defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
120         defined(__x86_64) || defined(__x86_64__) || \
121         defined(_M_AMD64) || defined(_M_X64)
122
123 extern unsigned int OPENSSL_ia32cap_P[4];
124 unsigned int *OPENSSL_ia32cap_loc(void)
125 {
126     return OPENSSL_ia32cap_P;
127 }
128
129 # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
130 #include <stdio.h>
131 #  define OPENSSL_CPUID_SETUP
132 typedef uint64_t IA32CAP;
133 void OPENSSL_cpuid_setup(void)
134 {
135     static int trigger = 0;
136     IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
137     IA32CAP vec;
138     char *env;
139
140     if (trigger)
141         return;
142
143     trigger = 1;
144     if ((env = getenv("OPENSSL_ia32cap"))) {
145         int off = (env[0] == '~') ? 1 : 0;
146 #  if defined(_WIN32)
147         if (!sscanf(env + off, "%I64i", &vec))
148             vec = strtoul(env + off, NULL, 0);
149 #  else
150         if (!sscanf(env + off, "%lli", (long long *)&vec))
151             vec = strtoul(env + off, NULL, 0);
152 #  endif
153         if (off)
154             vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~vec;
155         else if (env[0] == ':')
156             vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
157
158         OPENSSL_ia32cap_P[2] = 0;
159         if ((env = strchr(env, ':'))) {
160             unsigned int vecx;
161             env++;
162             off = (env[0] == '~') ? 1 : 0;
163             vecx = strtoul(env + off, NULL, 0);
164             if (off)
165                 OPENSSL_ia32cap_P[2] &= ~vecx;
166             else
167                 OPENSSL_ia32cap_P[2] = vecx;
168         }
169     } else
170         vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
171
172     /*
173      * |(1<<10) sets a reserved bit to signal that variable
174      * was initialized already... This is to avoid interference
175      * with cpuid snippets in ELF .init segment.
176      */
177     OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
178     OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
179 }
180 # else
181 unsigned int OPENSSL_ia32cap_P[4];
182 # endif
183
184 #else
185 unsigned int *OPENSSL_ia32cap_loc(void)
186 {
187     return NULL;
188 }
189 #endif
190 int OPENSSL_NONPIC_relocated = 0;
191 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
192 void OPENSSL_cpuid_setup(void)
193 {
194 }
195 #endif
196
197 #if defined(_WIN32) && !defined(__CYGWIN__)
198 # include <tchar.h>
199 # include <signal.h>
200 # ifdef __WATCOMC__
201 #  if defined(_UNICODE) || defined(__UNICODE__)
202 #   define _vsntprintf _vsnwprintf
203 #  else
204 #   define _vsntprintf _vsnprintf
205 #  endif
206 # endif
207 # ifdef _MSC_VER
208 #  define alloca _alloca
209 # endif
210
211 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
212 int OPENSSL_isservice(void)
213 {
214     HWINSTA h;
215     DWORD len;
216     WCHAR *name;
217     static union {
218         void *p;
219         FARPROC f;
220     } _OPENSSL_isservice = {
221         NULL
222     };
223
224     if (_OPENSSL_isservice.p == NULL) {
225         HANDLE mod = GetModuleHandle(NULL);
226         if (mod != NULL)
227             _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
228         if (_OPENSSL_isservice.p == NULL)
229             _OPENSSL_isservice.p = (void *)-1;
230     }
231
232     if (_OPENSSL_isservice.p != (void *)-1)
233         return (*_OPENSSL_isservice.f) ();
234
235     h = GetProcessWindowStation();
236     if (h == NULL)
237         return -1;
238
239     if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
240         GetLastError() != ERROR_INSUFFICIENT_BUFFER)
241         return -1;
242
243     if (len > 512)
244         return -1;              /* paranoia */
245     len++, len &= ~1;           /* paranoia */
246     name = (WCHAR *)alloca(len + sizeof(WCHAR));
247     if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
248         return -1;
249
250     len++, len &= ~1;           /* paranoia */
251     name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
252 #  if 1
253     /*
254      * This doesn't cover "interactive" services [working with real
255      * WinSta0's] nor programs started non-interactively by Task Scheduler
256      * [those are working with SAWinSta].
257      */
258     if (wcsstr(name, L"Service-0x"))
259         return 1;
260 #  else
261     /* This covers all non-interactive programs such as services. */
262     if (!wcsstr(name, L"WinSta0"))
263         return 1;
264 #  endif
265     else
266         return 0;
267 }
268 # else
269 int OPENSSL_isservice(void)
270 {
271     return 0;
272 }
273 # endif
274
275 void OPENSSL_showfatal(const char *fmta, ...)
276 {
277     va_list ap;
278     TCHAR buf[256];
279     const TCHAR *fmt;
280 # ifdef STD_ERROR_HANDLE        /* what a dirty trick! */
281     HANDLE h;
282
283     if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
284         GetFileType(h) != FILE_TYPE_UNKNOWN) {
285         /* must be console application */
286         int len;
287         DWORD out;
288
289         va_start(ap, fmta);
290         len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
291         WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
292         va_end(ap);
293         return;
294     }
295 # endif
296
297     if (sizeof(TCHAR) == sizeof(char))
298         fmt = (const TCHAR *)fmta;
299     else
300         do {
301             int keepgoing;
302             size_t len_0 = strlen(fmta) + 1, i;
303             WCHAR *fmtw;
304
305             fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
306             if (fmtw == NULL) {
307                 fmt = (const TCHAR *)L"no stack?";
308                 break;
309             }
310             if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
311                 for (i = 0; i < len_0; i++)
312                     fmtw[i] = (WCHAR)fmta[i];
313             for (i = 0; i < len_0; i++) {
314                 if (fmtw[i] == L'%')
315                     do {
316                         keepgoing = 0;
317                         switch (fmtw[i + 1]) {
318                         case L'0':
319                         case L'1':
320                         case L'2':
321                         case L'3':
322                         case L'4':
323                         case L'5':
324                         case L'6':
325                         case L'7':
326                         case L'8':
327                         case L'9':
328                         case L'.':
329                         case L'*':
330                         case L'-':
331                             i++;
332                             keepgoing = 1;
333                             break;
334                         case L's':
335                             fmtw[i + 1] = L'S';
336                             break;
337                         case L'S':
338                             fmtw[i + 1] = L's';
339                             break;
340                         case L'c':
341                             fmtw[i + 1] = L'C';
342                             break;
343                         case L'C':
344                             fmtw[i + 1] = L'c';
345                             break;
346                         }
347                     } while (keepgoing);
348             }
349             fmt = (const TCHAR *)fmtw;
350         } while (0);
351
352     va_start(ap, fmta);
353     _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
354     buf[OSSL_NELEM(buf) - 1] = _T('\0');
355     va_end(ap);
356
357 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
358     /* this -------------v--- guards NT-specific calls */
359     if (check_winnt() && OPENSSL_isservice() > 0) {
360         HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
361
362         if (hEventLog != NULL) {
363             const TCHAR *pmsg = buf;
364
365             if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
366                              1, 0, &pmsg, NULL)) {
367 #if defined(DEBUG)
368                 /*
369                  * We are in a situation where we tried to report a critical
370                  * error and this failed for some reason. As a last resort,
371                  * in debug builds, send output to the debugger or any other
372                  * tool like DebugView which can monitor the output.
373                  */
374                 OutputDebugString(pmsg);
375 #endif
376             }
377
378             (void)DeregisterEventSource(hEventLog);
379         }
380     } else
381 # endif
382         MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
383 }
384 #else
385 void OPENSSL_showfatal(const char *fmta, ...)
386 {
387 #ifndef OPENSSL_NO_STDIO
388     va_list ap;
389
390     va_start(ap, fmta);
391     vfprintf(stderr, fmta, ap);
392     va_end(ap);
393 #endif
394 }
395
396 int OPENSSL_isservice(void)
397 {
398     return 0;
399 }
400 #endif
401
402 void OPENSSL_die(const char *message, const char *file, int line)
403 {
404     OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
405                       file, line, message);
406 #if !defined(_WIN32) || defined(__CYGWIN__)
407     abort();
408 #else
409     /*
410      * Win32 abort() customarily shows a dialog, but we just did that...
411      */
412 # if !defined(_WIN32_WCE)
413     raise(SIGABRT);
414 # endif
415     _exit(3);
416 #endif
417 }
418
419 /* volatile unsigned char* pointers are there because
420  * 1. Accessing a variable declared volatile via a pointer
421  *    that lacks a volatile qualifier causes undefined behavior.
422  * 2. When the variable itself is not volatile the compiler is
423  *    not required to keep all those reads and can convert
424  *    this into canonical memcmp() which doesn't read the whole block.
425  * Pointers to volatile resolve the first problem fully. The second
426  * problem cannot be resolved in any Standard-compliant way but this
427  * works the problem around. Compilers typically react to
428  * pointers to volatile by preserving the reads and writes through them.
429  * The latter is not required by the Standard if the memory pointed to
430  * is not volatile.
431  * Pointers themselves are volatile in the function signature to work
432  * around a subtle bug in gcc 4.6+ which causes writes through
433  * pointers to volatile to not be emitted in some rare,
434  * never needed in real life, pieces of code.
435  */
436 int CRYPTO_memcmp(const volatile void * volatile in_a,
437                   const volatile void * volatile in_b,
438                   size_t len)
439 {
440     size_t i;
441     const volatile unsigned char *a = in_a;
442     const volatile unsigned char *b = in_b;
443     unsigned char x = 0;
444
445     for (i = 0; i < len; i++)
446         x |= a[i] ^ b[i];
447
448     return x;
449 }