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 32bit */
83 #if defined(__i386__) || defined(__i386) || defined(_M_IX86)
84    #define ENDIAN_LITTLE
85    #define ENDIAN_32BITWORD
86    #define LTC_FAST
87 #endif
88 
89 /* detect amd64/x64 */
90 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
91    #define ENDIAN_LITTLE
92    #define ENDIAN_64BITWORD
93    #define LTC_FAST
94 #endif
95 
96 /* detect PPC32 */
97 #if defined(LTC_PPC32)
98    #define ENDIAN_BIG
99    #define ENDIAN_32BITWORD
100    #define LTC_FAST
101 #endif
102 
103 /* detects MIPS R5900 processors (PS2) */
104 #if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && (defined(_mips) || defined(__mips__) || defined(mips))
105    #define ENDIAN_64BITWORD
106    #if defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
107      #define ENDIAN_BIG
108    #else
109      #define ENDIAN_LITTLE
110    #endif
111 #endif
112 
113 /* detect AIX */
114 #if defined(_AIX) && defined(_BIG_ENDIAN)
115   #define ENDIAN_BIG
116   #if defined(__LP64__) || defined(_ARCH_PPC64)
117     #define ENDIAN_64BITWORD
118   #else
119     #define ENDIAN_32BITWORD
120   #endif
121 #endif
122 
123 /* detect HP-UX */
124 #if defined(__hpux) || defined(__hpux__)
125   #define ENDIAN_BIG
126   #if defined(__ia64) || defined(__ia64__) || defined(__LP64__)
127     #define ENDIAN_64BITWORD
128   #else
129     #define ENDIAN_32BITWORD
130   #endif
131 #endif
132 
133 /* detect Apple OS X */
134 #if defined(__APPLE__) && defined(__MACH__)
135   #if defined(__LITTLE_ENDIAN__) || defined(__x86_64__)
136     #define ENDIAN_LITTLE
137   #else
138     #define ENDIAN_BIG
139   #endif
140   #if defined(__LP64__) || defined(__x86_64__)
141     #define ENDIAN_64BITWORD
142   #else
143     #define ENDIAN_32BITWORD
144   #endif
145 #endif
146 
147 /* detect SPARC and SPARC64 */
148 #if defined(__sparc__) || defined(__sparc)
149   #define ENDIAN_BIG
150   #if defined(__arch64__) || defined(__sparcv9) || defined(__sparc_v9__)
151     #define ENDIAN_64BITWORD
152   #else
153     #define ENDIAN_32BITWORD
154   #endif
155 #endif
156 
157 /* detect IBM S390(x) */
158 #if defined(__s390x__) || defined(__s390__)
159   #define ENDIAN_BIG
160   #if defined(__s390x__)
161     #define ENDIAN_64BITWORD
162   #else
163     #define ENDIAN_32BITWORD
164   #endif
165 #endif
166 
167 /* detect PPC64 */
168 #if defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
169    #define ENDIAN_64BITWORD
170    #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
171       #define ENDIAN_BIG
172    #elif  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
173       #define ENDIAN_LITTLE
174    #endif
175    #define LTC_FAST
176 #endif
177 
178 /* endianness fallback */
179 #if !defined(ENDIAN_BIG) && !defined(ENDIAN_LITTLE)
180   #if defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN || \
181       defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
182       defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
183       defined(__BIG_ENDIAN__) || \
184       defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
185       defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \
186       defined(__m68k__)
187     #define ENDIAN_BIG
188   #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN || \
189       defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
190       defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
191       defined(__LITTLE_ENDIAN__) || \
192       defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
193       defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
194     #define ENDIAN_LITTLE
195   #else
196     #error Cannot detect endianness
197   #endif
198 #endif
199 
200 /* ulong64: 64-bit data type */
201 #ifdef _MSC_VER
202    #define CONST64(n) n ## ui64
203    typedef unsigned __int64 ulong64;
204    typedef __int64 long64;
205 #else
206    #define CONST64(n) n ## ULL
207    typedef unsigned long long ulong64;
208    typedef long long long64;
209 #endif
210 
211 /* ulong32: "32-bit at least" data type */
212 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
213     defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
214     defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
215     defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
216     defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
217     defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
218    typedef unsigned ulong32;
219    #if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
220      #define ENDIAN_64BITWORD
221    #endif
222 #else
223    typedef unsigned long ulong32;
224    #if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
225      #define ENDIAN_32BITWORD
226    #endif
227 #endif
228 
229 #if defined(ENDIAN_64BITWORD) && !defined(_MSC_VER)
230 typedef unsigned long long ltc_mp_digit;
231 #else
232 typedef unsigned long ltc_mp_digit;
233 #endif
234 
235 /* No asm is a quick way to disable anything "not portable" */
236 #ifdef LTC_NO_ASM
237    #define ENDIAN_NEUTRAL
238    #undef ENDIAN_32BITWORD
239    #undef ENDIAN_64BITWORD
240    #undef LTC_FAST
241    #define LTC_NO_BSWAP
242    #define LTC_NO_ROLC
243    #define LTC_NO_ROTATE
244 #endif
245 
246 /* No LTC_FAST if: explicitly disabled OR non-gcc/non-clang compiler OR old gcc OR using -ansi -std=c99 */
247 #if defined(LTC_NO_FAST) || (__GNUC__ < 4) || defined(__STRICT_ANSI__)
248    #undef LTC_FAST
249 #endif
250 
251 #ifdef LTC_FAST
252    #define LTC_FAST_TYPE_PTR_CAST(x) ((LTC_FAST_TYPE*)(void*)(x))
253    #ifdef ENDIAN_64BITWORD
254    typedef ulong64 __attribute__((__may_alias__)) LTC_FAST_TYPE;
255    #else
256    typedef ulong32 __attribute__((__may_alias__)) LTC_FAST_TYPE;
257    #endif
258 #endif
259 
260 #if !defined(ENDIAN_NEUTRAL) && (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
261    #error You must specify a word size as well as endianess in tomcrypt_cfg.h
262 #endif
263 
264 #if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
265    #define ENDIAN_NEUTRAL
266 #endif
267 
268 #if (defined(ENDIAN_32BITWORD) && defined(ENDIAN_64BITWORD))
269    #error Cannot be 32 and 64 bit words...
270 #endif
271 
272 /* gcc 4.3 and up has a bswap builtin; detect it by gcc version.
273  * clang also supports the bswap builtin, and although clang pretends
274  * to be gcc (macro-wise, anyway), clang pretends to be a version
275  * prior to gcc 4.3, so we can't detect bswap that way.  Instead,
276  * clang has a __has_builtin mechanism that can be used to check
277  * for builtins:
278  * http://clang.llvm.org/docs/LanguageExtensions.html#feature_check */
279 #ifndef __has_builtin
280    #define __has_builtin(x) 0
281 #endif
282 #if !defined(LTC_NO_BSWAP) && defined(__GNUC__) &&                      \
283    ((__GNUC__ * 100 + __GNUC_MINOR__ >= 403) ||                         \
284     (__has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)))
285    #define LTC_HAVE_BSWAP_BUILTIN
286 #endif
287 
288 #if !defined(LTC_NO_ROTATE) && (__has_builtin(__builtin_rotateleft32) && __has_builtin(__builtin_rotateright32))
289    #define LTC_HAVE_ROTATE_BUILTIN
290 #endif
291 
292 #if defined(__GNUC__)
293    #define LTC_ALIGN(n) __attribute__((aligned(n)))
294 #else
295    #define LTC_ALIGN(n)
296 #endif
297 
298 /* Define `LTC_NO_NULL_TERMINATION_CHECK` in the user code
299  * before including `tomcrypt.h` to disable this functionality.
300  */
301 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(LTC_NO_NULL_TERMINATION_CHECK)
302 #   define LTC_NULL_TERMINATED __attribute__((sentinel))
303 #else
304 #   define LTC_NULL_TERMINATED
305 #endif
306 
307 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405)
308 #  define LTC_DEPRECATED(s) __attribute__((deprecated("replaced by " #s)))
309 #  define PRIVATE_LTC_DEPRECATED_PRAGMA(s) _Pragma(#s)
310 #  define LTC_DEPRECATED_PRAGMA(s) PRIVATE_LTC_DEPRECATED_PRAGMA(GCC warning s)
311 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301)
312 #  define LTC_DEPRECATED(s) __attribute__((deprecated))
313 #  define LTC_DEPRECATED_PRAGMA(s)
314 #elif defined(_MSC_VER) && _MSC_VER >= 1500
315    /* supported since Visual Studio 2008 */
316 #  define LTC_DEPRECATED(s) __declspec(deprecated("replaced by " #s))
317 #  define LTC_DEPRECATED_PRAGMA(s) __pragma(message(s))
318 #else
319 #  define LTC_DEPRECATED(s)
320 #  define LTC_DEPRECATED_PRAGMA(s)
321 #endif
322 
323 #endif /* TOMCRYPT_CFG_H */
324