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
|
/*
* anytun
*
* 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 Othmar Gsenger, Erwin Nindl,
* Christian Pointner <satp@wirdorange.org>
*
* This file is part of Anytun.
*
* Anytun 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.
*
* Anytun 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 anytun. If not, see <http://www.gnu.org/licenses/>.
*/
#include "log.h"
#include "keyDerivation.h"
#include "threadUtils.hpp"
#include "datatypes.h"
#include "endian.h"
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <string>
void KeyDerivation::setLogKDRate(const int8_t log_rate)
{
WritersLock lock(mutex_);
ld_kdr_ = log_rate;
if(ld_kdr_ > (int8_t)(sizeof(seq_nr_t) * 8))
ld_kdr_ = sizeof(seq_nr_t) * 8;
}
//****** NullKeyDerivation ******
bool NullKeyDerivation::generate(kd_dir dir, satp_prf_label label, seq_nr_t seq_nr, Buffer& key)
{
std::memset(key.getBuf(), 0, key.getLength());
return true;
}
#ifndef NOCRYPT
//****** AesIcmKeyDerivation ******
AesIcmKeyDerivation::AesIcmKeyDerivation() : KeyDerivation(DEFAULT_KEY_LENGTH)
{
#ifndef USE_SSL_CRYPTO
for(int i=0; i<2; i++)
handle_[i] = NULL;
#endif
}
AesIcmKeyDerivation::AesIcmKeyDerivation(u_int16_t key_length) : KeyDerivation(key_length)
{
#ifndef USE_SSL_CRYPTO
for(int i=0; i<2; i++)
handle_[i] = NULL;
#endif
}
AesIcmKeyDerivation::~AesIcmKeyDerivation()
{
WritersLock lock(mutex_);
#ifndef USE_SSL_CRYPTO
for(int i=0; i<2; i++)
if(handle_[i])
gcry_cipher_close(handle_[i]);
#endif
}
void AesIcmKeyDerivation::init(Buffer key, Buffer salt)
{
WritersLock lock(mutex_);
master_salt_ = SyncBuffer(salt);
master_key_ = SyncBuffer(key);
updateMasterKey();
}
void AesIcmKeyDerivation::updateMasterKey()
{
if(master_key_.getLength()*8 != key_length_) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_CRIT) << "KeyDerivation::updateMasterKey: key lengths don't match";
return;
}
if(master_salt_.getLength() != SALT_LENGTH) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_CRIT) << "KeyDerivation::updateMasterKey: salt lengths don't match";
return;
}
#ifndef USE_SSL_CRYPTO
int algo;
switch(key_length_) {
case 128: algo = GCRY_CIPHER_AES128; break;
case 192: algo = GCRY_CIPHER_AES192; break;
case 256: algo = GCRY_CIPHER_AES256; break;
default: {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_CRIT) << "KeyDerivation::updateMasterKey: cipher key length of " << key_length_ << " Bits is not supported";
return;
}
}
for(int i=0; i<2; i++) {
if(handle_[i])
gcry_cipher_close(handle_[i]);
gcry_error_t err = gcry_cipher_open(&handle_[i], algo, GCRY_CIPHER_MODE_CTR, 0);
if(err) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::updateMasterKey: Failed to open cipher: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
return;
}
err = gcry_cipher_setkey(handle_[i], master_key_.getBuf(), master_key_.getLength());
if(err) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::updateMasterKey: Failed to set cipher key: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
return;
}
}
#else
for(int i=0; i<2; i++) {
int ret = AES_set_encrypt_key(master_key_.getBuf(), master_key_.getLength()*8, &aes_key_[i]);
if(ret) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::updateMasterKey: Failed to set ssl key (code: " << ret << ")";
return;
}
}
#endif
}
std::string AesIcmKeyDerivation::printType()
{
ReadersLock lock(mutex_);
std::stringstream sstr;
sstr << "AesIcm" << key_length_ << "KeyDerivation";
return sstr.str();
}
bool AesIcmKeyDerivation::calcCtr(kd_dir dir, seq_nr_t* r, satp_prf_label label, seq_nr_t seq_nr)
{
*r = 0;
if(ld_kdr_ >= 0)
*r = seq_nr >> ld_kdr_;
// TODO: determine whether to generate a key or not
// if(key_store_[dir][label].key_.getBuf() && key_store_[dir][label].r_ == *r) {
// if(!(*r) || (seq_nr % (*r)))
// return false;
// }
if(master_salt_.getLength() != SALT_LENGTH) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_CRIT) << "KeyDerivation::calcCtr: salt lengths don't match";
return false;
}
memcpy(ctr_[dir].salt_.buf_, master_salt_.getBuf(), SALT_LENGTH);
ctr_[dir].salt_.zero_ = 0;
ctr_[dir].params_.label_ ^= label;
ctr_[dir].params_.r_ ^= SEQ_NR_T_HTON(*r);
return true;
}
bool AesIcmKeyDerivation::generate(kd_dir dir, satp_prf_label label, seq_nr_t seq_nr, Buffer& key)
{
ReadersLock lock(mutex_);
#ifndef USE_SSL_CRYPTO
if(!handle_[dir])
return false;
#endif
seq_nr_t r;
calcCtr(dir, &r, label, seq_nr);
// TODO: return stored key
// bool result = calcCtr(dir, &r, label, seq_nr);
// if(!result) {
// if(len > kd->key_store_[dir][label].key_.length_) {
// log_printf(WARNING, "stored (old) key for label 0x%02X is too short, filling with zeros", label);
// memset(key, 0, len);
// len = kd->key_store_[dir][label].key_.length_;
// }
// memcpy(key, kd->key_store_[dir][label].key_.buf_, len);
// return false;
// }
#ifndef USE_SSL_CRYPTO
gcry_error_t err = gcry_cipher_reset(handle_[dir]);
if(err) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::generate: Failed to reset cipher: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
}
err = gcry_cipher_setctr(handle_[dir], ctr_[dir].buf_, CTR_LENGTH);
if(err) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::generate: Failed to set CTR: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
return false;
}
std::memset(key.getBuf(), 0, key.getLength());
err = gcry_cipher_encrypt(handle_[dir], key, key.getLength(), NULL, 0);
if(err) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_ERR) << "KeyDerivation::generate: Failed to generate cipher bitstream: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX);
}
return true;
#else
if(CTR_LENGTH != AES_BLOCK_SIZE) {
char buf[STERROR_TEXT_MAX];
buf[0] = 0;
cLog.msg(Log::PRIO_ERR) << "AesIcmCipher: Failed to set cipher CTR: size don't fits";
return false;
}
u_int32_t num = 0;
std::memset(ecount_buf_[dir], 0, AES_BLOCK_SIZE);
std::memset(key.getBuf(), 0, key.getLength());
AES_ctr128_encrypt(key.getBuf(), key.getBuf(), key.getLength(), &aes_key_[dir], ctr_[dir].buf_, ecount_buf_[dir], &num);
#endif
// TODO: store key if key derivation rate is != 0
// if(!ld_kdr_)
// return true;
// if(!kd->key_store_[dir][label].key_.buf_) {
// kd->key_store_[dir][label].key_.length_ = 0;
// kd->key_store_[dir][label].key_.buf_ = malloc(len);
// if(!kd->key_store_[dir][label].key_.buf_)
// return -2;
// kd->key_store_[dir][label].key_.length_ = len;
// }
// else if(kd->key_store_[dir][label].key_.length_ < len) {
// u_int8_t* tmp = realloc(kd->key_store_[dir][label].key_.buf_, len);
// if(!tmp)
// return -2;
// kd->key_store_[dir][label].key_.buf_ = tmp;
// kd->key_store_[dir][label].key_.length_ = len;
// }
// memcpy(kd->key_store_[dir][label].key_.buf_, key, len);
// kd->key_store_[dir][label].r_ = r;
return true;
}
#endif
|