#include #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, kr; unsigned char *hl, *hr; unsigned char hlr[CRYPT_HELLO_LEN], hrl[CRYPT_HELLO_LEN]; unsigned char *nonce; net_t nl, nr; crypt_session_t sl, sr; 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; } if (rcvd == CRYPT_HELLO_LEN) { memcpy(hrl, incom, CRYPT_HELLO_LEN); } sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN); if (sent == -1) { ret = -1; goto cleanup; } rcvd = net_recv(&nr, incom, incom_len); if (rcvd == -1) { ret = -1; goto cleanup; } // fprintf(stderr, "%s:%d\n", __FILE__, __LINE__); if (rcvd == CRYPT_HELLO_LEN) { memcpy(hrl, incom, CRYPT_HELLO_LEN); nr.raddr = nr.inaddr; nr.raddr_len = nr.inaddr_len; } else { fprintf(stderr, "A broken hello packet was received."); 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; } // fprintf(stderr, "%s:%d\n", __FILE__, __LINE__); if (rcvd == CRYPT_HELLO_LEN) { memcpy(hlr, incom, CRYPT_HELLO_LEN); nl.raddr = nl.inaddr; nl.raddr_len = nl.inaddr_len; } else { fprintf(stderr, "A broken hello packet was received."); ret = -1; goto cleanup; } if (crypt_hello_verify(hlr, &kr) == -1) { fprintf(stderr, "hello remote -> local: not verified\n"); ret = -1; goto cleanup; } if (crypt_hello_verify(hrl, &kl) == -1) { fprintf(stderr, "hello local -> remote: not verified\n"); ret = -1; goto cleanup; } 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; } 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; }