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