ddde683db861d808b9fa5c0786c596dcec19888d
[anytun.git] / src / cipher.cpp
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-2008 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 version 3 as
21  *  published by the Free Software Foundation.
22  *
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.
27  *
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/>.
30  */
31
32 #include <stdexcept>
33 #include <iostream>
34 #include <string>
35 #include <cstdio>
36 #include <cstring>
37
38 #include "endian.h"
39
40 #include "cipher.h"
41 #include "log.h"
42 #include "anytunError.h"
43
44 void Cipher::encrypt(KeyDerivation& kd, PlainPacket & in, EncryptedPacket & out, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
45 {
46         u_int32_t len = cipher(kd, in, in.getLength(), out.getPayload(), out.getPayloadLength(), seq_nr, sender_id, mux);
47         out.setSenderId(sender_id);
48         out.setSeqNr(seq_nr);
49   out.setMux(mux);
50         out.setPayloadLength(len);
51 }
52
53 void Cipher::decrypt(KeyDerivation& kd, EncryptedPacket & in, PlainPacket & out)
54 {
55         u_int32_t len = decipher(kd, in.getPayload() , in.getPayloadLength(), out, out.getLength(), in.getSeqNr(), in.getSenderId(), in.getMux());
56         out.setLength(len);
57 }
58
59
60 //******* NullCipher *******
61
62 u_int32_t NullCipher::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)
63 {
64         std::memcpy(out, in, (ilen < olen) ? ilen : olen);
65   return (ilen < olen) ? ilen : olen;
66 }
67
68 u_int32_t NullCipher::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)
69 {
70         std::memcpy(out, in, (ilen < olen) ? ilen : olen);
71   return (ilen < olen) ? ilen : olen;
72 }
73
74 #ifndef NO_CRYPT
75 //****** AesIcmCipher ****** 
76
77 AesIcmCipher::AesIcmCipher(kd_dir_t d) : Cipher(d), key_(u_int32_t(DEFAULT_KEY_LENGTH/8)), salt_(u_int32_t(SALT_LENGTH))
78 {
79   init();
80 }
81
82 AesIcmCipher::AesIcmCipher(kd_dir_t d, u_int16_t key_length) : Cipher(d), key_(u_int32_t(key_length/8)), salt_(u_int32_t(SALT_LENGTH))
83 {
84   init(key_length);
85 }
86
87 void AesIcmCipher::init(u_int16_t key_length)
88 {
89 #ifndef USE_SSL_CRYPTO
90   handle_ = NULL;
91   int algo;
92   switch(key_length) {
93   case 128: algo = GCRY_CIPHER_AES128; break;
94   case 192: algo = GCRY_CIPHER_AES192; break;
95   case 256: algo = GCRY_CIPHER_AES256; break;
96   default: {
97     cLog.msg(Log::PRIO_ERROR) << "AesIcmCipher::AesIcmCipher: cipher key length of " << key_length << " Bits is not supported";
98     return;
99   }
100   }
101
102   gcry_error_t err = gcry_cipher_open(&handle_, algo, GCRY_CIPHER_MODE_CTR, 0);
103   if( err ) {
104     cLog.msg(Log::PRIO_ERROR) << "AesIcmCipher::AesIcmCipher: Failed to open cipher" << AnytunGpgError(err);
105   } 
106 #endif
107 }
108
109
110 AesIcmCipher::~AesIcmCipher()
111 {
112 #ifndef USE_SSL_CRYPTO
113   if(handle_)
114     gcry_cipher_close(handle_);
115 #endif
116 }
117
118 u_int32_t AesIcmCipher::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)
119 {
120   calc(kd, in, ilen, out, olen, seq_nr, sender_id, mux);
121   return (ilen < olen) ? ilen : olen;
122 }
123
124 u_int32_t AesIcmCipher::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)
125 {
126   calc(kd, in, ilen, out, olen, seq_nr, sender_id, mux);
127   return (ilen < olen) ? ilen : olen;
128 }
129
130 void AesIcmCipher::calcCtr(KeyDerivation& kd, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
131 {
132   kd.generate(dir_, LABEL_SALT, seq_nr, salt_);
133
134   std::memcpy(ctr_.salt_.buf_, salt_.getBuf(), SALT_LENGTH);
135   ctr_.salt_.zero_ = 0;
136   ctr_.params_.mux_ ^= MUX_T_HTON(mux);
137   ctr_.params_.sender_id_ ^= SENDER_ID_T_HTON(sender_id);
138   ctr_.params_.seq_nr_ ^= SEQ_NR_T_HTON(seq_nr);
139
140   return;
141 }
142
143 void AesIcmCipher::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)
144 {
145 #ifndef USE_SSL_CRYPTO
146   if(!handle_)
147     return;
148 #endif
149
150   kd.generate(dir_, LABEL_ENC, seq_nr, key_);
151 #ifdef USE_SSL_CRYPTO
152   int ret = AES_set_encrypt_key(key_.getBuf(), key_.getLength()*8, &aes_key_);
153   if(ret) {
154     cLog.msg(Log::PRIO_ERROR) << "AesIcmCipher: Failed to set cipher ssl key (code: " << ret << ")";
155     return;
156   }
157 #else
158   gcry_error_t err = gcry_cipher_setkey(handle_, key_.getBuf(), key_.getLength());
159   if(err) {
160     cLog.msg(Log::PRIO_ERROR) << "AesIcmCipher: Failed to set cipher key: " << AnytunGpgError(err);
161     return;
162   }
163 #endif
164
165   calcCtr(kd, seq_nr, sender_id, mux);
166  
167 #ifndef USE_SSL_CRYPTO
168   err = gcry_cipher_setctr(handle_, ctr_.buf_, CTR_LENGTH);
169   if(err) {
170     cLog.msg(Log::PRIO_ERROR) << "AesIcmCipher: Failed to set cipher CTR: " << AnytunGpgError(err);
171     return;
172   }
173
174   err = gcry_cipher_encrypt(handle_, out, olen, in, ilen);
175   if(err) {
176     cLog.msg(Log::PRIO_ERROR) << "AesIcmCipher: Failed to de/encrypt packet: " << AnytunGpgError(err);
177     return;
178   }
179 #else
180   if(CTR_LENGTH != AES_BLOCK_SIZE) {
181     cLog.msg(Log::PRIO_ERROR) << "AesIcmCipher: Failed to set cipher CTR: size don't fits";
182     return;
183   }
184   unsigned int num = 0;
185   std::memset(ecount_buf_, 0, AES_BLOCK_SIZE);
186   AES_ctr128_encrypt(in, out, (ilen < olen) ? ilen : olen, &aes_key_, ctr_.buf_, ecount_buf_, &num);
187 #endif
188 }
189 #endif
190