Add key setting

This commit is contained in:
Simon 2022-01-27 00:43:24 +00:00
parent 58234cc994
commit 768aa8a4c3
2 changed files with 14 additions and 13 deletions

View File

@ -11,13 +11,12 @@
#include "api.h" #include "api.h"
int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned char* key){
if(sizeof(u_int16_t) + sizeof(u_int16_t) + packet->Size > *len){ if(sizeof(u_int16_t) + sizeof(u_int16_t) + packet->Size > *len){
return 1; return 1;
} }
int packetSize = packet->Size; int packetSize = packet->Size;
// enc // enc
// ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES // ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES
unsigned long long clen = packetSize + CRYPTO_ABYTES; unsigned long long clen = packetSize + CRYPTO_ABYTES;
@ -35,9 +34,7 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){
unsigned char npub[CRYPTO_NPUBBYTES]; unsigned char npub[CRYPTO_NPUBBYTES];
getrandom(&npub[0], sizeof(npub), 0); getrandom(&npub[0], sizeof(npub), 0);
// key, CRYPTO_KEYBYTES size 16 int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], key);
unsigned char k[CRYPTO_KEYBYTES] = {0};
int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], &k[0]);
if(clen > *len + CRYPTO_NPUBBYTES || ret != 0){ if(clen > *len + CRYPTO_NPUBBYTES || ret != 0){
free(c); free(c);
@ -72,8 +69,11 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){
return 0; return 0;
} }
int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len){ int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len, unsigned char* key){
int loc = 0; int loc = 0;
if(inBuf == 0 ){
return 1;
}
memcpy(&packet->DataType, &inBuf[loc], sizeof(u_int16_t)); memcpy(&packet->DataType, &inBuf[loc], sizeof(u_int16_t));
loc += sizeof(u_int16_t); loc += sizeof(u_int16_t);
memcpy(&packet->Id, &inBuf[loc], sizeof(u_int32_t)); memcpy(&packet->Id, &inBuf[loc], sizeof(u_int32_t));
@ -85,6 +85,10 @@ int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len){
packet->Size = __bswap_16(packet->Size); packet->Size = __bswap_16(packet->Size);
packet->Id = __bswap_32(packet->Id); packet->Id = __bswap_32(packet->Id);
if(packet->Size < CRYPTO_NPUBBYTES){
return 1;
}
// Data // Data
// Nonce|Data // Nonce|Data
// dec // dec
@ -104,17 +108,14 @@ int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len){
int adlen = sizeof(packet->DataType); int adlen = sizeof(packet->DataType);
unsigned char* ad = (unsigned char*)&packet->DataType; unsigned char* ad = (unsigned char*)&packet->DataType;
// key, CRYPTO_KEYBYTES size 16 int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, key);
unsigned char k[CRYPTO_KEYBYTES] = {0};
int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, &k[0]);
if(mlen <= 1000 && ret == 0){ if(mlen <= 1000 && ret == 0){
memcpy(packet->Data, m, mlen); memcpy(packet->Data, m, mlen);
packet->Size = (u_int16_t)mlen; packet->Size = (u_int16_t)mlen;
} }
else{ else{
free(m); free(m);
return 1; return -1;
} }
free(m); free(m);
return 0; return 0;

View File

@ -14,7 +14,7 @@ typedef struct {
unsigned char* Data; unsigned char* Data;
}packetEx; }packetEx;
EXPORT int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len); EXPORT int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned char* key);
EXPORT int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len); EXPORT int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len, unsigned char* key);