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-2009 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 as published by
21 * the Free Software Foundation, either version 3 of the License, or
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.
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/>.
35 #include "anytunError.h"
37 #include "encryptedPacket.h"
42 //****** NullAuthAlgo ******
43 void NullAuthAlgo::generate(KeyDerivation& kd, EncryptedPacket& packet)
47 bool NullAuthAlgo::checkTag(KeyDerivation& kd, EncryptedPacket& packet)
53 //****** Sha1AuthAlgo ******
55 Sha1AuthAlgo::Sha1AuthAlgo(kd_dir_t d) : AuthAlgo(d), key_(DIGEST_LENGTH)
57 #ifndef USE_SSL_CRYPTO
58 gcry_error_t err = gcry_md_open(&handle_, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
60 cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::Sha1AuthAlgo: Failed to open message digest algo";
65 HMAC_Init_ex(&ctx_, NULL, 0, EVP_sha1(), NULL);
69 Sha1AuthAlgo::~Sha1AuthAlgo()
71 #ifndef USE_SSL_CRYPTO
73 gcry_md_close(handle_);
76 HMAC_CTX_cleanup(&ctx_);
80 void Sha1AuthAlgo::generate(KeyDerivation& kd, EncryptedPacket& packet)
82 #ifndef USE_SSL_CRYPTO
89 if(!packet.getAuthTagLength()) {
93 kd.generate(dir_, LABEL_AUTH, packet.getSeqNr(), key_);
94 #ifndef USE_SSL_CRYPTO
95 gcry_error_t err = gcry_md_setkey(handle_, key_.getBuf(), key_.getLength());
97 cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::setKey: Failed to set hmac key: " << AnytunGpgError(err);
101 gcry_md_reset(handle_);
102 gcry_md_write(handle_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
103 gcry_md_final(handle_);
104 uint8_t* hmac = gcry_md_read(handle_, 0);
106 HMAC_Init_ex(&ctx_, key_.getBuf(), key_.getLength(), EVP_sha1(), NULL);
108 uint8_t hmac[DIGEST_LENGTH];
109 HMAC_Update(&ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
110 HMAC_Final(&ctx_, hmac, NULL);
113 uint8_t* tag = packet.getAuthTag();
114 uint32_t length = (packet.getAuthTagLength() < DIGEST_LENGTH) ? packet.getAuthTagLength() : DIGEST_LENGTH;
116 if(length > DIGEST_LENGTH) {
117 std::memset(tag, 0, packet.getAuthTagLength());
120 std::memcpy(&tag[packet.getAuthTagLength() - length], &hmac[DIGEST_LENGTH - length], length);
123 bool Sha1AuthAlgo::checkTag(KeyDerivation& kd, EncryptedPacket& packet)
125 #ifndef USE_SSL_CRYPTO
131 packet.withAuthTag(true);
132 if(!packet.getAuthTagLength()) {
136 kd.generate(dir_, LABEL_AUTH, packet.getSeqNr(), key_);
137 #ifndef USE_SSL_CRYPTO
138 gcry_error_t err = gcry_md_setkey(handle_, key_.getBuf(), key_.getLength());
140 cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::setKey: Failed to set hmac key: " << AnytunGpgError(err);
144 gcry_md_reset(handle_);
145 gcry_md_write(handle_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
146 gcry_md_final(handle_);
147 uint8_t* hmac = gcry_md_read(handle_, 0);
149 HMAC_Init_ex(&ctx_, key_.getBuf(), key_.getLength(), EVP_sha1(), NULL);
151 uint8_t hmac[DIGEST_LENGTH];
152 HMAC_Update(&ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
153 HMAC_Final(&ctx_, hmac, NULL);
156 uint8_t* tag = packet.getAuthTag();
157 uint32_t length = (packet.getAuthTagLength() < DIGEST_LENGTH) ? packet.getAuthTagLength() : DIGEST_LENGTH;
159 if(length > DIGEST_LENGTH)
160 for(uint32_t i=0; i < (packet.getAuthTagLength() - DIGEST_LENGTH); ++i)
161 if(tag[i]) { return false; }
163 int ret = std::memcmp(&tag[packet.getAuthTagLength() - length], &hmac[DIGEST_LENGTH - length], length);
164 packet.removeAuthTag();