]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/cyassl/ctaocrypt/types.h
Final commit before tagging - cosmetic changes only.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / cyassl / ctaocrypt / types.h
1 /* types.h
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
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.
11  *
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.
16  *
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
20  */
21
22
23 #ifndef CTAO_CRYPT_TYPES_H
24 #define CTAO_CRYPT_TYPES_H
25
26 #include <cyassl/ctaocrypt/settings.h>
27 #include <cyassl/ctaocrypt/wc_port.h>
28
29 #ifdef __cplusplus
30     extern "C" {
31 #endif
32
33
34 #if defined(WORDS_BIGENDIAN)
35     #define BIG_ENDIAN_ORDER
36 #endif
37
38 #ifndef BIG_ENDIAN_ORDER
39     #define LITTLE_ENDIAN_ORDER
40 #endif
41
42 #ifndef CYASSL_TYPES
43     #ifndef byte
44         typedef unsigned char  byte;
45     #endif
46     typedef unsigned short word16;
47     typedef unsigned int   word32;
48 #endif
49
50
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 */
57             #define SIZEOF_LONG 8
58         #elif defined(__i386__) || defined(__CORTEX_M3__)
59             /* long long should be 64bit */
60             #define SIZEOF_LONG_LONG 8
61         #endif
62     #endif
63 #endif
64
65
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;
82 #else
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 */
85 #endif
86
87
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))
91     typedef word64 word;
92 #else
93     typedef word32 word;
94     #ifdef WORD64_AVAILABLE
95         #define CTAOCRYPT_SLOW_WORD64
96     #endif
97 #endif
98
99
100 enum {
101     CYASSL_WORD_SIZE  = sizeof(word),
102     CYASSL_BIT_SIZE   = 8,
103     CYASSL_WORD_BITS  = CYASSL_WORD_SIZE * CYASSL_BIT_SIZE
104 };
105
106 #define CYASSL_MAX_16BIT 0xffffU
107
108 /* use inlining if compiler allows */
109 #ifndef INLINE
110 #ifndef NO_INLINE
111     #ifdef _MSC_VER
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
119     #else
120         #define INLINE
121     #endif
122 #else
123     #define INLINE
124 #endif
125 #endif
126
127
128 /* set up rotate style */
129 #if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
130         #define INTEL_INTRINSICS
131         #define FAST_ROTATE
132 #elif defined(__MWERKS__) && TARGET_CPU_PPC
133         #define PPC_INTRINSICS
134         #define FAST_ROTATE
135 #elif defined(__GNUC__) && defined(__i386__)
136         /* GCC does peephole optimizations which should result in using rotate
137            instructions  */
138         #define FAST_ROTATE
139 #endif
140
141
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)
146     #else
147         #define THREAD_LS_T __thread
148     #endif
149 #else
150     #define THREAD_LS_T
151 #endif
152
153
154 /* Micrium will use Visual Studio for compilation but not the Win32 API */
155 #if defined(_WIN32) && !defined(MICRIUM) && !defined(FREERTOS) \
156         && !defined(EBSNET)
157     #define USE_WINDOWS_API
158 #endif
159
160
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 */
173     #include <stdlib.h>
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))
185 #endif
186
187 #ifndef STRING_USER
188     #include <string.h>
189     char* mystrnstr(const char* s1, const char* s2, unsigned int n);
190
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))
195
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
199        CTaoCrypt only */
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
207     #else
208         #define XSTRNCASECMP(s1,s2,n) _strnicmp((s1),(s2),(n))
209         #define XSNPRINTF _snprintf
210     #endif
211 #endif
212
213 #ifndef CTYPE_USER
214     #include <ctype.h>
215     #if defined(HAVE_ECC) || defined(HAVE_OCSP)
216         #define XTOUPPER(c)     toupper((c))
217         #define XISALPHA(c)     isalpha((c))
218     #endif
219     /* needed by CyaSSL_check_domain_name() */
220     #ifdef __CYGWIN__
221         /* Cygwin uses a macro version of tolower() by default, use the
222          * function version. */
223         #undef tolower
224     #endif
225     #define XTOLOWER(c)      tolower((c))
226 #endif
227
228
229 /* memory allocation types for user hints */
230 enum {
231     DYNAMIC_TYPE_CA           = 1,
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
276 };
277
278 /* max error buffer string size */
279 enum {
280     CYASSL_MAX_ERROR_SZ = 80
281 };
282
283 /* stack protection */
284 enum {
285     MIN_STACK_BUFFER = 8
286 };
287
288
289
290 /* settings detection for compile vs runtime math incombatibilities */
291 enum {
292 #if !defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
293     CTC_SETTINGS = 0x0
294 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
295     CTC_SETTINGS = 0x1
296 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
297     CTC_SETTINGS = 0x2
298 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
299     CTC_SETTINGS = 0x4
300 #elif defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
301     CTC_SETTINGS = 0x8
302 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
303     CTC_SETTINGS = 0x10
304 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
305     CTC_SETTINGS = 0x20
306 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
307     CTC_SETTINGS = 0x40
308 #else
309     #error "bad math long / long long settings"
310 #endif
311 };
312
313
314 CYASSL_API word32 CheckRunTimeSettings(void);
315
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())
320
321
322 #ifdef __cplusplus
323     }   /* extern "C" */
324 #endif
325
326
327 #endif /* CTAO_CRYPT_TYPES_H */
328