3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 #ifndef CTAO_CRYPT_TYPES_H
24 #define CTAO_CRYPT_TYPES_H
26 #include <cyassl/ctaocrypt/settings.h>
27 #include <cyassl/ctaocrypt/wc_port.h>
34 #if defined(WORDS_BIGENDIAN)
35 #define BIG_ENDIAN_ORDER
38 #ifndef BIG_ENDIAN_ORDER
39 #define LITTLE_ENDIAN_ORDER
44 typedef unsigned char byte;
46 typedef unsigned short word16;
47 typedef unsigned int word32;
51 /* try to set SIZEOF_LONG or LONG_LONG if user didn't */
52 #if !defined(_MSC_VER) && !defined(__BCPLUSPLUS__)
53 #if !defined(SIZEOF_LONG_LONG) && !defined(SIZEOF_LONG)
54 #if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) \
55 || defined(__mips64) || defined(__x86_64__))
56 /* long should be 64bit */
58 #elif defined(__i386__) || defined(__CORTEX_M3__)
59 /* long long should be 64bit */
60 #define SIZEOF_LONG_LONG 8
66 #if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
67 #define WORD64_AVAILABLE
68 #define W64LIT(x) x##ui64
69 typedef unsigned __int64 word64;
70 #elif defined(SIZEOF_LONG) && SIZEOF_LONG == 8
71 #define WORD64_AVAILABLE
72 #define W64LIT(x) x##LL
73 typedef unsigned long word64;
74 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == 8
75 #define WORD64_AVAILABLE
76 #define W64LIT(x) x##LL
77 typedef unsigned long long word64;
78 #elif defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ == 8
79 #define WORD64_AVAILABLE
80 #define W64LIT(x) x##LL
81 typedef unsigned long long word64;
83 #define MP_16BIT /* for mp_int, mp_word needs to be twice as big as
84 mp_digit, no 64 bit type so make mp_digit 16 bit */
88 /* These platforms have 64-bit CPU registers. */
89 #if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) || \
90 defined(__mips64) || defined(__x86_64__) || defined(_M_X64))
94 #ifdef WORD64_AVAILABLE
95 #define CTAOCRYPT_SLOW_WORD64
101 CYASSL_WORD_SIZE = sizeof(word),
103 CYASSL_WORD_BITS = CYASSL_WORD_SIZE * CYASSL_BIT_SIZE
106 #define CYASSL_MAX_16BIT 0xffffU
108 /* use inlining if compiler allows */
112 #define INLINE __inline
113 #elif defined(__GNUC__)
114 #define INLINE inline
115 #elif defined(__IAR_SYSTEMS_ICC__)
116 #define INLINE inline
117 #elif defined(THREADX)
118 #define INLINE _Inline
128 /* set up rotate style */
129 #if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
130 #define INTEL_INTRINSICS
132 #elif defined(__MWERKS__) && TARGET_CPU_PPC
133 #define PPC_INTRINSICS
135 #elif defined(__GNUC__) && defined(__i386__)
136 /* GCC does peephole optimizations which should result in using rotate
142 /* set up thread local storage if available */
143 #ifdef HAVE_THREAD_LS
144 #if defined(_MSC_VER)
145 #define THREAD_LS_T __declspec(thread)
147 #define THREAD_LS_T __thread
154 /* Micrium will use Visual Studio for compilation but not the Win32 API */
155 #if defined(_WIN32) && !defined(MICRIUM) && !defined(FREERTOS) \
157 #define USE_WINDOWS_API
161 /* idea to add global alloc override by Moisés Guimarães */
162 /* default to libc stuff */
163 /* XREALLOC is used once in normal math lib, not in fast math lib */
164 /* XFREE on some embeded systems doesn't like free(0) so test */
165 #if defined(XMALLOC_USER)
166 /* prototypes for user heap override functions */
167 #include <stddef.h> /* for size_t */
168 extern void *XMALLOC(size_t n, void* heap, int type);
169 extern void *XREALLOC(void *p, size_t n, void* heap, int type);
170 extern void XFREE(void *p, void* heap, int type);
171 #elif defined(NO_CYASSL_MEMORY)
172 /* just use plain C stdlib stuff if desired */
174 #define XMALLOC(s, h, t) ((void)h, (void)t, malloc((s)))
175 #define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
176 #define XREALLOC(p, n, h, t) realloc((p), (n))
177 #elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
178 && !defined(CYASSL_SAFERTOS) && !defined(FREESCALE_MQX) \
179 && !defined(CYASSL_LEANPSK)
180 /* default C runtime, can install different routines at runtime via cbs */
181 #include <cyassl/ctaocrypt/memory.h>
182 #define XMALLOC(s, h, t) ((void)h, (void)t, CyaSSL_Malloc((s)))
183 #define XFREE(p, h, t) {void* xp = (p); if((xp)) CyaSSL_Free((xp));}
184 #define XREALLOC(p, n, h, t) CyaSSL_Realloc((p), (n))
189 char* mystrnstr(const char* s1, const char* s2, unsigned int n);
191 #define XMEMCPY(d,s,l) memcpy((d),(s),(l))
192 #define XMEMSET(b,c,l) memset((b),(c),(l))
193 #define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n))
194 #define XMEMMOVE(d,s,l) memmove((d),(s),(l))
196 #define XSTRLEN(s1) strlen((s1))
197 #define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n))
198 /* strstr, strncmp, and strncat only used by CyaSSL proper, not required for
200 #define XSTRSTR(s1,s2) strstr((s1),(s2))
201 #define XSTRNSTR(s1,s2,n) mystrnstr((s1),(s2),(n))
202 #define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n))
203 #define XSTRNCAT(s1,s2,n) strncat((s1),(s2),(n))
204 #ifndef USE_WINDOWS_API
205 #define XSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n))
206 #define XSNPRINTF snprintf
208 #define XSTRNCASECMP(s1,s2,n) _strnicmp((s1),(s2),(n))
209 #define XSNPRINTF _snprintf
215 #if defined(HAVE_ECC) || defined(HAVE_OCSP)
216 #define XTOUPPER(c) toupper((c))
217 #define XISALPHA(c) isalpha((c))
219 /* needed by CyaSSL_check_domain_name() */
221 /* Cygwin uses a macro version of tolower() by default, use the
222 * function version. */
225 #define XTOLOWER(c) tolower((c))
229 /* memory allocation types for user hints */
232 DYNAMIC_TYPE_CERT = 2,
233 DYNAMIC_TYPE_KEY = 3,
234 DYNAMIC_TYPE_FILE = 4,
235 DYNAMIC_TYPE_SUBJECT_CN = 5,
236 DYNAMIC_TYPE_PUBLIC_KEY = 6,
237 DYNAMIC_TYPE_SIGNER = 7,
238 DYNAMIC_TYPE_NONE = 8,
239 DYNAMIC_TYPE_BIGINT = 9,
240 DYNAMIC_TYPE_RSA = 10,
241 DYNAMIC_TYPE_METHOD = 11,
242 DYNAMIC_TYPE_OUT_BUFFER = 12,
243 DYNAMIC_TYPE_IN_BUFFER = 13,
244 DYNAMIC_TYPE_INFO = 14,
245 DYNAMIC_TYPE_DH = 15,
246 DYNAMIC_TYPE_DOMAIN = 16,
247 DYNAMIC_TYPE_SSL = 17,
248 DYNAMIC_TYPE_CTX = 18,
249 DYNAMIC_TYPE_WRITEV = 19,
250 DYNAMIC_TYPE_OPENSSL = 20,
251 DYNAMIC_TYPE_DSA = 21,
252 DYNAMIC_TYPE_CRL = 22,
253 DYNAMIC_TYPE_REVOKED = 23,
254 DYNAMIC_TYPE_CRL_ENTRY = 24,
255 DYNAMIC_TYPE_CERT_MANAGER = 25,
256 DYNAMIC_TYPE_CRL_MONITOR = 26,
257 DYNAMIC_TYPE_OCSP_STATUS = 27,
258 DYNAMIC_TYPE_OCSP_ENTRY = 28,
259 DYNAMIC_TYPE_ALTNAME = 29,
260 DYNAMIC_TYPE_SUITES = 30,
261 DYNAMIC_TYPE_CIPHER = 31,
262 DYNAMIC_TYPE_RNG = 32,
263 DYNAMIC_TYPE_ARRAYS = 33,
264 DYNAMIC_TYPE_DTLS_POOL = 34,
265 DYNAMIC_TYPE_SOCKADDR = 35,
266 DYNAMIC_TYPE_LIBZ = 36,
267 DYNAMIC_TYPE_ECC = 37,
268 DYNAMIC_TYPE_TMP_BUFFER = 38,
269 DYNAMIC_TYPE_DTLS_MSG = 39,
270 DYNAMIC_TYPE_CAVIUM_TMP = 40,
271 DYNAMIC_TYPE_CAVIUM_RSA = 41,
272 DYNAMIC_TYPE_X509 = 42,
273 DYNAMIC_TYPE_TLSX = 43,
274 DYNAMIC_TYPE_OCSP = 44,
275 DYNAMIC_TYPE_SIGNATURE = 45
278 /* max error buffer string size */
280 CYASSL_MAX_ERROR_SZ = 80
283 /* stack protection */
290 /* settings detection for compile vs runtime math incombatibilities */
292 #if !defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
294 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
296 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
298 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
300 #elif defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
302 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
304 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
306 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
309 #error "bad math long / long long settings"
314 CYASSL_API word32 CheckRunTimeSettings(void);
316 /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math and long
317 types need to match at compile time and run time, CheckCtcSettings will
318 return 1 if a match otherwise 0 */
319 #define CheckCtcSettings() (CTC_SETTINGS == CheckRunTimeSettings())
327 #endif /* CTAO_CRYPT_TYPES_H */