1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
* uAnytun
*
* uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
* featured implementation uAnytun has no support for multiple connections
* or synchronisation. It is a small single threaded implementation intended
* to act as a client on small platforms.
* The secure anycast tunneling protocol (satp) defines a protocol used
* for communication between any combination of unicast and anycast
* tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
* mode and allows tunneling of every ETHER TYPE protocol (e.g.
* ethernet, ip, arp ...). satp directly includes cryptography and
* message authentication based on the methodes used by SRTP. It is
* intended to deliver a generic, scaleable and secure solution for
* tunneling and relaying of packets of any protocol.
*
*
* Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
*
* This file is part of uAnytun.
*
* uAnytun is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* uAnytun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
*/
#include "datatypes.h"
#include "encrypted_packet.h"
#include "auth_algo.h"
#include "log.h"
#include <stdlib.h>
#include <string.h>
auth_algo_type_t auth_algo_get_type(const char* type)
{
if(!strcmp(type, "null"))
return aa_null;
else if(!strcmp(type, "sha1"))
return aa_sha1;
return aa_unknown;
}
u_int32_t auth_algo_get_max_length(const char* type)
{
switch(auth_algo_get_type(type)) {
case aa_null: return 0;
case aa_sha1: return SHA1_LENGTH;
default: return 0;
}
}
int auth_algo_init(auth_algo_t* aa, const char* type)
{
if(!aa)
return -1;
aa->type_ = auth_algo_get_type(type);
if(aa->type_ == aa_unknown) {
log_printf(ERROR, "unknown auth algo type");
return -1;
}
aa->params_ = NULL;
aa->key_.buf_ = NULL;
aa->key_.length_ = 0;
int ret = 0;
if(aa->type_ == aa_sha1)
ret = auth_algo_sha1_init(aa);
if(ret)
auth_algo_close(aa);
return ret;
}
void auth_algo_close(auth_algo_t* aa)
{
if(!aa)
return;
if(aa->type_ == aa_sha1)
auth_algo_sha1_close(aa);
if(aa->key_.buf_)
free(aa->key_.buf_);
}
void auth_algo_generate(auth_algo_t* aa, key_derivation_t* kd, key_derivation_dir_t dir, encrypted_packet_t* packet)
{
if(!aa)
return;
if(aa->type_ == aa_null)
return;
else if(aa->type_ == aa_sha1)
auth_algo_sha1_generate(aa, kd, dir, packet);
else {
log_printf(ERROR, "unknown auth algo type");
return;
}
}
int auth_algo_check_tag(auth_algo_t* aa, key_derivation_t* kd, key_derivation_dir_t dir, encrypted_packet_t* packet)
{
if(!aa)
return 0;
if(aa->type_ == aa_null)
return 1;
else if(aa->type_ == aa_sha1)
return auth_algo_sha1_check_tag(aa, kd, dir, packet);
else {
log_printf(ERROR, "unknown auth algo type");
return 0;
}
}
/* ---------------- HMAC Sha1 Auth Algo ---------------- */
int auth_algo_sha1_init(auth_algo_t* aa)
{
if(!aa)
return -1;
if(aa->key_.buf_)
free(aa->key_.buf_);
aa->key_.length_ = SHA1_LENGTH;
aa->key_.buf_ = malloc(aa->key_.length_);
if(!aa->key_.buf_)
return -2;
if(aa->params_)
free(aa->params_);
aa->params_ = malloc(sizeof(auth_algo_sha1_param_t));
if(!aa->params_)
return -2;
auth_algo_sha1_param_t* params = aa->params_;
#ifndef USE_SSL_CRYPTO
gcry_error_t err = gcry_md_open(¶ms->handle_, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
if(err) {
log_printf(ERROR, "failed to open message digest algo: %s", gcry_strerror(err));
return -1;
}
#else
HMAC_CTX_init(¶ms->ctx_);
HMAC_Init_ex(¶ms->ctx_, NULL, 0, EVP_sha1(), NULL);
#endif
return 0;
}
void auth_algo_sha1_close(auth_algo_t* aa)
{
if(!aa)
return;
if(aa->params_) {
auth_algo_sha1_param_t* params = aa->params_;
#ifndef USE_SSL_CRYPTO
if(params->handle_)
gcry_md_close(params->handle_);
#else
HMAC_CTX_cleanup(¶ms->ctx_);
#endif
free(aa->params_);
}
}
void auth_algo_sha1_generate(auth_algo_t* aa, key_derivation_t* kd, key_derivation_dir_t dir, encrypted_packet_t* packet)
{
if(!encrypted_packet_get_auth_tag_length(packet))
return;
if(!aa || !aa->params_) {
log_printf(ERROR, "auth algo not initialized");
return;
}
if(!kd) {
log_printf(ERROR, "no key derivation supplied");
return;
}
auth_algo_sha1_param_t* params = aa->params_;
int ret = key_derivation_generate(kd, dir, LABEL_AUTH, encrypted_packet_get_seq_nr(packet), aa->key_.buf_, aa->key_.length_);
if(ret < 0)
return;
#ifndef USE_SSL_CRYPTO
gcry_error_t err = gcry_md_setkey(params->handle_, aa->key_.buf_, aa->key_.length_);
if(err) {
log_printf(ERROR, "failed to set hmac key: %s", gcry_strerror(err));
return;
}
gcry_md_reset(params->handle_);
gcry_md_write(params->handle_, encrypted_packet_get_auth_portion(packet), encrypted_packet_get_auth_portion_length(packet));
gcry_md_final(params->handle_);
u_int8_t* hmac = gcry_md_read(params->handle_, 0);
#else
HMAC_Init_ex(¶ms->ctx_, aa->key_.buf_, aa->key_.length_, EVP_sha1(), NULL);
u_int8_t hmac[SHA1_LENGTH];
HMAC_Update(¶ms->ctx_, encrypted_packet_get_auth_portion(packet), encrypted_packet_get_auth_portion_length(packet));
HMAC_Final(¶ms->ctx_, hmac, NULL);
#endif
u_int8_t* tag = encrypted_packet_get_auth_tag(packet);
u_int32_t length = (encrypted_packet_get_auth_tag_length(packet) < SHA1_LENGTH) ? encrypted_packet_get_auth_tag_length(packet) : SHA1_LENGTH;
if(length > SHA1_LENGTH)
memset(tag, 0, encrypted_packet_get_auth_tag_length(packet));
memcpy(&tag[encrypted_packet_get_auth_tag_length(packet) - length], &hmac[SHA1_LENGTH - length], length);
}
int auth_algo_sha1_check_tag(auth_algo_t* aa, key_derivation_t* kd, key_derivation_dir_t dir, encrypted_packet_t* packet)
{
if(!encrypted_packet_get_auth_tag_length(packet))
return 1;
if(!aa || !aa->params_) {
log_printf(ERROR, "auth algo not initialized");
return 0;
}
if(!kd) {
log_printf(ERROR, "no key derivation supplied");
return 0;
}
auth_algo_sha1_param_t* params = aa->params_;
int ret = key_derivation_generate(kd, dir, LABEL_AUTH, encrypted_packet_get_seq_nr(packet), aa->key_.buf_, aa->key_.length_);
if(ret < 0)
return 0;
#ifndef USE_SSL_CRYPTO
gcry_error_t err = gcry_md_setkey(params->handle_, aa->key_.buf_, aa->key_.length_);
if(err) {
log_printf(ERROR, "failed to set hmac key: %s", gcry_strerror(err));
return -1;
}
gcry_md_reset(params->handle_);
gcry_md_write(params->handle_, encrypted_packet_get_auth_portion(packet), encrypted_packet_get_auth_portion_length(packet));
gcry_md_final(params->handle_);
u_int8_t* hmac = gcry_md_read(params->handle_, 0);
#else
HMAC_Init_ex(¶ms->ctx_, aa->key_.buf_, aa->key_.length_, EVP_sha1(), NULL);
u_int8_t hmac[SHA1_LENGTH];
HMAC_Update(¶ms->ctx_, encrypted_packet_get_auth_portion(packet), encrypted_packet_get_auth_portion_length(packet));
HMAC_Final(¶ms->ctx_, hmac, NULL);
#endif
u_int8_t* tag = encrypted_packet_get_auth_tag(packet);
u_int32_t length = (encrypted_packet_get_auth_tag_length(packet) < SHA1_LENGTH) ? encrypted_packet_get_auth_tag_length(packet) : SHA1_LENGTH;
if(length > SHA1_LENGTH) {
u_int32_t i;
for(i=0; i < (encrypted_packet_get_auth_tag_length(packet) - SHA1_LENGTH); ++i)
if(tag[i]) return 0;
}
int result = memcmp(&tag[encrypted_packet_get_auth_tag_length(packet) - length], &hmac[SHA1_LENGTH - length], length);
if(result)
return 0;
return 1;
}
|