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