tinydtls  0.8.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
crypto.h
Go to the documentation of this file.
1 /* dtls -- a very basic DTLS implementation
2  *
3  * Copyright (C) 2011--2012 Olaf Bergmann <bergmann@tzi.org>
4  * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #ifndef _DTLS_CRYPTO_H_
28 #define _DTLS_CRYPTO_H_
29 
30 #include <stdlib.h> /* for rand() and srand() */
31 #include <stdint.h>
32 
33 #include "t_list.h"
34 
35 #include "aes/rijndael.h"
36 
37 #include "global.h"
38 #include "state.h"
39 #include "numeric.h"
40 #include "hmac.h"
41 #include "ccm.h"
42 
43 /* TLS_PSK_WITH_AES_128_CCM_8 */
44 #define DTLS_MAC_KEY_LENGTH 0
45 #define DTLS_KEY_LENGTH 16 /* AES-128 */
46 #define DTLS_BLK_LENGTH 16 /* AES-128 */
47 #define DTLS_MAC_LENGTH DTLS_HMAC_DIGEST_SIZE
48 #define DTLS_IV_LENGTH 4 /* length of nonce_explicit */
49 
55 #define MAX_KEYBLOCK_LENGTH \
56  (2 * DTLS_MAC_KEY_LENGTH + 2 * DTLS_KEY_LENGTH + 2 * DTLS_IV_LENGTH)
57 
59 #define DTLS_MASTER_SECRET_LENGTH 48
60 #define DTLS_RANDOM_LENGTH 32
61 
62 typedef enum { AES128=0
64 
65 typedef enum {
68 
70 typedef struct {
71  rijndael_ctx ctx;
72 } aes128_ccm_t;
73 
74 typedef struct dtls_cipher_context_t {
78 
79 typedef struct {
80  uint8 own_eph_priv[32];
81  uint8 other_eph_pub_x[32];
82  uint8 other_eph_pub_y[32];
83  uint8 other_pub_x[32];
84  uint8 other_pub_y[32];
86 
87 /* This is the maximal supported length of the psk client identity and psk
88  * server identity hint */
89 #define DTLS_PSK_MAX_CLIENT_IDENTITY_LEN 32
90 
91 /* This is the maximal supported length of the pre-shared key. */
92 #define DTLS_PSK_MAX_KEY_LEN 32
93 
94 typedef struct {
95  uint16_t id_length;
96  unsigned char identity[DTLS_PSK_MAX_CLIENT_IDENTITY_LEN];
98 
99 typedef struct {
103  uint16_t epoch;
104  uint64_t rseq;
114 
115 typedef struct {
116  union {
117  struct random_t {
120  } random;
123  } tmp;
124  LIST_STRUCT(reorder_queue);
129  unsigned int do_client_auth:1;
130  union {
131 #ifdef DTLS_ECC
133 #endif /* DTLS_ECC */
134 #ifdef DTLS_PSK
136 #endif /* DTLS_PSK */
137  } keyx;
139 
140 /* The following macros provide access to the components of the
141  * key_block in the security parameters. */
142 
143 #define dtls_kb_client_mac_secret(Param, Role) ((Param)->key_block)
144 #define dtls_kb_server_mac_secret(Param, Role) \
145  (dtls_kb_client_mac_secret(Param, Role) + DTLS_MAC_KEY_LENGTH)
146 #define dtls_kb_remote_mac_secret(Param, Role) \
147  ((Role) == DTLS_SERVER \
148  ? dtls_kb_client_mac_secret(Param, Role) \
149  : dtls_kb_server_mac_secret(Param, Role))
150 #define dtls_kb_local_mac_secret(Param, Role) \
151  ((Role) == DTLS_CLIENT \
152  ? dtls_kb_client_mac_secret(Param, Role) \
153  : dtls_kb_server_mac_secret(Param, Role))
154 #define dtls_kb_mac_secret_size(Param, Role) DTLS_MAC_KEY_LENGTH
155 #define dtls_kb_client_write_key(Param, Role) \
156  (dtls_kb_server_mac_secret(Param, Role) + DTLS_MAC_KEY_LENGTH)
157 #define dtls_kb_server_write_key(Param, Role) \
158  (dtls_kb_client_write_key(Param, Role) + DTLS_KEY_LENGTH)
159 #define dtls_kb_remote_write_key(Param, Role) \
160  ((Role) == DTLS_SERVER \
161  ? dtls_kb_client_write_key(Param, Role) \
162  : dtls_kb_server_write_key(Param, Role))
163 #define dtls_kb_local_write_key(Param, Role) \
164  ((Role) == DTLS_CLIENT \
165  ? dtls_kb_client_write_key(Param, Role) \
166  : dtls_kb_server_write_key(Param, Role))
167 #define dtls_kb_key_size(Param, Role) DTLS_KEY_LENGTH
168 #define dtls_kb_client_iv(Param, Role) \
169  (dtls_kb_server_write_key(Param, Role) + DTLS_KEY_LENGTH)
170 #define dtls_kb_server_iv(Param, Role) \
171  (dtls_kb_client_iv(Param, Role) + DTLS_IV_LENGTH)
172 #define dtls_kb_remote_iv(Param, Role) \
173  ((Role) == DTLS_SERVER \
174  ? dtls_kb_client_iv(Param, Role) \
175  : dtls_kb_server_iv(Param, Role))
176 #define dtls_kb_local_iv(Param, Role) \
177  ((Role) == DTLS_CLIENT \
178  ? dtls_kb_client_iv(Param, Role) \
179  : dtls_kb_server_iv(Param, Role))
180 #define dtls_kb_iv_size(Param, Role) DTLS_IV_LENGTH
181 
182 #define dtls_kb_size(Param, Role) \
183  (2 * (dtls_kb_mac_secret_size(Param, Role) + \
184  dtls_kb_key_size(Param, Role) + dtls_kb_iv_size(Param, Role)))
185 
186 /* just for consistency */
187 #define dtls_kb_digest_size(Param, Role) DTLS_MAC_LENGTH
188 
205 size_t dtls_p_hash(dtls_hashfunc_t h,
206  const unsigned char *key, size_t keylen,
207  const unsigned char *label, size_t labellen,
208  const unsigned char *random1, size_t random1len,
209  const unsigned char *random2, size_t random2len,
210  unsigned char *buf, size_t buflen);
211 
217 size_t dtls_prf(const unsigned char *key, size_t keylen,
218  const unsigned char *label, size_t labellen,
219  const unsigned char *random1, size_t random1len,
220  const unsigned char *random2, size_t random2len,
221  unsigned char *buf, size_t buflen);
222 
239 void dtls_mac(dtls_hmac_context_t *hmac_ctx,
240  const unsigned char *record,
241  const unsigned char *packet, size_t length,
242  unsigned char *buf);
243 
264 int dtls_encrypt(const unsigned char *src, size_t length,
265  unsigned char *buf,
266  unsigned char *nounce,
267  unsigned char *key, size_t keylen,
268  const unsigned char *aad, size_t aad_length);
269 
288 int dtls_decrypt(const unsigned char *src, size_t length,
289  unsigned char *buf,
290  unsigned char *nounce,
291  unsigned char *key, size_t keylen,
292  const unsigned char *a_data, size_t a_data_length);
293 
294 /* helper functions */
295 
306 int dtls_psk_pre_master_secret(unsigned char *key, size_t keylen,
307  unsigned char *result, size_t result_len);
308 
309 #define DTLS_EC_KEY_SIZE 32
310 
311 int dtls_ecdh_pre_master_secret(unsigned char *priv_key,
312  unsigned char *pub_key_x,
313  unsigned char *pub_key_y,
314  size_t key_size,
315  unsigned char *result,
316  size_t result_len);
317 
318 void dtls_ecdsa_generate_key(unsigned char *priv_key,
319  unsigned char *pub_key_x,
320  unsigned char *pub_key_y,
321  size_t key_size);
322 
323 void dtls_ecdsa_create_sig_hash(const unsigned char *priv_key, size_t key_size,
324  const unsigned char *sign_hash, size_t sign_hash_size,
325  uint32_t point_r[9], uint32_t point_s[9]);
326 
327 void dtls_ecdsa_create_sig(const unsigned char *priv_key, size_t key_size,
328  const unsigned char *client_random, size_t client_random_size,
329  const unsigned char *server_random, size_t server_random_size,
330  const unsigned char *keyx_params, size_t keyx_params_size,
331  uint32_t point_r[9], uint32_t point_s[9]);
332 
333 int dtls_ecdsa_verify_sig_hash(const unsigned char *pub_key_x,
334  const unsigned char *pub_key_y, size_t key_size,
335  const unsigned char *sign_hash, size_t sign_hash_size,
336  unsigned char *result_r, unsigned char *result_s);
337 
338 int dtls_ecdsa_verify_sig(const unsigned char *pub_key_x,
339  const unsigned char *pub_key_y, size_t key_size,
340  const unsigned char *client_random, size_t client_random_size,
341  const unsigned char *server_random, size_t server_random_size,
342  const unsigned char *keyx_params, size_t keyx_params_size,
343  unsigned char *result_r, unsigned char *result_s);
344 
345 int dtls_ec_key_from_uint32_asn1(const uint32_t *key, size_t key_size,
346  unsigned char *buf);
347 
348 
350 
352 
354 
356 void crypto_init();
357 
358 #endif /* _DTLS_CRYPTO_H_ */
359 
int dtls_ecdh_pre_master_secret(unsigned char *priv_key, unsigned char *pub_key_x, unsigned char *pub_key_y, size_t key_size, unsigned char *result, size_t result_len)
Definition: crypto.c:398
dtls_hs_state_t hs_state
Definition: crypto.h:125
void dtls_security_free(dtls_security_parameters_t *security)
Definition: crypto.c:174
size_t dtls_prf(const unsigned char *key, size_t keylen, const unsigned char *label, size_t labellen, const unsigned char *random1, size_t random1len, const unsigned char *random2, size_t random2len, unsigned char *buf, size_t buflen)
Definition: crypto.c:249
rijndael_ctx ctx
Definition: crypto.h:71
Wrappers for list structures and functions.
dtls_cipher_t cipher
Definition: crypto.h:128
dtls_compression_t
Definition: global.h:81
int dtls_encrypt(const unsigned char *src, size_t length, unsigned char *buf, unsigned char *nounce, unsigned char *key, size_t keylen, const unsigned char *aad, size_t aad_length)
Definition: crypto.c:524
void dtls_mac(dtls_hmac_context_t *hmac_ctx, const unsigned char *record, const unsigned char *packet, size_t length, unsigned char *buf)
Definition: crypto.c:266
dtls_handshake_parameters_t * dtls_handshake_new()
Definition: crypto.c:123
int dtls_psk_pre_master_secret(unsigned char *key, size_t keylen, unsigned char *result, size_t result_len)
Definition: crypto.c:318
void dtls_ecdsa_generate_key(unsigned char *priv_key, unsigned char *pub_key_x, unsigned char *pub_key_y, size_t key_size)
Definition: crypto.c:425
int dtls_decrypt(const unsigned char *src, size_t length, unsigned char *buf, unsigned char *nounce, unsigned char *key, size_t keylen, const unsigned char *a_data, size_t a_data_length)
Definition: crypto.c:550
aes128_ccm_t data
Definition: crypto.h:76
size_t dtls_p_hash(dtls_hashfunc_t h, const unsigned char *key, size_t keylen, const unsigned char *label, size_t labellen, const unsigned char *random1, size_t random1len, const unsigned char *random2, size_t random2len, unsigned char *buf, size_t buflen)
Definition: crypto.c:183
#define DTLS_PSK_MAX_CLIENT_IDENTITY_LEN
Definition: crypto.h:89
#define MAX_KEYBLOCK_LENGTH
Definition: crypto.h:55
void dtls_handshake_free(dtls_handshake_parameters_t *handshake)
Definition: crypto.c:146
#define DTLS_RANDOM_LENGTH
Definition: crypto.h:60
dtls_ecdh_curve
Definition: crypto.h:65
unsigned char uint8
Definition: global.h:47
dtls_compression_t compression
Definition: crypto.h:127
dtls_compression_t compression
Definition: crypto.h:100
dtls_cipher_t cipher
Definition: crypto.h:102
#define DTLS_MASTER_SECRET_LENGTH
Definition: crypto.h:59
void dtls_ecdsa_create_sig(const unsigned char *priv_key, size_t key_size, const unsigned char *client_random, size_t client_random_size, const unsigned char *server_random, size_t server_random_size, const unsigned char *keyx_params, size_t keyx_params_size, uint32_t point_r[9], uint32_t point_s[9])
Definition: crypto.c:463
int dtls_ecdsa_verify_sig_hash(const unsigned char *pub_key_x, const unsigned char *pub_key_y, size_t key_size, const unsigned char *sign_hash, size_t sign_hash_size, unsigned char *result_r, unsigned char *result_s)
Definition: crypto.c:483
state information for DTLS FSM
int dtls_ec_key_from_uint32_asn1(const uint32_t *key, size_t key_size, unsigned char *buf)
Definition: crypto.c:362
#define LIST_STRUCT(name)
Definition: t_list.h:86
dtls_hashfunc_t
Definition: hmac.h:83
dtls_cipher_t
Definition: global.h:74
Definition: crypto.h:62
dtls_crypto_alg
Definition: crypto.h:62
dtls_security_parameters_t * dtls_security_new()
Definition: crypto.c:155
void crypto_init()
Definition: crypto.c:76
struct dtls_cipher_context_t dtls_cipher_context_t
int dtls_ecdsa_verify_sig(const unsigned char *pub_key_x, const unsigned char *pub_key_y, size_t key_size, const unsigned char *client_random, size_t client_random_size, const unsigned char *server_random, size_t server_random_size, const unsigned char *keyx_params, size_t keyx_params_size, unsigned char *result_r, unsigned char *result_s)
Definition: crypto.c:503
void dtls_ecdsa_create_sig_hash(const unsigned char *priv_key, size_t key_size, const unsigned char *sign_hash, size_t sign_hash_size, uint32_t point_r[9], uint32_t point_s[9])
Definition: crypto.c:446