added 2nd part (SATP) of acn presentation
[anytun.git] / src / cipher.cpp
blob3667290eeaa1ec7c4cbe4c174b40fd25c16e764d
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 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <stdexcept>
32 #include <iostream>
33 #include <string>
34 #include <cstdio>
35 #include <gcrypt.h>
37 #include "cipher.h"
38 #include "mpi.h"
39 #include "log.h"
42 // TODO: in should be const but does not work with getBuf() :(
43 void Cipher::encrypt(PlainPacket & in, EncryptedPacket & out, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
45 u_int32_t len = cipher(in, in.getLength(), out.getPayload(), out.getPayloadLength(), seq_nr, sender_id, mux);
46 out.setSenderId(sender_id);
47 out.setSeqNr(seq_nr);
48 out.setMux(mux);
49 out.setPayloadLength(len);
52 // TODO: in should be const but does not work with getBuf() :(
53 void Cipher::decrypt(EncryptedPacket & in, PlainPacket & out)
55 u_int32_t len = decipher(in.getPayload() , in.getPayloadLength(), out, out.getLength(), in.getSeqNr(), in.getSenderId(), in.getMux());
56 out.setLength(len);
60 //******* NullCipher *******
62 u_int32_t NullCipher::cipher(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)
64 std::memcpy(out, in, (ilen < olen) ? ilen : olen);
65 return (ilen < olen) ? ilen : olen;
68 u_int32_t NullCipher::decipher(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)
70 std::memcpy(out, in, (ilen < olen) ? ilen : olen);
71 return (ilen < olen) ? ilen : olen;
75 //****** AesIcmCipher ******
77 AesIcmCipher::AesIcmCipher() : cipher_(NULL)
79 // TODO: hardcoded keysize
80 gcry_error_t err = gcry_cipher_open( &cipher_, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR, 0 );
81 if( err ) {
82 char buf[STERROR_TEXT_MAX];
83 buf[0] = 0;
84 cLog.msg(Log::PRIO_CRIT) << "AesIcmCipher::AesIcmCipher: Failed to open cipher" << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
89 AesIcmCipher::~AesIcmCipher()
91 if(cipher_)
92 gcry_cipher_close( cipher_ );
95 void AesIcmCipher::setKey(Buffer& key)
97 if(!cipher_)
98 return;
100 gcry_error_t err = gcry_cipher_setkey( cipher_, key.getBuf(), key.getLength() );
101 if( err ) {
102 char buf[STERROR_TEXT_MAX];
103 buf[0] = 0;
104 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher::setKey: Failed to set cipher key: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
108 void AesIcmCipher::setSalt(Buffer& salt)
110 salt_ = salt;
111 if(!salt_[u_int32_t(0)])
112 salt_[u_int32_t(0)] = 1; // TODO: this is a outstandingly ugly workaround
115 u_int32_t AesIcmCipher::cipher(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)
117 calc(in, ilen, out, olen, seq_nr, sender_id, mux);
118 return (ilen < olen) ? ilen : olen;
121 u_int32_t AesIcmCipher::decipher(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)
123 calc(in, ilen, out, olen, seq_nr, sender_id, mux);
124 return (ilen < olen) ? ilen : olen;
127 void AesIcmCipher::calc(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)
129 if(!cipher_)
130 return;
132 gcry_error_t err = gcry_cipher_reset( cipher_ );
133 if( err ) {
134 char buf[STERROR_TEXT_MAX];
135 buf[0] = 0;
136 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher: Failed to reset cipher: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
137 return;
140 // set the IV ( = CTR)
141 //==========================================================================
142 // // where the 128-bit integer value IV SHALL be defined by the SSRC, the
143 // // SRTP packet index i, and the SRTP session salting key k_s, as below.
144 // //
145 // // IV = (k_s * 2^16) XOR (SSRC * 2^64) XOR (i * 2^16)
146 // // sizeof(k_s) = 112 bit, random
148 Mpi ctr(128); // TODO: hardcoded size
149 Mpi salt(salt_.getBuf(), salt_.getLength());
150 Mpi sid_mux(32);
151 sid_mux = sender_id;
152 Mpi mux_mpi(32);
153 mux_mpi = mux;
154 sid_mux = sid_mux ^ mux_mpi.mul2exp(16);
155 Mpi seq(32);
156 seq = seq_nr;
158 ctr = salt.mul2exp(16) ^ sid_mux.mul2exp(64) ^ seq.mul2exp(16); // TODO: hardcoded size
160 size_t written;
161 u_int8_t *ctr_buf = ctr.getNewBuf(&written); // TODO: hardcoded size
162 err = gcry_cipher_setctr( cipher_, ctr_buf, written ); // TODO: hardcoded size
163 delete[] ctr_buf;
164 if( err ) {
165 char buf[STERROR_TEXT_MAX];
166 buf[0] = 0;
167 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher: Failed to set cipher CTR: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
168 return;
171 err = gcry_cipher_encrypt( cipher_, out, olen, in, ilen );
172 if( err ) {
173 char buf[STERROR_TEXT_MAX];
174 buf[0] = 0;
175 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher: Failed to generate cipher bitstream: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
176 return;