c6212f13283807100d0c83a0fdd63df3219a8b2c
[debian/uanytun.git] / src / key_derivation.h
1 /*
2  *  uAnytun
3  *
4  *  uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5  *  featured implementation uAnytun has no support for multiple connections
6  *  or synchronisation. It is a small single threaded implementation intended
7  *  to act as a client on small platforms.
8  *  The secure anycast tunneling protocol (satp) defines a protocol used
9  *  for communication between any combination of unicast and anycast
10  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
11  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
12  *  ethernet, ip, arp ...). satp directly includes cryptography and
13  *  message authentication based on the methodes used by SRTP.  It is
14  *  intended to deliver a generic, scaleable and secure solution for
15  *  tunneling and relaying of packets of any protocol.
16  *  
17  *
18  *  Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
19  *
20  *  This file is part of uAnytun.
21  *
22  *  uAnytun is free software: you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License version 3 as
24  *  published by the Free Software Foundation.
25  *
26  *  uAnytun is distributed in the hope that it will be useful,
27  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  *  GNU General Public License for more details.
30  *
31  *  You should have received a copy of the GNU General Public License
32  *  along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
33  */
34
35 #ifndef _KEY_DERIVATION_H_
36 #define _KEY_DERIVATION_H_
37
38 #ifndef USE_SSL_CRYPTO
39 #include <gcrypt.h>
40 #else
41 #include <openssl/aes.h>
42 #endif
43
44 #include "options.h"
45
46 #define LABEL_ENC 0
47 #define LABEL_AUTH 1
48 #define LABEL_SALT 2
49 #define LABEL_NIL 3
50
51 #define LABEL_LEFT_ENC 0x356A192B
52 #define LABEL_RIGHT_ENC 0xDA4B9237
53 #define LABEL_LEFT_SALT 0x77DE68DA
54 #define LABEL_RIGHT_SALT 0x1B645389
55 #define LABEL_LEFT_AUTH 0xAC3478D6
56 #define LABEL_RIGHT_AUTH 0xC1DFD96E
57
58 enum key_derivation_type_enum { kd_unknown, kd_null, kd_aes_ctr };
59 typedef enum key_derivation_type_enum key_derivation_type_t;
60 enum key_derivation_dir_enum { kd_inbound, kd_outbound };
61 typedef enum key_derivation_dir_enum key_derivation_dir_t;
62
63 struct key_derivation_struct {
64   key_derivation_type_t type_;
65   u_int16_t key_length_;
66   role_t role_;
67   buffer_t master_key_;
68   buffer_t master_salt_;
69   void* params_;
70 };
71 typedef struct key_derivation_struct key_derivation_t;
72
73 int key_derivation_init(key_derivation_t* kd, const char* type, role_t role, const char* passphrase, u_int8_t* key, u_int32_t key_len, u_int8_t* salt, u_int32_t salt_len);
74 #ifndef NO_PASSPHRASE
75 int key_derivation_generate_master_key(key_derivation_t* kd, const char* passphrase, u_int16_t key_length);
76 int key_derivation_generate_master_salt(key_derivation_t* kd, const char* passphrase, u_int16_t salt_length);
77 #endif
78 void key_derivation_close(key_derivation_t* kd);
79 int key_derivation_generate(key_derivation_t* kd, key_derivation_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, u_int8_t* key, u_int32_t len);
80 satp_prf_label_t convert_label(role_t role, key_derivation_dir_t dir, satp_prf_label_t label);
81
82 int key_derivation_null_generate(u_int8_t* key, u_int32_t len);
83
84
85 #define KD_AESCTR_DEFAULT_KEY_LENGTH 128
86 #define KD_AESCTR_CTR_LENGTH 16
87 #define KD_AESCTR_SALT_LENGTH 14
88
89 union __attribute__((__packed__)) key_derivation_aesctr_ctr_union {
90   u_int8_t buf_[KD_AESCTR_CTR_LENGTH];
91   struct __attribute__ ((__packed__)) {
92     u_int8_t buf_[KD_AESCTR_SALT_LENGTH];
93     u_int16_t zero_;
94   } salt_;
95   struct __attribute__((__packed__)) {
96     u_int8_t fill_[KD_AESCTR_SALT_LENGTH - sizeof(satp_prf_label_t) - sizeof(seq_nr_t)];
97     satp_prf_label_t label_;
98     seq_nr_t seq_;
99     u_int16_t zero_;
100   } params_;
101 };
102 typedef union key_derivation_aesctr_ctr_union key_derivation_aesctr_ctr_t;
103
104 struct key_derivation_aesctr_param_struct {
105 #ifndef USE_SSL_CRYPTO
106   gcry_cipher_hd_t handle_;
107 #else
108   AES_KEY aes_key_;
109   u_int8_t ecount_buf_[AES_BLOCK_SIZE];
110 #endif
111   key_derivation_aesctr_ctr_t ctr_;
112 };
113 typedef struct key_derivation_aesctr_param_struct key_derivation_aesctr_param_t;
114
115 int key_derivation_aesctr_init(key_derivation_t* kd, const char* passphrase);
116 void key_derivation_aesctr_close(key_derivation_t* kd);
117 int key_derivation_aesctr_calc_ctr(key_derivation_t* kd, key_derivation_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr);
118 int key_derivation_aesctr_generate(key_derivation_t* kd, key_derivation_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, u_int8_t* key, u_int32_t len);
119
120 #endif