4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #ifndef _KEYDERIVATION_H_
33 #define _KEYDERIVATION_H_
35 #include "datatypes.h"
37 #include "threadUtils.hpp"
38 #include "syncBuffer.h"
41 #ifndef USE_SSL_CRYPTO
44 #include <openssl/aes.h>
47 #include <boost/archive/text_oarchive.hpp>
48 #include <boost/archive/text_iarchive.hpp>
50 #define KD_LABEL_COUNT 3
52 LABEL_SATP_ENCRYPTION
= 0x00,
53 LABEL_SATP_MSG_AUTH
= 0x01,
54 LABEL_SATP_SALT
= 0x02,
70 KeyDerivation() : is_initialized_(false), ld_kdr_(0), key_length_(0), master_salt_(0), master_key_(0) {};
71 KeyDerivation(u_int16_t key_length
) : is_initialized_(false), ld_kdr_(0), key_length_(key_length
), master_salt_(0), master_key_(0) {};
72 virtual ~KeyDerivation() {};
74 void setLogKDRate(const int8_t ld_rate
);
76 virtual void init(Buffer key
, Buffer salt
, std::string passphrase
= "") = 0;
77 virtual bool generate(kd_dir_t dir
, satp_prf_label_t label
, seq_nr_t seq_nr
, Buffer
& key
) = 0;
79 virtual std::string
printType() { return "GenericKeyDerivation"; };
82 virtual void updateMasterKey() = 0;
85 void calcMasterKey(std::string passphrase
, u_int16_t length
);
86 void calcMasterSalt(std::string passphrase
, u_int16_t length
);
89 KeyDerivation(const KeyDerivation
& src
);
90 friend class boost::serialization::access
;
91 template<class Archive
>
92 void serialize(Archive
& ar
, const unsigned int version
)
94 WritersLock
lock(mutex_
);
102 bool is_initialized_
;
103 int8_t ld_kdr_
; // ld(key_derivation_rate)
104 u_int16_t key_length_
;
105 SyncBuffer master_salt_
;
106 SyncBuffer master_key_
;
111 BOOST_IS_ABSTRACT(KeyDerivation
)
113 //****** NullKeyDerivation ******
115 class NullKeyDerivation
: public KeyDerivation
118 NullKeyDerivation() {};
119 ~NullKeyDerivation() {};
121 void init(Buffer key
, Buffer salt
, std::string passphrase
= "") {};
122 bool generate(kd_dir_t dir
, satp_prf_label_t label
, seq_nr_t seq_nr
, Buffer
& key
);
124 std::string
printType() { return "NullKeyDerivation"; };
127 void updateMasterKey() {};
129 friend class boost::serialization::access
;
130 template<class Archive
>
131 void serialize(Archive
& ar
, const unsigned int version
)
133 ar
& boost::serialization::base_object
<KeyDerivation
>(*this);
139 //****** AesIcmKeyDerivation ******
141 class AesIcmKeyDerivation
: public KeyDerivation
144 AesIcmKeyDerivation();
145 AesIcmKeyDerivation(u_int16_t key_length
);
146 ~AesIcmKeyDerivation();
148 static const u_int16_t DEFAULT_KEY_LENGTH
= 128;
149 static const u_int16_t CTR_LENGTH
= 16;
150 static const u_int16_t SALT_LENGTH
= 14;
152 void init(Buffer key
, Buffer salt
, std::string passphrase
= "");
153 bool generate(kd_dir_t dir
, satp_prf_label_t label
, seq_nr_t seq_nr
, Buffer
& key
);
155 std::string
printType();
158 void updateMasterKey();
160 bool calcCtr(kd_dir_t dir
, seq_nr_t
* r
, satp_prf_label_t label
, seq_nr_t seq_nr
);
162 friend class boost::serialization::access
;
163 template<class Archive
>
164 void serialize(Archive
& ar
, const unsigned int version
)
166 ar
& boost::serialization::base_object
<KeyDerivation
>(*this);
169 #ifndef USE_SSL_CRYPTO
170 gcry_cipher_hd_t handle_
[2];
173 u_int8_t ecount_buf_
[2][AES_BLOCK_SIZE
];
176 key_store_t key_store_
[2][KD_LABEL_COUNT
];
179 #pragma pack(push, 1)
181 union ATTR_PACKED key_derivation_aesctr_ctr_union
{
182 u_int8_t buf_
[CTR_LENGTH
];
184 u_int8_t buf_
[SALT_LENGTH
];
187 #ifndef ANYTUN_02_COMPAT
189 u_int8_t fill_
[SALT_LENGTH
- sizeof(u_int8_t
) - sizeof(seq_nr_t
)];
196 u_int8_t fill_
[SALT_LENGTH
- sizeof(u_int8_t
) - 2 - sizeof(seq_nr_t
)];