2024-03-25 01:05:29 +04:00
|
|
|
#include <portaudio.h>
|
2024-03-25 03:53:55 +04:00
|
|
|
#include <sodium/core.h>
|
2024-03-25 03:59:38 +04:00
|
|
|
#include <stdbool.h>
|
2024-03-21 04:10:02 +04:00
|
|
|
#include <stdio.h>
|
2024-03-21 17:51:03 +04:00
|
|
|
#include <string.h>
|
2024-03-25 03:53:55 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
2024-03-21 04:10:02 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
// #include "audio.h"
|
2024-03-21 17:51:03 +04:00
|
|
|
#include "crypt.h"
|
2024-03-24 20:14:16 +04:00
|
|
|
#include "net.h"
|
2024-03-21 17:51:03 +04:00
|
|
|
|
|
|
|
#define CHANNELS 1
|
|
|
|
#define SAMPLE_RATE 48000
|
2024-03-25 01:05:29 +04:00
|
|
|
#define FRAMES_PER_BUFFFER 960
|
2024-03-21 17:51:03 +04:00
|
|
|
|
|
|
|
#define BUF_SIZE (FRAMES_PER_BUFFFER * CHANNELS)
|
2024-03-21 04:10:02 +04:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
(void)argv;
|
|
|
|
(void)argc;
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
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;
|
2024-03-25 03:59:38 +04:00
|
|
|
crypt_session_t sl, sr;
|
2024-03-25 03:53:55 +04:00
|
|
|
|
|
|
|
const int incom_len = 512;
|
|
|
|
char incom[incom_len];
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
2024-03-24 05:32:46 +04:00
|
|
|
if (sodium_init() == -1) {
|
2024-03-21 17:51:03 +04:00
|
|
|
fprintf(stderr, "A Sodium cryptography library is failed to initialise!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
// if (audio_init_soundsystem() == -1)
|
|
|
|
// return -1;
|
2024-03-21 17:51:03 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (crypt_key_gen(&kl) == -1) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
2024-03-21 17:51:03 +04:00
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (crypt_key_gen(&kr) == -1) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
2024-03-24 04:41:08 +04:00
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
hl = crypt_hello(&kl);
|
|
|
|
hr = crypt_hello(&kr);
|
2024-03-24 04:41:08 +04:00
|
|
|
|
2024-03-24 04:56:02 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (net_client_init(&nl, "127.0.0.1", "14816") == -1) {
|
|
|
|
fprintf(stderr, "Failed to intialise a client.");
|
|
|
|
ret = -1;
|
2024-03-24 04:41:08 +04:00
|
|
|
goto cleanup;
|
2024-03-23 04:22:01 +04:00
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (net_server_init(&nr, "127.0.0.1", "14816") == -1) {
|
|
|
|
fprintf(stderr, "Failed to intialise a server.");
|
|
|
|
ret = -1;
|
2024-03-24 04:41:08 +04:00
|
|
|
goto cleanup;
|
2024-03-23 04:22:01 +04:00
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
ssize_t sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN);
|
|
|
|
if (sent == -1) {
|
|
|
|
ret = -1;
|
2024-03-24 04:41:08 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2024-03-23 04:22:01 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
ssize_t rcvd = net_recv(&nr, incom, incom_len);
|
|
|
|
if (rcvd == -1) {
|
|
|
|
ret = -1;
|
2024-03-24 04:41:08 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2024-03-23 04:22:01 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (rcvd == CRYPT_HELLO_LEN) {
|
|
|
|
memcpy(hrl, incom, CRYPT_HELLO_LEN);
|
|
|
|
}
|
2024-03-23 04:22:01 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN);
|
|
|
|
if (sent == -1) {
|
|
|
|
ret = -1;
|
2024-03-24 04:41:08 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2024-03-23 04:22:01 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
rcvd = net_recv(&nr, incom, incom_len);
|
|
|
|
if (rcvd == -1) {
|
|
|
|
ret = -1;
|
2024-03-24 04:41:08 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2024-03-25 03:53:55 +04:00
|
|
|
// 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;
|
2024-03-24 20:14:16 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
sent = net_send(&nr, (const char *)hr, CRYPT_HELLO_LEN);
|
|
|
|
if (sent == -1) {
|
|
|
|
ret = -1;
|
2024-03-24 20:14:16 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
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;
|
2024-03-25 01:05:29 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2024-03-24 20:14:16 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (crypt_hello_verify(hlr, &kr) == -1) {
|
|
|
|
fprintf(stderr, "hello remote -> local: not verified\n");
|
|
|
|
ret = -1;
|
2024-03-25 01:05:29 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (crypt_hello_verify(hrl, &kl) == -1) {
|
|
|
|
fprintf(stderr, "hello local -> remote: not verified\n");
|
|
|
|
ret = -1;
|
2024-03-25 01:05:29 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2024-03-24 06:05:01 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
nonce = crypt_hello_get_nonce(hl, hlr, true);
|
|
|
|
unsigned char *rn = crypt_hello_get_nonce(hr, hrl, false);
|
2024-03-24 06:05:01 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
if (memcmp(nonce, rn, CRYPT_NONCE_LEN) != 0) {
|
|
|
|
fprintf(stderr, "nonce mismatch occured\n");
|
|
|
|
free(rn);
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
2024-03-24 06:05:01 +04:00
|
|
|
}
|
|
|
|
|
2024-03-25 03:59:38 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
/* Now we're talking securely! */
|
2024-03-24 06:05:01 +04:00
|
|
|
|
2024-03-24 04:56:02 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
cleanup:
|
2024-03-24 04:56:02 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
net_destroy(&nl);
|
|
|
|
net_destroy(&nr);
|
2024-03-23 04:22:01 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
crypt_key_destroy(&kl);
|
|
|
|
crypt_key_destroy(&kr);
|
2024-03-23 04:22:01 +04:00
|
|
|
|
2024-03-25 03:59:38 +04:00
|
|
|
crypt_session_destroy(&sl);
|
|
|
|
crypt_session_destroy(&sr);
|
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
free(hl);
|
|
|
|
free(hr);
|
|
|
|
free(nonce);
|
2024-03-24 20:14:16 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
// if (audio_terminate_soundsystem() == -1)
|
|
|
|
// return -1;
|
2024-03-25 01:05:29 +04:00
|
|
|
|
2024-03-25 03:53:55 +04:00
|
|
|
return ret;
|
2024-03-25 00:56:42 +04:00
|
|
|
}
|