2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
16 #include "crypto_box_curve25519xsalsa20poly1305.h"
28 static struct taia tolerance_taia
= {
34 #define crypto_box_zerobytes crypto_box_curve25519xsalsa20poly1305_ZEROBYTES
35 #define crypto_box_boxzerobytes crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES
37 #define crypto_box_noncebytes crypto_box_curve25519xsalsa20poly1305_NONCEBYTES
38 #define crypto_box_beforenmbytes crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES
40 struct curve25519_proto
{
41 unsigned char enonce
[crypto_box_noncebytes
] __aligned_16
;
42 unsigned char dnonce
[crypto_box_noncebytes
] __aligned_16
;
43 unsigned char key
[crypto_box_noncebytes
] __aligned_16
;
46 struct curve25519_struct
{
47 unsigned char *enc_buf
;
48 unsigned char *dec_buf
;
51 struct spinlock enc_lock
;
52 struct spinlock dec_lock
;
55 extern void curve25519_selftest(void);
56 extern void curve25519_alloc_or_maybe_die(struct curve25519_struct
*curve
);
57 extern void curve25519_free(void *curve
);
58 extern int curve25519_pubkey_hexparse_32(unsigned char *bin
, size_t blen
, const char *ascii
, size_t alen
);
59 extern int curve25519_proto_init(struct curve25519_proto
*proto
, unsigned char *pubkey_remote
, size_t len
,
60 char *home
, int server
);
61 extern ssize_t
curve25519_encode(struct curve25519_struct
*curve
, struct curve25519_proto
*proto
,
62 unsigned char *plaintext
, size_t size
, unsigned char **chipertext
);
63 extern ssize_t
curve25519_decode(struct curve25519_struct
*curve
, struct curve25519_proto
*proto
,
64 unsigned char *chipertext
, size_t size
, unsigned char **plaintext
,
65 struct taia
*arrival_taia
);
67 static inline void tai_pack(unsigned char *s
, struct tai
*t
)
72 s
[7] = x
& 255; x
>>= 8;
73 s
[6] = x
& 255; x
>>= 8;
74 s
[5] = x
& 255; x
>>= 8;
75 s
[4] = x
& 255; x
>>= 8;
76 s
[3] = x
& 255; x
>>= 8;
77 s
[2] = x
& 255; x
>>= 8;
78 s
[1] = x
& 255; x
>>= 8;
82 static inline void tai_unpack(unsigned char *s
, struct tai
*t
)
86 x
= (unsigned char) s
[0];
87 x
<<= 8; x
+= (unsigned char) s
[1];
88 x
<<= 8; x
+= (unsigned char) s
[2];
89 x
<<= 8; x
+= (unsigned char) s
[3];
90 x
<<= 8; x
+= (unsigned char) s
[4];
91 x
<<= 8; x
+= (unsigned char) s
[5];
92 x
<<= 8; x
+= (unsigned char) s
[6];
93 x
<<= 8; x
+= (unsigned char) s
[7];
97 static inline void taia_pack(unsigned char *s
, struct taia
*t
)
101 tai_pack(s
, &t
->sec
);
104 s
[7] = x
& 255; x
>>= 8;
105 s
[6] = x
& 255; x
>>= 8;
106 s
[5] = x
& 255; x
>>= 8;
109 s
[3] = x
& 255; x
>>= 8;
110 s
[2] = x
& 255; x
>>= 8;
111 s
[1] = x
& 255; x
>>= 8;
115 static inline void taia_unpack(unsigned char *s
, struct taia
*t
)
119 tai_unpack(s
, &t
->sec
);
121 x
= (unsigned char) s
[4];
122 x
<<= 8; x
+= (unsigned char) s
[5];
123 x
<<= 8; x
+= (unsigned char) s
[6];
124 x
<<= 8; x
+= (unsigned char) s
[7];
126 x
= (unsigned char) s
[0];
127 x
<<= 8; x
+= (unsigned char) s
[1];
128 x
<<= 8; x
+= (unsigned char) s
[2];
129 x
<<= 8; x
+= (unsigned char) s
[3];
133 #define tai_unix(t, u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
135 static inline void taia_now(struct taia
*t
)
139 gettimeofday(&now
, NULL
);
141 tai_unix(&t
->sec
, now
.tv_sec
);
142 t
->nano
= 1000 * now
.tv_usec
+ 500;
146 static inline void taia_sub(struct taia
*res
, const struct taia
*u
, const struct taia
*v
)
148 unsigned long unano
= u
->nano
;
149 unsigned long uatto
= u
->atto
;
151 res
->sec
.x
= u
->sec
.x
- v
->sec
.x
;
152 res
->nano
= unano
- v
->nano
;
153 res
->atto
= uatto
- v
->atto
;
155 if (res
->atto
> uatto
) {
156 res
->atto
+= 1000000000UL;
160 if (res
->nano
> unano
) {
161 res
->nano
+= 1000000000UL;
166 static inline void taia_add(struct taia
*res
, const struct taia
*u
, const struct taia
*v
)
168 res
->sec
.x
= u
->sec
.x
+ v
->sec
.x
;
169 res
->nano
= u
->nano
+ v
->nano
;
170 res
->atto
= u
->atto
+ v
->atto
;
172 if (res
->atto
> 999999999UL) {
173 res
->atto
-= 1000000000UL;
177 if (res
->nano
> 999999999UL) {
178 res
->nano
-= 1000000000UL;
183 static inline int taia_less(const struct taia
*t
, const struct taia
*u
)
185 if (t
->sec
.x
< u
->sec
.x
)
187 if (t
->sec
.x
> u
->sec
.x
)
189 if (t
->nano
< u
->nano
)
191 if (t
->nano
> u
->nano
)
193 return t
->atto
< u
->atto
;
196 static inline int is_good_taia(struct taia
*arrival_taia
, struct taia
*packet_taia
)
201 if (taia_less(arrival_taia
, packet_taia
)) {
202 taia_sub(&sub_res
, packet_taia
, arrival_taia
);
203 if (taia_less(&sub_res
, &tolerance_taia
))
208 taia_sub(&sub_res
, arrival_taia
, packet_taia
);
209 if (taia_less(&sub_res
, &tolerance_taia
))