got rid of NOPACKED (struct alignment works now under Windows)
[anytun.git] / src / cipher.h
blobb26416bf782fd8d036b04e6b79a3000c97abe36b
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 _CIPHER_H_
33 #define _CIPHER_H_
35 #include "datatypes.h"
36 #include "buffer.h"
37 #include "encryptedPacket.h"
38 #include "plainPacket.h"
39 #include "keyDerivation.h"
41 #ifndef NOCRYPT
42 #ifndef USE_SSL_CRYPTO
43 #include <gcrypt.h>
44 #else
45 #include <openssl/aes.h>
46 #endif
47 #endif
49 class Cipher
51 public:
52 Cipher() : dir_(KD_INBOUND) {};
53 Cipher(kd_dir_t d) : dir_(d) {};
54 virtual ~Cipher() {};
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);
59 protected:
60 virtual u_int32_t cipher(KeyDerivation& kd, u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux) = 0;
61 virtual u_int32_t decipher(KeyDerivation& kd, u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux) = 0;
63 kd_dir_t dir_;
66 //****** NullCipher ******
68 class NullCipher : public Cipher
70 protected:
71 u_int32_t cipher(KeyDerivation& kd, u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
72 u_int32_t decipher(KeyDerivation& kd, u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
75 #ifndef NOCRYPT
76 //****** AesIcmCipher ******
78 class AesIcmCipher : public Cipher
80 public:
81 AesIcmCipher(kd_dir_t d);
82 AesIcmCipher(kd_dir_t d, u_int16_t key_length);
83 ~AesIcmCipher();
85 static const u_int16_t DEFAULT_KEY_LENGTH = 128;
86 static const u_int16_t CTR_LENGTH = 16;
87 static const u_int16_t SALT_LENGTH = 14;
89 protected:
90 u_int32_t cipher(KeyDerivation& kd, u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
91 u_int32_t decipher(KeyDerivation& kd, u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
93 private:
94 void init(u_int16_t key_length = DEFAULT_KEY_LENGTH);
96 void calcCtr(KeyDerivation& kd, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
97 void calc(KeyDerivation& kd, u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
99 #ifndef USE_SSL_CRYPTO
100 gcry_cipher_hd_t handle_;
101 #else
102 AES_KEY aes_key_;
103 u_int8_t ecount_buf_[AES_BLOCK_SIZE];
104 #endif
105 Buffer key_;
106 Buffer salt_;
108 #ifdef _MSC_VER
109 #pragma pack(push, 1)
110 #endif
111 union ATTR_PACKED cipher_aesctr_ctr_union {
112 u_int8_t buf_[CTR_LENGTH];
113 struct ATTR_PACKED {
114 u_int8_t buf_[SALT_LENGTH];
115 u_int16_t zero_;
116 } salt_;
117 struct ATTR_PACKED {
118 u_int8_t fill_[SALT_LENGTH - sizeof(mux_t) - sizeof(sender_id_t) - 2 - sizeof(seq_nr_t)];
119 mux_t mux_;
120 sender_id_t sender_id_;
121 u_int8_t empty_[2];
122 seq_nr_t seq_nr_;
123 u_int16_t zero_;
124 } params_;
125 } ctr_;
126 #ifdef _MSC_VER
127 #pragma pack(pop)
128 #endif
130 #endif
132 #endif