-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.hpp
executable file
·350 lines (294 loc) · 8.87 KB
/
key.hpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// anynet
// Copyright (C) 2009 Steven Siloti
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects
// for all of the code used other than OpenSSL. If you modify
// file(s) with this exception, you may extend this exception to your
// version of the file(s), but you are not obligated to do so. If you
// do not wish to do so, delete this exception statement from your
// version. If you delete this exception statement from all source
// files in the program, then also delete it here.
//
// Contact: Steven Siloti <ssiloti@gmail.com>
#ifndef KEY_HPP
#define KEY_HPP
#include <glog/logging.h>
#include "field_utils.hpp"
#include "core.hpp"
#include <openssl/x509.h>
#include <openssl/sha.h>
#include <boost/asio/buffer.hpp>
#include <boost/operators.hpp>
#include <boost/cstdint.hpp>
#include <cstring>
#include <sstream>
#include <iomanip>
using boost::asio::const_buffer;
// A convenient interface to the network hash function (SHA256 as of protocol version 0)
class net_hash
{
public:
typedef boost::array<boost::uint8_t, SHA256_DIGEST_LENGTH> digest_t;
net_hash() { ::SHA256_Init(&ctx_); }
net_hash(const_buffer b)
{
::SHA256_Init(&ctx_);
update(b);
}
template <std::size_t N>
net_hash(const boost::array<boost::uint8_t, N>& d)
{
::SHA256_Init(&ctx_);
update(d);
}
operator digest_t() const
{
return final();
}
template <std::size_t N>
void update(const boost::array<boost::uint8_t, N>& d)
{
::SHA256_Update(&ctx_, d.data(), d.size());
}
void update(const digest_t& d)
{
::SHA256_Update(&ctx_, d.data(), d.size());
}
void update(const std::vector<boost::uint8_t>& d)
{
::SHA256_Update(&ctx_, &d[0], d.size());
}
void update(const_buffer b)
{
::SHA256_Update(&ctx_, buffer_cast<const void*>(b), buffer_size(b));
}
digest_t final() const
{
digest_t d;
::SHA256_CTX c(ctx_);
::SHA256_Final(d.data(), &c);
return d;
}
private:
::SHA256_CTX ctx_;
};
class network_key : boost::totally_ordered<network_key, boost::additive<network_key, boost::additive<network_key, unsigned int> > >
{
typedef boost::uint64_t intermediate_t;
public:
const static int packed_size = 32;
typedef boost::uint32_t digit_t;
network_key()
{}
explicit network_key(ip::tcp::endpoint ep)
{
if (ep.address().is_v4()) {
boost::uint8_t id[ip::address_v4::bytes_type::static_size + 2];
ip::address_v4::bytes_type bytes(ep.address().to_v4().to_bytes());
std::memcpy(id, bytes.data(), bytes.size());
u16(&id[bytes.size()], ep.port());
SHA256(id, bytes.size() + 2, reinterpret_cast<unsigned char*>(digits_));
}
else {
boost::uint8_t id[ip::address_v6::bytes_type::static_size + 2];
ip::address_v6::bytes_type bytes(ep.address().to_v6().to_bytes());
std::memcpy(id, bytes.data(), bytes.size());
u16(&id[bytes.size()], ep.port());
SHA256(id, bytes.size() + 2, reinterpret_cast<unsigned char*>(digits_));
}
for (int i=0;i<digit_elements;++i)
digits_[i] = u32(reinterpret_cast<unsigned char*>(&digits_[i]));
}
explicit network_key(::X509* cert)
{
::X509_pubkey_digest(cert, ::EVP_sha256(), reinterpret_cast<unsigned char*>(digits_), NULL);
for (int i=0;i<digit_elements;++i)
digits_[i] = u32(reinterpret_cast<unsigned char*>(&digits_[i]));
}
explicit network_key(const_buffer buf)
{
hash_of(buf);
}
explicit network_key(const boost::uint8_t* buf)
{
decode(buf);
}
network_key(const net_hash& hash)
{
decode(net_hash::digest_t(hash).data());
}
explicit network_key(std::istream& strm)
{
for (int i=0;i<digit_elements;++i) {
strm >> digits_[i];
}
}
network_key(std::string s)
{
for (int i=0;i<digit_elements;++i) {
std::stringstream ss(s.substr(i * sizeof(boost::uint32_t) * 2, sizeof(boost::uint32_t) * 2));
ss >> std::hex >> digits_[i];
}
}
void decode(const boost::uint8_t* buf)
{
for (int i=0;i<digit_elements;++i) {
digits_[i] = u32(buf);
buf += sizeof(boost::uint32_t);
}
}
boost::uint8_t* encode(boost::uint8_t* p) const
{
for (int i=0;i<digit_elements;++i) {
u32(p, digits_[i]);
p += sizeof(boost::uint32_t);
}
return p;
}
bool operator==(const network_key& o) const
{
return std::memcmp(digits_, o.digits_, packed_size) == 0;
}
bool operator<(const network_key& o) const
{
for (int i=0;i<digit_elements;++i)
if (digits_[i] < o.digits_[i])
return true;
else if (digits_[i] > o.digits_[i])
return false;
return false;
}
network_key& operator-=(const network_key& o)
{
// We don't care about underflow here, the result will still be what we wanted
// For now use the naive solution, it should be fast enough for our needs.
intermediate_t t = 0;
for (int i = digit_elements-1; i >= 0; --i) {
t = intermediate_t(digits_[i]) - o.digits_[i] - t;
digits_[i] = digit_t(t);
t >>= 63;
}
return *this;
}
network_key& operator-=(unsigned int o)
{
// We don't care about underflow here, the result will still be what we wanted
// For now use the naive solution, it should be fast enough for our needs.
intermediate_t t = o;
for (int i = digit_elements-1; i >= 0; --i) {
t = intermediate_t(digits_[i]) - t;
digits_[i] = digit_t(t);
t >>= 63;
}
return *this;
}
network_key& operator+=(const network_key& o)
{
// We don't care about overflow here, the result will still be what we wanted
// For now use the naive solution, it should be fast enough for our needs.
intermediate_t t = 0;
for (int i = digit_elements-1; i >= 0; --i) {
t = intermediate_t(digits_[i]) + o.digits_[i] + t;
digits_[i] = digit_t(t);
t >>= 32;
}
return *this;
}
network_key& operator+=(unsigned int o)
{
// We don't care about overflow here, the result will still be what we wanted
// For now use the naive solution, it should be fast enough for our needs.
intermediate_t t = o;
for (int i = digit_elements-1; i >= 0; --i) {
t = intermediate_t(digits_[i]) + t;
digits_[i] = digit_t(t);
t >>= 32;
}
return *this;
}
network_key& operator/=(digit_t o)
{
intermediate_t remainder = 0;
for (int i=0; i < digit_elements-1; ++i) {
intermediate_t dividend = (remainder << sizeof(digit_t) * 8) + digits_[i];
digits_[i] = digit_t(dividend / o);
remainder = dividend % o;
}
return *this;
}
network_key operator/(digit_t o)
{
network_key t(*this);
t /= o;
return t;
}
double operator/(const network_key& o)
{
const int double_digits = sizeof(boost::uint64_t) / sizeof(digit_t);
boost::uint64_t dividend = 0;
boost::uint64_t divisor = 0;
int first_digit = digit_elements - 1;
while (digits_[first_digit] == 0 && o.digits_[first_digit] == 0 && first_digit >= double_digits)
--first_digit;
for (int i=0; i < double_digits; ++i)
dividend |= boost::uint64_t(digits_[i]) << ((double_digits - 1 - i) * sizeof(digit_t) * 8);
for (int i=0; i < double_digits; ++i)
divisor |= boost::uint64_t(o.digits_[i]) << ((double_digits - 1 - i) * sizeof(digit_t) * 8);
// the divisor might be small enough that we truncated it to zero
// in any case just return 0.0
if (divisor == 0)
return 0;
return double(dividend) / double(divisor);
}
void hash_of(const_buffer buf)
{
SHA256(buffer_cast<const unsigned char*>(buf), buffer_size(buf), reinterpret_cast<unsigned char*>(digits_));
for (int i=0;i<digit_elements;++i)
digits_[i] = u32(reinterpret_cast<unsigned char*>(&digits_[i]));
}
network_key& operator=(const boost::uint8_t* buf)
{
decode(buf);
return *this;
}
operator std::string() const
{
std::stringstream string;
string << std::hex << std::setw(sizeof(digit_t)*2) << std::setfill('0');
for (int i=0;i<digit_elements;++i)
string << digits_[i];
return string.str();
}
private:
static const int digit_elements = packed_size / sizeof(digit_t);
digit_t digits_[digit_elements];
};
inline network_key distance(const network_key& src, const network_key& dest)
{
return src - dest;
}
inline network_key reverse_distance(const network_key& src, const network_key& dest)
{
return dest - src;
}
#endif