2011-08-25 14:41:19 -05:00
|
|
|
/* types.h
|
2011-02-05 13:14:47 -06:00
|
|
|
*
|
2013-02-05 14:44:17 -06:00
|
|
|
* Copyright (C) 2006-2013 wolfSSL Inc.
|
2011-02-05 13:14:47 -06:00
|
|
|
*
|
|
|
|
* This file is part of CyaSSL.
|
|
|
|
*
|
|
|
|
* CyaSSL is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* CyaSSL is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CTAO_CRYPT_TYPES_H
|
|
|
|
#define CTAO_CRYPT_TYPES_H
|
|
|
|
|
2011-08-25 14:41:19 -05:00
|
|
|
#include <cyassl/ctaocrypt/settings.h>
|
2011-02-05 13:14:47 -06:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-08-23 19:05:58 -05:00
|
|
|
#if defined(WORDS_BIGENDIAN)
|
2011-02-05 13:14:47 -06:00
|
|
|
#define BIG_ENDIAN_ORDER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BIG_ENDIAN_ORDER
|
|
|
|
#define LITTLE_ENDIAN_ORDER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CYASSL_TYPES
|
2011-09-07 18:03:17 -05:00
|
|
|
#ifndef byte
|
|
|
|
typedef unsigned char byte;
|
|
|
|
#endif
|
2011-02-05 13:14:47 -06:00
|
|
|
typedef unsigned short word16;
|
|
|
|
typedef unsigned int word32;
|
|
|
|
#endif
|
|
|
|
|
2011-08-29 17:37:12 -05:00
|
|
|
|
|
|
|
/* try to set SIZEOF_LONG or LONG_LONG if user didn't */
|
|
|
|
#if !defined(_MSC_VER) && !defined(__BCPLUSPLUS__)
|
|
|
|
#if !defined(SIZEOF_LONG_LONG) && !defined(SIZEOF_LONG)
|
|
|
|
#if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) \
|
|
|
|
|| defined(__mips64) || defined(__x86_64__))
|
|
|
|
/* long should be 64bit */
|
|
|
|
#define SIZEOF_LONG 8
|
2012-09-21 15:29:04 -05:00
|
|
|
#elif defined(__i386__)
|
2011-08-29 17:37:12 -05:00
|
|
|
/* long long should be 64bit */
|
|
|
|
#define SIZEOF_LONG_LONG 8
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-02-05 13:14:47 -06:00
|
|
|
#if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
|
|
|
|
#define WORD64_AVAILABLE
|
|
|
|
#define W64LIT(x) x##ui64
|
|
|
|
typedef unsigned __int64 word64;
|
2012-09-21 15:29:04 -05:00
|
|
|
#elif defined(SIZEOF_LONG) && SIZEOF_LONG == 8
|
2011-02-05 13:14:47 -06:00
|
|
|
#define WORD64_AVAILABLE
|
|
|
|
#define W64LIT(x) x##LL
|
|
|
|
typedef unsigned long word64;
|
2012-09-21 15:29:04 -05:00
|
|
|
#elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == 8
|
2011-02-05 13:14:47 -06:00
|
|
|
#define WORD64_AVAILABLE
|
|
|
|
#define W64LIT(x) x##LL
|
|
|
|
typedef unsigned long long word64;
|
2013-03-07 19:44:40 -06:00
|
|
|
#elif defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ == 8
|
|
|
|
#define WORD64_AVAILABLE
|
|
|
|
#define W64LIT(x) x##LL
|
|
|
|
typedef unsigned long long word64;
|
2011-02-05 13:14:47 -06:00
|
|
|
#else
|
|
|
|
#define MP_16BIT /* for mp_int, mp_word needs to be twice as big as
|
|
|
|
mp_digit, no 64 bit type so make mp_digit 16 bit */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* These platforms have 64-bit CPU registers. */
|
|
|
|
#if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) || \
|
2012-11-01 14:21:47 -05:00
|
|
|
defined(__mips64) || defined(__x86_64__) || defined(_M_X64))
|
2011-02-05 13:14:47 -06:00
|
|
|
typedef word64 word;
|
|
|
|
#else
|
|
|
|
typedef word32 word;
|
|
|
|
#ifdef WORD64_AVAILABLE
|
|
|
|
#define CTAOCRYPT_SLOW_WORD64
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
WORD_SIZE = sizeof(word),
|
|
|
|
BIT_SIZE = 8,
|
|
|
|
WORD_BITS = WORD_SIZE * BIT_SIZE
|
|
|
|
};
|
|
|
|
|
2013-01-29 18:22:49 -06:00
|
|
|
#define CYASSL_MAX_16BIT 0xffffU
|
2011-02-05 13:14:47 -06:00
|
|
|
|
|
|
|
/* use inlining if compiler allows */
|
|
|
|
#ifndef INLINE
|
|
|
|
#ifndef NO_INLINE
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define INLINE __inline
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#define INLINE inline
|
|
|
|
#elif defined(THREADX)
|
|
|
|
#define INLINE _Inline
|
2012-08-20 18:58:11 -05:00
|
|
|
#elif defined(__IAR_SYSTEMS_ICC__)
|
|
|
|
#define INLINE inline
|
2011-02-05 13:14:47 -06:00
|
|
|
#else
|
|
|
|
#define INLINE
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define INLINE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* set up rotate style */
|
|
|
|
#if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
|
|
|
|
#define INTEL_INTRINSICS
|
|
|
|
#define FAST_ROTATE
|
|
|
|
#elif defined(__MWERKS__) && TARGET_CPU_PPC
|
|
|
|
#define PPC_INTRINSICS
|
|
|
|
#define FAST_ROTATE
|
|
|
|
#elif defined(__GNUC__) && defined(__i386__)
|
|
|
|
/* GCC does peephole optimizations which should result in using rotate
|
|
|
|
instructions */
|
|
|
|
#define FAST_ROTATE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Micrium will use Visual Studio for compilation but not the Win32 API */
|
2012-08-13 18:10:05 -05:00
|
|
|
#if defined(_WIN32) && !defined(MICRIUM) && !defined(FREERTOS) \
|
|
|
|
&& !defined(EBSNET)
|
2011-02-05 13:14:47 -06:00
|
|
|
#define USE_WINDOWS_API
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* idea to add global alloc override by Moisés Guimarães */
|
|
|
|
/* default to libc stuff */
|
2011-08-04 17:42:55 -05:00
|
|
|
/* XREALLOC is used once in normal math lib, not in fast math lib */
|
2011-02-05 13:14:47 -06:00
|
|
|
/* XFREE on some embeded systems doesn't like free(0) so test */
|
|
|
|
#ifdef XMALLOC_USER
|
|
|
|
/* prototypes for user heap override functions */
|
|
|
|
#include <stddef.h> /* for size_t */
|
|
|
|
extern void *XMALLOC(size_t n, void* heap, int type);
|
|
|
|
extern void *XREALLOC(void *p, size_t n, void* heap, int type);
|
|
|
|
extern void XFREE(void *p, void* heap, int type);
|
2012-11-01 12:23:42 -05:00
|
|
|
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
|
2012-11-30 17:41:01 -06:00
|
|
|
&& !defined(CYASSL_SAFERTOS) && !defined(FREESCALE_MQX) \
|
|
|
|
&& !defined(CYASSL_LEANPSK)
|
2011-04-15 18:43:00 -05:00
|
|
|
/* default C runtime, can install different routines at runtime */
|
2011-08-25 14:41:19 -05:00
|
|
|
#include <cyassl/ctaocrypt/memory.h>
|
2012-09-04 12:48:26 -05:00
|
|
|
#define XMALLOC(s, h, t) ((void)h, (void)t, CyaSSL_Malloc((s)))
|
2011-04-15 18:43:00 -05:00
|
|
|
#define XFREE(p, h, t) {void* xp = (p); if((xp)) CyaSSL_Free((xp));}
|
|
|
|
#define XREALLOC(p, n, h, t) CyaSSL_Realloc((p), (n))
|
2011-02-05 13:14:47 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef STRING_USER
|
|
|
|
#include <string.h>
|
2012-07-17 14:01:37 -05:00
|
|
|
char* mystrnstr(const char* s1, const char* s2, unsigned int n);
|
|
|
|
|
2011-02-05 13:14:47 -06:00
|
|
|
#define XMEMCPY(d,s,l) memcpy((d),(s),(l))
|
|
|
|
#define XMEMSET(b,c,l) memset((b),(c),(l))
|
|
|
|
#define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n))
|
|
|
|
#define XMEMMOVE(d,s,l) memmove((d),(s),(l))
|
|
|
|
|
|
|
|
#define XSTRLEN(s1) strlen((s1))
|
|
|
|
#define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n))
|
2012-02-08 20:07:20 -06:00
|
|
|
/* strstr, strncmp, and strncat only used by CyaSSL proper, not required for
|
2011-02-05 13:14:47 -06:00
|
|
|
CTaoCrypt only */
|
|
|
|
#define XSTRSTR(s1,s2) strstr((s1),(s2))
|
2012-07-17 14:01:37 -05:00
|
|
|
#define XSTRNSTR(s1,s2,n) mystrnstr((s1),(s2),(n))
|
2011-02-05 13:14:47 -06:00
|
|
|
#define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n))
|
2012-02-08 20:07:20 -06:00
|
|
|
#define XSTRNCAT(s1,s2,n) strncat((s1),(s2),(n))
|
2013-01-03 19:19:56 -06:00
|
|
|
#define XSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n))
|
2011-02-05 13:14:47 -06:00
|
|
|
#endif
|
|
|
|
|
2013-01-03 19:19:56 -06:00
|
|
|
#if defined(HAVE_ECC) || defined(HAVE_OCSP)
|
2011-02-05 13:14:47 -06:00
|
|
|
#ifndef CTYPE_USER
|
|
|
|
#include <ctype.h>
|
|
|
|
#define XTOUPPER(c) toupper((c))
|
2013-01-03 19:19:56 -06:00
|
|
|
#define XISALPHA(c) isalpha((c))
|
2011-02-05 13:14:47 -06:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* memory allocation types for user hints */
|
|
|
|
enum {
|
|
|
|
DYNAMIC_TYPE_CA = 1,
|
|
|
|
DYNAMIC_TYPE_CERT = 2,
|
|
|
|
DYNAMIC_TYPE_KEY = 3,
|
|
|
|
DYNAMIC_TYPE_FILE = 4,
|
2011-04-14 15:26:09 -05:00
|
|
|
DYNAMIC_TYPE_SUBJECT_CN = 5,
|
2011-02-05 13:14:47 -06:00
|
|
|
DYNAMIC_TYPE_PUBLIC_KEY = 6,
|
|
|
|
DYNAMIC_TYPE_SIGNER = 7,
|
|
|
|
DYNAMIC_TYPE_NONE = 8,
|
|
|
|
DYNAMIC_TYPE_BIGINT = 9,
|
|
|
|
DYNAMIC_TYPE_RSA = 10,
|
|
|
|
DYNAMIC_TYPE_METHOD = 11,
|
|
|
|
DYNAMIC_TYPE_OUT_BUFFER = 12,
|
|
|
|
DYNAMIC_TYPE_IN_BUFFER = 13,
|
|
|
|
DYNAMIC_TYPE_INFO = 14,
|
|
|
|
DYNAMIC_TYPE_DH = 15,
|
|
|
|
DYNAMIC_TYPE_DOMAIN = 16,
|
|
|
|
DYNAMIC_TYPE_SSL = 17,
|
|
|
|
DYNAMIC_TYPE_CTX = 18,
|
|
|
|
DYNAMIC_TYPE_WRITEV = 19,
|
2012-05-03 20:07:31 -05:00
|
|
|
DYNAMIC_TYPE_OPENSSL = 20,
|
2012-05-07 18:35:23 -05:00
|
|
|
DYNAMIC_TYPE_DSA = 21,
|
2012-05-16 19:04:56 -05:00
|
|
|
DYNAMIC_TYPE_CRL = 22,
|
|
|
|
DYNAMIC_TYPE_REVOKED = 23,
|
|
|
|
DYNAMIC_TYPE_CRL_ENTRY = 24,
|
2012-05-24 16:07:11 -05:00
|
|
|
DYNAMIC_TYPE_CERT_MANAGER = 25,
|
2012-07-31 19:45:48 -05:00
|
|
|
DYNAMIC_TYPE_CRL_MONITOR = 26,
|
|
|
|
DYNAMIC_TYPE_OCSP_STATUS = 27,
|
|
|
|
DYNAMIC_TYPE_OCSP_ENTRY = 28,
|
2012-09-05 12:17:48 -05:00
|
|
|
DYNAMIC_TYPE_ALTNAME = 29,
|
2012-09-05 14:30:51 -05:00
|
|
|
DYNAMIC_TYPE_SUITES = 30,
|
2012-09-05 18:18:29 -05:00
|
|
|
DYNAMIC_TYPE_CIPHER = 31,
|
2012-09-14 11:35:34 -05:00
|
|
|
DYNAMIC_TYPE_RNG = 32,
|
2012-09-14 23:19:06 -05:00
|
|
|
DYNAMIC_TYPE_ARRAYS = 33,
|
2012-10-02 11:15:50 -05:00
|
|
|
DYNAMIC_TYPE_DTLS_POOL = 34,
|
2012-10-17 12:09:28 -05:00
|
|
|
DYNAMIC_TYPE_SOCKADDR = 35,
|
2012-12-26 18:39:19 -06:00
|
|
|
DYNAMIC_TYPE_LIBZ = 36,
|
2013-01-07 16:06:58 -06:00
|
|
|
DYNAMIC_TYPE_ECC = 37,
|
2013-01-29 18:22:49 -06:00
|
|
|
DYNAMIC_TYPE_TMP_BUFFER = 38,
|
2013-02-20 19:08:22 -06:00
|
|
|
DYNAMIC_TYPE_DTLS_MSG = 39,
|
2013-01-31 17:55:29 -06:00
|
|
|
DYNAMIC_TYPE_CAVIUM_TMP = 40,
|
|
|
|
DYNAMIC_TYPE_CAVIUM_RSA = 41
|
2011-02-05 13:14:47 -06:00
|
|
|
};
|
|
|
|
|
2011-04-27 19:31:08 -05:00
|
|
|
/* stack protection */
|
|
|
|
enum {
|
|
|
|
MIN_STACK_BUFFER = 8
|
|
|
|
};
|
|
|
|
|
2011-02-05 13:14:47 -06:00
|
|
|
|
2011-08-29 12:39:40 -05:00
|
|
|
|
|
|
|
/* settings detection for compile vs runtime math incombatibilities */
|
|
|
|
enum {
|
|
|
|
#if !defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
|
|
|
|
CTC_SETTINGS = 0x0
|
2011-08-29 13:11:55 -05:00
|
|
|
#elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
|
2011-08-29 12:39:40 -05:00
|
|
|
CTC_SETTINGS = 0x1
|
2011-08-29 13:11:55 -05:00
|
|
|
#elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
|
2011-08-29 12:39:40 -05:00
|
|
|
CTC_SETTINGS = 0x2
|
2011-08-29 17:37:12 -05:00
|
|
|
#elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
|
2011-08-29 12:39:40 -05:00
|
|
|
CTC_SETTINGS = 0x4
|
2011-08-29 17:37:12 -05:00
|
|
|
#elif defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
|
2011-08-29 12:39:40 -05:00
|
|
|
CTC_SETTINGS = 0x8
|
2011-08-29 17:37:12 -05:00
|
|
|
#elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
|
2011-08-29 12:39:40 -05:00
|
|
|
CTC_SETTINGS = 0x10
|
2011-08-29 17:37:12 -05:00
|
|
|
#elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
|
2011-08-29 13:11:55 -05:00
|
|
|
CTC_SETTINGS = 0x20
|
|
|
|
#elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
|
|
|
|
CTC_SETTINGS = 0x40
|
|
|
|
#else
|
|
|
|
#error "bad math long / long long settings"
|
2011-08-29 12:39:40 -05:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CYASSL_API word32 CheckRunTimeSettings(void);
|
|
|
|
|
2011-09-23 12:37:26 -05:00
|
|
|
/* If user uses RSA, DH, DSA, or ECC math lib directly then fast math and long
|
|
|
|
types need to match at compile time and run time, CheckCtcSettings will
|
|
|
|
return 1 if a match otherwise 0 */
|
2011-08-29 12:39:40 -05:00
|
|
|
#define CheckCtcSettings() (CTC_SETTINGS == CheckRunTimeSettings())
|
|
|
|
|
|
|
|
|
2011-02-05 13:14:47 -06:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* CTAO_CRYPT_TYPES_H */
|
|
|
|
|