bpf: split up instructions and extensions from header
[netsniff-ng.git] / curve.h
blobee4a322394ec8e038f454e083c47d9a41f449225
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #ifndef CURVE_H
8 #define CURVE_H
10 #include <stdint.h>
11 #include <sys/time.h>
13 #include "locking.h"
14 #include "built_in.h"
15 #include "ioops.h"
16 #include "rnd.h"
17 #include "crypto.h"
19 struct tai {
20 uint64_t x;
23 struct taia {
24 struct tai sec;
25 uint32_t nano;
26 uint32_t atto;
29 static struct taia tolerance_taia = {
30 .sec.x = 0,
31 .nano = 700000000ULL,
32 .atto = 0,
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;
44 size_t enc_buf_size;
45 size_t dec_buf_size;
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)
64 uint64_t x;
66 x = t->x;
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;
74 s[0] = x;
77 static inline void tai_unpack(unsigned char *s, struct tai *t)
79 uint64_t x;
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];
89 t->x = x;
92 static inline void taia_pack(unsigned char *s, struct taia *t)
94 unsigned long x;
96 tai_pack(s, &t->sec);
97 s += 8;
98 x = t->atto;
99 s[7] = x & 255; x >>= 8;
100 s[6] = x & 255; x >>= 8;
101 s[5] = x & 255; x >>= 8;
102 s[4] = x;
103 x = t->nano;
104 s[3] = x & 255; x >>= 8;
105 s[2] = x & 255; x >>= 8;
106 s[1] = x & 255; x >>= 8;
107 s[0] = x;
110 static inline void taia_unpack(unsigned char *s, struct taia *t)
112 unsigned long x;
114 tai_unpack(s, &t->sec);
115 s += 8;
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];
120 t->atto = x;
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];
125 t->nano = x;
128 #define tai_unix(t, u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
130 static inline void taia_now(struct taia *t)
132 struct timeval now;
134 gettimeofday(&now, NULL);
136 tai_unix(&t->sec, now.tv_sec);
137 t->nano = 1000 * now.tv_usec + 500;
138 t->atto = secrand();
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;
152 --res->nano;
155 if (res->nano > unano) {
156 res->nano += 1000000000UL;
157 --res->sec.x;
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;
169 ++res->nano;
172 if (res->nano > 999999999UL) {
173 res->nano -= 1000000000UL;
174 ++res->sec.x;
178 static inline int taia_less(const struct taia *t, const struct taia *u)
180 if (t->sec.x < u->sec.x)
181 return 1;
182 if (t->sec.x > u->sec.x)
183 return 0;
184 if (t->nano < u->nano)
185 return 1;
186 if (t->nano > u->nano)
187 return 0;
188 return t->atto < u->atto;
191 static inline int is_good_taia(struct taia *arrival_taia, struct taia *packet_taia)
193 int is_ts_good = 0;
194 struct taia sub_res;
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))
199 is_ts_good = 1;
200 else
201 is_ts_good = 0;
202 } else {
203 taia_sub(&sub_res, arrival_taia, packet_taia);
204 if (taia_less(&sub_res, &tolerance_taia))
205 is_ts_good = 1;
206 else
207 is_ts_good = 0;
210 return is_ts_good;
213 #endif /* CURVE_H */