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