moved rtp stuff to anyrtpproxy and removed it from anytun
[anytun.git] / src / authAlgo.cpp
blob7689374106ef6a37d9e225c7408446c39b663520
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-2008 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 version 3 as
21 * published by the Free Software Foundation.
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.
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/>.
32 #include "authAlgo.h"
33 #include "log.h"
34 #include "buffer.h"
35 #include "encryptedPacket.h"
37 #include <iostream>
38 #include <cstring>
40 #ifndef NOCRYPT
41 #include <gcrypt.h>
42 #endif
44 //****** NullAuthAlgo ******
45 void NullAuthAlgo::generate(EncryptedPacket& packet)
49 bool NullAuthAlgo::checkTag(EncryptedPacket& packet)
51 return true;
54 u_int32_t NullAuthAlgo::getMaxLength()
56 return MAX_LENGTH_;
59 #ifndef NOCRYPT
60 //****** Sha1AuthAlgo ******
62 Sha1AuthAlgo::Sha1AuthAlgo() : ctx_(NULL)
64 gcry_error_t err = gcry_md_open( &ctx_, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC );
65 if( err )
66 cLog.msg(Log::PRIO_CRIT) << "Sha1AuthAlgo::Sha1AuthAlgo: Failed to open message digest algo";
69 Sha1AuthAlgo::~Sha1AuthAlgo()
71 if(ctx_)
72 gcry_md_close( ctx_ );
75 void Sha1AuthAlgo::setKey(Buffer& key)
77 if(!ctx_)
78 return;
80 gcry_error_t err = gcry_md_setkey( ctx_, key.getBuf(), key.getLength() );
81 if( err ) {
82 char buf[STERROR_TEXT_MAX];
83 buf[0] = 0;
84 cLog.msg(Log::PRIO_ERR) << "Sha1AuthAlgo::setKey: Failed to set cipher key: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
88 void Sha1AuthAlgo::generate(EncryptedPacket& packet)
90 if(!packet.getAuthTagLength())
91 return;
93 gcry_md_reset( ctx_ );
95 gcry_md_write( ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength() );
96 gcry_md_final( ctx_ );
98 u_int8_t* tag = packet.getAuthTag();
99 if(packet.getAuthTagLength() > MAX_LENGTH_)
100 std::memset(tag, 0, (packet.getAuthTagLength() - MAX_LENGTH_));
102 u_int8_t* hmac = gcry_md_read(ctx_, 0);
103 u_int32_t length = (packet.getAuthTagLength() < MAX_LENGTH_) ? packet.getAuthTagLength() : MAX_LENGTH_;
104 std::memcpy(&tag[packet.getAuthTagLength() - length], &hmac[MAX_LENGTH_ - length], length);
107 bool Sha1AuthAlgo::checkTag(EncryptedPacket& packet)
109 if(!packet.getAuthTagLength())
110 return true;
112 gcry_md_reset( ctx_ );
114 gcry_md_write( ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength() );
115 gcry_md_final( ctx_ );
117 u_int8_t* tag = packet.getAuthTag();
118 if(packet.getAuthTagLength() > MAX_LENGTH_)
119 for(u_int32_t i=0; i < (packet.getAuthTagLength() - MAX_LENGTH_); ++i)
120 if(tag[i]) return false;
122 u_int8_t* hmac = gcry_md_read(ctx_, 0);
123 u_int32_t length = (packet.getAuthTagLength() < MAX_LENGTH_) ? packet.getAuthTagLength() : MAX_LENGTH_;
124 if(std::memcmp(&tag[packet.getAuthTagLength() - length], &hmac[MAX_LENGTH_ - length], length))
125 return false;
127 return true;
130 u_int32_t Sha1AuthAlgo::getMaxLength()
132 return MAX_LENGTH_;
134 #endif