New upstream version 0.3.7
[anytun.git] / src / authAlgo.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 methods 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-2014 Markus Grüneis, 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  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include "authAlgo.h"
47 #include "log.h"
48 #include "anytunError.h"
49 #include "buffer.h"
50 #include "encryptedPacket.h"
51
52 #include <iostream>
53 #include <cstring>
54
55 //****** NullAuthAlgo ******
56 void NullAuthAlgo::generate(KeyDerivation& kd, EncryptedPacket& packet)
57 {
58 }
59
60 bool NullAuthAlgo::checkTag(KeyDerivation& kd, EncryptedPacket& packet)
61 {
62   return true;
63 }
64
65 #ifndef NO_CRYPT
66 //****** Sha1AuthAlgo ******
67
68 Sha1AuthAlgo::Sha1AuthAlgo(kd_dir_t d) : AuthAlgo(d), key_(DIGEST_LENGTH)
69 {
70 #if defined(USE_SSL_CRYPTO)
71   ctx_ = NULL;
72 #elif defined(USE_NETTLE)
73   // nothing here
74 #else  // USE_GCRYPT is the default
75   handle_ = 0;
76 #endif
77 }
78
79 bool Sha1AuthAlgo::Init()
80 {
81 #if defined(USE_SSL_CRYPTO)
82 # if OPENSSL_VERSION_NUMBER >= 0x10100000L
83   if ((ctx_ = HMAC_CTX_new()) == NULL) {
84     return false;
85   }
86 # else
87   if ((ctx_ = (HMAC_CTX*)calloc(1, sizeof(HMAC_CTX))) == NULL) {
88     return false;
89   }
90   HMAC_CTX_init(ctx_);
91 # endif
92   HMAC_Init_ex(ctx_, NULL, 0, EVP_sha1(), NULL);
93 #elif defined(USE_NETTLE)
94   // nothing here
95 #else  // USE_GCRYPT is the default
96   gcry_error_t err = gcry_md_open(&handle_, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
97   if(err) {
98     cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::Sha1AuthAlgo: Failed to open message digest algo";
99     return false;
100   }
101 #endif
102   return true;
103 }
104
105 Sha1AuthAlgo::~Sha1AuthAlgo()
106 {
107 #if defined(USE_SSL_CRYPTO)
108   if(ctx_) {
109 # if OPENSSL_VERSION_NUMBER >= 0x10100000L
110     HMAC_CTX_free(ctx_);
111 # else
112     HMAC_CTX_cleanup(ctx_);
113     free(ctx_);
114 # endif
115   }
116 #elif defined(USE_NETTLE)
117   // nothing here
118 #else  // USE_GCRYPT is the default
119   if(handle_) {
120     gcry_md_close(handle_);
121   }
122 #endif
123 }
124
125 void Sha1AuthAlgo::generate(KeyDerivation& kd, EncryptedPacket& packet)
126 {
127 #if defined(USE_GCRYPT)
128   if(!handle_) {
129     return;
130   }
131 #endif
132
133   packet.addAuthTag();
134   if(!packet.getAuthTagLength()) {
135     return;
136   }
137
138   kd.generate(dir_, LABEL_AUTH, packet.getSeqNr(), key_);
139 #if defined(USE_SSL_CRYPTO)
140   HMAC_Init_ex(ctx_, key_.getBuf(), key_.getLength(), EVP_sha1(), NULL);
141
142   uint8_t hmac[DIGEST_LENGTH];
143   HMAC_Update(ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
144   HMAC_Final(ctx_, hmac, NULL);
145 #elif defined(USE_NETTLE)
146   hmac_sha1_set_key(&ctx_, key_.getLength(), key_.getBuf());
147
148   uint8_t hmac[DIGEST_LENGTH];
149   hmac_sha1_update(&ctx_, packet.getAuthenticatedPortionLength(), packet.getAuthenticatedPortion());
150   hmac_sha1_digest(&ctx_, DIGEST_LENGTH, hmac);
151 #else  // USE_GCRYPT is the default
152   gcry_error_t err = gcry_md_setkey(handle_, key_.getBuf(), key_.getLength());
153   if(err) {
154     cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::setKey: Failed to set hmac key: " << AnytunGpgError(err);
155     return;
156   }
157
158   gcry_md_reset(handle_);
159   gcry_md_write(handle_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
160   gcry_md_final(handle_);
161   uint8_t* hmac = gcry_md_read(handle_, 0);
162 #endif
163
164   uint8_t* tag = packet.getAuthTag();
165   uint32_t length = (packet.getAuthTagLength() < DIGEST_LENGTH) ? packet.getAuthTagLength() : DIGEST_LENGTH;
166
167   if(length > DIGEST_LENGTH) {
168     std::memset(tag, 0, packet.getAuthTagLength());
169   }
170
171   std::memcpy(&tag[packet.getAuthTagLength() - length], &hmac[DIGEST_LENGTH - length], length);
172 }
173
174 bool Sha1AuthAlgo::checkTag(KeyDerivation& kd, EncryptedPacket& packet)
175 {
176 #if defined(USE_GCRYPT)
177   if(!handle_) {
178     return false;
179   }
180 #endif
181
182   packet.withAuthTag(true);
183   if(!packet.getAuthTagLength()) {
184     return true;
185   }
186
187   kd.generate(dir_, LABEL_AUTH, packet.getSeqNr(), key_);
188 #if defined(USE_SSL_CRYPTO)
189   HMAC_Init_ex(ctx_, key_.getBuf(), key_.getLength(), EVP_sha1(), NULL);
190
191   uint8_t hmac[DIGEST_LENGTH];
192   HMAC_Update(ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
193   HMAC_Final(ctx_, hmac, NULL);
194 #elif defined(USE_NETTLE)
195   hmac_sha1_set_key(&ctx_, key_.getLength(), key_.getBuf());
196
197   uint8_t hmac[DIGEST_LENGTH];
198   hmac_sha1_update(&ctx_, packet.getAuthenticatedPortionLength(), packet.getAuthenticatedPortion());
199   hmac_sha1_digest(&ctx_, DIGEST_LENGTH, hmac);
200 #else  // USE_GCRYPT is the default
201   gcry_error_t err = gcry_md_setkey(handle_, key_.getBuf(), key_.getLength());
202   if(err) {
203     cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::setKey: Failed to set hmac key: " << AnytunGpgError(err);
204     return false;
205   }
206
207   gcry_md_reset(handle_);
208   gcry_md_write(handle_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
209   gcry_md_final(handle_);
210   uint8_t* hmac = gcry_md_read(handle_, 0);
211 #endif
212
213   uint8_t* tag = packet.getAuthTag();
214   uint32_t length = (packet.getAuthTagLength() < DIGEST_LENGTH) ? packet.getAuthTagLength() : DIGEST_LENGTH;
215
216   if(length > DIGEST_LENGTH)
217     for(uint32_t i=0; i < (packet.getAuthTagLength() - DIGEST_LENGTH); ++i)
218       if(tag[i]) { return false; }
219
220   int ret = std::memcmp(&tag[packet.getAuthTagLength() - length], &hmac[DIGEST_LENGTH - length], length);
221   packet.removeAuthTag();
222
223   if(ret) {
224     return false;
225   }
226
227   return true;
228 }
229
230 #endif