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 #ifdef DTLS_ECC
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_PSK_WITH_AES_128_CCM_8 = 0xC0A8, /**< see RFC 6655 */
77   TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 = 0xC0AE /**< see RFC 7251 */
78 } dtls_cipher_t;
79 
80 /** Known compression suites.*/
81 typedef enum {
82   TLS_COMPRESSION_NULL = 0x0000		/* NULL compression */
83 } dtls_compression_t;
84 
85 #define TLS_EXT_ELLIPTIC_CURVES		10 /* see RFC 4492 */
86 #define TLS_EXT_EC_POINT_FORMATS	11 /* see RFC 4492 */
87 #define TLS_EXT_SIG_HASH_ALGO		13 /* see RFC 5246 */
88 #define TLS_EXT_CLIENT_CERTIFICATE_TYPE	19 /* see RFC 7250 */
89 #define TLS_EXT_SERVER_CERTIFICATE_TYPE	20 /* see RFC 7250 */
90 #define TLS_EXT_ENCRYPT_THEN_MAC	22 /* see RFC 7366 */
91 
92 #define TLS_CERT_TYPE_RAW_PUBLIC_KEY	2 /* see RFC 7250 */
93 
94 #define TLS_EXT_ELLIPTIC_CURVES_SECP256R1	23 /* see RFC 4492 */
95 
96 #define TLS_EXT_EC_POINT_FORMATS_UNCOMPRESSED	0 /* see RFC 4492 */
97 
98 #define TLS_EC_CURVE_TYPE_NAMED_CURVE		3 /* see RFC 4492 */
99 
100 #define TLS_CLIENT_CERTIFICATE_TYPE_ECDSA_SIGN	64 /* see RFC 4492 */
101 
102 #define TLS_EXT_SIG_HASH_ALGO_SHA256		4 /* see RFC 5246 */
103 #define TLS_EXT_SIG_HASH_ALGO_ECDSA		3 /* see RFC 5246 */
104 
105 /**
106  * XORs \p n bytes byte-by-byte starting at \p y to the memory area
107  * starting at \p x. */
108 static inline void
memxor(unsigned char * x,const unsigned char * y,size_t n)109 memxor(unsigned char *x, const unsigned char *y, size_t n) {
110   while(n--) {
111     *x ^= *y;
112     x++; y++;
113   }
114 }
115 
116 /**
117  * Compares \p len bytes from @p a with @p b in constant time. This
118  * functions always traverses the entire length to prevent timing
119  * attacks.
120  *
121  * \param a Byte sequence to compare
122  * \param b Byte sequence to compare
123  * \param len Number of bytes to compare.
124  * \return \c 1 if \p a and \p b are equal, \c 0 otherwise.
125  */
126 static inline int
equals(unsigned char * a,unsigned char * b,size_t len)127 equals(unsigned char *a, unsigned char *b, size_t len) {
128   int result = 1;
129   while (len--) {
130     result &= (*a++ == *b++);
131   }
132   return result;
133 }
134 
135 #ifdef HAVE_FLS
136 #define dtls_fls(i) fls(i)
137 #else
138 static inline int
dtls_fls(unsigned int i)139 dtls_fls(unsigned int i) {
140   int n;
141   for (n = 0; i; n++)
142     i >>= 1;
143   return n;
144 }
145 #endif /* HAVE_FLS */
146 
147 #endif /* _DTLS_GLOBAL_H_ */
148