fixed windows endian include
[anytun.git] / src / keyDerivation.h
blob9e8c7b5d7f3191aab2ce68bb2e2a8bbc6cfa1682
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 #ifndef NOCRYPT
41 #include <gcrypt.h>
42 #endif
43 #include <boost/archive/text_oarchive.hpp>
44 #include <boost/archive/text_iarchive.hpp>
47 typedef enum {
48 LABEL_SATP_ENCRYPTION = 0x00,
49 LABEL_SATP_MSG_AUTH = 0x01,
50 LABEL_SATP_SALT = 0x02,
51 } satp_prf_label;
54 class KeyDerivation
56 public:
57 KeyDerivation() : ld_kdr_(0), master_salt_(0), master_key_(0) {};
58 virtual ~KeyDerivation() {};
60 void setLogKDRate(const u_int8_t ld_rate);
62 virtual void init(Buffer key, Buffer salt) = 0;
63 virtual void generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key) = 0;
65 virtual std::string printType() { return "KeyDerivation"; };
67 protected:
68 virtual void updateMasterKey() = 0;
70 KeyDerivation(const KeyDerivation & src);
71 friend class boost::serialization::access;
72 template<class Archive>
73 void serialize(Archive & ar, const unsigned int version)
75 Lock lock(mutex_);
76 ar & ld_kdr_;
77 ar & master_salt_;
78 ar & master_key_;
79 updateMasterKey();
82 int8_t ld_kdr_; // ld(key_derivation_rate)
83 SyncBuffer master_salt_;
84 SyncBuffer master_key_;
86 Mutex mutex_;
89 BOOST_IS_ABSTRACT(KeyDerivation)
91 //****** NullKeyDerivation ******
93 class NullKeyDerivation : public KeyDerivation
95 public:
96 NullKeyDerivation() {};
97 ~NullKeyDerivation() {};
99 void init(Buffer key, Buffer salt) {};
100 void generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key);
102 std::string printType() { return "NullKeyDerivation"; };
104 private:
105 void updateMasterKey() {};
107 friend class boost::serialization::access;
108 template<class Archive>
109 void serialize(Archive & ar, const unsigned int version)
111 ar & boost::serialization::base_object<KeyDerivation>(*this);
116 #ifndef NOCRYPT
117 //****** AesIcmKeyDerivation ******
119 class AesIcmKeyDerivation : public KeyDerivation
121 public:
122 AesIcmKeyDerivation() : cipher_(NULL) {};
123 ~AesIcmKeyDerivation();
125 void init(Buffer key, Buffer salt);
126 void generate(satp_prf_label label, seq_nr_t seq_nr, Buffer& key);
128 std::string printType() { return "AesIcmKeyDerivation"; };
130 private:
131 void updateMasterKey();
133 friend class boost::serialization::access;
134 template<class Archive>
135 void serialize(Archive & ar, const unsigned int version)
137 ar & boost::serialization::base_object<KeyDerivation>(*this);
140 gcry_cipher_hd_t cipher_;
143 #endif
145 #endif