1
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

View File

@ -1,8 +1,11 @@
#include <portaudio.h> #include <portaudio.h>
#include <sodium/core.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <errno.h>
#include "audio.h" // #include "audio.h"
#include "crypt.h" #include "crypt.h"
#include "net.h" #include "net.h"
@ -16,159 +19,152 @@ int main(int argc, char **argv) {
(void)argv; (void)argv;
(void)argc; (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) { if (sodium_init() == -1) {
fprintf(stderr, "A Sodium cryptography library is failed to initialise!\n"); fprintf(stderr, "A Sodium cryptography library is failed to initialise!\n");
return -1; return -1;
} }
crypt_key_t own, remote; // if (audio_init_soundsystem() == -1)
// return -1;
if (crypt_key_gen(&own) == -1) { if (crypt_key_gen(&kl) == -1) {
fprintf(stderr, "Failed to generate the keys."); ret = -1;
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;
goto cleanup; goto cleanup;
} }
if (crypt_hello_verify(rh, &remote) == -1) { if (crypt_key_gen(&kr) == -1) {
fprintf(stderr, "rh v fail"); ret = -1;
rt = -1;
goto cleanup; goto cleanup;
} }
nonce_own = crypt_hello_get_nonce(oh, rh, false); hl = crypt_hello(&kl);
nonce_rem = crypt_hello_get_nonce(rh, oh, true); hr = crypt_hello(&kr);
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;
}
if (crypt_session_init(&os, &own, &remote, nonce_own, true) == -1) { if (net_client_init(&nl, "127.0.0.1", "14816") == -1) {
rt = -1; fprintf(stderr, "Failed to intialise a client.");
ret = -1;
goto cleanup; goto cleanup;
} }
if (crypt_session_init(&rs, &remote, &own, nonce_rem, false) == -1) { if (net_server_init(&nr, "127.0.0.1", "14816") == -1) {
rt = -1; fprintf(stderr, "Failed to intialise a server.");
ret = -1;
goto cleanup; goto cleanup;
} }
char msg[] = "Hello!"; ssize_t sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN);
unsigned long long msglen = 6; if (sent == -1) {
ret = -1;
oc = crypt_session_encrypt(&os, (unsigned char *)msg, msglen, &ocl);
if (oc == NULL) {
fprintf(stderr, "oc is null");
rt = -1;
goto cleanup; goto cleanup;
} }
rmsg = (char *)crypt_session_decrypt(&rs, oc, ocl, &rml); ssize_t rcvd = net_recv(&nr, incom, incom_len);
if (rcvd == -1) {
if (rmsg == NULL) { ret = -1;
rt = -1;
goto cleanup; goto cleanup;
} }
if (strcmp(msg, rmsg) != 0) if (rcvd == CRYPT_HELLO_LEN) {
fprintf(stderr, "Yikes!"); memcpy(hrl, incom, CRYPT_HELLO_LEN);
}
net_t nc, ns; sent = net_send(&nl, (const char *)hl, CRYPT_HELLO_LEN);
if (sent == -1) {
if (net_client_init(&nc, "127.0.0.1", "14816") == -1) { ret = -1;
rt = -1;
goto cleanup; goto cleanup;
} }
if (net_server_init(&ns, "127.0.0.1", "14816") == -1) { rcvd = net_recv(&nr, incom, incom_len);
rt = -1; 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; goto cleanup;
} }
if (audio_init_soundsystem() == -1) { sent = net_send(&nr, (const char *)hr, CRYPT_HELLO_LEN);
rt = -1; if (sent == -1) {
ret = -1;
goto cleanup; 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]; if (rcvd == CRYPT_HELLO_LEN) {
memset(buf, 0, BUF_SIZE); memcpy(hlr, incom, CRYPT_HELLO_LEN);
nl.raddr = nl.inaddr;
audio_init_default(&aud, CHANNELS, SAMPLE_RATE, FRAMES_PER_BUFFFER); nl.raddr_len = nl.inaddr_len;
} else {
if (audio_stream_input_toggle(&aud) == -1) { fprintf(stderr, "A broken hello packet was received.");
rt = -1; ret = -1;
goto cleanup; goto cleanup;
} }
if (audio_stream_output_toggle(&aud) == -1) { if (crypt_hello_verify(hlr, &kr) == -1) {
rt = -1; fprintf(stderr, "hello remote -> local: not verified\n");
ret = -1;
goto cleanup; goto cleanup;
} }
unsigned char *c = NULL, *m = NULL; if (crypt_hello_verify(hrl, &kl) == -1) {
unsigned long long cl = 0, ml = 0; fprintf(stderr, "hello local -> remote: not verified\n");
ret = -1;
printf("Listening... "); goto cleanup;
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);
} }
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: cleanup:
free(oh);
free(rh);
free(oc); net_destroy(&nl);
free(rmsg); net_destroy(&nr);
free(nonce_own); crypt_key_destroy(&kl);
free(nonce_rem); crypt_key_destroy(&kr);
crypt_key_destroy(&own); free(hl);
crypt_key_destroy(&remote); free(hr);
free(nonce);
crypt_session_destroy(&os); // if (audio_terminate_soundsystem() == -1)
crypt_session_destroy(&rs); // return -1;
net_destroy(&ns); return ret;
net_destroy(&nc);
audio_destroy(&aud);
audio_terminate_soundsystem();
return rt;
} }