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-2009 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 as published by
21 * the Free Software Foundation, either version 3 of the License, or
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.
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/>.
32 #ifndef ANYTUN_cipher_h_INCLUDED
33 #define ANYTUN_cipher_h_INCLUDED
35 #include "datatypes.h"
37 #include "encryptedPacket.h"
38 #include "plainPacket.h"
39 #include "keyDerivation.h"
42 #ifndef USE_SSL_CRYPTO
45 #include <openssl/aes.h>
52 Cipher() : dir_(KD_INBOUND) {};
53 Cipher(kd_dir_t d) : dir_(d) {};
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);
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;
66 //****** NullCipher ******
68 class NullCipher : public Cipher
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);
76 //****** AesIcmCipher ******
78 class AesIcmCipher : public Cipher
81 AesIcmCipher(kd_dir_t d);
82 AesIcmCipher(kd_dir_t d, u_int16_t key_length);
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;
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);
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_;
103 u_int8_t ecount_buf_[AES_BLOCK_SIZE];
109 #pragma pack(push, 1)
111 union ATTR_PACKED cipher_aesctr_ctr_union {
112 u_int8_t buf_[CTR_LENGTH];
114 u_int8_t buf_[SALT_LENGTH];
118 u_int8_t fill_[SALT_LENGTH - sizeof(mux_t) - sizeof(sender_id_t) - 2*sizeof(u_int8_t) - sizeof(seq_nr_t)];
120 sender_id_t sender_id_;