romulus/Romulus-M/encrypt_wrapper.c

137 lines
3.5 KiB
C

#include "encrypt_wrapper.h"
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#define _CRT_RAND_S
#define __bswap_16 _byteswap_ushort
#define __bswap_32 _byteswap_ulong
#else
#include <byteswap.h>
#include <sys/random.h>
#endif
#include <stdlib.h>
#include "romulus_m.h"
#include "api.h"
int encrypt(packet *packet, unsigned char* outBuf, UINT16 *len, unsigned char* key){
if(sizeof(UINT16) + sizeof(UINT16) + packet->Size > *len){
return 1;
}
int packetSize = packet->Size;
// enc
// ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES
unsigned long long clen = packetSize + CRYPTO_ABYTES;
unsigned char* c = (unsigned char*) calloc(clen, 1);
// plaintext - in
int mlen = packetSize;
unsigned char* m = packet->Data;
// additional text
int adlen = sizeof(packet->DataType);
unsigned char* ad = (unsigned char*)&packet->DataType;
// n = nonce, CRYPTO_NPUBBYTES size 16
unsigned char npub[CRYPTO_NPUBBYTES];
#ifdef _MSC_VER
UINT32 secRandom;
for (size_t i = 0; i < CRYPTO_NPUBBYTES; i+=sizeof(UINT32))
{
rand_s(&secRandom);
memcpy(&npub[i], &secRandom, sizeof(UINT32));
}
#else
getrandom(&npub[0], sizeof(npub), 0);
#endif
int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], key);
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(UINT16));
loc += sizeof(UINT16);
// data size 2 3
UINT16 clen16 = (UINT16)clen;
// we copy data and nonce
clen16 += CRYPTO_NPUBBYTES;
clen16 = __bswap_16(clen16);
memcpy(&outBuf[loc], &clen16, sizeof(UINT16));
loc += sizeof(UINT16);
memcpy(&outBuf[loc], npub, CRYPTO_NPUBBYTES);
loc += CRYPTO_NPUBBYTES;
memcpy(&outBuf[loc], c, clen);
loc += clen;
*len = loc;
free(c);
return 0;
}
int decrypt(packetEx *packet, unsigned char* inBuf, UINT16 len, unsigned char* key){
int loc = 0;
if(inBuf == 0 ){
return 1;
}
memcpy(&packet->DataType, &inBuf[loc], sizeof(UINT16));
loc += sizeof(UINT16);
memcpy(&packet->Id, &inBuf[loc], sizeof(UINT32));
loc += sizeof(UINT32);
memcpy(&packet->Size, &inBuf[loc], sizeof(UINT16));
loc += sizeof(UINT16);
packet->DataType = __bswap_16(packet->DataType);
packet->Size = __bswap_16(packet->Size);
packet->Id = __bswap_32(packet->Id);
if(packet->Size < CRYPTO_NPUBBYTES){
return 1;
}
// 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;
int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, key);
if(mlen <= 1000 && ret == 0){
memcpy(packet->Data, m, mlen);
packet->Size = (UINT16)mlen;
}
else{
free(m);
return -1;
}
free(m);
return 0;
}