added compile time options NOCRYPT,NODAEMON,NOEXEC for easyier windows porting
[anytun.git] / src / keyDerivation.h
blob0f4189577e8ccfad034630d97bfc587921d77d8d
1 /*
2 * anytun
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"
36 #include "buffer.h"
37 #include "threadUtils.hpp"
38 #include "syncBuffer.h"
40 #include <gcrypt.h>
41 #include <boost/archive/text_oarchive.hpp>
42 #include <boost/archive/text_iarchive.hpp>
45 typedef enum {
46 LABEL_SATP_ENCRYPTION = 0x00,
47 LABEL_SATP_MSG_AUTH = 0x01,
48 LABEL_SATP_SALT = 0x02,
49 } satp_prf_label;
52 class KeyDerivation
54 public:
55 KeyDerivation() : ld_kdr_(0), master_salt_(0), master_key_(0) {};
56 virtual ~KeyDerivation() {};
58 void setLogKDRate(const u_int8_t ld_rate);
60 virtual void init(Buffer key, Buffer salt) = 0;
61 virtual void generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key) = 0;
63 virtual std::string printType() { return "KeyDerivation"; };
65 protected:
66 virtual void updateMasterKey() = 0;
68 KeyDerivation(const KeyDerivation & src);
69 friend class boost::serialization::access;
70 template<class Archive>
71 void serialize(Archive & ar, const unsigned int version)
73 Lock lock(mutex_);
74 ar & ld_kdr_;
75 ar & master_salt_;
76 ar & master_key_;
77 updateMasterKey();
80 int8_t ld_kdr_; // ld(key_derivation_rate)
81 SyncBuffer master_salt_;
82 SyncBuffer master_key_;
84 Mutex mutex_;
87 BOOST_IS_ABSTRACT(KeyDerivation)
89 //****** NullKeyDerivation ******
91 class NullKeyDerivation : public KeyDerivation
93 public:
94 NullKeyDerivation() {};
95 ~NullKeyDerivation() {};
97 void init(Buffer key, Buffer salt) {};
98 void generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key);
100 std::string printType() { return "NullKeyDerivation"; };
102 private:
103 void updateMasterKey() {};
105 friend class boost::serialization::access;
106 template<class Archive>
107 void serialize(Archive & ar, const unsigned int version)
109 ar & boost::serialization::base_object<KeyDerivation>(*this);
114 #ifndef NOCRYPT
115 //****** AesIcmKeyDerivation ******
117 class AesIcmKeyDerivation : public KeyDerivation
119 public:
120 AesIcmKeyDerivation() : cipher_(NULL) {};
121 ~AesIcmKeyDerivation();
123 void init(Buffer key, Buffer salt);
124 void generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key);
126 std::string printType() { return "AesIcmKeyDerivation"; };
128 private:
129 void updateMasterKey();
131 friend class boost::serialization::access;
132 template<class Archive>
133 void serialize(Archive & ar, const unsigned int version)
135 ar & boost::serialization::base_object<KeyDerivation>(*this);
138 gcry_cipher_hd_t cipher_;
141 #endif
143 #endif