diff options
| author | Nakidai <nakidai@disroot.org> | 2025-07-31 17:12:27 +0300 |
|---|---|---|
| committer | Nakidai <nakidai@disroot.org> | 2025-07-31 17:12:27 +0300 |
| commit | b4709429ed88563982412a5a027b92143c37e268 (patch) | |
| tree | cd56dfeac7cafcfcb69925b12376d79b8776d3fd /common.h | |
| download | fatvpn-1.0.0.tar.gz fatvpn-1.0.0.zip | |
Add files v1.0.0
Diffstat (limited to 'common.h')
| -rw-r--r-- | common.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/common.h b/common.h new file mode 100644 index 0000000..9bb5912 --- /dev/null +++ b/common.h @@ -0,0 +1,74 @@ +/* common (not config, need code patch after change) */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/select.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/ioctl.h> +#include <termios.h> +#include <time.h> +#include <net/if.h> +#include <fcntl.h> + +#include <linux/if_tun.h> + +#include "shorttypes.h" +#include "sponge-bob.h" + +#define ERRDIE(a, b) if ((a) == -1) perror((b)), exit(1) + +#define NONCE_SZ 16 +#define HMAC_SZ 16 + +u8 key[32]; +u32 prng_state[14]; + + +static void send_pkt +(s32 skt, void *buf, u16 sz, struct sockaddr_storage *addr){ + ssize_t res; + u8 *nonce, *tag; + + /* nonce, tag */ + nonce = (u8*)buf + sz; + tag = nonce + NONCE_SZ; + duplex257_prng_rand16(prng_state, nonce); + + /* encrypt */ + duplex257_ae_encrypt(key, nonce, tag, buf, sz); + sz += NONCE_SZ + HMAC_SZ; + + /* send */ + res = sendto(skt, buf, sz, 0,(struct sockaddr*)addr, sizeof(*addr)); + ERRDIE(res, "sendto"); + return; +} + +static s32 recv_pkt +(s32 skt, void *buf, u16 sz, struct sockaddr_storage *addr){ + socklen_t skl; + s32 res; + u8 *nonce, *rtag; + u8 tag[HMAC_SZ]; + + /* recv */ + skl = sizeof(*addr); + res = recvfrom(skt, buf, sz, 0, (struct sockaddr*)addr, &skl); + ERRDIE(res, "recvfrom"); + + /* nonce, recieved tag */ + rtag = buf + res - HMAC_SZ; + nonce = rtag - NONCE_SZ; + + /* decrypt */ + res -= NONCE_SZ + HMAC_SZ; + duplex257_ae_decrypt(key, nonce, tag, buf, res); + if (0 != memcmp(tag, rtag, HMAC_SZ)) return -1; + + return res; +} + |