8f947e7159e75b43da441e085ecf5cf1c236999a
[platform/upstream/iotivity.git] / extlibs / tinydtls / global.h
1 /* dtls -- a very basic DTLS implementation
2  *
3  * Copyright (C) 2011--2014 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #ifndef _DTLS_GLOBAL_H_
27 #define _DTLS_GLOBAL_H_
28
29 #include <stdlib.h>
30 #include <sys/types.h>
31
32 #include "tinydtls.h"
33
34 #ifndef DTLSv12
35 /* The current version of tinyDTLS supports DTLSv1.2 only. */
36 #define DTLSv12 1
37 #endif
38
39 #ifndef WITH_SHA256
40 /* The current version of tinyDTLS supports DTLSv1.2 with SHA256 PRF
41    only. */
42 #define WITH_SHA256 1
43 #endif
44
45 /* Define our own types as at least uint32_t does not work on my amd64. */
46
47 typedef unsigned char uint8;
48 typedef unsigned char uint16[2];
49 typedef unsigned char uint24[3];
50 typedef unsigned char uint32[4];
51 typedef unsigned char uint48[6];
52
53 #ifndef DTLS_MAX_BUF
54 /** Maximum size of DTLS message.
55     When Peers are sending bigger messages this causes problems. Californium
56     with ECDSA needs at least 220 */
57 #ifdef WITH_CONTIKI
58 #if defined(DTLS_ECC) || defined(DTLS_X509)
59 #define DTLS_MAX_BUF 200
60 #else /* DTLS_ECC */
61 #define DTLS_MAX_BUF 100
62 #endif /* DTLS_ECC */
63 #else /* WITH_CONTIKI */
64 #define DTLS_MAX_BUF 1400
65 #endif /* WITH_CONTIKI */
66 #endif
67
68 #ifndef DTLS_DEFAULT_MAX_RETRANSMIT
69 /** Number of message retransmissions. */
70 #define DTLS_DEFAULT_MAX_RETRANSMIT 7
71 #endif
72
73 /** Known cipher suites.*/
74 typedef enum {
75   TLS_NULL_WITH_NULL_NULL = 0x0000,   /**< NULL cipher  */
76   TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256 = 0xFF00, /**< OCF Vendor Specific Ciphersuite */
77   TLS_PSK_WITH_AES_128_CCM_8 = 0xC0A8, /**< see RFC 6655 */
78   TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
79   TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 = 0xC0AE /**< see RFC 7251 */
80 } dtls_cipher_t;
81
82 typedef enum {
83     DTLS_CIPHER_DISABLE = 0,
84     DTLS_CIPHER_ENABLE = 1
85 } dtls_cipher_enable_t;
86
87 /** Known compression suites.*/
88 typedef enum {
89   TLS_COMPRESSION_NULL = 0x0000         /* NULL compression */
90 } dtls_compression_t;
91
92 #define TLS_EXT_ELLIPTIC_CURVES         10 /* see RFC 4492 */
93 #define TLS_EXT_EC_POINT_FORMATS        11 /* see RFC 4492 */
94 #define TLS_EXT_SIG_HASH_ALGO           13 /* see RFC 5246 */
95 #define TLS_EXT_CLIENT_CERTIFICATE_TYPE 19 /* see RFC 7250 */
96 #define TLS_EXT_SERVER_CERTIFICATE_TYPE 20 /* see RFC 7250 */
97 #define TLS_EXT_ENCRYPT_THEN_MAC        22 /* see RFC 7366 */
98
99 /* see http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#tls-extensiontype-values-3 */
100 #define TLS_CERT_TYPE_X509              0 /* see RFC 6091 */
101 #define TLS_CERT_TYPE_RAW_PUBLIC_KEY    2 /* see RFC 7250 */
102
103
104 #define TLS_EXT_ELLIPTIC_CURVES_SECP256R1       23 /* see RFC 4492 */
105
106 #define TLS_EXT_EC_POINT_FORMATS_UNCOMPRESSED   0 /* see RFC 4492 */
107
108 #define TLS_EC_CURVE_TYPE_NAMED_CURVE           3 /* see RFC 4492 */
109
110 #define TLS_CLIENT_CERTIFICATE_TYPE_ECDSA_SIGN  64 /* see RFC 4492 */
111
112 #define TLS_EXT_SIG_HASH_ALGO_SHA256            4 /* see RFC 5246 */
113 #define TLS_EXT_SIG_HASH_ALGO_ECDSA             3 /* see RFC 5246 */
114
115 /**
116  * XORs \p n bytes byte-by-byte starting at \p y to the memory area
117  * starting at \p x. */
118 INLINE_API void
119 memxor(unsigned char *x, const unsigned char *y, size_t n) {
120   while(n--) {
121     *x ^= *y;
122     x++; y++;
123   }
124 }
125
126 /**
127  * Compares \p len bytes from @p a with @p b in constant time. This
128  * functions always traverses the entire length to prevent timing
129  * attacks.
130  *
131  * \param a Byte sequence to compare
132  * \param b Byte sequence to compare
133  * \param len Number of bytes to compare.
134  * \return \c 1 if \p a and \p b are equal, \c 0 otherwise.
135  */
136 INLINE_API int
137 equals(unsigned char *a, unsigned char *b, size_t len) {
138   int result = 1;
139   while (len--) {
140     result &= (*a++ == *b++);
141   }
142   return result;
143 }
144
145 #ifdef HAVE_FLS
146 #define dtls_fls(i) fls(i)
147 #else
148 INLINE_API int
149 dtls_fls(unsigned int i) {
150   int n;
151   for (n = 0; i; n++)
152     i >>= 1;
153   return n;
154 }
155 #endif /* HAVE_FLS */
156
157 #endif /* _DTLS_GLOBAL_H_ */