2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
29 static struct taia tolerance_taia
= {
35 struct curve25519_proto
{
36 unsigned char enonce
[crypto_box_noncebytes
] __aligned_16
;
37 unsigned char dnonce
[crypto_box_noncebytes
] __aligned_16
;
38 unsigned char key
[crypto_box_noncebytes
] __aligned_16
;
41 struct curve25519_struct
{
42 unsigned char *enc_buf
;
43 unsigned char *dec_buf
;
46 struct spinlock enc_lock
;
47 struct spinlock dec_lock
;
50 extern void curve25519_selftest(void);
51 extern void curve25519_alloc_or_maybe_die(struct curve25519_struct
*curve
);
52 extern void curve25519_free(void *curve
);
53 extern int curve25519_pubkey_hexparse_32(unsigned char *bin
, size_t blen
, const char *ascii
, size_t alen
);
54 extern int curve25519_proto_init(struct curve25519_proto
*proto
, unsigned char *pubkey_remote
, size_t len
,
55 char *home
, int server
);
56 extern ssize_t
curve25519_encode(struct curve25519_struct
*curve
, struct curve25519_proto
*proto
,
57 unsigned char *plaintext
, size_t size
, unsigned char **chipertext
);
58 extern ssize_t
curve25519_decode(struct curve25519_struct
*curve
, struct curve25519_proto
*proto
,
59 unsigned char *chipertext
, size_t size
, unsigned char **plaintext
,
60 struct taia
*arrival_taia
);
62 static inline void tai_pack(unsigned char *s
, struct tai
*t
)
67 s
[7] = x
& 255; x
>>= 8;
68 s
[6] = x
& 255; x
>>= 8;
69 s
[5] = x
& 255; x
>>= 8;
70 s
[4] = x
& 255; x
>>= 8;
71 s
[3] = x
& 255; x
>>= 8;
72 s
[2] = x
& 255; x
>>= 8;
73 s
[1] = x
& 255; x
>>= 8;
77 static inline void tai_unpack(unsigned char *s
, struct tai
*t
)
81 x
= (unsigned char) s
[0];
82 x
<<= 8; x
+= (unsigned char) s
[1];
83 x
<<= 8; x
+= (unsigned char) s
[2];
84 x
<<= 8; x
+= (unsigned char) s
[3];
85 x
<<= 8; x
+= (unsigned char) s
[4];
86 x
<<= 8; x
+= (unsigned char) s
[5];
87 x
<<= 8; x
+= (unsigned char) s
[6];
88 x
<<= 8; x
+= (unsigned char) s
[7];
92 static inline void taia_pack(unsigned char *s
, struct taia
*t
)
99 s
[7] = x
& 255; x
>>= 8;
100 s
[6] = x
& 255; x
>>= 8;
101 s
[5] = x
& 255; x
>>= 8;
104 s
[3] = x
& 255; x
>>= 8;
105 s
[2] = x
& 255; x
>>= 8;
106 s
[1] = x
& 255; x
>>= 8;
110 static inline void taia_unpack(unsigned char *s
, struct taia
*t
)
114 tai_unpack(s
, &t
->sec
);
116 x
= (unsigned char) s
[4];
117 x
<<= 8; x
+= (unsigned char) s
[5];
118 x
<<= 8; x
+= (unsigned char) s
[6];
119 x
<<= 8; x
+= (unsigned char) s
[7];
121 x
= (unsigned char) s
[0];
122 x
<<= 8; x
+= (unsigned char) s
[1];
123 x
<<= 8; x
+= (unsigned char) s
[2];
124 x
<<= 8; x
+= (unsigned char) s
[3];
128 #define tai_unix(t, u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
130 static inline void taia_now(struct taia
*t
)
134 gettimeofday(&now
, NULL
);
136 tai_unix(&t
->sec
, now
.tv_sec
);
137 t
->nano
= 1000 * now
.tv_usec
+ 500;
141 static inline void taia_sub(struct taia
*res
, const struct taia
*u
, const struct taia
*v
)
143 unsigned long unano
= u
->nano
;
144 unsigned long uatto
= u
->atto
;
146 res
->sec
.x
= u
->sec
.x
- v
->sec
.x
;
147 res
->nano
= unano
- v
->nano
;
148 res
->atto
= uatto
- v
->atto
;
150 if (res
->atto
> uatto
) {
151 res
->atto
+= 1000000000UL;
155 if (res
->nano
> unano
) {
156 res
->nano
+= 1000000000UL;
161 static inline void taia_add(struct taia
*res
, const struct taia
*u
, const struct taia
*v
)
163 res
->sec
.x
= u
->sec
.x
+ v
->sec
.x
;
164 res
->nano
= u
->nano
+ v
->nano
;
165 res
->atto
= u
->atto
+ v
->atto
;
167 if (res
->atto
> 999999999UL) {
168 res
->atto
-= 1000000000UL;
172 if (res
->nano
> 999999999UL) {
173 res
->nano
-= 1000000000UL;
178 static inline int taia_less(const struct taia
*t
, const struct taia
*u
)
180 if (t
->sec
.x
< u
->sec
.x
)
182 if (t
->sec
.x
> u
->sec
.x
)
184 if (t
->nano
< u
->nano
)
186 if (t
->nano
> u
->nano
)
188 return t
->atto
< u
->atto
;
191 static inline int is_good_taia(struct taia
*arrival_taia
, struct taia
*packet_taia
)
196 if (taia_less(arrival_taia
, packet_taia
)) {
197 taia_sub(&sub_res
, packet_taia
, arrival_taia
);
198 if (taia_less(&sub_res
, &tolerance_taia
))
203 taia_sub(&sub_res
, arrival_taia
, packet_taia
);
204 if (taia_less(&sub_res
, &tolerance_taia
))