added SyncRtpCommand
[anytun.git] / cipher.cpp
blob51034801af11873edef16dbdec266201e54f0944
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)
45 u_int32_t len = cipher(in, in.getLength(), out.getPayload(), out.getPayloadLength(), seq_nr, sender_id);
46 out.setSenderId(sender_id);
47 out.setSeqNr(seq_nr);
48 out.setPayloadLength(len);
51 // TODO: in should be const but does not work with getBuf() :(
52 void Cipher::decrypt(EncryptedPacket & in, PlainPacket & out)
54 u_int32_t len = decipher(in.getPayload() , in.getPayloadLength(), out, out.getLength(), in.getSeqNr(), in.getSenderId());
55 out.setLength(len);
59 //******* NullCipher *******
61 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)
63 std::memcpy(out, in, (ilen < olen) ? ilen : olen);
64 return (ilen < olen) ? ilen : olen;
67 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)
69 std::memcpy(out, in, (ilen < olen) ? ilen : olen);
70 return (ilen < olen) ? ilen : olen;
74 //****** AesIcmCipher ******
76 AesIcmCipher::AesIcmCipher() : cipher_(NULL)
78 // TODO: hardcoded keysize
79 gcry_error_t err = gcry_cipher_open( &cipher_, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR, 0 );
80 if( err )
81 cLog.msg(Log::PRIO_CRIT) << "AesIcmCipher::AesIcmCipher: Failed to open cipher";
85 AesIcmCipher::~AesIcmCipher()
87 if(cipher_)
88 gcry_cipher_close( cipher_ );
91 void AesIcmCipher::setKey(Buffer& key)
93 if(!cipher_)
94 return;
96 gcry_error_t err = gcry_cipher_setkey( cipher_, key.getBuf(), key.getLength() );
97 if( err )
98 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher::setKey: Failed to set cipher key: " << gpg_strerror( err );
101 void AesIcmCipher::setSalt(Buffer& salt)
103 salt_ = salt;
104 if(!salt_[u_int32_t(0)])
105 salt_[u_int32_t(0)] = 1; // TODO: this is a outstandingly ugly workaround
108 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)
110 calc(in, ilen, out, olen, seq_nr, sender_id);
111 return (ilen < olen) ? ilen : olen;
114 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)
116 calc(in, ilen, out, olen, seq_nr, sender_id);
117 return (ilen < olen) ? ilen : olen;
120 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)
122 if(!cipher_)
123 return;
125 gcry_error_t err = gcry_cipher_reset( cipher_ );
126 if( err ) {
127 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher: Failed to reset cipher: " << gpg_strerror( err );
128 return;
131 // set the IV ( = CTR)
132 //==========================================================================
133 // // where the 128-bit integer value IV SHALL be defined by the SSRC, the
134 // // SRTP packet index i, and the SRTP session salting key k_s, as below.
135 // //
136 // // IV = (k_s * 2^16) XOR (SSRC * 2^64) XOR (i * 2^16)
137 // // sizeof(k_s) = 112 bit, random
139 Mpi ctr(128); // TODO: hardcoded size
140 Mpi salt(salt_.getBuf(), salt_.getLength());
141 Mpi sid(32); // TODO: Q@OTTI add mux to sender_id????
142 sid = sender_id;
143 Mpi seq(32);
144 seq = seq_nr;
146 ctr = salt.mul2exp(16) ^ sid.mul2exp(64) ^ seq.mul2exp(16); // TODO: hardcoded size
148 u_int32_t written;
149 u_int8_t *ctr_buf = ctr.getNewBuf(&written); // TODO: hardcoded size
150 err = gcry_cipher_setctr( cipher_, ctr_buf, written ); // TODO: hardcoded size
151 delete[] ctr_buf;
152 if( err ) {
153 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher: Failed to set cipher CTR: " << gpg_strerror( err );
154 return;
157 err = gcry_cipher_encrypt( cipher_, out, olen, in, ilen );
158 if( err ) {
159 cLog.msg(Log::PRIO_ERR) << "AesIcmCipher: Failed to generate cipher bitstream: " << gpg_strerror( err );
160 return;