1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* This is the build config file.
5  *
6  * With this you can setup what to include/exclude automatically during any build.  Just comment
7  * out the line that #define's the word for the thing you want to remove.  phew!
8  */
9 
10 #ifndef TOMCRYPT_CFG_H
11 #define TOMCRYPT_CFG_H
12 
13 #if defined(_WIN32) || defined(_MSC_VER)
14    #define LTC_CALL __cdecl
15 #elif !defined(LTC_CALL)
16    #define LTC_CALL
17 #endif
18 
19 #ifndef LTC_EXPORT
20    #define LTC_EXPORT
21 #endif
22 
23 /* certain platforms use macros for these, making the prototypes broken */
24 #ifndef LTC_NO_PROTOTYPES
25 
26 /* you can change how memory allocation works ... */
27 LTC_EXPORT void * LTC_CALL XMALLOC(size_t n);
28 LTC_EXPORT void * LTC_CALL XREALLOC(void *p, size_t n);
29 LTC_EXPORT void * LTC_CALL XCALLOC(size_t n, size_t s);
30 LTC_EXPORT void LTC_CALL XFREE(void *p);
31 
32 LTC_EXPORT void LTC_CALL XQSORT(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
33 
34 
35 /* change the clock function too */
36 LTC_EXPORT clock_t LTC_CALL XCLOCK(void);
37 
38 /* various other functions */
39 LTC_EXPORT void * LTC_CALL XMEMCPY(void *dest, const void *src, size_t n);
40 LTC_EXPORT int   LTC_CALL XMEMCMP(const void *s1, const void *s2, size_t n);
41 LTC_EXPORT void * LTC_CALL XMEMSET(void *s, int c, size_t n);
42 
43 LTC_EXPORT int   LTC_CALL XSTRCMP(const char *s1, const char *s2);
44 
45 #endif
46 
47 /* some compilers do not like "inline" (or maybe "static inline"), namely: HP cc, IBM xlc */
48 #if defined(__GNUC__) || defined(__xlc__)
49    #define LTC_INLINE __inline__
50 #elif defined(_MSC_VER) || defined(__HP_cc)
51    #define LTC_INLINE __inline
52 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
53    #define LTC_INLINE inline
54 #else
55    #define LTC_INLINE
56 #endif
57 
58 #if defined(__clang__) || defined(__GNUC_MINOR__)
59 #define LTC_NORETURN __attribute__ ((noreturn))
60 #elif defined(_MSC_VER)
61 #define LTC_NORETURN __declspec(noreturn)
62 #else
63 #define LTC_NORETURN
64 #endif
65 
66 /* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */
67 #ifndef ARGTYPE
68    #define ARGTYPE  0
69 #endif
70 
71 #undef LTC_ENCRYPT
72 #define LTC_ENCRYPT 0
73 #undef LTC_DECRYPT
74 #define LTC_DECRYPT 1
75 
76 /* Controls endianess and size of registers.  Leave uncommented to get platform neutral [slower] code
77  *
78  * Note: in order to use the optimized macros your platform must support unaligned 32 and 64 bit read/writes.
79  * The x86 platforms allow this but some others [ARM for instance] do not.  On those platforms you **MUST**
80  * use the portable [slower] macros.
81  */
82 /* detect x86/i386/ARM 32bit */
83 #if defined(__i386__) || defined(__i386) || defined(_M_IX86) || defined(_M_ARM)
84    #define ENDIAN_LITTLE
85    #define ENDIAN_32BITWORD
86    #define LTC_FAST
87 #endif
88 
89 /* detect amd64/x64/arm64 */
90 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || defined(_M_ARM64)
91    #define ENDIAN_LITTLE
92    #define ENDIAN_64BITWORD
93    #define LTC_FAST
94    #if defined(__SSE4_1__)
95       #if __SSE4_1__ == 1
96          #define LTC_AMD64_SSE4_1
97       #endif
98    #endif
99 #endif
100 
101 /* detect PPC32 */
102 #if defined(LTC_PPC32)
103    #define ENDIAN_BIG
104    #define ENDIAN_32BITWORD
105    #define LTC_FAST
106 #endif
107 
108 /* detects MIPS R5900 processors (PS2) */
109 #if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && (defined(_mips) || defined(__mips__) || defined(mips))
110    #define ENDIAN_64BITWORD
111    #if defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
112      #define ENDIAN_BIG
113    #else
114      #define ENDIAN_LITTLE
115    #endif
116 #endif
117 
118 /* detect AIX */
119 #if defined(_AIX) && defined(_BIG_ENDIAN)
120   #define ENDIAN_BIG
121   #if defined(__LP64__) || defined(_ARCH_PPC64)
122     #define ENDIAN_64BITWORD
123   #else
124     #define ENDIAN_32BITWORD
125   #endif
126 #endif
127 
128 /* detect HP-UX */
129 #if defined(__hpux) || defined(__hpux__)
130   #define ENDIAN_BIG
131   #if defined(__ia64) || defined(__ia64__) || defined(__LP64__)
132     #define ENDIAN_64BITWORD
133   #else
134     #define ENDIAN_32BITWORD
135   #endif
136 #endif
137 
138 /* detect Apple OS X */
139 #if defined(__APPLE__) && defined(__MACH__)
140   #if defined(__LITTLE_ENDIAN__) || defined(__x86_64__)
141     #define ENDIAN_LITTLE
142   #else
143     #define ENDIAN_BIG
144   #endif
145   #if defined(__LP64__) || defined(__x86_64__)
146     #define ENDIAN_64BITWORD
147   #else
148     #define ENDIAN_32BITWORD
149   #endif
150 #endif
151 
152 /* detect SPARC and SPARC64 */
153 #if defined(__sparc__) || defined(__sparc)
154   #define ENDIAN_BIG
155   #if defined(__arch64__) || defined(__sparcv9) || defined(__sparc_v9__)
156     #define ENDIAN_64BITWORD
157   #else
158     #define ENDIAN_32BITWORD
159   #endif
160 #endif
161 
162 /* detect IBM S390(x) */
163 #if defined(__s390x__) || defined(__s390__)
164   #define ENDIAN_BIG
165   #if defined(__s390x__)
166     #define ENDIAN_64BITWORD
167   #else
168     #define ENDIAN_32BITWORD
169   #endif
170 #endif
171 
172 /* detect PPC64 */
173 #if defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
174    #define ENDIAN_64BITWORD
175    #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
176       #define ENDIAN_BIG
177    #elif  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
178       #define ENDIAN_LITTLE
179    #endif
180    #define LTC_FAST
181 #endif
182 
183 /* endianness fallback */
184 #if !defined(ENDIAN_BIG) && !defined(ENDIAN_LITTLE)
185   #if defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN || \
186       defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
187       defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
188       defined(__BIG_ENDIAN__) || \
189       defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
190       defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \
191       defined(__m68k__)
192     #define ENDIAN_BIG
193   #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN || \
194       defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
195       defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
196       defined(__LITTLE_ENDIAN__) || \
197       defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
198       defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
199       defined(_M_ARM) || defined(_M_ARM64)
200     #define ENDIAN_LITTLE
201   #else
202     #error Cannot detect endianness
203   #endif
204 #endif
205 
206 /* ulong64: 64-bit data type */
207 #ifdef _MSC_VER
208    #define CONST64(n) n ## ui64
209    typedef unsigned __int64 ulong64;
210    typedef __int64 long64;
211 #else
212    #define CONST64(n) n ## uLL
213    typedef unsigned long long ulong64;
214    typedef long long long64;
215 #endif
216 
217 /* ulong32: "32-bit at least" data type */
218 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
219     defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
220     defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
221     defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
222     defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
223     defined(__LP64__) || defined(_LP64) || defined(__64BIT__) || defined(_M_ARM64)
224    typedef unsigned ulong32;
225    #if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
226      #define ENDIAN_64BITWORD
227    #endif
228 #else
229    typedef unsigned long ulong32;
230    #if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
231      #define ENDIAN_32BITWORD
232    #endif
233 #endif
234 
235 #if defined(ENDIAN_64BITWORD) && !defined(_MSC_VER)
236 typedef unsigned long long ltc_mp_digit;
237 #else
238 typedef unsigned long ltc_mp_digit;
239 #endif
240 
241 /* No asm is a quick way to disable anything "not portable" */
242 #ifdef LTC_NO_ASM
243    #define ENDIAN_NEUTRAL
244    #undef ENDIAN_32BITWORD
245    #undef ENDIAN_64BITWORD
246    #undef LTC_FAST
247    #define LTC_NO_BSWAP
248    #define LTC_NO_ROLC
249    #define LTC_NO_ROTATE
250 #endif
251 
252 /* No LTC_FAST if: explicitly disabled OR non-gcc/non-clang compiler OR old gcc OR using -ansi -std=c99 */
253 #if defined(LTC_NO_FAST) || (__GNUC__ < 4) || defined(__STRICT_ANSI__)
254    #undef LTC_FAST
255 #endif
256 
257 #ifdef LTC_FAST
258    #define LTC_FAST_TYPE_PTR_CAST(x) ((LTC_FAST_TYPE*)(void*)(x))
259    #ifdef ENDIAN_64BITWORD
260    typedef ulong64 __attribute__((__may_alias__)) LTC_FAST_TYPE;
261    #else
262    typedef ulong32 __attribute__((__may_alias__)) LTC_FAST_TYPE;
263    #endif
264 #endif
265 
266 #if !defined(ENDIAN_NEUTRAL) && (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
267    #error You must specify a word size as well as endianess in tomcrypt_cfg.h
268 #endif
269 
270 #if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
271    #define ENDIAN_NEUTRAL
272 #endif
273 
274 #if (defined(ENDIAN_32BITWORD) && defined(ENDIAN_64BITWORD))
275    #error Cannot be 32 and 64 bit words...
276 #endif
277 
278 /* gcc 4.3 and up has a bswap builtin; detect it by gcc version.
279  * clang also supports the bswap builtin, and although clang pretends
280  * to be gcc (macro-wise, anyway), clang pretends to be a version
281  * prior to gcc 4.3, so we can't detect bswap that way.  Instead,
282  * clang has a __has_builtin mechanism that can be used to check
283  * for builtins:
284  * http://clang.llvm.org/docs/LanguageExtensions.html#feature_check */
285 #ifndef __has_builtin
286    #define __has_builtin(x) 0
287 #endif
288 #if !defined(LTC_NO_BSWAP) && defined(__GNUC__) &&                      \
289    ((__GNUC__ * 100 + __GNUC_MINOR__ >= 403) ||                         \
290     (__has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)))
291    #define LTC_HAVE_BSWAP_BUILTIN
292 #endif
293 
294 #if !defined(LTC_NO_ROTATE) && (__has_builtin(__builtin_rotateleft32) && __has_builtin(__builtin_rotateright32))
295    #define LTC_HAVE_ROTATE_BUILTIN
296 #endif
297 
298 #if defined(__GNUC__)
299    #define LTC_ALIGN(n) __attribute__((aligned(n)))
300 #else
301    #define LTC_ALIGN(n)
302 #endif
303 
304 /* Choose Windows Vista as minimum Version if we're compiling with at least VS2019
305  * This is done in order to test the bcrypt RNG and can still be overridden by the user. */
306 #if defined(_MSC_VER) && _MSC_VER >= 1920
307 #   ifndef _WIN32_WINNT
308 #      define _WIN32_WINNT 0x0600
309 #   endif
310 #   ifndef WINVER
311 #      define WINVER 0x0600
312 #   endif
313 #endif
314 
315 #if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600 && !defined(LTC_WIN32_BCRYPT)
316 #   define LTC_WIN32_BCRYPT
317 #endif
318 
319 /* Define `LTC_NO_NULL_TERMINATION_CHECK` in the user code
320  * before including `tomcrypt.h` to disable this functionality.
321  */
322 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(LTC_NO_NULL_TERMINATION_CHECK)
323 #   define LTC_NULL_TERMINATED __attribute__((sentinel))
324 #else
325 #   define LTC_NULL_TERMINATED
326 #endif
327 
328 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405)
329 #  define LTC_DEPRECATED(s) __attribute__((deprecated("replaced by " #s)))
330 #  define PRIVATE_LTC_DEPRECATED_PRAGMA(s) _Pragma(#s)
331 #  define LTC_DEPRECATED_PRAGMA(s) PRIVATE_LTC_DEPRECATED_PRAGMA(GCC warning s)
332 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301)
333 #  define LTC_DEPRECATED(s) __attribute__((deprecated))
334 #  define LTC_DEPRECATED_PRAGMA(s)
335 #elif defined(_MSC_VER) && _MSC_VER >= 1500
336    /* supported since Visual Studio 2008 */
337 #  define LTC_DEPRECATED(s) __declspec(deprecated("replaced by " #s))
338 #  define LTC_DEPRECATED_PRAGMA(s) __pragma(message(s))
339 #else
340 #  define LTC_DEPRECATED(s)
341 #  define LTC_DEPRECATED_PRAGMA(s)
342 #endif
343 
344 #endif /* TOMCRYPT_CFG_H */
345