Imported Upstream version 0.3.2
[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 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.
12  *
13  *
14  *  Copyright (C) 2007-2009 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
33 #ifndef ANYTUN_keyDerivation_h_INCLUDED
34 #define ANYTUN_keyDerivation_h_INCLUDED
35
36 #include "datatypes.h"
37 #include "buffer.h"
38 #include "threadUtils.hpp"
39 #include "syncBuffer.h"
40 #include "options.h"
41
42 #ifndef NO_CRYPT
43 #ifndef USE_SSL_CRYPTO
44 #include <gcrypt.h>
45 #else
46 #include <openssl/aes.h>
47 #endif
48 #endif
49 #include <boost/archive/text_oarchive.hpp>
50 #include <boost/archive/text_iarchive.hpp>
51 #include <boost/version.hpp>
52
53 #define LABEL_ENC 0
54 #define LABEL_AUTH 1
55 #define LABEL_SALT 2
56
57 #define LABEL_LEFT_ENC 0x356A192B
58 #define LABEL_RIGHT_ENC 0xDA4B9237
59 #define LABEL_LEFT_SALT 0x77DE68DA
60 #define LABEL_RIGHT_SALT 0x1B645389
61 #define LABEL_LEFT_AUTH 0xAC3478D6
62 #define LABEL_RIGHT_AUTH 0xC1DFD96E
63
64 typedef enum { KD_INBOUND, KD_OUTBOUND } kd_dir_t;
65
66 class KeyDerivation
67 {
68 public:
69   KeyDerivation() : is_initialized_(false), role_(ROLE_LEFT), key_length_(0), master_salt_(0), master_key_(0) {};
70   KeyDerivation(u_int16_t key_length) : is_initialized_(false), role_(ROLE_LEFT), key_length_(key_length), master_salt_(0), master_key_(0) {};
71   virtual ~KeyDerivation() {};
72
73   void setRole(const role_t role);
74
75   virtual void init(Buffer key, Buffer salt, std::string passphrase = "") = 0;
76   virtual bool generate(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, Buffer& key) = 0;
77
78   virtual std::string printType() { return "GenericKeyDerivation"; };
79
80   satp_prf_label_t convertLabel(kd_dir_t dir, satp_prf_label_t label);  
81
82 protected:
83   virtual void updateMasterKey() = 0;
84   
85 #ifndef NO_PASSPHRASE
86   void calcMasterKey(std::string passphrase, u_int16_t length);
87   void calcMasterSalt(std::string passphrase, u_int16_t length);
88 #endif
89
90         KeyDerivation(const KeyDerivation & src);
91         friend class boost::serialization::access;
92         template<class Archive>
93         void serialize(Archive & ar, const unsigned int version)
94         {
95                 WritersLock lock(mutex_);
96     ar & role_;
97     ar & key_length_;
98     ar & master_salt_;
99     ar & master_key_;
100     updateMasterKey();
101         }
102
103   bool is_initialized_;
104   role_t role_;
105   u_int16_t key_length_;
106   SyncBuffer master_salt_;
107   SyncBuffer master_key_;
108
109   SharedMutex mutex_;
110 };
111
112 #if BOOST_VERSION <= 103500 
113 BOOST_IS_ABSTRACT(KeyDerivation);
114 #else
115 BOOST_SERIALIZATION_ASSUME_ABSTRACT(KeyDerivation);
116 #endif
117
118 //****** NullKeyDerivation ******
119
120 class NullKeyDerivation : public KeyDerivation
121 {
122 public:
123   NullKeyDerivation() {};
124   ~NullKeyDerivation() {};
125
126   void init(Buffer key, Buffer salt, std::string passphrase = "") {};
127   bool generate(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, Buffer& key);
128
129   std::string printType() { return "NullKeyDerivation"; };
130
131 private:
132   void updateMasterKey() {};
133
134         friend class boost::serialization::access;
135         template<class Archive>
136         void serialize(Archive & ar, const unsigned int version)
137         {
138     ar & boost::serialization::base_object<KeyDerivation>(*this);
139         }
140
141 };
142
143 #ifndef NO_CRYPT
144 //****** AesIcmKeyDerivation ******
145
146 class AesIcmKeyDerivation : public KeyDerivation
147 {
148 public:
149   AesIcmKeyDerivation();
150   AesIcmKeyDerivation(u_int16_t key_length);
151   ~AesIcmKeyDerivation();
152
153   static const u_int16_t DEFAULT_KEY_LENGTH = 128;
154   static const u_int16_t CTR_LENGTH = 16;
155   static const u_int16_t SALT_LENGTH = 14;
156    
157   void init(Buffer key, Buffer salt, std::string passphrase = "");
158   bool generate(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, Buffer& key);
159
160   std::string printType();
161
162 private:
163   void updateMasterKey();
164
165   bool calcCtr(kd_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr);
166
167         friend class boost::serialization::access;
168         template<class Archive>
169         void serialize(Archive & ar, const unsigned int version)
170         {
171     ar & boost::serialization::base_object<KeyDerivation>(*this);
172         }
173
174 #ifndef USE_SSL_CRYPTO
175   gcry_cipher_hd_t handle_[2];
176 #else
177   AES_KEY aes_key_[2];
178   u_int8_t ecount_buf_[2][AES_BLOCK_SIZE];
179 #endif
180
181 #ifdef _MSC_VER
182   #pragma pack(push, 1)
183 #endif  
184   union ATTR_PACKED key_derivation_aesctr_ctr_union {
185     u_int8_t buf_[CTR_LENGTH];
186     struct ATTR_PACKED {
187       u_int8_t buf_[SALT_LENGTH];
188       u_int16_t zero_;
189     } salt_;
190     struct ATTR_PACKED {
191       u_int8_t fill_[SALT_LENGTH - sizeof(satp_prf_label_t) - sizeof(seq_nr_t)];
192       satp_prf_label_t label_;
193       seq_nr_t seq_;
194       u_int16_t zero_;
195     } params_;
196   } ctr_[2];
197 #ifdef _MSC_VER  
198   #pragma pack(pop)
199 #endif
200 };
201
202 #endif
203
204 #endif