1
0
Fork 0

Keep on using a main() func for testing. Now a secure connection instantiation was completed. Yay!

This commit is contained in:
Alexander Andreev 2024-03-25 03:53:55 +04:00
parent 5fbbea3686
commit 92d7659306
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
1 changed files with 100 additions and 104 deletions

View File

@ -1,8 +1,11 @@
#include <portaudio.h>
#include <sodium/core.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include "audio.h"
// #include "audio.h"
#include "crypt.h"
#include "net.h"
@ -16,159 +19,152 @@ 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;
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;
}
crypt_key_t own, remote;
// if (audio_init_soundsystem() == -1)
// return -1;
if (crypt_key_gen(&own) == -1) {
fprintf(stderr, "Failed to generate the keys.");
return -1;
}
if (crypt_key_gen(&remote) == -1) {
fprintf(stderr, "Failed to generate the keys.");
return -1;
}
unsigned char *oh = crypt_hello(&own);
unsigned char *rh = crypt_hello(&remote);
unsigned char *oc = NULL;
char *rmsg = NULL;
unsigned long long ocl = 0, rml = 0;
int rt = 0;
crypt_session_t os, rs;
unsigned char *nonce_own = NULL, *nonce_rem = NULL;
if (crypt_hello_verify(oh, &own) == -1) {
fprintf(stderr, "oh v fail");
rt = -1;
if (crypt_key_gen(&kl) == -1) {
ret = -1;
goto cleanup;
}
if (crypt_hello_verify(rh, &remote) == -1) {
fprintf(stderr, "rh v fail");
rt = -1;
if (crypt_key_gen(&kr) == -1) {
ret = -1;
goto cleanup;
}
nonce_own = crypt_hello_get_nonce(oh, rh, false);
nonce_rem = crypt_hello_get_nonce(rh, oh, true);
for (unsigned int i = 0; i < CRYPT_NONCE_LEN; ++i)
if (nonce_own[i] != nonce_rem[i]) {
fprintf(stderr, "A nonce mismatch occured.");
rt = -1;
goto cleanup;
}
hl = crypt_hello(&kl);
hr = crypt_hello(&kr);
if (crypt_session_init(&os, &own, &remote, nonce_own, true) == -1) {
rt = -1;
if (net_client_init(&nl, "127.0.0.1", "14816") == -1) {
fprintf(stderr, "Failed to intialise a client.");
ret = -1;
goto cleanup;
}
if (crypt_session_init(&rs, &remote, &own, nonce_rem, false) == -1) {
rt = -1;
if (net_server_init(&nr, "127.0.0.1", "14816") == -1) {
fprintf(stderr, "Failed to intialise a server.");
ret = -1;
goto cleanup;
}
char msg[] = "Hello!";
unsigned long long msglen = 6;
oc = crypt_session_encrypt(&os, (unsigned char *)msg, msglen, &ocl);
if (oc == NULL) {
fprintf(stderr, "oc is null");
rt = -1;
ssize_t sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN);
if (sent == -1) {
ret = -1;
goto cleanup;
}
rmsg = (char *)crypt_session_decrypt(&rs, oc, ocl, &rml);
if (rmsg == NULL) {
rt = -1;
ssize_t rcvd = net_recv(&nr, incom, incom_len);
if (rcvd == -1) {
ret = -1;
goto cleanup;
}
if (strcmp(msg, rmsg) != 0)
fprintf(stderr, "Yikes!");
if (rcvd == CRYPT_HELLO_LEN) {
memcpy(hrl, incom, CRYPT_HELLO_LEN);
}
net_t nc, ns;
if (net_client_init(&nc, "127.0.0.1", "14816") == -1) {
rt = -1;
sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN);
if (sent == -1) {
ret = -1;
goto cleanup;
}
if (net_server_init(&ns, "127.0.0.1", "14816") == -1) {
rt = -1;
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;
}
if (audio_init_soundsystem() == -1) {
rt = -1;
sent = net_send(&nr, (const char *)hr, CRYPT_HELLO_LEN);
if (sent == -1) {
ret = -1;
goto cleanup;
}
audio_t aud;
rcvd = net_recv(&nl, incom, incom_len);
if (rcvd == -1) {
ret = -1;
goto cleanup;
}
// fprintf(stderr, "%s:%d\n", __FILE__, __LINE__);
unsigned char buf[BUF_SIZE];
memset(buf, 0, BUF_SIZE);
audio_init_default(&aud, CHANNELS, SAMPLE_RATE, FRAMES_PER_BUFFFER);
if (audio_stream_input_toggle(&aud) == -1) {
rt = -1;
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 (audio_stream_output_toggle(&aud) == -1) {
rt = -1;
if (crypt_hello_verify(hlr, &kr) == -1) {
fprintf(stderr, "hello remote -> local: not verified\n");
ret = -1;
goto cleanup;
}
unsigned char *c = NULL, *m = NULL;
unsigned long long cl = 0, ml = 0;
printf("Listening... ");
for (int i = 0; i < (10 * SAMPLE_RATE)/FRAMES_PER_BUFFFER; ++i) {
audio_read(&aud, buf, BUF_SIZE);
c = crypt_session_encrypt(&os, buf, BUF_SIZE, &cl);
net_send(&nc, (const char *)c, cl);
net_recv(&ns, c, cl);
m = crypt_session_decrypt(&rs, c, cl, &ml);
audio_write(&aud, m, ml);
free(c); free(m);
if (crypt_hello_verify(hrl, &kl) == -1) {
fprintf(stderr, "hello local -> remote: not verified\n");
ret = -1;
goto cleanup;
}
printf("Done!\n");
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;
}
/* Now we're talking securely! */
cleanup:
free(oh);
free(rh);
free(oc);
free(rmsg);
net_destroy(&nl);
net_destroy(&nr);
free(nonce_own);
free(nonce_rem);
crypt_key_destroy(&kl);
crypt_key_destroy(&kr);
crypt_key_destroy(&own);
crypt_key_destroy(&remote);
free(hl);
free(hr);
free(nonce);
crypt_session_destroy(&os);
crypt_session_destroy(&rs);
// if (audio_terminate_soundsystem() == -1)
// return -1;
net_destroy(&ns);
net_destroy(&nc);
audio_destroy(&aud);
audio_terminate_soundsystem();
return rt;
return ret;
}