Imported Upstream version 0.3
[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 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.
12  *
13  *
14  *  Copyright (C) 2007-2008 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 version 3 as
21  *  published by the Free Software Foundation.
22  *
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.
27  *
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/>.
30  */
31
32 #include "authAlgo.h"
33 #include "log.h"
34 #include "anytunError.h"
35 #include "buffer.h"
36 #include "encryptedPacket.h"
37
38 #include <iostream>
39 #include <cstring>
40
41 //****** NullAuthAlgo ******
42 void NullAuthAlgo::generate(KeyDerivation& kd, EncryptedPacket& packet)
43 {
44 }
45
46 bool NullAuthAlgo::checkTag(KeyDerivation& kd, EncryptedPacket& packet)
47 {
48   return true;
49 }
50
51 #ifndef NO_CRYPT
52 //****** Sha1AuthAlgo ******
53
54 Sha1AuthAlgo::Sha1AuthAlgo(kd_dir_t d) : AuthAlgo(d), key_(DIGEST_LENGTH)
55 {
56 #ifndef USE_SSL_CRYPTO
57   gcry_error_t err = gcry_md_open(&handle_, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
58   if(err) {
59     cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::Sha1AuthAlgo: Failed to open message digest algo";
60     return;
61   } 
62 #else
63   HMAC_CTX_init(&ctx_);
64   HMAC_Init_ex(&ctx_, NULL, 0, EVP_sha1(), NULL);
65 #endif
66 }
67
68 Sha1AuthAlgo::~Sha1AuthAlgo()
69 {
70 #ifndef USE_SSL_CRYPTO
71   if(handle_)
72     gcry_md_close(handle_);
73 #else
74   HMAC_CTX_cleanup(&ctx_);
75 #endif    
76 }
77
78 void Sha1AuthAlgo::generate(KeyDerivation& kd, EncryptedPacket& packet)
79 {
80 #ifndef USE_SSL_CRYPTO
81   if(!handle_)
82     return;
83 #endif
84
85   packet.addAuthTag();
86   if(!packet.getAuthTagLength())
87     return;
88   
89   kd.generate(dir_, LABEL_AUTH, packet.getSeqNr(), key_);
90 #ifndef USE_SSL_CRYPTO
91   gcry_error_t err = gcry_md_setkey(handle_, key_.getBuf(), key_.getLength());
92   if(err) {
93     cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::setKey: Failed to set hmac key: " << AnytunGpgError(err);
94     return;
95   } 
96
97   gcry_md_reset(handle_);
98   gcry_md_write(handle_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
99   gcry_md_final(handle_);
100   u_int8_t* hmac = gcry_md_read(handle_, 0);
101 #else
102   HMAC_Init_ex(&ctx_, key_.getBuf(), key_.getLength(), EVP_sha1(), NULL);
103
104   u_int8_t hmac[DIGEST_LENGTH];
105   HMAC_Update(&ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
106   HMAC_Final(&ctx_, hmac, NULL);
107 #endif
108
109   u_int8_t* tag = packet.getAuthTag();
110   u_int32_t length = (packet.getAuthTagLength() < DIGEST_LENGTH) ? packet.getAuthTagLength() : DIGEST_LENGTH;
111
112   if(length > DIGEST_LENGTH)
113     std::memset(tag, 0, packet.getAuthTagLength());
114
115   std::memcpy(&tag[packet.getAuthTagLength() - length], &hmac[DIGEST_LENGTH - length], length);
116 }
117
118 bool Sha1AuthAlgo::checkTag(KeyDerivation& kd, EncryptedPacket& packet)
119 {
120 #ifndef USE_SSL_CRYPTO
121   if(!handle_)
122     return false;
123 #endif
124
125   packet.withAuthTag(true);
126   if(!packet.getAuthTagLength())
127     return true;
128
129   kd.generate(dir_, LABEL_AUTH, packet.getSeqNr(), key_);
130 #ifndef USE_SSL_CRYPTO
131   gcry_error_t err = gcry_md_setkey(handle_, key_.getBuf(), key_.getLength());
132   if(err) {
133     cLog.msg(Log::PRIO_ERROR) << "Sha1AuthAlgo::setKey: Failed to set hmac key: " << AnytunGpgError(err);
134     return false;
135   } 
136   
137   gcry_md_reset(handle_);
138   gcry_md_write(handle_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
139   gcry_md_final(handle_);
140   u_int8_t* hmac = gcry_md_read(handle_, 0);
141 #else
142   HMAC_Init_ex(&ctx_, key_.getBuf(), key_.getLength(), EVP_sha1(), NULL);
143   
144   u_int8_t hmac[DIGEST_LENGTH];
145   HMAC_Update(&ctx_, packet.getAuthenticatedPortion(), packet.getAuthenticatedPortionLength());
146   HMAC_Final(&ctx_, hmac, NULL);
147 #endif
148
149   u_int8_t* tag = packet.getAuthTag();
150   u_int32_t length = (packet.getAuthTagLength() < DIGEST_LENGTH) ? packet.getAuthTagLength() : DIGEST_LENGTH;
151
152   if(length > DIGEST_LENGTH)
153     for(u_int32_t i=0; i < (packet.getAuthTagLength() - DIGEST_LENGTH); ++i)
154       if(tag[i]) return false;
155
156   int ret = std::memcmp(&tag[packet.getAuthTagLength() - length], &hmac[DIGEST_LENGTH - length], length);
157   packet.removeAuthTag();
158   
159   if(ret)
160     return false;
161
162   return true;
163
164 }
165
166 #endif
167