romulus/Romulus-M/encrypt_wrapper.c

137 lines
3.5 KiB
C
Raw Normal View History

2022-01-25 23:01:41 +00:00
#include "encrypt_wrapper.h"
#include <stdio.h>
#include <string.h>
2022-01-27 22:08:40 +00:00
#ifdef _MSC_VER
#define _CRT_RAND_S
#define __bswap_16 _byteswap_ushort
#define __bswap_32 _byteswap_ulong
2022-01-26 00:21:27 +00:00
#else
2022-01-27 22:08:40 +00:00
#include <byteswap.h>
2022-01-25 23:01:41 +00:00
#include <sys/random.h>
2022-01-26 00:21:27 +00:00
#endif
2022-01-27 22:08:40 +00:00
#include <stdlib.h>
2022-01-25 23:01:41 +00:00
#include "romulus_m.h"
#include "api.h"
2022-01-27 22:08:40 +00:00
int encrypt(packet *packet, unsigned char* outBuf, UINT16 *len, unsigned char* key){
if(sizeof(UINT16) + sizeof(UINT16) + packet->Size > *len){
2022-01-25 23:01:41 +00:00
return 1;
}
2022-01-26 00:21:27 +00:00
int packetSize = packet->Size;
2022-01-25 23:01:41 +00:00
// 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];
2022-01-27 22:08:40 +00:00
#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
2022-01-25 23:01:41 +00:00
getrandom(&npub[0], sizeof(npub), 0);
2022-01-27 22:08:40 +00:00
#endif
2022-01-25 23:01:41 +00:00
2022-01-27 00:43:24 +00:00
int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], key);
2022-01-25 23:01:41 +00:00
2022-01-26 00:21:27 +00:00
if(clen > *len + CRYPTO_NPUBBYTES || ret != 0){
2022-01-25 23:01:41 +00:00
free(c);
return 1;
}
// type 0 1
2022-01-26 00:21:27 +00:00
// Swap after adding it as additional data
packet->DataType = __bswap_16(packet->DataType);
2022-01-25 23:01:41 +00:00
int loc = 0;
2022-01-27 22:08:40 +00:00
memcpy(&outBuf[loc], &packet->DataType,sizeof(UINT16));
loc += sizeof(UINT16);
2022-01-25 23:01:41 +00:00
// data size 2 3
2022-01-27 22:08:40 +00:00
UINT16 clen16 = (UINT16)clen;
2022-01-25 23:01:41 +00:00
// we copy data and nonce
clen16 += CRYPTO_NPUBBYTES;
clen16 = __bswap_16(clen16);
2022-01-27 22:08:40 +00:00
memcpy(&outBuf[loc], &clen16, sizeof(UINT16));
loc += sizeof(UINT16);
2022-01-25 23:01:41 +00:00
memcpy(&outBuf[loc], npub, CRYPTO_NPUBBYTES);
loc += CRYPTO_NPUBBYTES;
memcpy(&outBuf[loc], c, clen);
loc += clen;
*len = loc;
free(c);
return 0;
}
2022-01-27 22:08:40 +00:00
int decrypt(packetEx *packet, unsigned char* inBuf, UINT16 len, unsigned char* key){
2022-01-25 23:01:41 +00:00
int loc = 0;
2022-01-27 00:43:24 +00:00
if(inBuf == 0 ){
return 1;
}
2022-01-27 22:08:40 +00:00
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);
2022-01-25 23:01:41 +00:00
packet->DataType = __bswap_16(packet->DataType);
packet->Size = __bswap_16(packet->Size);
packet->Id = __bswap_32(packet->Id);
2022-01-27 00:43:24 +00:00
if(packet->Size < CRYPTO_NPUBBYTES){
return 1;
}
2022-01-26 00:21:27 +00:00
// 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;
2022-01-27 00:43:24 +00:00
int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, key);
2022-01-26 00:21:27 +00:00
if(mlen <= 1000 && ret == 0){
memcpy(packet->Data, m, mlen);
2022-01-27 22:08:40 +00:00
packet->Size = (UINT16)mlen;
2022-01-25 23:01:41 +00:00
}
else{
2022-01-26 00:21:27 +00:00
free(m);
2022-01-27 00:43:24 +00:00
return -1;
2022-01-25 23:01:41 +00:00
}
2022-01-26 00:21:27 +00:00
free(m);
2022-01-25 23:01:41 +00:00
return 0;
}