Imported Upstream version 0.3.4
[anytun.git] / src / cipher.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 #ifndef ANYTUN_cipher_h_INCLUDED
33 #define ANYTUN_cipher_h_INCLUDED
34
35 #include "datatypes.h"
36 #include "buffer.h"
37 #include "encryptedPacket.h"
38 #include "plainPacket.h"
39 #include "keyDerivation.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
49 class Cipher
50 {
51 public:
52   Cipher() : dir_(KD_INBOUND) {};
53   Cipher(kd_dir_t d) : dir_(d) {};
54   virtual ~Cipher() {};
55
56   void encrypt(KeyDerivation& kd, PlainPacket& in, EncryptedPacket& out, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
57   void decrypt(KeyDerivation& kd, EncryptedPacket& in, PlainPacket& out);
58
59 protected:
60   virtual uint32_t cipher(KeyDerivation& kd, uint8_t* in, uint32_t ilen, uint8_t* out, uint32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux) = 0;
61   virtual uint32_t decipher(KeyDerivation& kd, uint8_t* in, uint32_t ilen, uint8_t* out, uint32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux) = 0;
62
63   kd_dir_t dir_;
64 };
65
66 //****** NullCipher ******
67
68 class NullCipher : public Cipher
69 {
70 protected:
71   uint32_t cipher(KeyDerivation& kd, uint8_t* in, uint32_t ilen, uint8_t* out, uint32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
72   uint32_t decipher(KeyDerivation& kd, uint8_t* in, uint32_t ilen, uint8_t* out, uint32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
73 };
74
75 #ifndef NO_CRYPT
76 //****** AesIcmCipher ******
77
78 class AesIcmCipher : public Cipher
79 {
80 public:
81   AesIcmCipher(kd_dir_t d);
82   AesIcmCipher(kd_dir_t d, uint16_t key_length);
83   ~AesIcmCipher();
84
85   static const uint16_t DEFAULT_KEY_LENGTH = 128;
86   static const uint16_t CTR_LENGTH = 16;
87   static const uint16_t SALT_LENGTH = 14;
88
89 protected:
90   uint32_t cipher(KeyDerivation& kd, uint8_t* in, uint32_t ilen, uint8_t* out, uint32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
91   uint32_t decipher(KeyDerivation& kd, uint8_t* in, uint32_t ilen, uint8_t* out, uint32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
92
93 private:
94   void init(uint16_t key_length = DEFAULT_KEY_LENGTH);
95
96   void calcCtr(KeyDerivation& kd, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
97   void calc(KeyDerivation& kd, uint8_t* in, uint32_t ilen, uint8_t* out, uint32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
98
99 #ifndef USE_SSL_CRYPTO
100   gcry_cipher_hd_t handle_;
101 #else
102   AES_KEY aes_key_;
103   uint8_t ecount_buf_[AES_BLOCK_SIZE];
104 #endif
105   Buffer key_;
106   Buffer salt_;
107
108 #ifdef _MSC_VER
109 #pragma pack(push, 1)
110 #endif
111   union ATTR_PACKED cipher_aesctr_ctr_union {
112     uint8_t buf_[CTR_LENGTH];
113     struct ATTR_PACKED {
114       uint8_t buf_[SALT_LENGTH];
115       uint16_t zero_;
116     } salt_;
117     struct ATTR_PACKED {
118       uint8_t fill_[SALT_LENGTH - sizeof(mux_t) - sizeof(sender_id_t) - 2*sizeof(uint8_t) - sizeof(seq_nr_t)];
119       mux_t mux_;
120       sender_id_t sender_id_;
121       uint8_t empty_[2];
122       seq_nr_t seq_nr_;
123       uint16_t zero_;
124     } params_;
125   } ctr_;
126 #ifdef _MSC_VER
127 #pragma pack(pop)
128 #endif
129 };
130 #endif
131
132 #endif