implement packet decryption

This commit is contained in:
Simon 2022-01-26 00:21:27 +00:00
parent 8192496116
commit 58234cc994
1 changed files with 36 additions and 12 deletions

View File

@ -3,7 +3,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <byteswap.h> #include <byteswap.h>
#ifdef _WIN32
#else
#include <sys/random.h> #include <sys/random.h>
#endif
#include "romulus_m.h" #include "romulus_m.h"
#include "api.h" #include "api.h"
@ -13,9 +16,7 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){
return 1; return 1;
} }
int packetSize = packet->Size; int packetSize = packet->Size;
packet->DataType = __bswap_16(packet->DataType);
// 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
@ -26,28 +27,26 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){
int mlen = packetSize; int mlen = packetSize;
unsigned char* m = packet->Data; unsigned char* m = packet->Data;
// additional text // additional text
int adlen = sizeof(packet->DataType); int adlen = sizeof(packet->DataType);
unsigned char* ad = (unsigned char*)&packet->DataType; unsigned char* ad = (unsigned char*)&packet->DataType;
// unused
unsigned char* nsec;
// n = nonce, CRYPTO_NPUBBYTES size 16 // n = nonce, CRYPTO_NPUBBYTES size 16
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 // key, CRYPTO_KEYBYTES size 16
unsigned char k[CRYPTO_KEYBYTES] = {0}; unsigned char k[CRYPTO_KEYBYTES] = {0};
romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], &k[0]); int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], &k[0]);
if(clen > *len + CRYPTO_NPUBBYTES){ if(clen > *len + CRYPTO_NPUBBYTES || ret != 0){
free(c); free(c);
return 1; return 1;
} }
// type 0 1 // type 0 1
// Swap after adding it as additional data
packet->DataType = __bswap_16(packet->DataType);
int loc = 0; int loc = 0;
memcpy(&outBuf[loc], &packet->DataType,sizeof(u_int16_t)); memcpy(&outBuf[loc], &packet->DataType,sizeof(u_int16_t));
loc += sizeof(u_int16_t); loc += sizeof(u_int16_t);
@ -86,12 +85,37 @@ 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 <= 1000){ // Data
memcpy(packet->Data, &inBuf[loc], packet->Size); // Nonce|Data
// dec
// n = nonce, CRYPTO_NPUBBYTES size 16
unsigned char* npub = &inBuf[loc];
loc += CRYPTO_NPUBBYTES;
// ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES
int clen = packet->Size - CRYPTO_NPUBBYTES;
unsigned char* c = &inBuf[loc];
// plaintext - in
unsigned long long mlen = clen;
unsigned char* m = (unsigned char*)calloc(mlen, 1);
// additional text
int adlen = sizeof(packet->DataType);
unsigned char* ad = (unsigned char*)&packet->DataType;
// key, CRYPTO_KEYBYTES size 16
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){
memcpy(packet->Data, m, mlen);
packet->Size = (u_int16_t)mlen;
} }
else{ else{
free(m);
return 1; return 1;
} }
free(m);
return 0; return 0;
} }