#include #include #include #include #include #include // #include "audio.h" #include "crypt.h" #include "net.h" #define CHANNELS 1 #define SAMPLE_RATE 48000 #define FRAMES_PER_BUFFFER 960 #define BUF_SIZE (FRAMES_PER_BUFFFER * CHANNELS) int main(int argc, char **argv) { (void)argv; (void)argc; crypt_key_t kl = {0}, kr = {0}; unsigned char *hl = NULL, *hr = NULL; unsigned char hlr[CRYPT_HELLO_LEN], hrl[CRYPT_HELLO_LEN]; unsigned char *nonce = NULL; net_t nl = {0}, nr = {0}; crypt_session_t sl = {0}, sr = {0}; const int incom_len = 512; char incom[incom_len]; int ret = 0; if (sodium_init() == -1) { fprintf(stderr, "A Sodium cryptography library is failed to initialise!\n"); return -1; } // if (audio_init_soundsystem() == -1) // return -1; if (crypt_key_gen(&kl) == -1) { ret = -1; goto cleanup; } if (crypt_key_gen(&kr) == -1) { ret = -1; goto cleanup; } hl = crypt_hello(&kl); hr = crypt_hello(&kr); if (net_client_init(&nl, "127.0.0.1", "14816") == -1) { fprintf(stderr, "Failed to intialise a client."); ret = -1; goto cleanup; } if (net_server_init(&nr, "127.0.0.1", "14816") == -1) { fprintf(stderr, "Failed to intialise a server."); ret = -1; goto cleanup; } ssize_t sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN); if (sent == -1) { ret = -1; goto cleanup; } ssize_t rcvd = net_recv(&nr, incom, incom_len); if (rcvd == -1) { ret = -1; goto cleanup; } nr.raddr = nr.inaddr; nr.raddr_len = nr.inaddr_len; if (rcvd == CRYPT_HELLO_LEN) { memcpy(hrl, incom, CRYPT_HELLO_LEN); } else { fprintf(stderr, "remote: A broken hello packet was received."); net_send(&nr, "FUCKOFF", 7); ret = -1; goto cleanup; } sent = net_send(&nr, (const char *)hr, CRYPT_HELLO_LEN); if (sent == -1) { ret = -1; goto cleanup; } rcvd = net_recv(&nl, incom, incom_len); if (rcvd == -1) { ret = -1; goto cleanup; } if (rcvd == CRYPT_HELLO_LEN) { memcpy(hlr, incom, CRYPT_HELLO_LEN); } else { if (memcmp("FUCKOFF", incom, 7) == 0) { fprintf(stderr, "local: A broken hello packet was sent by me."); } else { fprintf(stderr, "local: A broken hello packet was received."); net_send(&nl, "FUCKOFF", 7); } ret = -1; goto cleanup; } if (crypt_hello_verify(hlr, &kr) == -1) { fprintf(stderr, "hello remote -> local: not verified\n"); net_send(&nl, "FUCKOFF", 7); ret = -1; goto cleanup; } else { net_send(&nl, "YOU'RE WELCOME", 14); } rcvd = net_recv(&nr, incom, incom_len); if (rcvd == -1) { ret = -1; goto cleanup; } if (crypt_hello_verify(hrl, &kl) == -1) { fprintf(stderr, "hello local -> remote: not verified\n"); net_send(&nr, "FUCKOFF", 7); ret = -1; goto cleanup; } else { net_send(&nr, "YOU'RE WELCOME", 14); } nonce = crypt_hello_get_nonce(hl, hlr, true); unsigned char *rn = crypt_hello_get_nonce(hr, hrl, false); if (memcmp(nonce, rn, CRYPT_NONCE_LEN) != 0) { fprintf(stderr, "nonce mismatch occured\n"); free(rn); ret = -1; goto cleanup; } free(rn); if (crypt_session_init(&sl, &kl, &kr, nonce, true) == -1) { fprintf(stderr, "local session init fail\n"); ret = -1; goto cleanup; } if (crypt_session_init(&sr, &kr, &kl, nonce, false) == -1) { fprintf(stderr, "remote session init fail\n"); ret = -1; goto cleanup; } /* Now we're talking securely! */ cleanup: net_destroy(&nl); net_destroy(&nr); crypt_key_destroy(&kl); crypt_key_destroy(&kr); crypt_session_destroy(&sl); crypt_session_destroy(&sr); free(hl); free(hr); free(nonce); // if (audio_terminate_soundsystem() == -1) // return -1; return ret; }