[crypto/ec] don't assume points are of order group->order
[openssl.git] / e_os.h
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef HEADER_E_OS_H
11 # define HEADER_E_OS_H
12
13 # include <openssl/opensslconf.h>
14
15 # include <openssl/e_os2.h>
16 /*
17  * <openssl/e_os2.h> contains what we can justify to make visible to the
18  * outside; this file e_os.h is not part of the exported interface.
19  */
20
21 #ifdef  __cplusplus
22 extern "C" {
23 #endif
24
25 /* Used to checking reference counts, most while doing perl5 stuff :-) */
26 # if defined(OPENSSL_NO_STDIO)
27 #  if defined(REF_PRINT)
28 #   error "REF_PRINT requires stdio"
29 #  endif
30 # endif
31
32 /*
33  * BIO_printf format modifier for [u]int64_t.
34  */
35 # if defined(__LP64__) || (defined(__SIZEOF_LONG__) && __SIZEOF_LONG__==8)
36 #  define BIO_PRI64 "l"     /* 'll' does work "universally", but 'l' is
37                              * here to shut -Wformat warnings in LP64... */
38 # else
39 #  define BIO_PRI64 "ll"
40 # endif
41
42 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
43 #  define REF_ASSERT_ISNT(test) \
44     (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
45 # else
46 #  define REF_ASSERT_ISNT(i)
47 # endif
48 # ifdef REF_PRINT
49 #  define REF_PRINT_COUNT(a, b) \
50         fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
51 # else
52 #  define REF_PRINT_COUNT(a, b)
53 # endif
54
55 # define osslargused(x)      (void)x
56 # define OPENSSL_CONF        "openssl.cnf"
57
58 # ifndef DEVRANDOM
59 /*
60  * set this to a comma-separated list of 'random' device files to try out. My
61  * default, we will try to read at least one of these files
62  */
63 #  define DEVRANDOM "/dev/urandom","/dev/random","/dev/srandom"
64 # endif
65 # if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
66 /*
67  * set this to a comma-separated list of 'egd' sockets to try out. These
68  * sockets will be tried in the order listed in case accessing the device
69  * files listed in DEVRANDOM did not return enough entropy.
70  */
71 #  define DEVRANDOM_EGD "/var/run/egd-pool","/dev/egd-pool","/etc/egd-pool","/etc/entropy"
72 # endif
73
74 # if defined(OPENSSL_SYS_VXWORKS)
75 #  define NO_SYS_PARAM_H
76 #  define NO_CHMOD
77 #  define NO_SYSLOG
78 # endif
79
80 /********************************************************************
81  The Microsoft section
82  ********************************************************************/
83 # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
84 #  define WIN32
85 # endif
86 # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
87 #  define WINDOWS
88 # endif
89 # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
90 #  define MSDOS
91 # endif
92
93 # if (defined(MSDOS) || defined(OPENSSL_SYS_UEFI)) && !defined(GETPID_IS_MEANINGLESS)
94 #  define GETPID_IS_MEANINGLESS
95 # endif
96
97 # ifdef WIN32
98 #  define NO_SYS_UN_H
99 #  define get_last_sys_error()    GetLastError()
100 #  define clear_sys_error()       SetLastError(0)
101 #  if !defined(WINNT)
102 #   define WIN_CONSOLE_BUG
103 #  endif
104 # else
105 #  define get_last_sys_error()    errno
106 #  define clear_sys_error()       errno=0
107 # endif
108
109 # if defined(WINDOWS)
110 #  define get_last_socket_error() WSAGetLastError()
111 #  define clear_socket_error()    WSASetLastError(0)
112 #  define readsocket(s,b,n)       recv((s),(b),(n),0)
113 #  define writesocket(s,b,n)      send((s),(b),(n),0)
114 # elif defined(__DJGPP__)
115 #  define WATT32
116 #  define WATT32_NO_OLDIES
117 #  define get_last_socket_error() errno
118 #  define clear_socket_error()    errno=0
119 #  define closesocket(s)          close_s(s)
120 #  define readsocket(s,b,n)       read_s(s,b,n)
121 #  define writesocket(s,b,n)      send(s,b,n,0)
122 # elif defined(OPENSSL_SYS_VMS)
123 #  define get_last_socket_error() errno
124 #  define clear_socket_error()    errno=0
125 #  define ioctlsocket(a,b,c)      ioctl(a,b,c)
126 #  define closesocket(s)          close(s)
127 #  define readsocket(s,b,n)       recv((s),(b),(n),0)
128 #  define writesocket(s,b,n)      send((s),(b),(n),0)
129 # elif defined(OPENSSL_SYS_VXWORKS)
130 #  define get_last_socket_error() errno
131 #  define clear_socket_error()    errno=0
132 #  define ioctlsocket(a,b,c)          ioctl((a),(b),(int)(c))
133 #  define closesocket(s)              close(s)
134 #  define readsocket(s,b,n)           read((s),(b),(n))
135 #  define writesocket(s,b,n)          write((s),(char *)(b),(n))
136 # else
137 #  define get_last_socket_error() errno
138 #  define clear_socket_error()    errno=0
139 #  define ioctlsocket(a,b,c)      ioctl(a,b,c)
140 #  define closesocket(s)          close(s)
141 #  define readsocket(s,b,n)       read((s),(b),(n))
142 #  define writesocket(s,b,n)      write((s),(b),(n))
143 # endif
144
145 # if (defined(WINDOWS) || defined(MSDOS))
146
147 #  ifdef __DJGPP__
148 #   include <unistd.h>
149 #   include <sys/stat.h>
150 #   include <sys/socket.h>
151 #   include <sys/un.h>
152 #   include <tcp.h>
153 #   include <netdb.h>
154 #   define _setmode setmode
155 #   define _O_TEXT O_TEXT
156 #   define _O_BINARY O_BINARY
157 #   define HAS_LFN_SUPPORT(name)  (pathconf((name), _PC_NAME_MAX) > 12)
158 #   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
159 #   undef DEVRANDOM
160 #   define DEVRANDOM "/dev/urandom\x24"
161 #  endif                        /* __DJGPP__ */
162
163 #  ifndef S_IFDIR
164 #   define S_IFDIR     _S_IFDIR
165 #  endif
166
167 #  ifndef S_IFMT
168 #   define S_IFMT      _S_IFMT
169 #  endif
170
171 #  if !defined(WINNT) && !defined(__DJGPP__)
172 #   define NO_SYSLOG
173 #  endif
174
175 #  ifdef WINDOWS
176 #   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
177        /*
178         * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
179         * Most notably we ought to check for availability of each specific
180         * routine that was introduced after denoted _WIN32_WINNT with
181         * GetProcAddress(). Normally newer functions are masked with higher
182         * _WIN32_WINNT in SDK headers. So that if you wish to use them in
183         * some module, you'd need to override _WIN32_WINNT definition in
184         * the target module in order to "reach for" prototypes, but replace
185         * calls to new functions with indirect calls. Alternatively it
186         * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
187         * and check for current OS version instead.
188         */
189 #    define _WIN32_WINNT 0x0501
190 #   endif
191 #   if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
192        /*
193         * Just like defining _WIN32_WINNT including winsock2.h implies
194         * certain "discipline" for maintaining [broad] binary compatibility.
195         * As long as structures are invariant among Winsock versions,
196         * it's sufficient to check for specific Winsock2 API availability
197         * at run-time [DSO_global_lookup is recommended]...
198         */
199 #    include <winsock2.h>
200 #    include <ws2tcpip.h>
201        /* yes, they have to be #included prior to <windows.h> */
202 #   endif
203 #   include <windows.h>
204 #   include <stdio.h>
205 #   include <stddef.h>
206 #   include <errno.h>
207 #   if defined(_WIN32_WCE) && !defined(EACCES)
208 #    define EACCES   13
209 #   endif
210 #   include <string.h>
211 #   ifdef _WIN64
212 #    define strlen(s) _strlen31(s)
213 /* cut strings to 2GB */
214 static __inline unsigned int _strlen31(const char *str)
215 {
216     unsigned int len = 0;
217     while (*str && len < 0x80000000U)
218         str++, len++;
219     return len & 0x7FFFFFFF;
220 }
221 #   endif
222 #   include <malloc.h>
223 #   if defined(_MSC_VER) && _MSC_VER<=1200 && defined(_MT) && defined(isspace)
224        /* compensate for bug in VC6 ctype.h */
225 #    undef isspace
226 #    undef isdigit
227 #    undef isalnum
228 #    undef isupper
229 #    undef isxdigit
230 #   endif
231 #   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
232 #    if _MSC_VER>=1300 && _MSC_VER<1600
233 #     undef stdin
234 #     undef stdout
235 #     undef stderr
236 FILE *__iob_func();
237 #     define stdin  (&__iob_func()[0])
238 #     define stdout (&__iob_func()[1])
239 #     define stderr (&__iob_func()[2])
240 #    elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
241 #     undef stdin
242 #     undef stdout
243 #     undef stderr
244          /*
245           * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
246           * or in other words with /MD. Declaring implicit import, i.e. with
247           * _imp_ prefix, works correctly with all compiler options, but
248           * without /MD results in LINK warning LNK4049: 'locally defined
249           * symbol "__iob" imported'.
250           */
251 extern FILE *_imp___iob;
252 #     define stdin  (&_imp___iob[0])
253 #     define stdout (&_imp___iob[1])
254 #     define stderr (&_imp___iob[2])
255 #    endif
256 #   endif
257 #  endif
258 #  include <io.h>
259 #  include <fcntl.h>
260
261 #  ifdef OPENSSL_SYS_WINCE
262 #   define OPENSSL_NO_POSIX_IO
263 #  endif
264
265 #  define EXIT(n) exit(n)
266 #  define LIST_SEPARATOR_CHAR ';'
267 #  ifndef X_OK
268 #   define X_OK        0
269 #  endif
270 #  ifndef W_OK
271 #   define W_OK        2
272 #  endif
273 #  ifndef R_OK
274 #   define R_OK        4
275 #  endif
276 #  ifdef OPENSSL_SYS_WINCE
277 #   define DEFAULT_HOME  ""
278 #  else
279 #   define DEFAULT_HOME  "C:"
280 #  endif
281
282 /* Avoid Visual Studio 13 GetVersion deprecated problems */
283 #  if defined(_MSC_VER) && _MSC_VER>=1800
284 #   define check_winnt() (1)
285 #   define check_win_minplat(x) (1)
286 #  else
287 #   define check_winnt() (GetVersion() < 0x80000000)
288 #   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
289 #  endif
290
291 # else                          /* The non-microsoft world */
292
293 #  ifdef OPENSSL_SYS_VMS
294 #   define VMS 1
295   /*
296    * some programs don't include stdlib, so exit() and others give implicit
297    * function warnings
298    */
299 #   include <stdlib.h>
300 #   if defined(__DECC)
301 #    include <unistd.h>
302 #   else
303 #    include <unixlib.h>
304 #   endif
305 #   define LIST_SEPARATOR_CHAR ','
306   /* We don't have any well-defined random devices on VMS, yet... */
307 #   undef DEVRANDOM
308   /*-
309      We need to do this since VMS has the following coding on status codes:
310
311      Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
312                The important thing to know is that odd numbers are considered
313                good, while even ones are considered errors.
314      Bits 3-15: actual status number
315      Bits 16-27: facility number.  0 is considered "unknown"
316      Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
317                  output the message (which, for random codes, just looks ugly)
318
319      So, what we do here is to change 0 to 1 to get the default success status,
320      and everything else is shifted up to fit into the status number field, and
321      the status is tagged as an error, which is what is wanted here.
322
323      Finally, we add the VMS C facility code 0x35a000, because there are some
324      programs, such as Perl, that will reinterpret the code back to something
325      POSIXly.  'man perlvms' explains it further.
326
327      NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
328      codes (status type = 1).  I couldn't disagree more.  Fortunately, the
329      status type doesn't seem to bother Perl.
330      -- Richard Levitte
331   */
332 #   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
333
334 #   define NO_SYS_PARAM_H
335 #   define NO_SYS_UN_H
336
337 #   define DEFAULT_HOME "SYS$LOGIN:"
338
339 #  else
340      /* !defined VMS */
341 #   ifdef OPENSSL_UNISTD
342 #    include OPENSSL_UNISTD
343 #   else
344 #    include <unistd.h>
345 #   endif
346 #   include <sys/types.h>
347 #   ifdef OPENSSL_SYS_WIN32_CYGWIN
348 #    include <io.h>
349 #    include <fcntl.h>
350 #   endif
351
352 #   define LIST_SEPARATOR_CHAR ':'
353 #   define EXIT(n)             exit(n)
354 #  endif
355
356 # endif
357
358 /*************/
359
360 # ifdef USE_SOCKETS
361 #  ifdef OPENSSL_NO_SOCK
362 #  elif defined(WINDOWS) || defined(MSDOS)
363       /* windows world */
364 #   if !defined(__DJGPP__)
365 #    if defined(_WIN32_WCE) && _WIN32_WCE<410
366 #     define getservbyname _masked_declaration_getservbyname
367 #    endif
368 #    if !defined(IPPROTO_IP)
369          /* winsock[2].h was included already? */
370 #     include <winsock.h>
371 #    endif
372 #    ifdef getservbyname
373 #     undef getservbyname
374          /* this is used to be wcecompat/include/winsock_extras.h */
375 struct servent *PASCAL getservbyname(const char *, const char *);
376 #    endif
377
378 #    ifdef _WIN64
379 /*
380  * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because
381  * the value constitutes an index in per-process table of limited size
382  * and not a real pointer. And we also depend on fact that all processors
383  * Windows run on happen to be two's-complement, which allows to
384  * interchange INVALID_SOCKET and -1.
385  */
386 #     define socket(d,t,p)   ((int)socket(d,t,p))
387 #     define accept(s,f,l)   ((int)accept(s,f,l))
388 #    endif
389 #   else
390 #   endif
391
392 #  else
393
394 #   ifndef NO_SYS_PARAM_H
395 #    include <sys/param.h>
396 #   endif
397 #   ifdef OPENSSL_SYS_VXWORKS
398 #    include <time.h>
399 #   endif
400
401 #   include <netdb.h>
402 #   if defined(OPENSSL_SYS_VMS_NODECC)
403 #    include <socket.h>
404 #    include <in.h>
405 #    include <inet.h>
406 #   else
407 #    include <sys/socket.h>
408 #    ifndef NO_SYS_UN_H
409 #     ifdef OPENSSL_SYS_VXWORKS
410 #      include <streams/un.h>
411 #     else
412 #      include <sys/un.h>
413 #     endif
414 #     ifndef UNIX_PATH_MAX
415 #      define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
416 #     endif
417 #    endif
418 #    ifdef FILIO_H
419 #     include <sys/filio.h> /* FIONBIO in some SVR4, e.g. unixware, solaris */
420 #    endif
421 #    include <netinet/in.h>
422 #    include <arpa/inet.h>
423 #    include <netinet/tcp.h>
424 #   endif
425
426 #   ifdef OPENSSL_SYS_AIX
427 #    include <sys/select.h>
428 #   endif
429
430 #   ifdef __QNX__
431 #    include <sys/select.h>
432 #   endif
433
434 #   ifndef VMS
435 #    include <sys/ioctl.h>
436 #   else
437         /* ioctl is only in VMS > 7.0 and when socketshr is not used */
438 #    if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000)
439 #     include <sys/ioctl.h>
440 #    endif
441 #   endif
442
443 #   ifdef VMS
444 #    include <unixio.h>
445 #    if defined(TCPIP_TYPE_SOCKETSHR)
446 #     include <socketshr.h>
447 #    endif
448 #   endif
449
450 #   ifndef INVALID_SOCKET
451 #    define INVALID_SOCKET      (-1)
452 #   endif                       /* INVALID_SOCKET */
453 #  endif
454
455 /*
456  * Some IPv6 implementations are broken, disable them in known bad versions.
457  */
458 #  if !defined(OPENSSL_USE_IPV6)
459 #   if defined(AF_INET6) && !defined(NETWARE_CLIB)
460 #    define OPENSSL_USE_IPV6 1
461 #   else
462 #    define OPENSSL_USE_IPV6 0
463 #   endif
464 #  endif
465
466 # endif
467
468 # ifndef OPENSSL_EXIT
469 #  if defined(MONOLITH) && !defined(OPENSSL_C)
470 #   define OPENSSL_EXIT(n) return(n)
471 #  else
472 #   define OPENSSL_EXIT(n) do { EXIT(n); return(n); } while(0)
473 #  endif
474 # endif
475
476 /***********************************************/
477
478 # if defined(OPENSSL_SYS_WINDOWS)
479 #  define strcasecmp _stricmp
480 #  define strncasecmp _strnicmp
481 #  if (_MSC_VER >= 1310)
482 #   define open _open
483 #   define fdopen _fdopen
484 #   define close _close
485 #   ifndef strdup
486 #    define strdup _strdup
487 #   endif
488 #   define unlink _unlink
489 #  endif
490 # else
491 #  include <strings.h>
492 # endif
493
494 /* vxworks */
495 # if defined(OPENSSL_SYS_VXWORKS)
496 #  include <ioLib.h>
497 #  include <tickLib.h>
498 #  include <sysLib.h>
499
500 #  define TTY_STRUCT int
501
502 #  define sleep(a) taskDelay((a) * sysClkRateGet())
503
504 #  include <vxWorks.h>
505 #  include <sockLib.h>
506 #  include <taskLib.h>
507
508 #  define getpid taskIdSelf
509
510 /*
511  * NOTE: these are implemented by helpers in database app! if the database is
512  * not linked, we need to implement them elswhere
513  */
514 struct hostent *gethostbyname(const char *name);
515 struct hostent *gethostbyaddr(const char *addr, int length, int type);
516 struct servent *getservbyname(const char *name, const char *proto);
517
518 # endif
519 /* end vxworks */
520
521 #define OSSL_NELEM(x)    (sizeof(x)/sizeof((x)[0]))
522
523 #ifdef  __cplusplus
524 }
525 #endif
526
527 #endif