A simple CyaSSL-based HMAC-MD5 implementation. Licensed under GPL v2.

Dependents:   RFrec_full RFtrans_full

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers types.h Source File

types.h

00001 /* types.h
00002  *
00003  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * CyaSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00020  */
00021 
00022 
00023 #ifndef CTAO_CRYPT_TYPES_H
00024 #define CTAO_CRYPT_TYPES_H
00025 
00026 #include "settings.h"
00027 
00028 #ifdef __cplusplus
00029     extern "C" {
00030 #endif
00031 
00032 
00033 #if defined(WORDS_BIGENDIAN)
00034     #define BIG_ENDIAN_ORDER
00035 #endif
00036 
00037 #ifndef BIG_ENDIAN_ORDER
00038     #define LITTLE_ENDIAN_ORDER
00039 #endif
00040 
00041 #ifndef CYASSL_TYPES
00042     #ifndef byte
00043         typedef unsigned char  byte;
00044     #endif
00045     typedef unsigned short word16;
00046     typedef unsigned int   word32;
00047 #endif
00048 
00049 
00050 /* try to set SIZEOF_LONG or LONG_LONG if user didn't */
00051 #if !defined(_MSC_VER) && !defined(__BCPLUSPLUS__)
00052     #if !defined(SIZEOF_LONG_LONG) && !defined(SIZEOF_LONG)
00053         #if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) \
00054                 || defined(__mips64)  || defined(__x86_64__)) 
00055             /* long should be 64bit */
00056             #define SIZEOF_LONG 8
00057         #elif defined(__i386__) 
00058             /* long long should be 64bit */
00059             #define SIZEOF_LONG_LONG 8
00060         #endif
00061     #endif
00062 #endif
00063 
00064 
00065 #if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
00066     #define WORD64_AVAILABLE
00067     #define W64LIT(x) x##ui64
00068     typedef unsigned __int64 word64;
00069 #elif defined(SIZEOF_LONG) && SIZEOF_LONG == 8
00070     #define WORD64_AVAILABLE
00071     #define W64LIT(x) x##LL
00072     typedef unsigned long word64;
00073 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == 8 
00074     #define WORD64_AVAILABLE
00075     #define W64LIT(x) x##LL
00076     typedef unsigned long long word64;
00077 #else
00078     #define MP_16BIT  /* for mp_int, mp_word needs to be twice as big as
00079                          mp_digit, no 64 bit type so make mp_digit 16 bit */
00080 #endif
00081 
00082 
00083 /* These platforms have 64-bit CPU registers.  */
00084 #if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) || \
00085      defined(__mips64)  || defined(__x86_64__) || defined(_M_X64)) 
00086     typedef word64 word;
00087 #else
00088     typedef word32 word;
00089     #ifdef WORD64_AVAILABLE
00090         #define CTAOCRYPT_SLOW_WORD64
00091     #endif
00092 #endif
00093 
00094 
00095 enum {
00096     WORD_SIZE  = sizeof(word),
00097     BIT_SIZE   = 8,
00098     WORD_BITS  = WORD_SIZE * BIT_SIZE
00099 };
00100 
00101 #define CYASSL_MAX_16BIT 0xffffU
00102 
00103 /* use inlining if compiler allows */
00104 #ifndef INLINE
00105 #ifndef NO_INLINE
00106     #ifdef _MSC_VER
00107         #define INLINE __inline
00108     #elif defined(__GNUC__)
00109         #define INLINE inline
00110     #elif defined(THREADX)
00111         #define INLINE _Inline
00112     #elif defined(__IAR_SYSTEMS_ICC__)
00113         #define INLINE inline
00114     #else
00115         #define INLINE 
00116     #endif
00117 #else
00118     #define INLINE 
00119 #endif
00120 #endif
00121 
00122 
00123 /* set up rotate style */
00124 #if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
00125     #define INTEL_INTRINSICS
00126     #define FAST_ROTATE
00127 #elif defined(__MWERKS__) && TARGET_CPU_PPC
00128     #define PPC_INTRINSICS
00129     #define FAST_ROTATE
00130 #elif defined(__GNUC__) && defined(__i386__)
00131         /* GCC does peephole optimizations which should result in using rotate
00132            instructions  */
00133     #define FAST_ROTATE
00134 #endif
00135 
00136 
00137 /* Micrium will use Visual Studio for compilation but not the Win32 API */
00138 #if defined(_WIN32) && !defined(MICRIUM) && !defined(FREERTOS) \
00139         && !defined(EBSNET)
00140     #define USE_WINDOWS_API
00141 #endif
00142 
00143 
00144 /* idea to add global alloc override by Moisés Guimarães  */
00145 /* default to libc stuff */
00146 /* XREALLOC is used once in normal math lib, not in fast math lib */
00147 /* XFREE on some embeded systems doesn't like free(0) so test  */
00148 #ifdef XMALLOC_USER
00149     /* prototypes for user heap override functions */
00150     #include <stddef.h>  /* for size_t */
00151     extern void *XMALLOC(size_t n, void* heap, int type);
00152     extern void *XREALLOC(void *p, size_t n, void* heap, int type);
00153     extern void XFREE(void *p, void* heap, int type);
00154 #elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
00155         && !defined(CYASSL_SAFERTOS) && !defined(FREESCALE_MQX) \
00156         && !defined(CYASSL_LEANPSK)
00157     /* default C runtime, can install different routines at runtime */
00158     #include "memory.h"
00159     #define XMALLOC(s, h, t)     ((void)h, (void)t, CyaSSL_Malloc((s)))
00160     #define XFREE(p, h, t)       {void* xp = (p); if((xp)) CyaSSL_Free((xp));}
00161     #define XREALLOC(p, n, h, t) CyaSSL_Realloc((p), (n))
00162 #endif
00163 
00164 #ifndef STRING_USER
00165     #include <string.h>
00166     char* mystrnstr(const char* s1, const char* s2, unsigned int n);
00167 
00168     #define XMEMCPY(d,s,l)    memcpy((d),(s),(l))
00169     #define XMEMSET(b,c,l)    memset((b),(c),(l))
00170     #define XMEMCMP(s1,s2,n)  memcmp((s1),(s2),(n))
00171     #define XMEMMOVE(d,s,l)   memmove((d),(s),(l))
00172 
00173     #define XSTRLEN(s1)       strlen((s1))
00174     #define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n))
00175     /* strstr, strncmp, and strncat only used by CyaSSL proper, not required for
00176        CTaoCrypt only */
00177     #define XSTRSTR(s1,s2)    strstr((s1),(s2))
00178     #define XSTRNSTR(s1,s2,n) mystrnstr((s1),(s2),(n))
00179     #define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n))
00180     #define XSTRNCAT(s1,s2,n) strncat((s1),(s2),(n))
00181     #define XSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n))
00182 #endif
00183 
00184 #if defined(HAVE_ECC) || defined(HAVE_OCSP)
00185     #ifndef CTYPE_USER
00186         #include <ctype.h>
00187         #define XTOUPPER(c)     toupper((c))
00188         #define XISALPHA(c)     isalpha((c))
00189     #endif
00190 #endif
00191 
00192 
00193 /* memory allocation types for user hints */
00194 enum {
00195     DYNAMIC_TYPE_CA         = 1,
00196     DYNAMIC_TYPE_CERT       = 2,
00197     DYNAMIC_TYPE_KEY        = 3,
00198     DYNAMIC_TYPE_FILE       = 4,
00199     DYNAMIC_TYPE_SUBJECT_CN = 5,
00200     DYNAMIC_TYPE_PUBLIC_KEY = 6,
00201     DYNAMIC_TYPE_SIGNER     = 7,
00202     DYNAMIC_TYPE_NONE       = 8,
00203     DYNAMIC_TYPE_BIGINT     = 9,
00204     DYNAMIC_TYPE_RSA        = 10,
00205     DYNAMIC_TYPE_METHOD     = 11,
00206     DYNAMIC_TYPE_OUT_BUFFER = 12,
00207     DYNAMIC_TYPE_IN_BUFFER  = 13,
00208     DYNAMIC_TYPE_INFO       = 14,
00209     DYNAMIC_TYPE_DH         = 15,
00210     DYNAMIC_TYPE_DOMAIN     = 16,
00211     DYNAMIC_TYPE_SSL        = 17,
00212     DYNAMIC_TYPE_CTX        = 18,
00213     DYNAMIC_TYPE_WRITEV     = 19,
00214     DYNAMIC_TYPE_OPENSSL    = 20,
00215     DYNAMIC_TYPE_DSA        = 21,
00216     DYNAMIC_TYPE_CRL        = 22,
00217     DYNAMIC_TYPE_REVOKED    = 23,
00218     DYNAMIC_TYPE_CRL_ENTRY  = 24,
00219     DYNAMIC_TYPE_CERT_MANAGER = 25,
00220     DYNAMIC_TYPE_CRL_MONITOR  = 26,
00221     DYNAMIC_TYPE_OCSP_STATUS  = 27,
00222     DYNAMIC_TYPE_OCSP_ENTRY   = 28,
00223     DYNAMIC_TYPE_ALTNAME      = 29,
00224     DYNAMIC_TYPE_SUITES       = 30,
00225     DYNAMIC_TYPE_CIPHER       = 31,
00226     DYNAMIC_TYPE_RNG          = 32,
00227     DYNAMIC_TYPE_ARRAYS       = 33,
00228     DYNAMIC_TYPE_DTLS_POOL    = 34,
00229     DYNAMIC_TYPE_SOCKADDR     = 35,
00230     DYNAMIC_TYPE_LIBZ         = 36,
00231     DYNAMIC_TYPE_ECC          = 37,
00232     DYNAMIC_TYPE_TMP_BUFFER   = 38,
00233     DYNAMIC_TYPE_CAVIUM_TMP   = 40,
00234     DYNAMIC_TYPE_CAVIUM_RSA   = 41 
00235 };
00236 
00237 /* stack protection */
00238 enum {
00239     MIN_STACK_BUFFER = 8
00240 };
00241 
00242 
00243 
00244 /* settings detection for compile vs runtime math incombatibilities */
00245 enum {
00246 #if !defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
00247     CTC_SETTINGS = 0x0
00248 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
00249     CTC_SETTINGS = 0x1
00250 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
00251     CTC_SETTINGS = 0x2
00252 #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
00253     CTC_SETTINGS = 0x4
00254 #elif defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
00255     CTC_SETTINGS = 0x8
00256 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
00257     CTC_SETTINGS = 0x10
00258 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
00259     CTC_SETTINGS = 0x20
00260 #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
00261     CTC_SETTINGS = 0x40
00262 #else
00263     #error "bad math long / long long settings"
00264 #endif
00265 };
00266 
00267 
00268 CYASSL_API word32 CheckRunTimeSettings(void);
00269 
00270 /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math and long
00271    types need to match at compile time and run time, CheckCtcSettings will
00272    return 1 if a match otherwise 0 */
00273 #define CheckCtcSettings() (CTC_SETTINGS == CheckRunTimeSettings())
00274 
00275 
00276 #ifdef __cplusplus
00277     }   /* extern "C" */
00278 #endif
00279 
00280 
00281 #endif /* CTAO_CRYPT_TYPES_H */
00282