Imported Upstream version 0.3.5
[anytun.git] / src / keyDerivation.h
1 /*
2  *  anytun
3  *
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 methods 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.
12  *
13  *
14  *  Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with Anytun.  If not, see <http://www.gnu.org/licenses/>.
31  *
32  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #ifndef ANYTUN_keyDerivation_h_INCLUDED
47 #define ANYTUN_keyDerivation_h_INCLUDED
48
49 #include "datatypes.h"
50 #include "buffer.h"
51 #include "threadUtils.hpp"
52 #include "syncBuffer.h"
53 #include "options.h"
54
55 #ifndef NO_CRYPT
56
57 #if defined(USE_SSL_CRYPTO)
58 #include <openssl/aes.h>
59 #elif defined(USE_NETTLE)
60 #include <nettle/aes.h>
61 #else  // USE_GCRYPT is the default
62 #include <gcrypt.h>
63 #endif
64
65 #endif
66 #include <boost/archive/text_oarchive.hpp>
67 #include <boost/archive/text_iarchive.hpp>
68 #include <boost/version.hpp>
69
70 #define LABEL_ENC 0
71 #define LABEL_AUTH 1
72 #define LABEL_SALT 2
73
74 #define LABEL_LEFT_ENC 0x356A192B
75 #define LABEL_RIGHT_ENC 0xDA4B9237
76 #define LABEL_LEFT_SALT 0x77DE68DA
77 #define LABEL_RIGHT_SALT 0x1B645389
78 #define LABEL_LEFT_AUTH 0xAC3478D6
79 #define LABEL_RIGHT_AUTH 0xC1DFD96E
80
81 typedef enum { KD_INBOUND, KD_OUTBOUND } kd_dir_t;
82
83 class KeyDerivation
84 {
85 public:
86   KeyDerivation() : is_initialized_(false), role_(ROLE_LEFT), key_length_(0), master_salt_(0), master_key_(0) {};
87   KeyDerivation(uint16_t key_length) : is_initialized_(false), role_(ROLE_LEFT), key_length_(key_length), master_salt_(0), master_key_(0) {};
88   virtual ~KeyDerivation() {};
89
90   void setRole(const role_t role);
91
92   virtual void init(Buffer key, Buffer salt, std::string passphrase = "") = 0;
93   virtual bool generate(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, Buffer& key) = 0;
94
95   virtual std::string printType() { return "GenericKeyDerivation"; };
96
97   satp_prf_label_t convertLabel(kd_dir_t dir, satp_prf_label_t label);
98
99 protected:
100   virtual void updateMasterKey() = 0;
101
102 #ifndef NO_PASSPHRASE
103   void calcMasterKey(std::string passphrase, uint16_t length);
104   void calcMasterSalt(std::string passphrase, uint16_t length);
105 #endif
106
107   KeyDerivation(const KeyDerivation& src);
108   friend class boost::serialization::access;
109   template<class Archive>
110   void serialize(Archive& ar, const unsigned int version) {
111     WritersLock lock(mutex_);
112     ar& role_;
113     ar& key_length_;
114     ar& master_salt_;
115     ar& master_key_;
116     updateMasterKey();
117   }
118
119   bool is_initialized_;
120   role_t role_;
121   uint16_t key_length_;
122   SyncBuffer master_salt_;
123   SyncBuffer master_key_;
124
125   SharedMutex mutex_;
126 };
127
128 #if BOOST_VERSION <= 103500
129 BOOST_IS_ABSTRACT(KeyDerivation);
130 #else
131 BOOST_SERIALIZATION_ASSUME_ABSTRACT(KeyDerivation);
132 #endif
133
134 //****** NullKeyDerivation ******
135
136 class NullKeyDerivation : public KeyDerivation
137 {
138 public:
139   NullKeyDerivation() {};
140   ~NullKeyDerivation() {};
141
142   void init(Buffer key, Buffer salt, std::string passphrase = "") {};
143   bool generate(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, Buffer& key);
144
145   std::string printType() { return "NullKeyDerivation"; };
146
147 private:
148   void updateMasterKey() {};
149
150   friend class boost::serialization::access;
151   template<class Archive>
152   void serialize(Archive& ar, const unsigned int version) {
153     ar& boost::serialization::base_object<KeyDerivation>(*this);
154   }
155
156 };
157
158 #ifndef NO_CRYPT
159 //****** AesIcmKeyDerivation ******
160
161 class AesIcmKeyDerivation : public KeyDerivation
162 {
163 public:
164   AesIcmKeyDerivation();
165   AesIcmKeyDerivation(uint16_t key_length);
166   ~AesIcmKeyDerivation();
167
168   static const uint16_t DEFAULT_KEY_LENGTH = 128;
169   static const uint16_t CTR_LENGTH = 16;
170   static const uint16_t SALT_LENGTH = 14;
171
172   void init(Buffer key, Buffer salt, std::string passphrase = "");
173   bool generate(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, Buffer& key);
174
175   std::string printType();
176
177 private:
178   void updateMasterKey();
179
180   bool calcCtr(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr);
181
182   friend class boost::serialization::access;
183   template<class Archive>
184   void serialize(Archive& ar, const unsigned int version) {
185     ar& boost::serialization::base_object<KeyDerivation>(*this);
186   }
187
188 #if defined(USE_SSL_CRYPTO)
189   AES_KEY aes_key_[2];
190   uint8_t ecount_buf_[2][AES_BLOCK_SIZE];
191 #elif defined(USE_NETTLE)
192   struct aes_ctx ctx_[2];
193 #else  // USE_GCRYPT is the default
194   gcry_cipher_hd_t handle_[2];
195 #endif
196
197 #ifdef _MSC_VER
198 #pragma pack(push, 1)
199 #endif
200   union ATTR_PACKED key_derivation_aesctr_ctr_union {
201     uint8_t buf_[CTR_LENGTH];
202     struct ATTR_PACKED {
203       uint8_t buf_[SALT_LENGTH];
204       uint16_t zero_;
205     } salt_;
206     struct ATTR_PACKED {
207       uint8_t fill_[SALT_LENGTH - sizeof(satp_prf_label_t) - sizeof(seq_nr_t)];
208       satp_prf_label_t label_;
209       seq_nr_t seq_;
210       uint16_t zero_;
211     } params_;
212   } ctr_[2];
213 #ifdef _MSC_VER
214 #pragma pack(pop)
215 #endif
216 };
217
218 #endif
219
220 #endif