forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyn-cookie.c
172 lines (150 loc) · 4.25 KB
/
syn-cookie.c
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
#include "syn-cookie.h"
#include "pixie-timer.h"
#include "string_s.h"
#include "crypto-siphash24.h"
#include <assert.h>
#include <time.h>
#include <stdarg.h>
#if defined(_MSC_VER)
#include <intrin.h>
#endif
/***************************************************************************
* Go gather some entropy (aka. randmoness) to seed hashing with.
*
* NOTE: Mostly it's here to amuse cryptographers with its lulz.
***************************************************************************/
uint64_t
get_entropy(void)
{
uint64_t entropy[2] = {0,0};
unsigned i;
int err;
/*
* Gather some random bits
*/
for (i=0; i<64; i++) {
FILE *fp;
entropy[0] += pixie_nanotime();
#if defined(_MSC_VER)
entropy[0] ^= __rdtsc();
#endif
time(0);
err = fopen_s(&fp, "/", "r");
entropy[1] <<= 1;
entropy[1] |= entropy[0]>>63;
entropy[0] <<= 1;
if (err == 0 && fp) {
fclose(fp);
}
}
entropy[0] ^= time(0);
#if defined(__linux__)
{
FILE *fp;
err = fopen_s(&fp, "/dev/urandom", "r");
if (err == 0 && fp) {
int x;
uint64_t urand = 0;
x = fread(&urand, 1, sizeof(urand), fp);
entropy[0] ^= urand;
entropy[0] ^= x;
x = fread(&urand, 1, sizeof(urand), fp);
entropy[1] ^= urand;
entropy[1] ^= x;
fclose(fp);
}
entropy[0] ^= pixie_nanotime();
}
#endif
return entropy[0] ^ entropy[1];
}
#if 0
/***************************************************************************
* This implements the "murmur" hash function.
***************************************************************************/
static unsigned
murmur(uint64_t entropy, ...)
{
/* reference:
* http://en.wikipedia.org/wiki/MurmurHash
*/
static const unsigned c1 = 0xcc9e2d51;
static const unsigned c2 = 0x1b873593;
unsigned r1 = 15;
unsigned r2 = 13;
unsigned m = 5;
unsigned n = 0xe6546b64;
va_list key;
unsigned len;
unsigned hash = (unsigned)entropy;
va_start(key, entropy);
for (len=0; len<2; len++) {
unsigned k = va_arg(key, unsigned);
k = k * c1;
k = (k << r1) | (k >> (32-r1));
k = k * c2;
hash = hash ^ k;
hash = (hash << r2) | (hash >> (32-r2));
hash = hash * m + n;
}
hash = hash ^ (len*4);
hash = hash ^ (hash >> 16);
hash = hash * 0x85ebca6b;
hash = hash ^ (hash >> 13);
hash = hash * 0xc2b2ae35;
hash = hash ^ (hash >> 16);
return hash;
}
#endif
/***************************************************************************
***************************************************************************/
uint64_t
syn_cookie( ipaddress ip_them, unsigned port_them,
ipaddress ip_me, unsigned port_me,
uint64_t entropy)
{
switch (ip_them.version) {
case 4:
return syn_cookie_ipv4(ip_them.ipv4, port_them, ip_me.ipv4, port_me, entropy);
case 6:
return syn_cookie_ipv6(ip_them.ipv6, port_them, ip_me.ipv6, port_me, entropy);
default:
assert(!"unknown ip version");
return 0;
}
}
/***************************************************************************
***************************************************************************/
uint64_t
syn_cookie_ipv4( unsigned ip_them, unsigned port_them,
unsigned ip_me, unsigned port_me,
uint64_t entropy)
{
unsigned data[4];
uint64_t x[2];
x[0] = entropy;
x[1] = entropy;
data[0] = ip_them;
data[1] = port_them;
data[2] = ip_me;
data[3] = port_me;
return siphash24(data, sizeof(data), x);
}
/***************************************************************************
***************************************************************************/
uint64_t
syn_cookie_ipv6( ipv6address ip_them, unsigned port_them,
ipv6address ip_me, unsigned port_me,
uint64_t entropy)
{
uint64_t data[5];
uint64_t x[2];
x[0] = entropy;
x[1] = entropy;
data[0] = ip_them.hi;
data[1] = ip_them.lo;
data[2] = ip_me.hi;
data[3] = ip_me.lo;
data[4] = port_them<<16ULL | port_me;
return siphash24(data, sizeof(data), x);
}