1
0
Fork 0

An actually working handshake example in a main().

This commit is contained in:
Alexander Andreev 2024-03-25 05:03:46 +04:00
parent 5174c2685e
commit ccca06e70f
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
1 changed files with 30 additions and 29 deletions

View File

@ -4,7 +4,6 @@
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
// #include "audio.h"
#include "crypt.h"
@ -20,12 +19,12 @@ int main(int argc, char **argv) {
(void)argv;
(void)argc;
crypt_key_t kl, kr;
unsigned char *hl, *hr;
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;
net_t nl, nr;
crypt_session_t sl, sr;
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];
@ -78,29 +77,14 @@ int main(int argc, char **argv) {
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__);
nr.raddr = nr.inaddr;
nr.raddr_len = nr.inaddr_len;
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.");
fprintf(stderr, "remote: A broken hello packet was received.");
net_send(&nr, "FUCKOFF", 7);
ret = -1;
goto cleanup;
}
@ -116,28 +100,43 @@ int main(int argc, char **argv) {
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.");
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);
@ -150,6 +149,8 @@ int main(int argc, char **argv) {
goto cleanup;
}
free(rn);
if (crypt_session_init(&sl, &kl, &kr, nonce, true) == -1) {
fprintf(stderr, "local session init fail\n");
ret = -1;