1 /*
2  * Copyright (c) 2018 Yubico AB. All rights reserved.
3  * Use of this source code is governed by a BSD-style
4  * license that can be found in the LICENSE file.
5  */
6 
7 #ifndef _OPENBSD_COMPAT_H
8 #define _OPENBSD_COMPAT_H
9 
10 #if defined(_MSC_VER)
11 #include "types.h"
12 #endif
13 
14 #if defined(HAVE_ENDIAN_H)
15 #include <endian.h>
16 #endif
17 
18 #if defined(__APPLE__) && !defined(HAVE_ENDIAN_H)
19 #include <libkern/OSByteOrder.h>
20 #define be16toh(x) OSSwapBigToHostInt16((x))
21 #define htobe16(x) OSSwapHostToBigInt16((x))
22 #define be32toh(x) OSSwapBigToHostInt32((x))
23 #define htole32(x) OSSwapHostToLittleInt32((x))
24 #define htole64(x) OSSwapHostToLittleInt64((x))
25 #endif /* __APPLE__ && !HAVE_ENDIAN_H */
26 
27 #if defined(_WIN32) && !defined(HAVE_ENDIAN_H)
28 #include <stdint.h>
29 #include <winsock2.h>
30 #if !defined(_MSC_VER)
31 #include <sys/param.h>
32 #endif
33 #define be16toh(x) ntohs((x))
34 #define htobe16(x) htons((x))
35 #define be32toh(x) ntohl((x))
36 uint32_t htole32(uint32_t);
37 uint64_t htole64(uint64_t);
38 #endif /* _WIN32 && !HAVE_ENDIAN_H */
39 
40 #if defined(__FreeBSD__) && !defined(HAVE_ENDIAN_H)
41 #include <sys/endian.h>
42 #endif
43 
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #if !defined(HAVE_STRLCAT)
48 size_t strlcat(char *, const char *, size_t);
49 #endif
50 
51 #if !defined(HAVE_STRLCPY)
52 size_t strlcpy(char *, const char *, size_t);
53 #endif
54 
55 #if !defined(HAVE_RECALLOCARRAY)
56 void *recallocarray(void *, size_t, size_t, size_t);
57 #endif
58 
59 #if !defined(HAVE_EXPLICIT_BZERO)
60 void explicit_bzero(void *, size_t);
61 #endif
62 
63 #if !defined(HAVE_FREEZERO)
64 void freezero(void *, size_t);
65 #endif
66 
67 #if !defined(HAVE_GETPAGESIZE)
68 int getpagesize(void);
69 #endif
70 
71 #if !defined(HAVE_TIMINGSAFE_BCMP)
72 int timingsafe_bcmp(const void *, const void *, size_t);
73 #endif
74 
75 #if !defined(HAVE_READPASSPHRASE)
76 #include "readpassphrase.h"
77 #else
78 #include <readpassphrase.h>
79 #endif
80 
81 #include <openssl/opensslv.h>
82 
83 #if OPENSSL_VERSION_NUMBER < 0x10100000L
84 #include <stdint.h>
85 #include "hkdf.h"
86 #define EVP_PKEY_get0_EC_KEY(x) ((x)->pkey.ec)
87 #define EVP_PKEY_get0_RSA(x) ((x)->pkey.rsa)
88 #endif
89 
90 #if !defined(HAVE_ERR_H)
91 #include "err.h"
92 #else
93 #include <err.h>
94 #endif
95 
96 #if !defined(HAVE_GETOPT)
97 #include "getopt.h"
98 #else
99 #include <unistd.h>
100 #endif
101 
102 #if !defined(HAVE_GETLINE)
103 #include <stdio.h>
104 ssize_t getline(char **, size_t *, FILE *);
105 #endif
106 
107 #if defined(_MSC_VER)
108 #define strerror_r(e, b, l) strerror_s((b), (l), (e))
109 #endif
110 
111 #include "time.h"
112 
113 #if !defined(HAVE_POSIX_IOCTL)
114 #define IOCTL_REQ(x)	(x)
115 #else
116 #define IOCTL_REQ(x)	((int)(x))
117 #endif
118 
119 #endif /* !_OPENBSD_COMPAT_H */
120