implement packet decryption
This commit is contained in:
parent
8192496116
commit
58234cc994
|
@ -3,7 +3,10 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <byteswap.h>
|
||||
#ifdef _WIN32
|
||||
#else
|
||||
#include <sys/random.h>
|
||||
#endif
|
||||
#include "romulus_m.h"
|
||||
#include "api.h"
|
||||
|
||||
|
@ -15,8 +18,6 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){
|
|||
|
||||
int packetSize = packet->Size;
|
||||
|
||||
packet->DataType = __bswap_16(packet->DataType);
|
||||
|
||||
// enc
|
||||
// ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES
|
||||
unsigned long long clen = packetSize + CRYPTO_ABYTES;
|
||||
|
@ -26,28 +27,26 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){
|
|||
int mlen = packetSize;
|
||||
unsigned char* m = packet->Data;
|
||||
|
||||
|
||||
// additional text
|
||||
int adlen = sizeof(packet->DataType);
|
||||
unsigned char* ad = (unsigned char*)&packet->DataType;
|
||||
|
||||
// unused
|
||||
unsigned char* nsec;
|
||||
|
||||
// n = nonce, CRYPTO_NPUBBYTES size 16
|
||||
unsigned char npub[CRYPTO_NPUBBYTES];
|
||||
getrandom(&npub[0], sizeof(npub), 0);
|
||||
|
||||
// key, CRYPTO_KEYBYTES size 16
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// type 0 1
|
||||
// Swap after adding it as additional data
|
||||
packet->DataType = __bswap_16(packet->DataType);
|
||||
int loc = 0;
|
||||
memcpy(&outBuf[loc], &packet->DataType,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->Id = __bswap_32(packet->Id);
|
||||
|
||||
if(packet->Size <= 1000){
|
||||
memcpy(packet->Data, &inBuf[loc], packet->Size);
|
||||
// Data
|
||||
// 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{
|
||||
free(m);
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(m);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue